UNPKG

1.59 kBPlain TextView Raw
1import fs from 'fs-extra'
2import { ActionResult, RunnerConfig } from './types'
3import params from './params'
4
5const 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(`
15Usage:
16 hygen [option] GENERATOR ACTION [--name NAME] [data-options]
17
18Options:
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://www.hygen.io/quick-start
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
52export default engine
53
\No newline at end of file