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