better CLI usage
This commit is contained in:
parent
de7c5a1acf
commit
740062db7c
7 changed files with 151 additions and 165 deletions
|
@ -9,9 +9,12 @@ const config = require('./config')
|
|||
|
||||
module.exports = async () => {
|
||||
const defaults = {
|
||||
name: '',
|
||||
description: '',
|
||||
author: config.get('author'),
|
||||
manager: config.get('manager', 'npm'),
|
||||
license: config.get('license', 'MIT')
|
||||
repo: (info) => `${info.author}/${info.name}`,
|
||||
license: config.get('license', 'MIT'),
|
||||
manager: config.get('manager', 'npm')
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -38,7 +41,7 @@ module.exports = async () => {
|
|||
defaults.manager = 'yarn'
|
||||
}
|
||||
|
||||
config.set('manager', defaults.manager)
|
||||
config.set('manager', defaults.manager || 'npm')
|
||||
}
|
||||
} catch (err) { }
|
||||
|
|
@ -1,47 +1,76 @@
|
|||
#!/usr/bin/env node
|
||||
'use strict'
|
||||
|
||||
const meow = require('meow')
|
||||
const program = require('commander')
|
||||
const { version } = require('../package')
|
||||
|
||||
const getLibraryDefaults = require('./get-library-defaults')
|
||||
const getDefaultLibraryParams = require('./get-default-library-params')
|
||||
const createLibrary = require('./create-library')
|
||||
const promptLibraryInfo = require('./prompt-library-info')
|
||||
const promptLibraryParams = require('./prompt-library-params')
|
||||
|
||||
module.exports = async () => {
|
||||
const defaults = await getLibraryDefaults()
|
||||
const info = await promptLibraryInfo(defaults)
|
||||
await createLibrary(info)
|
||||
const defaults = await getDefaultLibraryParams()
|
||||
|
||||
return info
|
||||
}
|
||||
program
|
||||
.name('create-react-library')
|
||||
.version(version)
|
||||
.usage('[options] [package-name]')
|
||||
.option('-d, --desc <string>', 'package description')
|
||||
.option('-a, --author <string>', 'author\'s github handle', defaults.author)
|
||||
.option('-l, --license <string>', 'package license', defaults.license)
|
||||
.option('-r, --repo <string>', 'package repo path')
|
||||
.option('-m, --manager <npm|yarn>', 'package manager to use', /^(npm|yarn)$/, defaults.manager)
|
||||
.option('-s, --skip-prompts', 'skip all prompts (must provide package-name via cli)')
|
||||
.parse(process.argv)
|
||||
|
||||
meow(`
|
||||
Usage
|
||||
$ create-react-library
|
||||
`)
|
||||
const opts = {
|
||||
description: program.desc,
|
||||
author: program.author,
|
||||
license: program.license,
|
||||
repo: program.repo,
|
||||
manager: program.manager,
|
||||
skipPrompts: program.skipPrompts
|
||||
}
|
||||
|
||||
module.exports()
|
||||
.then((info) => {
|
||||
console.log(`
|
||||
Object.keys(opts).forEach((key) => {
|
||||
if (!opts[key] && defaults[key]) {
|
||||
opts[key] = defaults[key]
|
||||
}
|
||||
})
|
||||
|
||||
Your module has been created at ${info.dest}.
|
||||
if (program.args.length === 1) {
|
||||
opts.name = program.args[0]
|
||||
} else if (program.args.length > 1) {
|
||||
console.error('invalid arguments')
|
||||
program.help()
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const params = await promptLibraryParams(opts)
|
||||
const lib = await createLibrary(params)
|
||||
|
||||
console.log(`
|
||||
|
||||
Your module has been created at ${lib.dest}.
|
||||
|
||||
To get started, in one tab, run:
|
||||
$ cd ${info.name} && ${info.manager} start
|
||||
$ cd ${lib.name} && ${lib.manager} start
|
||||
|
||||
And in another tab, run the create-react-app devserver:
|
||||
$ cd ${info.name}/example && ${info.manager} start
|
||||
$ cd ${lib.name}/example && ${lib.manager} start
|
||||
`)
|
||||
|
||||
if (info.manager === 'npm') {
|
||||
console.log(`
|
||||
if (lib.manager === 'npm') {
|
||||
console.log(`
|
||||
|
||||
Because you're using npm, you'll need to publish a dummy version of ${info.name} first before you can "npm link" your package into the example app.
|
||||
Because you're using npm, you'll need to publish a dummy version of ${lib.name} first before you can "npm link" your package into the example app.
|
||||
`)
|
||||
}
|
||||
}
|
||||
|
||||
process.exit(0)
|
||||
})
|
||||
return lib
|
||||
}
|
||||
|
||||
module.exports()
|
||||
.catch((err) => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const inquirer = require('inquirer')
|
||||
const validateNpmName = require('validate-npm-package-name')
|
||||
|
||||
const config = require('./config')
|
||||
|
||||
module.exports = async (defaults) => {
|
||||
const info = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'name',
|
||||
message: 'Package Name',
|
||||
validate: (name) => {
|
||||
return name && validateNpmName(name).validForNewPackages
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'description',
|
||||
message: 'Package Description',
|
||||
default: ''
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'author',
|
||||
message: 'Author\'s GitHub Handle',
|
||||
default: defaults.author
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'repo',
|
||||
message: 'GitHub Repo Path',
|
||||
default: (info) => `${info.author}/${info.name}`
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'license',
|
||||
message: 'License',
|
||||
default: defaults.license
|
||||
},
|
||||
{
|
||||
type: 'list',
|
||||
name: 'manager',
|
||||
message: 'Package Manager',
|
||||
choices: [ 'npm', 'yarn' ],
|
||||
default: defaults.manager
|
||||
}
|
||||
])
|
||||
|
||||
config.set('author', info.author)
|
||||
config.set('manager', info.manager)
|
||||
config.set('license', info.license)
|
||||
|
||||
return info
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
'use strict'
|
||||
|
||||
const inquirer = require('inquirer')
|
||||
const validateNpmName = require('validate-npm-package-name')
|
||||
|
||||
const config = require('./config')
|
||||
|
||||
module.exports = async (opts) => {
|
||||
if (opts.name && !validateNpmName(opts.name).validForNewPackages) {
|
||||
throw new Error(`invalid package name "${opts.name}"`)
|
||||
}
|
||||
|
||||
if (opts.skipPrompts) {
|
||||
if (!opts.name) {
|
||||
throw new Error('invalid input; you must pass a package name with --skip-prompts')
|
||||
}
|
||||
|
||||
Object.keys(opts).forEach((key) => {
|
||||
const value = opts[key]
|
||||
if (typeof value === 'function') {
|
||||
opts[key] = value(opts)
|
||||
}
|
||||
})
|
||||
|
||||
return opts
|
||||
} else {
|
||||
const info = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'name',
|
||||
message: 'Package Name',
|
||||
validate: (name) => {
|
||||
return name && validateNpmName(name).validForNewPackages
|
||||
},
|
||||
default: opts.name
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'description',
|
||||
message: 'Package Description',
|
||||
default: opts.description
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'author',
|
||||
message: 'Author\'s GitHub Handle',
|
||||
default: opts.author
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'repo',
|
||||
message: 'GitHub Repo Path',
|
||||
default: opts.repo
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
name: 'license',
|
||||
message: 'License',
|
||||
default: opts.license
|
||||
},
|
||||
{
|
||||
type: 'list',
|
||||
name: 'manager',
|
||||
message: 'Package Manager',
|
||||
choices: [ 'npm', 'yarn' ],
|
||||
default: opts.manager
|
||||
}
|
||||
])
|
||||
|
||||
config.set('author', info.author)
|
||||
config.set('license', info.license)
|
||||
config.set('manager', info.manager)
|
||||
|
||||
return info
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue