-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
57 lines (53 loc) · 1.21 KB
/
helpers.js
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
//@ts-nocheck
const fs = require('fs');
const path = require('path');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const lstat = util.promisify(fs.lstat);
const unlink = util.promisify(fs.unlink);
const rmdir = util.promisify(fs.rmdir);
const writeFile = util.promisify(fs.writeFile);
const readFile = util.promisify(fs.readFile);
const rename = util.promisify(fs.rename);
function getPackageFolder(package) {
const unpacked = path.resolve(__dirname, '../.yarn/unplugged');
const items = fs.readdirSync(unpacked);
return path.resolve(
path.resolve(
unpacked,
items.find((item) => item.split('-npm')[0] == package)
),
`node_modules/${package}`
);
}
const removeDir = async (dir) => {
try {
const files = await readdir(dir);
await Promise.all(
files.map(async (file) => {
try {
const p = path.join(dir, file);
const stat = await lstat(p);
if (stat.isDirectory()) {
await removeDir(p);
} else {
await unlink(p);
}
} catch (err) {
console.error(err);
}
})
);
await rmdir(dir);
} catch (err) {
console.error(err);
}
};
module.exports = {
rmrf: removeDir,
readdir,
writeFile,
readFile,
rename,
getPackageFolder,
};