Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load local parameters from a yaml-file too. #435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions hygen.io/docs/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ to: app/emails/<%= name %>.html

Try making the text variant yourself by editing `text.ejs.t`. Note: you want to put it in the correct place with `to:`.

## Configuration file

You can load complex data structures to the template engine from a configuration file.

```
$ hygen mailer new --configfile config.yaml
```

## Interactive Prompt

To create an interactive generator, add `prompt.js` file to the generator root directory.
Expand Down
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