-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
filesystem.js
142 lines (118 loc) · 3.13 KB
/
filesystem.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import fs from 'fs';
import path from 'path';
/** @param {string} dir */
export function mkdirp(dir) {
try {
fs.mkdirSync(dir, { recursive: true });
} catch (/** @type {any} */ e) {
if (e.code === 'EEXIST') {
if (!fs.statSync(dir).isDirectory()) {
throw new Error(`Cannot create directory ${dir}, a file already exists at this position`);
}
return;
}
throw e;
}
}
/** @param {string} path */
export function rimraf(path) {
fs.rmSync(path, { force: true, recursive: true });
}
/**
* @param {string} source
* @param {string} target
* @param {{
* filter?: (basename: string) => boolean;
* replace?: Record<string, string>;
* }} opts
*/
export function copy(source, target, opts = {}) {
if (!fs.existsSync(source)) return [];
/** @type {string[]} */
const files = [];
const prefix = posixify(target) + '/';
const regex = opts.replace
? new RegExp(`\\b(${Object.keys(opts.replace).join('|')})\\b`, 'g')
: null;
/**
* @param {string} from
* @param {string} to
*/
function go(from, to) {
if (opts.filter && !opts.filter(path.basename(from))) return;
const stats = fs.statSync(from);
if (stats.isDirectory()) {
fs.readdirSync(from).forEach((file) => {
go(path.join(from, file), path.join(to, file));
});
} else {
mkdirp(path.dirname(to));
if (opts.replace) {
const data = fs.readFileSync(from, 'utf-8');
fs.writeFileSync(
to,
data.replace(
/** @type {RegExp} */ (regex),
(_match, key) => /** @type {Record<string, string>} */ (opts.replace)[key]
)
);
} else {
fs.copyFileSync(from, to);
}
files.push(to === target ? posixify(path.basename(to)) : posixify(to).replace(prefix, ''));
}
}
go(source, target);
return files;
}
/**
* Get a list of all files in a directory
* @param {string} cwd - the directory to walk
* @param {boolean} [dirs] - whether to include directories in the result
*/
export function walk(cwd, dirs = false) {
/** @type {string[]} */
const all_files = [];
/** @param {string} dir */
function walk_dir(dir) {
const files = fs.readdirSync(path.join(cwd, dir));
for (const file of files) {
const joined = path.join(dir, file);
const stats = fs.statSync(path.join(cwd, joined));
if (stats.isDirectory()) {
if (dirs) all_files.push(joined);
walk_dir(joined);
} else {
all_files.push(joined);
}
}
}
return walk_dir(''), all_files;
}
/** @param {string} str */
export function posixify(str) {
return str.replace(/\\/g, '/');
}
/**
* Given an entry point like [cwd]/src/hooks, returns a filename like [cwd]/src/hooks.js or [cwd]/src/hooks/index.js
* @param {string} entry
* @returns {string|null}
*/
export function resolve_entry(entry) {
if (fs.existsSync(entry)) {
const stats = fs.statSync(entry);
if (stats.isDirectory()) {
return resolve_entry(path.join(entry, 'index'));
}
return entry;
} else {
const dir = path.dirname(entry);
if (fs.existsSync(dir)) {
const base = path.basename(entry);
const files = fs.readdirSync(dir);
const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);
if (found) return path.join(dir, found);
}
}
return null;
}