|
| 1 | +import { parseArgs } from 'node:util'; |
| 2 | + |
| 3 | +import { type Inputs } from './inputCore'; |
| 4 | +import { kebabCase } from './strings'; |
| 5 | + |
| 6 | +type ArgInputs = { |
| 7 | + address?: string; |
| 8 | + anchorProgram: boolean; |
| 9 | + clients: Array<'js' | 'rust'>; |
| 10 | + force: boolean; |
| 11 | + jsClientPackageName?: string; |
| 12 | + noClients: boolean; |
| 13 | + organizationName?: string; |
| 14 | + programCrateName?: string; |
| 15 | + programName?: string; |
| 16 | + rustClientCrateName?: string; |
| 17 | + rustVersion?: string; |
| 18 | + shankProgram: boolean; |
| 19 | + solanaVersion?: string; |
| 20 | + useDefaults: boolean; |
| 21 | + targetDirectoryName?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +export function getInputsFromArgs(): Partial<Inputs> { |
| 25 | + const args = process.argv.slice(2); |
| 26 | + const { values: options, positionals } = parseArgs({ |
| 27 | + args, |
| 28 | + options: { |
| 29 | + address: { type: 'string' }, |
| 30 | + anchor: { type: 'boolean' }, |
| 31 | + client: { type: 'string', multiple: true }, |
| 32 | + default: { type: 'boolean', short: 'd' }, |
| 33 | + force: { type: 'boolean' }, |
| 34 | + 'js-client-package-name': { type: 'string' }, |
| 35 | + 'no-clients': { type: 'boolean' }, |
| 36 | + org: { type: 'string' }, |
| 37 | + 'program-crate-name': { type: 'string' }, |
| 38 | + rust: { type: 'string' }, |
| 39 | + 'rust-client-crate-name': { type: 'string' }, |
| 40 | + shank: { type: 'boolean' }, |
| 41 | + solana: { type: 'string' }, |
| 42 | + }, |
| 43 | + strict: false, |
| 44 | + }); |
| 45 | + |
| 46 | + return parseArgInputs({ |
| 47 | + address: options.address, |
| 48 | + anchorProgram: options.anchor ?? false, |
| 49 | + clients: options.client, |
| 50 | + force: options.force ?? false, |
| 51 | + jsClientPackageName: options['js-client-package-name'], |
| 52 | + noClients: options['no-clients'] ?? false, |
| 53 | + organizationName: options.org, |
| 54 | + programCrateName: options['program-crate-name'], |
| 55 | + programName: positionals[1], |
| 56 | + rustClientCrateName: options['rust-client-crate-name'], |
| 57 | + rustVersion: options.rust, |
| 58 | + shankProgram: options.shank ?? false, |
| 59 | + solanaVersion: options.solana, |
| 60 | + useDefaults: options.default ?? false, |
| 61 | + targetDirectoryName: positionals[0], |
| 62 | + } as ArgInputs); |
| 63 | +} |
| 64 | + |
| 65 | +function parseArgInputs(argInputs: ArgInputs): Partial<Inputs> { |
| 66 | + const inputs = {} as Partial<Inputs>; |
| 67 | + |
| 68 | + if (argInputs.address) inputs.programAddress = argInputs.address; |
| 69 | + if (argInputs.organizationName) |
| 70 | + inputs.organizationName = kebabCase(argInputs.organizationName); |
| 71 | + if (argInputs.programName) |
| 72 | + inputs.programName = kebabCase(argInputs.programName); |
| 73 | + if (argInputs.rustVersion) inputs.rustVersion = argInputs.rustVersion; |
| 74 | + if (argInputs.solanaVersion) inputs.solanaVersion = argInputs.solanaVersion; |
| 75 | + if (argInputs.targetDirectoryName) |
| 76 | + inputs.targetDirectoryName = argInputs.targetDirectoryName; |
| 77 | + if (argInputs.jsClientPackageName) |
| 78 | + inputs.jsClientPackageName = argInputs.jsClientPackageName; |
| 79 | + if (argInputs.programCrateName) |
| 80 | + inputs.programCrateName = argInputs.programCrateName; |
| 81 | + if (argInputs.rustClientCrateName) |
| 82 | + inputs.rustClientCrateName = argInputs.rustClientCrateName; |
| 83 | + if (argInputs.force) inputs.shouldOverride = true; |
| 84 | + if (argInputs.useDefaults) inputs.useDefaults = true; |
| 85 | + |
| 86 | + if (argInputs.anchorProgram) { |
| 87 | + inputs.programFramework = 'anchor'; |
| 88 | + } else if (argInputs.shankProgram) { |
| 89 | + inputs.programFramework = 'shank'; |
| 90 | + } |
| 91 | + |
| 92 | + if (argInputs.noClients) { |
| 93 | + inputs.jsClient = false; |
| 94 | + inputs.rustClient = false; |
| 95 | + } else if (argInputs.clients) { |
| 96 | + inputs.jsClient = argInputs.clients.includes('js'); |
| 97 | + inputs.rustClient = argInputs.clients.includes('rust'); |
| 98 | + } |
| 99 | + |
| 100 | + return inputs; |
| 101 | +} |
0 commit comments