Skip to content

Commit

Permalink
feat: add flag options to run
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Oct 12, 2021
1 parent bd6bcc1 commit 81dfbb3
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 8 deletions.
10 changes: 6 additions & 4 deletions create-gogen/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ run(
)

// OR
// const {create} = require('gogen')
// create({
// _: [__dirname, ...process.argv.slice(2)],
// })
// const {create, minimist} = require('gogen')
// create(
// minimist([__dirname, ...process.argv.slice(2)], {
// boolean: ['myflag']
// })
// )
11 changes: 11 additions & 0 deletions examples/cli-create/.gogenrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @type {import('gogen').Generator}
*/
module.exports = async ({src, dest, pipeline, modify}, {name, argv}) => {
const {_, ...args} = argv
await pipeline(
src('template/**'),
modify.json(/data/, (_, x) => ({...x, name, args})),
dest()
)
}
5 changes: 5 additions & 0 deletions examples/cli-create/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Development

```
npx cli-create <directory>
```
17 changes: 17 additions & 0 deletions examples/cli-create/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node

const {create, minimist} = require('gogen')

const run = async () => {
const argv = minimist([__dirname, ...process.argv.slice(2)], {
alias: {h: 'help'},
boolean: ['help', 'install'],
})
if (argv.help || argv._.length < 2) {
console.info(`Usage: npx cli-create <directory>`)
return
}
await create(argv)
}

run().catch(console.error)
12 changes: 12 additions & 0 deletions examples/cli-create/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "cli-create",
"private": true,
"version": "2.1.0",
"bin": "cli.js",
"scripts": {
"gen": "rm -rf dist && npx cli-create dist"
},
"dependencies": {
"gogen": "^2.1.0"
}
}
1 change: 1 addition & 0 deletions examples/cli-create/template/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 6 additions & 0 deletions examples/cli-create/template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "mylib",
"version": "1.0.0",
"main": "index.js",
"license": "MIT"
}
53 changes: 53 additions & 0 deletions examples/cli-create/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const path = require('path')
const fs = require('fs').promises
const {exec} = require('child_process')
const {promisify} = require('util')
const {mock} = require('gogen')

const command = promisify(exec)
const resolveDir = path.resolve.bind(null, __dirname)
const cli = resolveDir('cli.js')
const outdir = resolveDir('dist')
const readJson = async (file) =>
JSON.parse(await fs.readFile(resolveDir(outdir, file)))

beforeEach(async () => {
fs.rm
? await fs.rm(outdir, {recursive: true, force: true})
: // remove in Node v14
await fs.rmdir(outdir, {recursive: true}).catch(() => {})
})

test('basic', async () => {
const {files, readFile} = await mock(__dirname, 'mylib')
expect(files).toMatchInlineSnapshot(`
Array [
"data.json",
"package.json",
]
`)
})

test('cli args - help', async () => {
const r = await command(`${cli} ${outdir} -h`)
expect(r.stdout).toMatch(/Usage:/)
expect((await command(`${cli}`)).stdout).toMatch(/Usage:/)
})

test('cli args - post', async () => {
await command(`${cli} ${outdir} --no-install`)
expect((await readJson('data.json')).args).toMatchInlineSnapshot(`
Object {
"install": false,
}
`)
})

test('cli args - pre', async () => {
await command(`${cli} --no-install ${outdir}`)
expect((await readJson('data.json')).args).toMatchInlineSnapshot(`
Object {
"install": false,
}
`)
})
11 changes: 11 additions & 0 deletions examples/cli-run/.gogenrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @type {import('gogen').Generator}
*/
module.exports = async ({src, dest, pipeline, modify}, {name, argv}) => {
const {_, ...args} = argv
await pipeline(
src('template/**'),
modify.json(/data/, (_, x) => ({...x, name, args})),
dest()
)
}
5 changes: 5 additions & 0 deletions examples/cli-run/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Development

```
npx cli-run <directory>
```
7 changes: 7 additions & 0 deletions examples/cli-run/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node

const {run} = require('gogen')

run([__dirname, ...process.argv.slice(2)], 'Usage: npx cli-run <directory>', {
boolean: ['install'],
})
12 changes: 12 additions & 0 deletions examples/cli-run/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "cli-run",
"private": true,
"version": "2.1.0",
"bin": "cli.js",
"scripts": {
"gen": "rm -rf dist && npx cli-run dist"
},
"dependencies": {
"gogen": "^2.1.0"
}
}
1 change: 1 addition & 0 deletions examples/cli-run/template/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 6 additions & 0 deletions examples/cli-run/template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "mylib",
"version": "1.0.0",
"main": "index.js",
"license": "MIT"
}
47 changes: 47 additions & 0 deletions examples/cli-run/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require('path')
const fs = require('fs').promises
const {exec} = require('child_process')
const {promisify} = require('util')
const {mock} = require('gogen')

const command = promisify(exec)
const resolveDir = path.resolve.bind(null, __dirname)
const cli = resolveDir('cli.js')
const outdir = resolveDir('dist')
const readJson = async (file) =>
JSON.parse(await fs.readFile(resolveDir(outdir, file)))

beforeEach(async () => {
fs.rm
? await fs.rm(outdir, {recursive: true, force: true})
: // remove in Node v14
await fs.rmdir(outdir, {recursive: true}).catch(() => {})
})

test('basic', async () => {
const {files, readFile} = await mock(__dirname, 'mylib')
expect(files).toMatchInlineSnapshot(`
Array [
"data.json",
"package.json",
]
`)
})

test('cli args - post', async () => {
await command(`${cli} ${outdir} --no-install`)
expect((await readJson('data.json')).args).toMatchInlineSnapshot(`
Object {
"install": false,
}
`)
})

test('cli args - pre', async () => {
await command(`${cli} --no-install ${outdir}`)
expect((await readJson('data.json')).args).toMatchInlineSnapshot(`
Object {
"install": false,
}
`)
})
4 changes: 3 additions & 1 deletion gogen/lib/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import create from './create'

const run = async (
args: string[],
usage = 'Usage: npx gogen <generator> <directory>'
usage = 'Usage: npx gogen <generator> <directory>',
opts: mri.Options
) => {
const argv = mri<Argv>(args, {
alias: {c: 'clone'},
boolean: ['clone'],
...opts,
})
if (argv._.length < 2) {
console.error(usage)
Expand Down
2 changes: 1 addition & 1 deletion gogen/test/fixtures/change-context/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "test-basic",
"name": "change-context",
"private": true,
"version": "0.0.0",
"dependencies": {}
Expand Down
2 changes: 1 addition & 1 deletion gogen/test/fixtures/change-dest/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "test-basic",
"name": "change-dest",
"private": true,
"version": "0.0.0",
"dependencies": {}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"modulePathIgnorePatterns": [
"/examples/.*/dist",
"/examples/.*/template",
"/gogen/test/fixtures/.*/template"
"/gogen/test/fixtures/.*"
]
},
"resolutions": {
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,26 @@ __metadata:
languageName: node
linkType: hard

"cli-create@workspace:examples/cli-create":
version: 0.0.0-use.local
resolution: "cli-create@workspace:examples/cli-create"
dependencies:
gogen: ^2.1.0
bin:
cli-create: cli.js
languageName: unknown
linkType: soft

"cli-run@workspace:examples/cli-run":
version: 0.0.0-use.local
resolution: "cli-run@workspace:examples/cli-run"
dependencies:
gogen: ^2.1.0
bin:
cli-run: cli.js
languageName: unknown
linkType: soft

"cliui@npm:^7.0.2":
version: 7.0.4
resolution: "cliui@npm:7.0.4"
Expand Down

0 comments on commit 81dfbb3

Please sign in to comment.