forked from rstackjs/rstack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathHelpers.ts
More file actions
23 lines (17 loc) · 885 Bytes
/
Copy pathpathHelpers.ts
File metadata and controls
23 lines (17 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import path from 'node:path';
type RelativePathResolver = (filePath: string) => string;
const toPosixPath: (filePath: string) => string =
path.sep === '\\' ? (filePath) => filePath.replaceAll('\\', '/') : (filePath) => filePath;
const createRelativePathResolver = (rootPath: string): RelativePathResolver => {
const rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;
return (filePath) =>
filePath === rootPath
? ''
: filePath.startsWith(rootPrefix)
? filePath.slice(rootPrefix.length)
: path.relative(rootPath, filePath);
};
/** Prettier only inspects a file's shebang when its basename contains no dot. */
const hasDottedBasename = (filePath: string): boolean => path.basename(filePath).includes('.');
export { createRelativePathResolver, hasDottedBasename, toPosixPath };
export type { RelativePathResolver };