Use stream API to scaffold projects or files.
- Simple, easy to use
- Lightweight, single file bundled (<40K gzip size), no need to install globally, just run
npx - Automatically rename
gitignoreto.gitignore, due to npm/issues/1862 - Automatically set
namefield inpackage.json - Automatically parse command line arguments with mri
- Add command line prompts with prompts
- Render
*.t.foo(or*.foo.t) to*.foowith lodash template - Provide fast, in-memory testing API
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> # folder- Scaffold project
- npm initializer or starter kits
- create-gogen
- new-mina CRA-like initializer in 12 lines.
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:
/**
* @type {import('gogen').Generator}
*/
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>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>.gogenrc.js:
run(api: Object, context: Object) => voidapicore stream and helper APIssrc(glob: string | string[], options: fg.Options) => Streamread files, support fast-glob optionsdest(path?: string) => Streamwrite filespipeline(...streams: Stream[]) => Promisepipe a series of streamstemplate(data: Object, {ext: RegExp, test: RegExp, render: Function}) => Streamrender*.tor*.t.foofiles 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
install(deps: string[], {dev: boolean, silent: boolean}) => Promiseinstall dependenciesgitInit(message: string) => Promiseinit git repositoryprompts(Array | Object) => Promisesee prompts
contextgenerator contextpath: stringnew project's path (it's also a setter)name: stringnew project's name (it's also a setter)argv: Objectcommand line arguments, parsed by mri
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/)
})| Library | Package Phobia |
|---|---|
| gogen | |
| sao | |
| yeoman |