1 | import fs from 'fs-extra'
|
2 | import { ActionResult, RunnerConfig } from './types'
|
3 | import params from './params'
|
4 |
|
5 | const engine = async (
|
6 | argv: string[],
|
7 | config: RunnerConfig,
|
8 | ): Promise<ActionResult[]> => {
|
9 | const { cwd, templates, logger } = config
|
10 | const args = Object.assign(await params(config, argv), { cwd })
|
11 | const { generator, action, actionfolder } = args
|
12 |
|
13 | if (['-h', '--help'].includes(argv[0])) {
|
14 | logger.log(`
|
15 | Usage:
|
16 | hygen [option] GENERATOR ACTION [--name NAME] [data-options]
|
17 |
|
18 | Options:
|
19 | -h, --help # Show this message and quit
|
20 | --dry # Perform a dry run. Files will be generated but not saved.`)
|
21 | process.exit(0)
|
22 | }
|
23 |
|
24 | logger.log(args.dry ? '(dry mode)' : '')
|
25 | if (!generator) {
|
26 | throw new Error('please specify a generator.')
|
27 | }
|
28 |
|
29 | if (!action) {
|
30 | throw new Error(`please specify an action for ${generator}.`)
|
31 | }
|
32 |
|
33 | logger.log(`Loaded templates: ${templates.replace(`${cwd}/`, '')}`)
|
34 | if (!(await fs.exists(actionfolder))) {
|
35 | throw new Error(`I can't find action '${action}' for generator '${generator}'.
|
36 |
|
37 | You can try:
|
38 | 1. 'hygen init self' to initialize your project, and
|
39 | 2. 'hygen generator new --name ${generator}' to build the generator you wanted.
|
40 |
|
41 | Check out the quickstart for more: http:
|
42 | `)
|
43 | }
|
44 |
|
45 | // lazy loading these dependencies gives a better feel once
|
46 | // a user is exploring hygen (not specifying what to execute)
|
47 | const execute = require('./execute')
|
48 | const render = require('./render')
|
49 | return execute(await render(args, config), args, config)
|
50 | }
|
51 |
|
52 | export default engine
|
53 |
|
\ | No newline at end of file |