-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
54 lines (46 loc) · 1.2 KB
/
utils.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
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
/** @param {string} dir */
export function mkdirp(dir) {
try {
fs.mkdirSync(dir, { recursive: true });
} catch (e) {
if (e.code === 'EEXIST') return;
throw e;
}
}
/** @param {string} path */
export function rimraf(path) {
(fs.rmSync || fs.rmdirSync)(path, { recursive: true, force: true });
}
/**
* @template T
* @param {T} x
*/
function identity(x) {
return x;
}
export function copy(from, to, rename = identity) {
if (!fs.existsSync(from)) return;
const stack = [{ from, to }];
while (stack.length > 0) {
const { from, to } = stack.pop();
const stats = fs.statSync(from);
if (stats.isDirectory()) {
mkdirp(to);
fs.readdirSync(from).forEach((name) => {
stack.push({
from: path.join(from, name),
to: path.join(to, rename(name)),
});
});
} else {
mkdirp(path.dirname(to));
fs.copyFileSync(from, to);
}
}
}
export function dist(path) {
return fileURLToPath(new URL(`./${path}`, import.meta.url).href);
}