-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex.ts
58 lines (50 loc) · 1.48 KB
/
index.ts
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { Path, PathFragment, getSystemPath, join, normalize, virtualFs } from '../../src';
import { NodeJsSyncHost } from '../host';
/**
* A Sync Scoped Host that creates a temporary directory and scope to it.
*/
export class TempScopedNodeJsSyncHost extends virtualFs.ScopedHost<fs.Stats> {
protected _sync?: virtualFs.SyncDelegateHost<fs.Stats>;
protected override _root: Path;
constructor() {
const root = normalize(path.join(os.tmpdir(), `devkit-host-${+Date.now()}-${process.pid}`));
fs.mkdirSync(getSystemPath(root));
super(new NodeJsSyncHost(), root);
this._root = root;
}
get files(): Path[] {
const sync = this.sync;
function _visit(p: Path): Path[] {
return sync
.list(p)
.map((fragment) => join(p, fragment))
.reduce((files, path) => {
if (sync.isDirectory(path)) {
return files.concat(_visit(path));
} else {
return files.concat(path);
}
}, [] as Path[]);
}
return _visit(normalize('/'));
}
get root() {
return this._root;
}
get sync() {
if (!this._sync) {
this._sync = new virtualFs.SyncDelegateHost<fs.Stats>(this);
}
return this._sync;
}
}