-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathcopy-assets.mjs
More file actions
73 lines (61 loc) · 1.8 KB
/
copy-assets.mjs
File metadata and controls
73 lines (61 loc) · 1.8 KB
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
import fs from 'node:fs';
import path from 'node:path';
import fse from 'fs-extra';
const WATCH_FLAG = '--watch';
const ASSET_EXTENSIONS = new Set(['.css']);
const IGNORED_EXTENSIONS = new Set(['.ts', '.tsx', '.d.ts']);
const cwd = process.cwd();
const srcRoot = path.join(cwd, 'src', 'theme');
const destRoot = path.join(cwd, 'lib', 'theme');
async function copyAssetFile(filePath) {
const relativePath = path.relative(srcRoot, filePath);
const destPath = path.join(destRoot, relativePath);
await fse.ensureDir(path.dirname(destPath));
await fse.copyFile(filePath, destPath);
}
async function copyAssetsOnce() {
if (!(await fse.pathExists(srcRoot))) {
return;
}
const entries = await fse.readdir(srcRoot, { recursive: true });
const filePaths = entries
.filter((entry) => typeof entry === 'string')
.map((entry) => path.join(srcRoot, entry))
.filter((entryPath) => fs.statSync(entryPath).isFile());
await Promise.all(
filePaths
.filter((filePath) => {
const extension = path.extname(filePath);
if (IGNORED_EXTENSIONS.has(extension)) {
return false;
}
return ASSET_EXTENSIONS.has(extension);
})
.map((filePath) => copyAssetFile(filePath)),
);
}
function watchAssets() {
if (!fs.existsSync(srcRoot)) {
return;
}
copyAssetsOnce();
fs.watch(srcRoot, { recursive: true }, (_eventType, filename) => {
if (!filename) {
return;
}
const filePath = path.join(srcRoot, filename);
const extension = path.extname(filePath);
if (IGNORED_EXTENSIONS.has(extension) || !ASSET_EXTENSIONS.has(extension)) {
return;
}
if (!fs.existsSync(filePath)) {
return;
}
copyAssetFile(filePath);
});
}
if (process.argv.includes(WATCH_FLAG)) {
watchAssets();
} else {
copyAssetsOnce();
}