UNPKG

1.23 kBPlain TextView Raw
1import {tmpdir} from 'os';
2
3import {dirname, join, resolve} from 'path';
4
5import {randomId} from '../static';
6
7import {PathReference, FileReference} from './contracts';
8
9import {FileImpl, PathImpl} from './impl';
10
11export const pathFromString = (sourcePath: PathReference | string): PathReference => {
12 if (typeof sourcePath === 'string') {
13 return new PathImpl(sourcePath) as PathReference;
14 }
15 return sourcePath;
16};
17
18export const fileFromString = (filePath: FileReference | string): FileReference => {
19 if (typeof filePath === 'string') {
20 return new FileImpl(pathFromString(dirname(filePath)), filePath) as FileReference;
21 }
22 return filePath;
23};
24
25export const makeAbsolute = (basePath: string | PathReference, subpath: string): string =>
26 resolve(basePath.toString(), subpath);
27
28export const absolutePath = (basePath: string | PathReference, subpath: string) =>
29 pathFromString(makeAbsolute(basePath.toString(), subpath));
30
31export const absoluteFile = (basePath: string | PathReference, subpath: string) =>
32 fileFromString(makeAbsolute(basePath.toString(), subpath));
33
34export const pathFromRandomId = (): PathReference => {
35 const path = pathFromString(join(tmpdir(), randomId()));
36 path.mkdir();
37 return path;
38};
\No newline at end of file