forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
51 lines (44 loc) · 1.04 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
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 (/** @type {any} */ (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;
}
/**
* @param {string} from
* @param {string} to
* @param {(basename: string) => string} rename
*/
export function copy(from, to, rename = identity) {
if (!fs.existsSync(from)) return;
const stats = fs.statSync(from);
if (stats.isDirectory()) {
fs.readdirSync(from).forEach((file) => {
copy(path.join(from, file), path.join(to, rename(file)));
});
} else {
mkdirp(path.dirname(to));
fs.copyFileSync(from, to);
}
}
/** @param {string} path */
export function dist(path) {
return fileURLToPath(new URL(`./dist/${path}`, import.meta.url).href);
}