-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
121 lines (102 loc) · 3.31 KB
/
build.js
File metadata and controls
121 lines (102 loc) · 3.31 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
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
#!/usr/bin/env node
/**
* Build AICW Ask AI Widget
*
* 1. Validate syntax (stop if errors found)
* 2. Minify with terser
* 3. Add version banner
*
* Flags:
* --watch Watch src/widget.js and rebuild on change
* --validate-only Only validate syntax, skip minification
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const vm = require('vm');
const SOURCE_FILE = path.join(__dirname, 'src', 'widget.js');
const OUTPUT_FILE = path.join(__dirname, 'dist', 'aicw-summarize.min.js');
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
blue: '\x1b[34m',
yellow: '\x1b[33m',
red: '\x1b[31m',
gray: '\x1b[90m'
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
function generateVersion() {
const now = new Date();
const pad = (n) => String(n).padStart(2, '0');
const date = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}`;
const time = `${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
return `${date}.${time}`;
}
function getFileSize(filePath) {
return (fs.statSync(filePath).size / 1024).toFixed(2);
}
function validate() {
log('Validating syntax...', 'blue');
const code = fs.readFileSync(SOURCE_FILE, 'utf8');
try {
new vm.Script(code, { filename: 'widget.js' });
log(' Syntax OK', 'green');
return true;
} catch (error) {
log(` Syntax error: ${error.message}`, 'red');
return false;
}
}
function build() {
log('\nBuilding AICW Ask AI Widget...', 'blue');
log(colors.gray + '-'.repeat(50) + colors.reset);
if (!fs.existsSync(SOURCE_FILE)) {
log(`Source file not found: ${SOURCE_FILE}`, 'red');
process.exit(1);
}
if (!validate()) {
log('Build stopped due to validation errors', 'red');
process.exit(1);
}
const sourceSizeBefore = getFileSize(SOURCE_FILE);
log(` Source: ${path.relative(process.cwd(), SOURCE_FILE)} (${sourceSizeBefore} KB)`, 'gray');
log('Minifying...', 'blue');
try {
execSync(`npx -y terser "${SOURCE_FILE}" -c -m -o "${OUTPUT_FILE}"`, { stdio: 'pipe' });
} catch (error) {
log('Minification failed: ' + error.message, 'red');
process.exit(1);
}
const version = generateVersion();
const banner = `/*! AICW Ask AI Widget v${version} */`;
const minified = fs.readFileSync(OUTPUT_FILE, 'utf8');
fs.writeFileSync(OUTPUT_FILE, banner + minified);
const outputSize = getFileSize(OUTPUT_FILE);
const savings = ((1 - (parseFloat(outputSize) / parseFloat(sourceSizeBefore))) * 100).toFixed(1);
log(` Output: ${path.relative(process.cwd(), OUTPUT_FILE)} (${outputSize} KB)`, 'gray');
log(` Size reduction: ${savings}%`, 'green');
log(colors.gray + '-'.repeat(50) + colors.reset);
log(`Build complete! (v${version})`, 'green');
}
// Parse flags
const args = process.argv.slice(2);
if (args.includes('--validate-only')) {
if (!validate()) process.exit(1);
process.exit(0);
}
if (args.includes('--watch')) {
build();
log('\nWatching for changes...', 'blue');
let debounce = null;
fs.watch(SOURCE_FILE, () => {
if (debounce) clearTimeout(debounce);
debounce = setTimeout(() => {
log('\nFile changed, rebuilding...', 'yellow');
try { build(); } catch (e) { log(e.message, 'red'); }
}, 200);
});
} else {
build();
}