@c6fc/spellcraft 0.0.6 → 0.0.7
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.
- package/bin/spellcraft.js +11 -3
- package/package.json +1 -1
- package/src/index.js +44 -24
package/bin/spellcraft.js
CHANGED
@@ -78,18 +78,26 @@ const spellframe = new SpellFrame();
|
|
78
78
|
describe: 'Jsonnet configuration file to consume',
|
79
79
|
type: 'string',
|
80
80
|
demandOption: true,
|
81
|
+
}).option('skip-module-cleanup', {
|
82
|
+
alias: 's',
|
83
|
+
type: 'boolean',
|
84
|
+
description: 'Leave temporary modules intact after rendering'
|
81
85
|
});
|
82
86
|
},
|
83
87
|
async (argv) => { // No JSDoc for internal handler
|
84
|
-
try {
|
88
|
+
// try {
|
89
|
+
if (argv['s']) {
|
90
|
+
sfInstance.cleanModulesAfterRender = false;
|
91
|
+
}
|
92
|
+
|
85
93
|
await sfInstance.init();
|
86
94
|
await sfInstance.render(argv.filename);
|
87
95
|
await sfInstance.write();
|
88
96
|
console.log("[+] Generation complete.");
|
89
|
-
} catch (error) {
|
97
|
+
/*} catch (error) {
|
90
98
|
console.error(`[!] Error during generation: ${error.message.red}`);
|
91
99
|
process.exit(1);
|
92
|
-
}
|
100
|
+
}*/
|
93
101
|
})
|
94
102
|
|
95
103
|
.command("importModule <npmPackage> [name]", "Configures the current project to use a SpellCraft plugin as an import", (yargsInstance) => {
|
package/package.json
CHANGED
package/src/index.js
CHANGED
@@ -23,9 +23,10 @@ exports.SpellFrame = class SpellFrame {
|
|
23
23
|
|
24
24
|
constructor(options = {}) {
|
25
25
|
const defaults = {
|
26
|
-
renderPath: "
|
27
|
-
|
26
|
+
renderPath: "render",
|
27
|
+
spellcraftModuleRelativePath: ".spellcraft_linked_modules",
|
28
28
|
cleanBeforeRender: true,
|
29
|
+
cleanModulesAfterRender: true,
|
29
30
|
useDefaultFileHandlers: true
|
30
31
|
};
|
31
32
|
|
@@ -42,26 +43,31 @@ exports.SpellFrame = class SpellFrame {
|
|
42
43
|
this.lastRender = null;
|
43
44
|
this.activePath = null;
|
44
45
|
this.loadedModules = [];
|
45
|
-
this.modulePath = path.resolve(path.join(process.cwd(), this.modulePath));
|
46
46
|
this.magicContent = {}; // { modulefile: [...snippets] }
|
47
47
|
this.registeredFunctions = {}; // { modulefile: [...functionNames] }
|
48
48
|
|
49
49
|
this.renderPath = path.resolve(this.currentPackagePath, this.renderPath);
|
50
|
-
this.modulePath = path.resolve(this.currentPackagePath, this.
|
50
|
+
this.modulePath = path.resolve(this.currentPackagePath, this.spellcraftModuleRelativePath);
|
51
51
|
|
52
|
-
this.jsonnet = new Jsonnet()
|
53
|
-
.addJpath(path.join(__dirname, '../lib')) // For core SpellCraft libsonnet files
|
54
|
-
.addJpath(this.modulePath); // For dynamically generated module imports
|
52
|
+
this.jsonnet = new Jsonnet();
|
55
53
|
|
56
|
-
//
|
57
|
-
|
58
|
-
|
54
|
+
this.addJpath(path.join(__dirname, '../lib')) // For core SpellCraft libsonnet files
|
55
|
+
.addJpath(path.join(this.modulePath)) // For dynamically generated module imports
|
56
|
+
.addNativeFunction("envvar", (name) => process.env[name] || false, "name")
|
57
|
+
.addNativeFunction("path", () => this.activePath || process.cwd()) // Use activePath if available
|
58
|
+
.cleanModulePath();
|
59
59
|
|
60
|
+
this.loadedModules = this.loadModulesFromPackageList();
|
61
|
+
this.loadModulesFromModuleDirectory();
|
62
|
+
|
63
|
+
return this;
|
64
|
+
}
|
65
|
+
|
66
|
+
cleanModulePath() {
|
60
67
|
if (!fs.existsSync(this.modulePath)) {
|
61
68
|
fs.mkdirSync(this.modulePath, { recursive: true });
|
62
69
|
}
|
63
70
|
|
64
|
-
// Clean up the modules on init
|
65
71
|
try {
|
66
72
|
fs.readdirSync(this.modulePath)
|
67
73
|
.map(e => path.join(this.modulePath, e))
|
@@ -71,9 +77,6 @@ exports.SpellFrame = class SpellFrame {
|
|
71
77
|
throw new Error(`[!] Could not create/clean up temporary module folder ${path.dirname(this.modulePath).green}: ${e.message.red}`);
|
72
78
|
}
|
73
79
|
|
74
|
-
this.loadedModules = this.loadModulesFromPackageList();
|
75
|
-
this.loadModulesFromModuleDirectory();
|
76
|
-
|
77
80
|
return this;
|
78
81
|
}
|
79
82
|
|
@@ -126,7 +129,8 @@ exports.SpellFrame = class SpellFrame {
|
|
126
129
|
}
|
127
130
|
|
128
131
|
addJpath(jpath) {
|
129
|
-
|
132
|
+
console.log(`[*] Adding Jpath ${jpath}`);
|
133
|
+
this.jsonnet.addJpath(jpath);
|
130
134
|
return this;
|
131
135
|
}
|
132
136
|
|
@@ -266,7 +270,7 @@ exports.SpellFrame = class SpellFrame {
|
|
266
270
|
|
267
271
|
jsModuleFiles.forEach(file => {
|
268
272
|
this.loadFunctionsFromFile(file, as);
|
269
|
-
console.log(`[+] Loaded [${this.registeredFunctions.join(', ').cyan}] from ${path.basename(file).green} into
|
273
|
+
console.log(`[+] Loaded [${this.registeredFunctions[as].join(', ').cyan}] from ${path.basename(file).green} into modules.${as.green}`);
|
270
274
|
});
|
271
275
|
|
272
276
|
return this;
|
@@ -275,12 +279,10 @@ exports.SpellFrame = class SpellFrame {
|
|
275
279
|
loadModulesFromModuleDirectory() {
|
276
280
|
const spellcraftModulesPath = path.join(this.currentPackagePath, 'spellcraft_modules');
|
277
281
|
if (!fs.existsSync(spellcraftModulesPath)) {
|
278
|
-
return
|
282
|
+
return this;
|
279
283
|
}
|
280
284
|
|
281
|
-
|
282
|
-
|
283
|
-
if (!!spellcraftConfig?.spellcraft_module_default_name) {
|
285
|
+
if (!!this.currentPackage?.config?.spellcraft_module_default_name) {
|
284
286
|
console.log("[-] This package is a SpellCraft module. Skipping directory-based module import.");
|
285
287
|
return { registeredFunctions: [], magicContent: [] };
|
286
288
|
}
|
@@ -308,11 +310,10 @@ exports.SpellFrame = class SpellFrame {
|
|
308
310
|
console.log(`[+] Successfully installed ${npmPackage.blue}.`);
|
309
311
|
}
|
310
312
|
|
311
|
-
const importModuleConfig = this.getModulePackage(npmPackage).config;
|
313
|
+
const importModuleConfig = this.getModulePackage(`${npmPackage}/package.json`).config;
|
312
314
|
const currentPackageConfig = this.currentPackage.config;
|
313
315
|
|
314
316
|
if (!name && !!!importModuleConfig?.spellcraft_module_default_name) {
|
315
|
-
// console.log("Package config:", moduleJson);
|
316
317
|
throw new Error(`[!] No import name specified for ${npmPackage.blue}, and it has no 'spellcraft_module_default_name' in its package.json config.`.red);
|
317
318
|
}
|
318
319
|
|
@@ -360,13 +361,32 @@ exports.SpellFrame = class SpellFrame {
|
|
360
361
|
|
361
362
|
this.activePath = path.dirname(absoluteFilePath); // Set active path for relative 'path()' calls
|
362
363
|
|
364
|
+
this.magicContent.modules.push(this.loadedModules.flatMap(e => {
|
365
|
+
return `\t${e}:: import '${e}.libsonnet'`;
|
366
|
+
}));
|
367
|
+
|
368
|
+
if (this.registeredFunctions.modules.length > 0) {
|
369
|
+
fs.writeFileSync(path.join(this.modulePath, `modules`), `{\n${this.magicContent.modules.join(",\n")}\n}`, 'utf-8');
|
370
|
+
console.log(`[+] Registered native functions [${this.registeredFunctions.modules.join(', ').cyan}] to modules.${'modules'.green}`);
|
371
|
+
}
|
372
|
+
|
373
|
+
delete this.magicContent.modules;
|
374
|
+
|
363
375
|
Object.keys(this.magicContent).forEach(e => {
|
364
|
-
fs.
|
376
|
+
fs.appendFileSync(path.join(this.modulePath, `${e}.libsonnet`), ` + {\n${this.magicContent[e].join(",\n")}\n}`, 'utf-8');
|
365
377
|
console.log(`[+] Registered native functions [${this.registeredFunctions[e].join(', ').cyan}] to modules.${e.green} `);
|
366
378
|
});
|
367
|
-
|
379
|
+
|
368
380
|
console.log(`[+] Evaluating Jsonnet file ${path.basename(absoluteFilePath).green}`);
|
369
381
|
this.lastRender = JSON.parse(await this.jsonnet.evaluateFile(absoluteFilePath));
|
382
|
+
|
383
|
+
if (this.cleanModulesAfterRender) {
|
384
|
+
this.cleanModulePath();
|
385
|
+
|
386
|
+
fs.rmdirSync(this.modulePath);
|
387
|
+
} else {
|
388
|
+
console.log(`[*] Leaving ${this.spellcraftModuleRelativePath} in place.`.magenta);
|
389
|
+
}
|
370
390
|
|
371
391
|
return this.lastRender;
|
372
392
|
}
|