-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathtest-project-host.ts
165 lines (144 loc) · 4.84 KB
/
test-project-host.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @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.dev/license
*/
import {
Path,
PathFragment,
basename,
dirname,
join,
normalize,
relative,
virtualFs,
} from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import { Stats } from 'node:fs';
import {
EMPTY,
Observable,
concatMap,
delay,
finalize,
from,
map,
mergeMap,
of,
retry,
tap,
} from 'rxjs';
/**
* @deprecated
*/
export class TestProjectHost extends NodeJsSyncHost {
private _currentRoot: Path | null = null;
private _scopedSyncHost: virtualFs.SyncDelegateHost<Stats> | null = null;
constructor(protected _templateRoot: Path) {
super();
}
root(): Path {
if (this._currentRoot === null) {
throw new Error('TestProjectHost must be initialized before being used.');
}
return this._currentRoot;
}
scopedSync(): virtualFs.SyncDelegateHost<Stats> {
if (this._currentRoot === null || this._scopedSyncHost === null) {
throw new Error('TestProjectHost must be initialized before being used.');
}
return this._scopedSyncHost;
}
initialize(): Observable<void> {
const recursiveList = (path: Path): Observable<Path> =>
this.list(path).pipe(
// Emit each fragment individually.
concatMap((fragments) => from(fragments)),
// Join the path with fragment.
map((fragment) => join(path, fragment)),
// Emit directory content paths instead of the directory path.
mergeMap((path) =>
this.isDirectory(path).pipe(
concatMap((isDir) => (isDir ? recursiveList(path) : of(path))),
),
),
);
// Find a unique folder that we can write to use as current root.
return this.findUniqueFolderPath().pipe(
// Save the path and create a scoped host for it.
tap((newFolderPath) => {
this._currentRoot = newFolderPath;
this._scopedSyncHost = new virtualFs.SyncDelegateHost(
new virtualFs.ScopedHost(this, this.root()),
);
}),
// List all files in root.
concatMap(() => recursiveList(this._templateRoot)),
// Copy them over to the current root.
concatMap((from) => {
const to = join(this.root(), relative(this._templateRoot, from));
return this.read(from).pipe(concatMap((buffer) => this.write(to, buffer)));
}),
map(() => {}),
);
}
restore(): Observable<void> {
if (this._currentRoot === null) {
return EMPTY;
}
// Delete the current root and clear the variables.
// Wait 50ms and retry up to 10 times, to give time for file locks to clear.
return this.exists(this.root()).pipe(
delay(50),
concatMap((exists) => (exists ? this.delete(this.root()) : EMPTY)),
retry(10),
finalize(() => {
this._currentRoot = null;
this._scopedSyncHost = null;
}),
);
}
writeMultipleFiles(files: { [path: string]: string | ArrayBufferLike | Buffer }): void {
Object.keys(files).forEach((fileName) => {
let content = files[fileName];
if (typeof content == 'string') {
content = virtualFs.stringToFileBuffer(content);
} else if (content instanceof Buffer) {
content = content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength);
}
this.scopedSync().write(normalize(fileName), content);
});
}
replaceInFile(path: string, match: RegExp | string, replacement: string) {
const content = virtualFs.fileBufferToString(this.scopedSync().read(normalize(path)));
this.scopedSync().write(
normalize(path),
virtualFs.stringToFileBuffer(content.replace(match, replacement)),
);
}
appendToFile(path: string, str: string) {
const content = virtualFs.fileBufferToString(this.scopedSync().read(normalize(path)));
this.scopedSync().write(normalize(path), virtualFs.stringToFileBuffer(content.concat(str)));
}
fileMatchExists(dir: string, regex: RegExp): PathFragment | undefined {
const [fileName] = this.scopedSync()
.list(normalize(dir))
.filter((name) => name.match(regex));
return fileName || undefined;
}
copyFile(from: string, to: string) {
const content = this.scopedSync().read(normalize(from));
this.scopedSync().write(normalize(to), content);
}
private findUniqueFolderPath(): Observable<Path> {
// 11 character alphanumeric string.
const randomString = Math.random().toString(36).slice(2);
const newFolderName = `test-project-host-${basename(this._templateRoot)}-${randomString}`;
const newFolderPath = join(dirname(this._templateRoot), newFolderName);
return this.exists(newFolderPath).pipe(
concatMap((exists) => (exists ? this.findUniqueFolderPath() : of(newFolderPath))),
);
}
}