diff --git a/src/__tests__/fixtures/templates/configfile/configfile.yaml b/src/__tests__/fixtures/templates/configfile/configfile.yaml new file mode 100644 index 00000000..1dfb9ac3 --- /dev/null +++ b/src/__tests__/fixtures/templates/configfile/configfile.yaml @@ -0,0 +1,7 @@ +--- + +comparisons: + good: 1 + better: 2 + best: 3 + bestest: 4 diff --git a/src/__tests__/params.spec.ts b/src/__tests__/params.spec.ts index 18dd29f9..71aff428 100644 --- a/src/__tests__/params.spec.ts +++ b/src/__tests__/params.spec.ts @@ -1,5 +1,5 @@ import path from 'path' -import params from '../params' +import {configfile, params } from '../params' const fixture = (...segments) => path.join(__dirname, 'fixtures', 'templates', ...segments) @@ -54,3 +54,17 @@ describe('params', () => { }) }) }) + +describe('local parameters can be loaded from a yaml file', () => { + it('should resolve yaml file', () => { + const configContents = configfile(fixture("configfile","configfile.yaml")) + expect(configContents).toEqual({ + comparisons: { + good: 1, + better: 2, + best: 3, + bestest: 4 + } + }) + }) +}) \ No newline at end of file diff --git a/src/engine.ts b/src/engine.ts index 7b33a7ce..633f6255 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -1,6 +1,6 @@ import fs from 'fs-extra' import type { ActionResult, RunnerConfig } from './types' -import params from './params' +import { params, configfile } from './params' class ShowHelpError extends Error { constructor(message: string) { @@ -23,8 +23,9 @@ Usage: hygen [option] GENERATOR ACTION [--name NAME] [data-options] Options: - -h, --help # Show this message and quit - --dry # Perform a dry run. Files will be generated but not saved.`) + -h, --help # Show this message and quit + --dry # Perform a dry run. Files will be generated but not saved. + --configfile # Load this file as the local parameters for the template`) process.exit(0) } @@ -39,6 +40,7 @@ Options: logger.log(`Loaded templates: ${templates.replace(`${cwd}/`, '')}`) if (!(await fs.exists(actionfolder))) { + console.log(actionfolder) throw new ShowHelpError(`I can't find action '${action}' for generator '${generator}'. You can try: @@ -48,6 +50,9 @@ Options: Check out the quickstart for more: https://hygen.io/docs/quick-start `) } + if (args.configfile) { + config.localsDefaults = configfile(args.configfile) + } // lazy loading these dependencies gives a better feel once // a user is exploring hygen (not specifying what to execute) diff --git a/src/params.ts b/src/params.ts index 348fac87..32e2e966 100644 --- a/src/params.ts +++ b/src/params.ts @@ -2,6 +2,7 @@ import path from 'path' import yargs from 'yargs-parser' import fs from 'fs-extra' import type { ParamsResult, RunnerConfig } from './types' +import YAML from 'yaml' import prompt from './prompt' export const DEFAULT_ACTION = '_default' @@ -44,7 +45,7 @@ const resolvePositionals = async (templates: string, args: string[]) => { return [generator, action, name] } -const params = async ( +export const params = async ( { templates, createPrompter }: RunnerConfig, externalArgv: string[], ): Promise => { @@ -86,4 +87,7 @@ const params = async ( return args } -export default params +export const configfile = (configfile: string) => { + const file = fs.readFileSync(configfile, 'utf8') + return YAML.parse(file) +} diff --git a/src/types.ts b/src/types.ts index fd0d0a92..abff587f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -52,4 +52,5 @@ export type ParamsResult = { actionfolder?: string name?: string dry?: boolean + configfile?: string } & object