Skip to content

Commit cc8ae16

Browse files
committed
Load local parameters from a yaml-file too.
This way complex nested data structures can be loaded to the templating engine.
1 parent 26f76d8 commit cc8ae16

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

hygen.io/docs/generators.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ to: app/emails/<%= name %>.html
113113

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

116+
## Configuration file
117+
118+
You can load complex data structures to the template engine from a configuration file.
119+
120+
```
121+
$ hygen mailer new --configfile config.yaml
122+
```
123+
116124
## Interactive Prompt
117125

118126
To create an interactive generator, add `prompt.js` file to the generator root directory.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
3+
comparisons:
4+
good: 1
5+
better: 2
6+
best: 3
7+
bestest: 4

src/__tests__/params.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import params from '../params'
2+
import {configfile, params } from '../params'
33

44
const fixture = (...segments) =>
55
path.join(__dirname, 'fixtures', 'templates', ...segments)
@@ -54,3 +54,17 @@ describe('params', () => {
5454
})
5555
})
5656
})
57+
58+
describe('local parameters can be loaded from a yaml file', () => {
59+
it('should resolve yaml file', () => {
60+
const configContents = configfile(fixture("configfile","configfile.yaml"))
61+
expect(configContents).toEqual({
62+
comparisons: {
63+
good: 1,
64+
better: 2,
65+
best: 3,
66+
bestest: 4
67+
}
68+
})
69+
})
70+
})

src/engine.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs-extra'
22
import type { ActionResult, RunnerConfig } from './types'
3-
import params from './params'
3+
import { params, configfile } from './params'
44

55
class ShowHelpError extends Error {
66
constructor(message: string) {
@@ -23,8 +23,9 @@ Usage:
2323
hygen [option] GENERATOR ACTION [--name NAME] [data-options]
2424
2525
Options:
26-
-h, --help # Show this message and quit
27-
--dry # Perform a dry run. Files will be generated but not saved.`)
26+
-h, --help # Show this message and quit
27+
--dry # Perform a dry run. Files will be generated but not saved.
28+
--configfile # Load this file as the local parameters for the template`)
2829
process.exit(0)
2930
}
3031

@@ -39,6 +40,7 @@ Options:
3940

4041
logger.log(`Loaded templates: ${templates.replace(`${cwd}/`, '')}`)
4142
if (!(await fs.exists(actionfolder))) {
43+
console.log(actionfolder)
4244
throw new ShowHelpError(`I can't find action '${action}' for generator '${generator}'.
4345
4446
You can try:
@@ -48,6 +50,9 @@ Options:
4850
Check out the quickstart for more: https://hygen.io/docs/quick-start
4951
`)
5052
}
53+
if (args.configfile) {
54+
config.localsDefaults = configfile(args.configfile)
55+
}
5156

5257
// lazy loading these dependencies gives a better feel once
5358
// a user is exploring hygen (not specifying what to execute)

src/params.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'path'
22
import yargs from 'yargs-parser'
33
import fs from 'fs-extra'
44
import type { ParamsResult, RunnerConfig } from './types'
5+
import YAML from 'yaml'
56

67
import prompt from './prompt'
78
export const DEFAULT_ACTION = '_default'
@@ -44,7 +45,7 @@ const resolvePositionals = async (templates: string, args: string[]) => {
4445
return [generator, action, name]
4546
}
4647

47-
const params = async (
48+
export const params = async (
4849
{ templates, createPrompter }: RunnerConfig,
4950
externalArgv: string[],
5051
): Promise<ParamsResult> => {
@@ -86,4 +87,7 @@ const params = async (
8687
return args
8788
}
8889

89-
export default params
90+
export const configfile = (configfile: string) => {
91+
const file = fs.readFileSync(configfile, 'utf8')
92+
return YAML.parse(file)
93+
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ export type ParamsResult = {
5252
actionfolder?: string
5353
name?: string
5454
dry?: boolean
55+
configfile?: string
5556
} & object

0 commit comments

Comments
 (0)