|
| 1 | +import { Command } from 'commander'; |
| 2 | +import prompts, { PromptType } from 'prompts'; |
| 3 | + |
| 4 | +import { Config, ScriptConfig, ScriptName } from '../config'; |
| 5 | +import { canRead, logBanner, logSuccess, resolveRelativePath, writeFile } from '../utils'; |
| 6 | + |
| 7 | +export function setInitCommand(program: Command): void { |
| 8 | + program |
| 9 | + .command('init') |
| 10 | + .argument('[output]', 'Optional path used to output the configuration file') |
| 11 | + .option('-d, --default', 'Bypass prompts and select all defaults options') |
| 12 | + .option('--js', 'Forces the output to be a JavaScript file') |
| 13 | + .action(doInit); |
| 14 | +} |
| 15 | + |
| 16 | +type InitOptions = { |
| 17 | + default?: boolean; |
| 18 | + js?: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +async function doInit(explicitOutput: string | undefined, options: InitOptions) { |
| 22 | + const output = getOutputPath(explicitOutput, options); |
| 23 | + const useJsFile = options.js || output.endsWith('.js'); |
| 24 | + if (await canRead(output)) { |
| 25 | + throw new Error(`Configuration file already exists at "${output}".`); |
| 26 | + } |
| 27 | + |
| 28 | + logBanner(); |
| 29 | + const result = await getPromptResult(options); |
| 30 | + const content = getContentFromPromptResult(result, useJsFile); |
| 31 | + await writeFile(output, content); |
| 32 | + logSuccess(`Configuration file created at "${output}".`); |
| 33 | +} |
| 34 | + |
| 35 | +function getOutputPath(explicitOutput: string | undefined, options: Pick<InitOptions, 'js'>): string { |
| 36 | + if (explicitOutput) { |
| 37 | + return resolveRelativePath(explicitOutput); |
| 38 | + } |
| 39 | + return resolveRelativePath(options.js ? 'codama.js' : 'codama.json'); |
| 40 | +} |
| 41 | + |
| 42 | +type PromptResult = { |
| 43 | + idlPath: string; |
| 44 | + jsPath?: string; |
| 45 | + rustCrate?: string; |
| 46 | + rustPath?: string; |
| 47 | + scripts: string[]; |
| 48 | +}; |
| 49 | + |
| 50 | +async function getPromptResult(options: Pick<InitOptions, 'default'>): Promise<PromptResult> { |
| 51 | + const defaults = getDefaultPromptResult(); |
| 52 | + if (options.default) { |
| 53 | + return defaults; |
| 54 | + } |
| 55 | + |
| 56 | + const hasScript = |
| 57 | + (script: string, type: PromptType = 'text') => |
| 58 | + (_: unknown, values: { scripts: string[] }) => |
| 59 | + values.scripts.includes(script) ? type : null; |
| 60 | + const result: PromptResult = await prompts( |
| 61 | + [ |
| 62 | + { |
| 63 | + initial: defaults.idlPath, |
| 64 | + message: 'Where is your IDL located? (Supports Codama and Anchor IDLs).', |
| 65 | + name: 'idlPath', |
| 66 | + type: 'text', |
| 67 | + }, |
| 68 | + { |
| 69 | + choices: [ |
| 70 | + { selected: true, title: 'Generate JavaScript client', value: 'js' }, |
| 71 | + { selected: true, title: 'Generate Rust client', value: 'rust' }, |
| 72 | + ], |
| 73 | + instructions: '[space] to toggle / [a] to toggle all / [enter] to submit', |
| 74 | + message: 'Which script preset would you like to use?', |
| 75 | + name: 'scripts', |
| 76 | + type: 'multiselect', |
| 77 | + }, |
| 78 | + { |
| 79 | + initial: defaults.jsPath, |
| 80 | + message: '[js] Where should the JavaScript code be generated?', |
| 81 | + name: 'jsPath', |
| 82 | + type: hasScript('js'), |
| 83 | + }, |
| 84 | + { |
| 85 | + initial: defaults.rustCrate, |
| 86 | + message: '[rust] Where is the Rust client crate located?', |
| 87 | + name: 'rustCrate', |
| 88 | + type: hasScript('rust'), |
| 89 | + }, |
| 90 | + { |
| 91 | + initial: (prev: string) => `${prev}/src/generated`, |
| 92 | + message: '[rust] Where should the Rust code be generated?', |
| 93 | + name: 'rustPath', |
| 94 | + type: hasScript('rust'), |
| 95 | + }, |
| 96 | + ], |
| 97 | + { |
| 98 | + onCancel: () => { |
| 99 | + throw new Error('Operation cancelled.'); |
| 100 | + }, |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + return result; |
| 105 | +} |
| 106 | + |
| 107 | +function getDefaultPromptResult(): PromptResult { |
| 108 | + return { |
| 109 | + idlPath: 'program/idl.json', |
| 110 | + jsPath: 'clients/js/src/generated', |
| 111 | + rustCrate: 'clients/rust', |
| 112 | + rustPath: 'clients/rust/src/generated', |
| 113 | + scripts: ['js', 'rust'], |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +function getContentFromPromptResult(result: PromptResult, useJsFile: boolean): string { |
| 118 | + const scripts: Record<ScriptName, ScriptConfig> = {}; |
| 119 | + if (result.scripts.includes('js')) { |
| 120 | + scripts.js = { |
| 121 | + from: '@codama/renderers-js', |
| 122 | + args: [result.jsPath], |
| 123 | + }; |
| 124 | + } |
| 125 | + if (result.scripts.includes('rust')) { |
| 126 | + scripts.rust = { |
| 127 | + from: '@codama/renderers-rust', |
| 128 | + args: [result.rustPath, { crateFolder: result.rustCrate, formatCode: true }], |
| 129 | + }; |
| 130 | + } |
| 131 | + const content: Config = { idl: result.idlPath, before: [], scripts }; |
| 132 | + |
| 133 | + if (!useJsFile) { |
| 134 | + return JSON.stringify(content, null, 4); |
| 135 | + } |
| 136 | + |
| 137 | + return ( |
| 138 | + 'export default ' + |
| 139 | + JSON.stringify(content, null, 4) |
| 140 | + // Remove quotes around property names |
| 141 | + .replace(/"([^"]+)":/g, '$1:') |
| 142 | + // Convert double-quoted strings to single quotes |
| 143 | + .replace(/"([^"]*)"/g, "'$1'") |
| 144 | + ); |
| 145 | +} |
0 commit comments