Skip to content

Commit

Permalink
Load local parameters from a yaml-file too.
Browse files Browse the repository at this point in the history
This way complex nested data structures can be loaded to the templating engine.
  • Loading branch information
kivilahtio committed Nov 5, 2023
1 parent 26f76d8 commit cbba020
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/fixtures/templates/configfile/configfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

comparisons:
good: 1
better: 2
best: 3
bestest: 4
16 changes: 15 additions & 1 deletion src/__tests__/params.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
}
})
})
})
11 changes: 8 additions & 3 deletions src/engine.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}

Expand All @@ -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:
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<ParamsResult> => {
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export type ParamsResult = {
actionfolder?: string
name?: string
dry?: boolean
configfile?: string
} & object

0 comments on commit cbba020

Please sign in to comment.