-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocalDisk.ts
More file actions
72 lines (66 loc) · 2.65 KB
/
LocalDisk.ts
File metadata and controls
72 lines (66 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as path from 'path';
import * as fse from 'fs-extra';
import { LocalDiskConfig } from './types';
import { FSDisk } from '../../lib/fs/FSDisk';
import { AsyncFSModule } from '../..';
/**
* Used to cache the calculated root path without conflicts.
*/
export const rootPathCacheKey: unique symbol = Symbol(
'localDiskCachedRootPath',
);
/**
* Represents a local filesystem to be used as a disk.
*
* This implementation is OS-independent and always takes posix-like absolute and relative paths
* in all of its public method arguments to refer to directories and files on the disk.
*
* The only OS-dependent care that needs to be taken is in the `root` config option which must be
* a path in the form that the OS expects (i.e. using backslashes and drive letters on windows).
*/
export class LocalDisk extends FSDisk {
/**
* The cached calculated root path.
*/
private [rootPathCacheKey]: string | undefined;
/**
* @inheritDoc
*/
protected config!: LocalDiskConfig;
/**
* Use the fs-extras which already includes all of the promisified node fs methods and mkdirp
*/
protected getAsyncFsModule(): AsyncFSModule {
return fse;
}
/**
* Get the absolute root path on the local filesystem based on the `root` config option.
*
* Note that this expects the `root` option in the config to be accurate to the local
* filesystem's OS. For example if you specify `'/foo/bar'` as the `root` config option,
* the absolute root used on POSIX (mac and linux) systems will be `/foo/bar` but on windows
* systems will be (`<cwd>/foo/bar`) since absolute paths on windows must start with the drive
* letter, a colon, and a backslash.
*
* @see path.isAbsolute
* @see https://nodejs.org/docs/latest/api/path.html#path_windows_vs_posix
*/
public getRootPath(): string {
/*
* We memoize essentially by caching the calculated root path on the class instance.
* This assumes the config of the instance never changes.
*/
if (typeof this[rootPathCacheKey] !== 'string') {
const { root: rawRoot = null } = this.config;
if (rawRoot) {
const root = `${rawRoot}`.trim().replace(/[/\\]*$/, '');
// `path.resolve` automatically resolve non-absolute paths from the CWD.
this[rootPathCacheKey] = path.resolve(root);
} else {
// If not root was provided we use the server process current working directory.
this[rootPathCacheKey] = path.resolve('.');
}
}
return this[rootPathCacheKey] as string;
}
}