Skip to content

Commit f535037

Browse files
committed
switched to proper Set and Map instances
replacing poor man's versions in the process, also got rid of obsolete workarounds for legacy versions
1 parent b925e32 commit f535037

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

lib/util/files/index.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
11
"use strict";
22

3-
let { abort, repr } = require("../");
43
let { writeFile } = require("fs/promises");
54
let { mkdirSync } = require("fs");
65
let path = require("path");
76

8-
let KNOWN = {}; // avoids redundant `mkdirp` invocations
9-
let LOCKS = {};
7+
let KNOWN = new Set(); // avoids redundant `mkdir` invocations
8+
let LOCKS = new Map();
109

1110
// avoids concurrent write operations and creates target directory if necessary
1211
module.exports = function createFile(filepath, contents) {
13-
let lock = LOCKS[filepath];
12+
let lock = LOCKS.get(filepath);
1413
if(lock) { // defer
1514
return lock.then(_ => createFile(filepath, contents));
1615
}
1716

1817
// create directory if necessary
19-
if(!KNOWN[filepath]) {
20-
KNOWN[filepath] = true;
21-
// NB: `sync` avoids race condition for subsequent operations
22-
mkdirpSync(path.dirname(filepath));
18+
if(!KNOWN.has(filepath)) {
19+
KNOWN.add(filepath);
20+
mkdirSync(path.dirname(filepath), { recursive: true });
2321
}
2422

2523
let prom = writeFile(filepath, contents);
26-
LOCKS[filepath] = prom;
24+
LOCKS.set(filepath, prom);
2725
return prom.then(_ => {
28-
delete LOCKS[filepath];
26+
LOCKS.delete(filepath);
2927
});
3028
};
31-
32-
function mkdirpSync(directory) {
33-
try {
34-
// NB: `recursive` option was introduced in Node v10.12.0
35-
mkdirSync(directory, { recursive: true });
36-
} catch(err) {
37-
abort(`ERROR: auto-creating ${repr(directory)} requires ` +
38-
"Node v10.12.0 or above");
39-
}
40-
}

0 commit comments

Comments
 (0)