forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
45 lines (39 loc) · 863 Bytes
/
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
import fs from 'fs';
import path from 'path';
/** @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;
}
/**
* @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);
}
}