-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepro.js
More file actions
43 lines (37 loc) · 1 KB
/
repro.js
File metadata and controls
43 lines (37 loc) · 1 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
/// <reference types="node" />
import fs from "node:fs";
import path from "node:path";
/** @type {Set<string>} */
const created = new Set();
/** @type {Map<string, Promise<void>>} */
const promises = new Map();
const dir = ".tmp/deep1/deep2/deep3";
const name = "name";
const code = "ok";
async function reproWriteAndRead() {
const { id } = await reproWrite();
return fs.readFileSync(id, "utf-8");
}
async function reproWrite() {
const tmp = path.join(dir, name);
if (promises.has(tmp)) {
await promises.get(tmp);
return { id: tmp };
}
if (!created.has(dir)) {
await fs.promises.mkdir(dir, { recursive: true });
created.add(dir);
}
promises.set(
tmp,
fs.promises.writeFile(tmp, code).finally(() => promises.delete(tmp)),
);
await promises.get(tmp);
return { id: tmp };
}
async function main() {
await fs.promises.rm(".tmp", { recursive: true, force: true });
const results = await Promise.all([reproWriteAndRead(), reproWriteAndRead()]);
console.log(results);
}
main();