Skip to content

Unified configuration#142

Closed
anithri wants to merge 3 commits intojondot:masterfrom
anithri:prePostHooks
Closed

Unified configuration#142
anithri wants to merge 3 commits intojondot:masterfrom
anithri:prePostHooks

Conversation

@anithri
Copy link
Contributor

@anithri anithri commented Sep 4, 2019

  • config contains data needed for hygen and templates to run.

    • cwd,logger,templates, helpers
  • args contains data needed for templates

    • {...config.defaultArgs, config.cliArgs}
  • .hygen.js finds first in tree from process.cwd() up to / and use's only that .

  • {action,custom,index,prompt,...}.js are found and used from cwd/_templates/gen1/action/:subAction to cwd/_templates

    • // logically not actually
      const finalConfig = {...homeDirConfig}
      const finalArgs = {..._templateActions, ..._generatorActions, ...actionAtcions, ...subActionActions}
  • the config obj , located at /home/rincewind/code/project is built...

    • starting at process.cwd() and searching up through the parents find the first .hygen.js file
    • read the file
    • each directory as you descend overrides any settings from the directories above. and the project directory overrides them all
    • the config obj has a number of reserved keys, but could otherwise contain anything
  • the args obj from an action at '_templates/run/away/fast'

    • starting with config.defaultArgs
    • args = {...args, ...cliArgs}
    • if (config.preParams) args = config.preParams(args)
    • read from _templates/run/away/fast/index.js
      • if it exports params or prompt
        • nextArgs = indexParams({args, config})
    • repeat for _templates/run/away/index.js, _templates/run/index.js, and _templates/index.js
    • if (config.postParams) args = config.postParams(args)

Why?

  • automatically handle hygen run away:fast --help, alternate logging, different prompter...
  • encourages composition and reuse
  • easier to provide tools to all of your generators and actions

edit: for parent .hygen.js to be used in conjunction with a project level file, you need to merge config objects in a specific manor that would not scale well with more keywords.

@anithri
Copy link
Contributor Author

anithri commented Sep 18, 2019

A take I like a lot.

/* HygenHook = (hygenVars: HygenVars) => HygenVars */
/* HygenVars = {config: HygenConfig, params: HygenParams, generator: HygenGenerator}

/* module:
  logger: initializes the logger, set logLevel
  help: adds preGeneratorHook to check for globalhelp request
  myModule: adds some helpers
*/
const resolveModules = moduleResolver(['logger', 'help', 'myModule'])

const hygen = async (argv: Array<string>): Promise<HygenVars> => {
  /*  every then function is
      fn: (vars: HygenVars): Promise<HygenVars>
   */

  return (
    resolveConfig(mkDefaultConfig(argv))
      .then(preModuleHooks) // prep for modules
      .then(resolveModules) // load modules
      .then(postModuleHooks) // finish modules config - often module setup

      .then(preGeneratorHooks) // prior to looking for a specific generator
      .then(resolveGenerator) // find the specific generator
      .then(postGeneratorHooks) // manipulate generator prior to params

      .then(preParamsHooks) // module step
      .then(resolveParams) // find all params
      .then(postParamsHooks) // twiddle prams from config

      .then(preTemplatesHooks) // prior to scanning for template files
      .then(resolveTemplates) // read template files
      .then(postTemplatesHooks) // twiddle templates from config
      // or act on what's found

      .then(preRenderHooks) // final chance to tweak
      .then(render) // process those templates
      .then(postRenderHooks) // finish up

      .catch(err => {
        configBase.logger.error(err.toString())
        configBase.logger.debug('======== details ========')
        configBase.logger.debug(err.stack)
        configBase.logger.debug('=========================')
      })
  )
}

@anithri anithri mentioned this pull request Sep 20, 2019
@anithri
Copy link
Contributor Author

anithri commented Sep 20, 2019

closing in favor of #149

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant