-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathbuild_templates.js
More file actions
117 lines (92 loc) · 3.01 KB
/
Copy pathbuild_templates.js
File metadata and controls
117 lines (92 loc) · 3.01 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
// Script to compile Angular templates into a JavaScript module
// This replaces the angular-rails-templates gem functionality
const fs = require('fs');
const path = require('path');
const TEMPLATE_DIRS = [
'app/assets/javascripts/components',
'app/assets/templates'
];
const OUTPUT_FILE = 'app/assets/builds/angular_templates.js';
function escapeHtml(html) {
return html
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r');
}
function processDirectory(dir, baseDir = '', templates = {}) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
processDirectory(fullPath, path.join(baseDir, file), templates);
} else if (file.endsWith('.html') || file.endsWith('.html.erb')) {
let content = fs.readFileSync(fullPath, 'utf8');
// Remove .erb extension if present for the template name
let templateName = path.join(baseDir, file).replace(/\.erb$/, '');
// Store template content
templates[templateName] = content;
}
});
return templates;
}
function generateTemplateModule() {
let templates = {};
// Process all template directories
TEMPLATE_DIRS.forEach(dir => {
processDirectory(dir, '', templates);
});
let output = `// Angular Templates Bundle
// Generated on ${new Date().toISOString()}
(function() {
'use strict';
angular.module('templates', [])
.run(['$templateCache', function($templateCache) {
`;
Object.keys(templates).forEach(templateName => {
const content = escapeHtml(templates[templateName]);
output += ` $templateCache.put('${templateName}', '${content}');\n`;
});
output += ` }]);
})();
`;
fs.writeFileSync(OUTPUT_FILE, output);
console.log(`Generated ${Object.keys(templates).length} templates in ${OUTPUT_FILE}`);
console.log(`File size: ${(fs.statSync(OUTPUT_FILE).size / 1024).toFixed(1)}KB`);
}
// Run the template generation
generateTemplateModule();
// Watch mode - rebuild if any template files change
if (process.argv.includes('--watch')) {
console.log('Watching for template changes...');
const chokidar = require('chokidar');
let debounceTimer;
const DEBOUNCE_DELAY = 300; // Wait 300ms before rebuilding
const watcher = chokidar.watch(TEMPLATE_DIRS, {
ignored: [/(^|[\/\\])\./, 'node_modules', 'app/assets/builds'],
persistent: true,
awaitWriteFinish: {
stabilityThreshold: 100,
pollInterval: 100
}
});
const debouncedRebuild = () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
console.log('Rebuilding templates...');
generateTemplateModule();
}, DEBOUNCE_DELAY);
};
watcher.on('change', (path) => {
console.log(`Template changed: ${path}`);
debouncedRebuild();
});
watcher.on('add', (path) => {
debouncedRebuild();
});
watcher.on('unlink', (path) => {
console.log(`Template removed: ${path}`);
debouncedRebuild();
});
}