Skip to content

Commit 391b30d

Browse files
authored
feat: transform json manifest into js command (#417)
1 parent 509a4fe commit 391b30d

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

lib/commands/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,12 @@ import buildCommand from './build.commands.js';
22
import devCommand from './dev.commands.js';
33
import initCommand from './init.commands.js';
44
import presetsCommand from './presets.commands.js';
5+
import manifestCommand from './manifest.commands.js';
56

6-
export { buildCommand, devCommand, initCommand, presetsCommand };
7+
export {
8+
buildCommand,
9+
devCommand,
10+
initCommand,
11+
presetsCommand,
12+
manifestCommand,
13+
};

lib/commands/manifest.commands.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { readFileSync, writeFileSync } from 'fs';
2+
import { resolve, extname } from 'path';
3+
import { feedback, debug } from '#utils';
4+
import { Messages } from '#constants';
5+
import { convertJsonConfigToObject } from 'azion';
6+
7+
import { Commands } from '#namespaces';
8+
/**
9+
* @function manifestCommand
10+
* @memberof Commands
11+
* @description
12+
* transforms a JSON manifest file to a JavaScript module.
13+
*
14+
* Usage:
15+
* ```bash
16+
* az manifest transform <input.json> -o <output.js>
17+
* ```
18+
*
19+
* Example:
20+
* ```bash
21+
* az manifest transform .edge/manifest.json -o azion.config.js
22+
* ```
23+
* @param {string} command - The operation to perform (only 'transform' for now)
24+
* @param {string} entry - Path to the input JSON file
25+
* @param {object} options - Command options
26+
* @param {string} options.output - Output file path for JS module
27+
*/
28+
async function manifestCommand(command, entry, options) {
29+
try {
30+
if (command !== 'transform') {
31+
feedback.error('Only transform command is supported');
32+
process.exit(1);
33+
}
34+
35+
if (!entry) {
36+
feedback.error('Input file path is required');
37+
process.exit(1);
38+
}
39+
40+
if (!options.output) {
41+
feedback.error('Output file path is required (--output)');
42+
process.exit(1);
43+
}
44+
45+
const fileExtension = extname(entry).toLowerCase();
46+
if (fileExtension !== '.json') {
47+
feedback.error('Input file must be .json');
48+
process.exit(1);
49+
}
50+
51+
const absolutePath = resolve(process.cwd(), entry);
52+
const jsonString = readFileSync(absolutePath, 'utf8');
53+
const config = convertJsonConfigToObject(jsonString);
54+
55+
const jsContent = `export default ${JSON.stringify(config, null, 2)};`;
56+
writeFileSync(options.output, jsContent);
57+
58+
feedback.success(
59+
`Azion Platform configuration transformed into JavaScript module at ${options.output}`,
60+
);
61+
} catch (error) {
62+
debug.error(error);
63+
feedback.error(Messages.errors.unknown_error);
64+
process.exit(1);
65+
}
66+
}
67+
68+
export default manifestCommand;

lib/main.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,25 @@ function startVulcanProgram() {
219219
program
220220
.command('presets <command>')
221221
.description(
222-
'Create <create> or list <ls> defined project presets for Edge',
222+
'Create <create> or list <ls> defined project presets for Azion',
223223
)
224224
.action(async (command) => {
225225
const { presetsCommand } = await import('#commands');
226226
await presetsCommand(command);
227227
});
228228

229+
program
230+
.command('manifest <command>')
231+
.description(
232+
'Trasnform <transform> or validate <validate> manifest files for Azion',
233+
)
234+
.argument('[entry]', 'Path to the input file')
235+
.option('-o, --output <path>', 'Output file path for convert command')
236+
.action(async (command, entry, options) => {
237+
const { manifestCommand } = await import('#commands');
238+
await manifestCommand(command, entry, convertOptions(options));
239+
});
240+
229241
program.parse(process.argv);
230242
}
231243

0 commit comments

Comments
 (0)