|
| 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; |
0 commit comments