@agentrix/cli 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ import { readdir, readFile, stat, writeFile } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { minify } from 'terser';
4
+
5
+ const distDir = path.resolve(process.cwd(), 'dist');
6
+ const jsExtensions = new Set(['.js', '.mjs', '.cjs']);
7
+ const dtsSuffixes = ['.d.ts', '.d.mts', '.d.cts'];
8
+
9
+ function isJavaScriptFile(fileName) {
10
+ if (!jsExtensions.has(path.extname(fileName))) {
11
+ return false;
12
+ }
13
+ return !dtsSuffixes.some((suffix) => fileName.endsWith(suffix));
14
+ }
15
+
16
+ async function collectFiles(dir, files) {
17
+ const entries = await readdir(dir, { withFileTypes: true });
18
+ for (const entry of entries) {
19
+ const fullPath = path.join(dir, entry.name);
20
+ if (entry.isDirectory()) {
21
+ await collectFiles(fullPath, files);
22
+ continue;
23
+ }
24
+ if (isJavaScriptFile(entry.name)) {
25
+ files.push(fullPath);
26
+ }
27
+ }
28
+ }
29
+
30
+ async function minifyFile(filePath) {
31
+ const code = await readFile(filePath, 'utf8');
32
+ const ext = path.extname(filePath);
33
+ const result = await minify(code, {
34
+ module: ext === '.mjs',
35
+ compress: { passes: 2 },
36
+ mangle: true,
37
+ format: { comments: false }
38
+ });
39
+ if (!result.code) {
40
+ throw new Error(`Terser returned empty output for ${filePath}`);
41
+ }
42
+ await writeFile(filePath, result.code, 'utf8');
43
+ }
44
+
45
+ async function main() {
46
+ const stats = await stat(distDir);
47
+ if (!stats.isDirectory()) {
48
+ throw new Error(`dist is not a directory: ${distDir}`);
49
+ }
50
+ const files = [];
51
+ await collectFiles(distDir, files);
52
+ await Promise.all(files.map((filePath) => minifyFile(filePath)));
53
+ }
54
+
55
+ main().catch((error) => {
56
+ console.error('[minify-dist] failed:', error);
57
+ process.exit(1);
58
+ });