-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.js
More file actions
316 lines (261 loc) · 9.33 KB
/
generator.js
File metadata and controls
316 lines (261 loc) · 9.33 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env node
/**
* Manifest-Driven Content Generator
*
* Main entry point for manifest-based content generation.
* Loads manifest, validates configuration, extracts content, and generates output files.
*/
const fs = require('fs');
const path = require('path');
const {
loadManifest,
validateManifest,
validateFiles,
processExtractions
} = require('./extractor');
const {
findPlaceholders,
validatePlaceholders,
generateOutput
} = require('./template-processor');
// ANSI color codes for terminal output
const colors = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m'
};
/**
* Print error message
*/
function printError(error) {
console.error(`${colors.red}ERROR: ${error.message}${colors.reset}`);
if (error.file) {
console.error(` File: ${error.file}`);
}
if (error.document) {
console.error(` Document: ${error.document}`);
}
if (error.extraction) {
console.error(` Extraction: ${error.extraction}`);
}
if (error.details) {
console.error(` Details: ${error.details}`);
}
if (error.validValues) {
console.error(` Valid values: ${error.validValues.join(', ')}`);
}
if (error.resolvedPath) {
console.error(` Resolved path: ${error.resolvedPath}`);
}
console.error('');
}
/**
* Print warning message
*/
function printWarning(warning) {
console.warn(`${colors.yellow}WARNING: ${warning.message}${colors.reset}`);
if (warning.placeholder) {
console.warn(` Placeholder: //${warning.placeholder}`);
}
if (warning.extraction) {
console.warn(` Extraction: ${warning.extraction}`);
}
if (warning.suggestion) {
console.warn(` ${warning.suggestion}`);
}
console.warn('');
}
/**
* Print success message
*/
function printSuccess(message) {
console.log(`${colors.green}✓ ${message}${colors.reset}`);
}
/**
* Print info message
*/
function printInfo(message) {
console.log(`${colors.cyan}${message}${colors.reset}`);
}
/**
* Display usage information
*/
function showUsage() {
console.log(`
${colors.cyan}Manifest-Driven Content Generator${colors.reset}
Usage: node manifest-generator.js [options]
Options:
--manifest <file> Path to manifest YAML file (default: manifest.yml)
--validate Validate manifest without generating content
--help, -h Show this help message
Examples:
node manifest-generator.js
node manifest-generator.js --manifest my-config.yml
node manifest-generator.js --validate
node manifest-generator.js --manifest manifest.yml --validate
Documentation:
See SCOPE_TYPES.md for scope type reference
See VALIDATION.md for validation rules
See manifest.yml for configuration examples
`);
}
/**
* Parse command line arguments
*/
function parseArgs() {
const args = process.argv.slice(2);
const config = {
manifestPath: 'manifest.yml',
validateOnly: false,
showHelp: false
};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--help' || arg === '-h') {
config.showHelp = true;
} else if (arg === '--validate') {
config.validateOnly = true;
} else if (arg === '--manifest') {
if (i + 1 < args.length) {
config.manifestPath = args[i + 1];
i++;
} else {
console.error(`${colors.red}Error: --manifest requires a file path${colors.reset}`);
process.exit(1);
}
}
}
return config;
}
/**
* Main execution
*/
async function main() {
const config = parseArgs();
if (config.showHelp) {
showUsage();
return;
}
console.log(`${colors.cyan}=== Manifest-Driven Content Generator ===${colors.reset}\n`);
// Resolve manifest path
const manifestPath = path.resolve(process.cwd(), config.manifestPath);
const manifestDir = path.dirname(manifestPath);
// Check if manifest exists
if (!fs.existsSync(manifestPath)) {
console.error(`${colors.red}ERROR: Manifest file not found${colors.reset}`);
console.error(` File: ${config.manifestPath}`);
console.error(` Resolved path: ${manifestPath}`);
console.error(`\nRun with --help for usage information.`);
process.exit(1);
}
printInfo(`Loading manifest: ${config.manifestPath}`);
// Load manifest
const { manifest, errors: loadErrors } = loadManifest(manifestPath);
if (loadErrors.length > 0) {
console.error(`\n${colors.red}Failed to load manifest${colors.reset}\n`);
loadErrors.forEach(printError);
process.exit(1);
}
printSuccess('Manifest loaded successfully');
// Validate manifest structure
printInfo('Validating manifest structure...');
const { errors: structureErrors, warnings: structureWarnings } = validateManifest(manifest, manifestPath);
if (structureErrors.length > 0) {
console.error(`\n${colors.red}Manifest validation failed${colors.reset}\n`);
structureErrors.forEach(printError);
process.exit(1);
}
if (structureWarnings.length > 0) {
structureWarnings.forEach(printWarning);
}
printSuccess('Manifest structure is valid');
// Validate file existence
printInfo('Validating files...');
const { errors: fileErrors, warnings: fileWarnings } = validateFiles(manifest, manifestDir);
if (fileErrors.length > 0) {
console.error(`\n${colors.red}File validation failed${colors.reset}\n`);
fileErrors.forEach(printError);
process.exit(1);
}
if (fileWarnings.length > 0) {
fileWarnings.forEach(printWarning);
}
printSuccess('All files exist');
// Validate template placeholders (only for documents with templates)
printInfo('Validating template placeholders...');
let allTemplateWarnings = [];
for (const [outputPath, docConfig] of Object.entries(manifest.documents)) {
if (docConfig.template) {
const templatePath = path.resolve(manifestDir, docConfig.template);
const placeholders = findPlaceholders(templatePath);
const extractionKeys = Object.keys(docConfig.extractions);
const { warnings: templateWarnings } = validatePlaceholders(placeholders, extractionKeys);
templateWarnings.forEach(w => {
w.document = outputPath;
w.template = docConfig.template;
allTemplateWarnings.push(w);
});
}
}
if (allTemplateWarnings.length > 0) {
allTemplateWarnings.forEach(printWarning);
}
const documentCount = Object.keys(manifest.documents).length;
const totalExtractions = Object.values(manifest.documents)
.reduce((sum, doc) => sum + Object.keys(doc.extractions).length, 0);
console.log('');
printSuccess(`Validation passed`);
console.log(` Documents: ${documentCount}`);
console.log(` Extractions: ${totalExtractions}`);
console.log(` Warnings: ${structureWarnings.length + fileWarnings.length + allTemplateWarnings.length}`);
if (config.validateOnly) {
console.log(`\n${colors.green}Validation complete. Use without --validate to generate content.${colors.reset}`);
return;
}
// Generate content
console.log(`\n${colors.cyan}=== Generating Content ===${colors.reset}\n`);
for (const [outputPath, docConfig] of Object.entries(manifest.documents)) {
// Resolve output path to absolute path
const resolvedOutputPath = path.resolve(manifestDir, outputPath);
printInfo(`Processing: ${resolvedOutputPath}`);
// Extract content
const extractionResults = processExtractions(manifest, manifestDir, docConfig);
// Check for extraction errors
const failed = Object.entries(extractionResults)
.filter(([_, result]) => !result.success);
if (failed.length > 0) {
console.error(`${colors.red} Some extractions failed:${colors.reset}`);
failed.forEach(([key, result]) => {
console.error(` - ${key}: ${result.error}`);
});
}
// Generate output
const templatePath = docConfig.template
? path.resolve(manifestDir, docConfig.template)
: null;
const result = generateOutput(templatePath, extractionResults, resolvedOutputPath);
if (result.success) {
printSuccess(` Generated: ${resolvedOutputPath}`);
// Show extraction stats
const successCount = Object.values(extractionResults)
.filter(r => r.success).length;
console.log(` Extractions: ${successCount}/${Object.keys(extractionResults).length} successful`);
} else {
console.error(`${colors.red} Failed to generate: ${resolvedOutputPath}${colors.reset}`);
console.error(` Error: ${result.error}`);
}
console.log('');
}
console.log(`${colors.green}=== Content Generation Complete ===${colors.reset}`);
}
// Run if called directly
if (require.main === module) {
main().catch(error => {
console.error(`${colors.red}Unexpected error:${colors.reset}`, error);
process.exit(1);
});
}
module.exports = { main };