2.3.0 • Published 3 years ago
gogen v2.3.0
GoGen
Use vinyl-fs based stream API to create generator/initializer packages.
Features
- Gulp-compatible stream API
- Lightweight, single file bundled (<50K gzip size), no need to install globally, just run
npx - Automatically rename
gitignoreto.gitignore, due to npm/issues/1862 - Automatically set
namefield inpackage.json - Render
*.foo.tto*.foowith lodash template - Add command line prompts with prompts
- Add command line arguments with minimist
- Provide fast, in-memory testing API
Usage
Run existing generator
Run from any npm package (registry/GitHub/git/folder...), same as npm install or yarn add:
# install generator to directory
npx gogen <generator> <directory>
# eg.
npx gogen [<@scope>/]<name> <directory> # npm registry
npx gogen <user>/<repo> <directory> # GitHub
npx gogen <host>:<name>/<repo> <directory> # git
npx gogen <folder> <directory> # folderCreate generator
The default directory structure, used in examples:
.
├── .gogenrc.js # optional, defaults to `lib/.gogenrc.default.js`
├── package.json
└── template
├── index.js
└── package.jsonEdit the .gogenrc.js file:
module.exports = async ({src, dest, pipeline}, {install, gitInit}) => {
await pipeline(src('template/**'), dest())
await install()
await gitInit()
}Run the generator:
npx gogen <your-generator> <your-project>Create initializer
Add a bin file, eg. examples/create-gogen/cli.js.
const {run} = require('gogen')
run(
[__dirname, ...process.argv.slice(2)],
'Usage: npm init gogen <my-generator>'
)Run the initializer:
npm init <your-initializer> <your-project>
# or: yarn create <your-initializer> <your-project>Examples
Configuration file
.gogenrc.js:
run(go: Object, context: Object) => voidgovinyl-fs based stream APIssrc(glob: string | string[]) => Streamread filesdest(path?: string) => Streamwrite filespipeline(...streams: Stream[]) => Promisepipe a series of streamstemplate(data: Object, {ext: RegExp, render: Function}) => Streamrender*.tfiles with lodash templatepackages(content: Object | Function) => Streamchangepackage.jsonmodify(match: RegExp | Function, transform: file => file) => Streamchange filesmodify.text(match: RegExp | Function, transform: (file, text: string) => text) => Streamchange text filesmodify.json(match: RegExp | Function, transform: (file, json: Object) => json) => Streamchange json filesmodify.rename(match: RegExp | Function, transform: (file, paths: Object) => paths) => Streamrename files
contextgenerator contextpath: stringnew project's pathname: stringnew project's nameargv: Objectcommand line argumentsinstall(deps: string[], {dev: boolean, silent: boolean}) => Promiseinstall dependenciesgitInit(message: string) => Promiseinit git repositoryprompts(Array | Object) => Promisesee prompts
Testing
Use the mock API:
mock(generator: string, directory: string, options: Object):generatorpath to generatordirectorypath to outputoptionsanswers: Objectinject prompt values
const {mock} = require('gogen')
it('generate correctly', async () => {
const {files, readFile} = await mock('.', 'dist', {
answers: {description: 'superb'},
})
expect(files).toMatchSnapshot()
expect(readFile('package.json')).toMatch(/superb/)
})Comparison with alternatives
| Library | Package Phobia |
|---|---|
| gogen | |
| sao | |
| yeoman |