-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileSystemQueue.ts
246 lines (210 loc) · 6.02 KB
/
FileSystemQueue.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// #region preamble
import fs from "fs/promises";
import path from "path";
import replaceInFilePkg from "replace-in-file";
const { replaceInFile } = replaceInFilePkg;
import PathResolver from "./PathResolver.js";
import fileExists from "#cli/utilities/fileExists.js";
import projectRoot from "#cli/utilities/projectRoot.js";
type ConfigFileFormat = {
formatVersion: string;
}
type PromiseQueueTask = () => Promise<void>;
type FSQueueContext = {
command: (
"writeConfiguration" |
never
),
[key: string]: unknown
};
// #endregion preamble
export default class FSQueue
{
readonly #pathResolver: PathResolver;
readonly #requiredToCallOnce = new Set<string | symbol>([
"writeConfiguration",
]);
/** Add a requirement to the queue before committing, and return a symbol key to resolve it. */
addRequirement(label: string) : symbol
{
const requirement = Symbol(label);
this.#requiredToCallOnce.add(requirement);
return requirement;
}
resolveRequirement(requiredSymbol: symbol | undefined) : void
{
if (requiredSymbol)
this.#requiredToCallOnce.delete(requiredSymbol);
}
/** A map of pending tasks to descriptions of the task. */
readonly #tasks = new Map<PromiseQueueTask, string>;
#started = false;
#hasCommitted = false;
constructor(pathResolver: PathResolver)
{
this.#pathResolver = pathResolver.clone();
}
/** Get a list of operations we have not started. */
pendingOperations() : ReadonlyArray<string>
{
return Array.from(this.#tasks.values());
}
/**
* Add writing the configuration to the filesystem to the queue.
* @param config - the configuration to use in its current state.
* @param relativePath - the path to the configuration's filesystem location.
*/
writeConfiguration(
config: ConfigFileFormat,
relativePath: string
) : Promise<void>
{
if (!this.#requiredToCallOnce.has("writeConfiguration"))
throw new Error("You've already requested to write the configuration!");
const contents = JSON.stringify(config, null, 2) + "\n";
this.#requiredToCallOnce.delete("writeConfiguration");
return this.#appendResolverTask(
relativePath,
async () => {
await fs.writeFile(
this.#pathResolver.getPath(true),
contents,
{ encoding: "utf-8" }
)
},
`write configuration to ${path.resolve(this.#pathResolver.getPath(true), relativePath)}`,
{
command: "writeConfiguration",
relativePath,
contents
},
);
}
/**
* Build a source application directory, from Motherhen's `cleanroom/source` directory.
* @param targetSourcesDirectory - the "sources" directory location. Normally `{$projectRoot}/sources`.
* @param sourceDirName - The source directory to create under the target sources.
* @param requiredSymbol - a flag to clear, signalling we've created a required sources directory.
*/
buildSource(
targetSourcesDirectory: string,
sourceDirName: string,
requiredSymbol: symbol | undefined
) : Promise<void>
{
this.resolveRequirement(requiredSymbol);
const targetDir = path.join(
targetSourcesDirectory, sourceDirName
);
const templateDir = path.join(
projectRoot, "cleanroom/source"
);
this.#tasks.set(
async () => {
const parentDir = path.dirname(targetDir);
if (!await fileExists(parentDir, true)) {
await fs.mkdir(parentDir, { recursive: true });
}
await fs.cp(templateDir, targetDir, {
recursive: true
});
if (sourceDirName === "hatchedegg")
return;
await replaceInFile({
files: targetDir + "/**/*",
from: /hatchedegg/gm,
to: sourceDirName
});
},
`build source directory "${sourceDirName}"`
);
return Promise.resolve();
}
/**
* Add a task requiring use of the path resolver.
* @param overridePath - the relative path to use.
* @param task - the asynchronous callback.
* @param context - console.warn metadata in case the callback fails.
*/
#appendResolverTask(
overridePath: string,
task: PromiseQueueTask,
description: string,
context: FSQueueContext
) : Promise<void>
{
this.#assertNotStarted();
this.#tasks.set(
() => this.#withTemporaryPath(overridePath, task, context),
description
);
return Promise.resolve();
}
async #withTemporaryPath(
overridePath: string,
task: PromiseQueueTask,
context?: FSQueueContext
) : Promise<void>
{
const currentPath = this.#pathResolver.getPath(false);
this.#pathResolver.setPath(false, overridePath);
try {
return await task();
}
catch (ex) {
if (this.#enableWarnings && context) {
console.warn(context);
}
throw ex;
}
finally {
this.#pathResolver.setPath(false, currentPath);
}
}
/** Run all tasks in the queue. */
async commit() : Promise<void>
{
this.#assertNotStarted();
if (this.#requiredToCallOnce.size > 0) {
if (this.#enableWarnings) {
console.warn(Array.from(this.#requiredToCallOnce.values()).map(String).join(", "));
}
throw new Error("You have required tasks to execute!");
}
this.#started = true;
const tasks = Array.from(this.#tasks.keys());
while (tasks.length) {
await (tasks.shift() as PromiseQueueTask)();
}
this.#hasCommitted = true;
}
hasCommitted() : boolean
{
return this.#hasCommitted;
}
#assertNotStarted() : void
{
if (this.#started)
throw new Error("I have already started running tasks!");
}
#enableWarnings = true;
/**
* Suspend warnings to the console. This is for tests only.
* @param callback - a callback to run during the suspension.
* @returns the result of the callback.
*
* @internal
*/
async suspendWarnings<T>(
callback: () => Promise<T>
) : Promise<T>
{
this.#enableWarnings = false;
try {
return await callback();
}
finally {
this.#enableWarnings = true;
}
}
}