Repository navigation
Expand file tree
/
Copy pathbuild_css.js
More file actions
executable file
·228 lines (188 loc) · 6.1 KB
/
Copy pathbuild_css.js
File metadata and controls
executable file
·228 lines (188 loc) · 6.1 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env node
// Concatenates per-bundle CSS sources into app/assets/builds/ and copies
// vendor/font/image assets alongside. Pass --watch to rebuild on changes
// to any path in WATCH_PATHS (debounced via chokidar).
const fs = require('fs');
const path = require('path');
// Directories and files to watch
const WATCH_PATHS = [
'app/assets/stylesheets',
'node_modules/bootstrap/dist/css',
'node_modules/bootstrap-icons/font',
];
function ensureDirectoryExists(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
function readFileIfExists(filePath) {
try {
if (fs.existsSync(filePath)) {
return fs.readFileSync(filePath, 'utf8');
}
} catch (error) {
console.warn(`Warning: Could not read ${filePath}:`, error.message);
}
return '';
}
function copyFileIfExists(src, dest) {
try {
if (fs.existsSync(src)) {
ensureDirectoryExists(path.dirname(dest));
fs.copyFileSync(src, dest);
return true;
}
} catch (error) {
console.warn(`Warning: Could not copy ${src} to ${dest}:`, error.message);
}
return false;
}
const STYLES = 'app/assets/stylesheets';
// Every bundle starts with Bootstrap 5, its icons, Quepid's fonts, and the
// page skin shared by both bundles.
const BOOTSTRAP_BASE = [
'node_modules/bootstrap/dist/css/bootstrap.css',
'node_modules/bootstrap-icons/font/bootstrap-icons.css',
`${STYLES}/fonts.css`,
`${STYLES}/base.css`,
];
// Concatenates `files` in order (order matters for the cascade) into
// app/assets/builds/<name>.css.
function writeBundle(name, title, files) {
console.log(`Building ${name}.css...`);
const outputFile = `app/assets/builds/${name}.css`;
let output = `/* ${title} */\n`;
output += `/* Generated on ${new Date().toISOString()} */\n`;
output += '\n';
for (const file of files) {
output += readFileIfExists(file);
output += '\n';
}
fs.writeFileSync(outputFile, output);
const stats = fs.statSync(outputFile);
console.log(`${name}.css created (${(stats.size / 1024).toFixed(1)}KB)`);
}
function buildApplicationCSS() {
writeBundle('application', 'Application CSS Bundle (Bootstrap 5)', [
...BOOTSTRAP_BASE,
`${STYLES}/navbar-brand.css`,
`${STYLES}/application-overrides.css`,
`${STYLES}/signup.css`,
`${STYLES}/judgements.css`,
]);
}
function buildCoreCSS() {
writeBundle('core', 'Core CSS Bundle (Bootstrap 5 for the core case UI)', [
...BOOTSTRAP_BASE,
'node_modules/tom-select/dist/css/tom-select.bootstrap5.css',
// Quepid layout, then the BS5 overrides (px sizing, popovers, header nav,
// modals, sub-results toolbar, ...).
`${STYLES}/core-additions.css`,
`${STYLES}/navbar-brand.css`,
`${STYLES}/core-overrides.css`,
`${STYLES}/style.css`,
`${STYLES}/panes.css`,
`${STYLES}/stackedChart.css`,
// Tour/Guides
'node_modules/shepherd.js/dist/css/shepherd.css',
`${STYLES}/tour.css`,
// Screen-specific styles
`${STYLES}/docs.css`,
`${STYLES}/qscore.css`,
`${STYLES}/qgraph.css`,
// Other styles
`${STYLES}/misc.css`,
`${STYLES}/animation.css`,
`${STYLES}/froggy.css`,
]);
}
// Standalone stylesheets linked from layouts/_case_head.html.erb (not folded into core.css).
function copyLinkedStylesheets() {
console.log('Copying linked stylesheets...');
ensureDirectoryExists('app/assets/builds');
copyFileIfExists(
'app/assets/stylesheets/json-explorer.css',
'app/assets/builds/json-explorer.css'
);
}
function copyFontFiles() {
console.log('Copying font files...');
ensureDirectoryExists('app/assets/builds/fonts');
copyFileIfExists('node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff', 'app/assets/builds/fonts/bootstrap-icons.woff');
copyFileIfExists('node_modules/bootstrap-icons/font/fonts/bootstrap-icons.woff2', 'app/assets/builds/fonts/bootstrap-icons.woff2');
}
function copyImageFiles() {
console.log('Copying image files...');
ensureDirectoryExists('app/assets/builds/images');
copyFileIfExists('public/images/querqy-icon.png', 'app/assets/builds/images/querqy-icon.png');
}
function buildAllCSS() {
console.log('Building CSS bundles...');
try {
// Create builds directory if it doesn't exist
ensureDirectoryExists('app/assets/builds');
// Build all CSS bundles
buildApplicationCSS();
buildCoreCSS();
// Copy linked stylesheets and other asset files
copyLinkedStylesheets();
copyFontFiles();
copyImageFiles();
console.log('CSS bundles created successfully!');
return true;
} catch (error) {
console.error('Error building CSS:', error.message);
return false;
}
}
// Main execution
function main() {
const isWatchMode = process.argv.includes('--watch');
// Initial build
buildAllCSS();
if (isWatchMode) {
console.log('Watching for CSS changes...');
try {
const chokidar = require('chokidar');
let debounceTimer;
const DEBOUNCE_DELAY = 300; // Wait 300ms before rebuilding
const watcher = chokidar.watch(WATCH_PATHS, {
ignored: [/(^|[\/\\])\./, 'node_modules/.bin', 'app/assets/builds'],
persistent: true,
awaitWriteFinish: {
stabilityThreshold: 100,
pollInterval: 100
}
});
const debouncedRebuild = () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
console.log('Rebuilding CSS...');
buildAllCSS();
}, DEBOUNCE_DELAY);
};
watcher.on('change', (path) => {
console.log(`CSS file changed: ${path}`);
debouncedRebuild();
});
watcher.on('add', () => {
debouncedRebuild();
});
watcher.on('unlink', (path) => {
console.log(`CSS file removed: ${path}`);
debouncedRebuild();
});
watcher.on('error', error => {
console.error('CSS watcher error:', error);
});
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
console.error('chokidar not found. Please run: npm install');
process.exit(1);
}
throw error;
}
}
}
// Run the script
main();