Skip to content

Commit 184e315

Browse files
committed
feat: Add copyFile and minifyFile build scripts (#8761)
1 parent 15a9a1e commit 184e315

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

buildScripts/copyFile.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Command} from 'commander/esm.mjs';
2+
import fs from 'fs-extra';
3+
import path from 'path';
4+
import {sanitizeInput} from './util/Sanitizer.mjs';
5+
6+
const program = new Command('copyFile')
7+
.version('1.0.0')
8+
.option('-s, --source <value>', 'path to the source file', sanitizeInput)
9+
.option('-t, --target <value>', 'path to the target file', sanitizeInput)
10+
.allowUnknownOption()
11+
.parse(process.argv);
12+
13+
const programOpts = program.opts();
14+
15+
if (!programOpts.source) {
16+
throw new Error('Missing -s param');
17+
}
18+
19+
if (!programOpts.target) {
20+
throw new Error('Missing -t param');
21+
}
22+
23+
fs.mkdirpSync(path.dirname(programOpts.target));
24+
fs.copySync(programOpts.source, programOpts.target);

buildScripts/minifyFile.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import fs from 'fs-extra';
2+
import {minify} from 'terser';
3+
import path from 'path';
4+
5+
const [,, source, target] = process.argv;
6+
7+
if (!source || !target) {
8+
console.error('Usage: node minifyFile.mjs <source> <target>');
9+
process.exit(1);
10+
}
11+
12+
(async () => {
13+
try {
14+
const code = fs.readFileSync(source, 'utf8');
15+
const result = await minify(code, {module: true});
16+
17+
fs.mkdirpSync(path.dirname(target));
18+
fs.outputFileSync(target, result.code);
19+
} catch (e) {
20+
console.error('Minification failed:', e);
21+
process.exit(1);
22+
}
23+
})();

0 commit comments

Comments
 (0)