diff options
author | Olaf Tomalka <olaf@tomalka.me> | 2018-01-27 01:22:32 +0800 |
---|---|---|
committer | Olaf Tomalka <olaf@tomalka.me> | 2018-01-27 01:22:32 +0800 |
commit | dc5694e54489ecf69d23b7ebbf085eae01491d66 (patch) | |
tree | e65df1f16e7cd4077f1494549f119c3df46af45b /packages | |
parent | ad85011d62ded60a0f4c4d69f2fda0665e12c425 (diff) | |
download | dexon-0x-contracts-dc5694e54489ecf69d23b7ebbf085eae01491d66.tar dexon-0x-contracts-dc5694e54489ecf69d23b7ebbf085eae01491d66.tar.gz dexon-0x-contracts-dc5694e54489ecf69d23b7ebbf085eae01491d66.tar.bz2 dexon-0x-contracts-dc5694e54489ecf69d23b7ebbf085eae01491d66.tar.lz dexon-0x-contracts-dc5694e54489ecf69d23b7ebbf085eae01491d66.tar.xz dexon-0x-contracts-dc5694e54489ecf69d23b7ebbf085eae01491d66.tar.zst dexon-0x-contracts-dc5694e54489ecf69d23b7ebbf085eae01491d66.zip |
Added CLI options for explicit specifying location of partials and main template
Diffstat (limited to 'packages')
-rw-r--r-- | packages/abi-gen/CHANGELOG.md | 1 | ||||
-rw-r--r-- | packages/abi-gen/src/index.ts | 63 |
2 files changed, 50 insertions, 14 deletions
diff --git a/packages/abi-gen/CHANGELOG.md b/packages/abi-gen/CHANGELOG.md index fb2c6eb24..0f54c2480 100644 --- a/packages/abi-gen/CHANGELOG.md +++ b/packages/abi-gen/CHANGELOG.md @@ -2,6 +2,7 @@ ## v0.1.0 - _January 11, 2018_ +* Added CLI options for explicit specifying location of partials and main template * Fixed array typings with union types (#295) * Add event ABIs to context data passed to templates (#302) * Add constructor ABIs to context data passed to templates (#304) diff --git a/packages/abi-gen/src/index.ts b/packages/abi-gen/src/index.ts index 527af32b1..3628adff8 100644 --- a/packages/abi-gen/src/index.ts +++ b/packages/abi-gen/src/index.ts @@ -20,21 +20,53 @@ const ABI_TYPE_EVENT = 'event'; const MAIN_TEMPLATE_NAME = 'contract.mustache'; const args = yargs - .option('abiGlob', { + .option('abis', { + alias: ['abiGlob'], describe: 'Glob pattern to search for ABI JSON files', type: 'string', - demand: true, + demandOption: true, }) + .option('output', { + alias: ['o', 'out'], + describe: 'Folder where to put the output files', + type: 'string', + normalize: true, + demandOption: true, + }) + .option('partials', { + describe: 'Glob pattern for the partial template files', + type: 'string', + implies: 'template', + }) + .option('template', { + describe: 'Path for the main template file that will be used to generate each contract', + type: 'string', + normalize: true, + }) + .group(['templates'], 'Deprecated') .option('templates', { describe: 'Folder where to search for templates', type: 'string', - demand: true, }) - .option('output', { - describe: 'Folder where to put the output files', - type: 'string', - demand: true, - }).argv; + .conflicts('templates', ['partials', 'template-file']) + .check(argv => { + if (!argv.template && !argv.templates) { + throw new Error('You need specify the location of the template'); + } + return true; + }) + .example("$0 --abis 'src/artifacts/**/*.json' --out 'src/contracts/generated/' --partials 'src/templates/partials/**/*.handlebars' --template 'src/templates/contract.handlebars'", 'Full usage example') + .argv; + +function registerPartials(partialsGlob: string) { + const partialTemplateFileNames = globSync(partialsGlob); + utils.log(`Found ${chalk.green(`${partialTemplateFileNames.length}`)} ${chalk.bold('partial')} templates`); + for (const partialTemplateFileName of partialTemplateFileNames) { + const namedContent = utils.getNamedContent(partialTemplateFileName); + Handlebars.registerPartial(namedContent.name, namedContent.content); + } + return partialsGlob; +} function writeOutputFile(name: string, renderedTsCode: string): void { const fileName = toSnakeCase(name); @@ -45,15 +77,18 @@ function writeOutputFile(name: string, renderedTsCode: string): void { Handlebars.registerHelper('parameterType', utils.solTypeToTsType.bind(utils, ParamKind.Input)); Handlebars.registerHelper('returnType', utils.solTypeToTsType.bind(utils, ParamKind.Output)); -const partialTemplateFileNames = globSync(`${args.templates}/partials/**/*.mustache`); -for (const partialTemplateFileName of partialTemplateFileNames) { - const namedContent = utils.getNamedContent(partialTemplateFileName); - Handlebars.registerPartial(namedContent.name, namedContent.content); + +if (args.partials) { + registerPartials(args.partials); +} +if (args.templates) { + registerPartials(`${args.templates}/partials/**/*.{mustache,handlebars}`); } -const mainTemplate = utils.getNamedContent(`${args.templates}/${MAIN_TEMPLATE_NAME}`); +const mainTemplate = utils.getNamedContent(args.template ? args.template : `${args.templates}/${MAIN_TEMPLATE_NAME}`); const template = Handlebars.compile<ContextData>(mainTemplate.content); -const abiFileNames = globSync(args.abiGlob); +const abiFileNames = globSync(args.abis); + if (_.isEmpty(abiFileNames)) { utils.log(`${chalk.red(`No ABI files found.`)}`); utils.log(`Please make sure you've passed the correct folder name and that the files have |