-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassets-symlink-maker.mjs
54 lines (43 loc) · 1.33 KB
/
assets-symlink-maker.mjs
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 'fs';
import path from 'path';
function assetsSymlinkMaker(
/** @type {string} */ tmpDir, // NOTE: ".watch/"
/** @type {string} */ distDir, // NOTE: "dist/"
/** @type {string} */ assetDirName // NOTE: "assets"
) {
const tmpPath = path.resolve(tmpDir);
if (! fs.existsSync(tmpPath)) {
fs.mkdirSync(tmpPath, { recursive: true });
}
fs.readdirSync(path.resolve(distDir), {}).forEach(dirName => {
const distPath = path.resolve(path.join(distDir, dirName));
if (dirName !== assetDirName) {
if (! fs.statSync(distPath).isDirectory()) {
return;
}
fs.readdirSync(distPath, {}).forEach(_dirName => {
const _distPath = path.resolve(path.join(distDir, dirName, _dirName));
if (_dirName !== assetDirName) {
return;
}
const tempPath = path.join(tmpPath, dirName, _dirName);
if (fs.existsSync(tempPath)) {
return;
}
if (! fs.existsSync(path.join(tmpPath, dirName))) {
fs.mkdirSync(path.join(tmpPath, dirName), { recursive: true });
}
fs.symlinkSync(_distPath, tempPath, 'dir');
});
return;
}
const tempPath = path.join(tmpPath, dirName);
if (fs.existsSync(tempPath)) {
return;
}
fs.symlinkSync(distPath, tempPath, 'dir');
});
}
export {
assetsSymlinkMaker
}