create-jslib
Version:
CLI tool for building JavaScript libraries.
191 lines (164 loc) • 6.69 kB
JavaScript
// Check node version before requiring/doing anything else
// The user may be on a very old node version
const chalk = require('chalk')
const semver = require('semver')
const requiredVersion = require('../package.json').engines.node
function checkNodeVersion (wanted, id) {
if (!semver.satisfies(process.version, wanted)) {
console.log(chalk.red(
'You are using Node ' + process.version + ', but this version of ' + id +
' requires Node ' + wanted + '.\nPlease upgrade your Node version.'
))
process.exit(1)
}
}
checkNodeVersion(requiredVersion, 'create-jslib')
if (semver.satisfies(process.version, '9.x')) {
console.log(chalk.red(
`You are using Node ${process.version}.\n` +
`Node.js 9.x has already reached end-of-life and will not be supported in future major releases.\n` +
`It's strongly recommended to use an active LTS version instead.`
))
}
const fs = require('fs')
const path = require('path')
const slash = require('slash')
const minimist = require('minimist')
// enter debug mode when creating test repo
if (
slash(process.cwd()).indexOf('/packages/test') > 0 && (
fs.existsSync(path.resolve(process.cwd(), '../create-jslib')) ||
fs.existsSync(path.resolve(process.cwd(), '../../create-jslib'))
)
) {
process.env.JSLIB_DEBUG = true
}
const program = require('commander')
program
.version(require('../package').version)
.usage('<command> [options]')
// create-jslib create <lib-name>
program
.command('create <lib-name>')
.description('create a new project powered by jslib-service')
.option('-d, --default', 'Skip prompts and use default preset')
.option('-i, --inlinePreset <json>', 'Skip prompts and use inline JSON string as preset')
.option('-m, --packageManager <command>', 'Use specified npm client when installing dependencies')
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
.option('-g, --git [message]', 'Force git initialization with initial commit message')
.option('-n, --no-git', 'Skip git initialization')
.option('-f, --force', 'Overwrite target directory if it exists')
.option('-x, --proxy', 'Use specified proxy when creating project')
.action((name, cmd) => {
createJSLib(name, cmd, 3)
})
// create-jslib info
program
.command('info')
.description('print debugging information about your environment')
.action((cmd) => {
console.log(chalk.bold('\nEnvironment Info:'))
require('envinfo').run(
{
System: ['OS', 'CPU'],
Binaries: ['Node', 'Yarn', 'npm'],
Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari'],
npmPackages: '/**/{typescript,*jslib*}',
npmGlobalPackages: ['create-jslib']
},
{
showNotFound: true,
duplicates: true,
fullTree: true
}
).then(console.log)
})
// create-jslib add typescript
program
.command('add <plugin> [pluginOptions]')
.description('install a plugin and invoke its generator in an already created project')
.option('--registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
.allowUnknownOption()
.action((plugin) => {
require('../lib/add')(plugin, minimist(process.argv.slice(3)))
})
// create-jslib invoke typescript
program
.command('invoke <plugin> [pluginOptions]')
.description('invoke the generator of a plugin in an already created project')
.option('--registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
.allowUnknownOption()
.action((plugin) => {
require('../lib/invoke')(plugin, minimist(process.argv.slice(3)))
})
// create-jslib foo
program
.arguments('<lib-name>')
.description(`Run ${chalk.cyan(`create-jslib <lib-name>`)} to create a new project powered by jslib-service`)
.option('-d, --default', 'Skip prompts and use default preset')
.option('-i, --inlinePreset <json>', 'Skip prompts and use inline JSON string as preset')
.option('-m, --packageManager <command>', 'Use specified npm client when installing dependencies')
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
.option('-g, --git [message]', 'Force git initialization with initial commit message')
.option('-n, --no-git', 'Skip git initialization')
.option('-f, --force', 'Overwrite target directory if it exists')
.option('-x, --proxy', 'Use specified proxy when creating project')
.action((name, cmd) => {
createJSLib(name, cmd, 2)
})
// add some useful info on help
program.on('--help', () => {
console.log()
console.log(` Run ${chalk.cyan(`create-jslib <command> --help`)} for detailed usage of given command.`)
console.log()
})
program.commands.forEach(c => c.on('--help', () => console.log()))
// enhance common error messages
const { enhanceErrorMessages } = require('../lib/util/enhanceErrorMessages')
enhanceErrorMessages('missingArgument', argName => {
return `Missing required argument ${chalk.yellow(`<${argName}>`)}.`
})
enhanceErrorMessages('unknownOption', optionName => {
return `Unknown option ${chalk.yellow(optionName)}.`
})
enhanceErrorMessages('optionMissingArgument', (option, flag) => {
return `Missing required argument for option ${chalk.yellow(option.flags)}` + (
flag ? `, got ${chalk.yellow(flag)}` : ``
)
})
program.parse(process.argv)
if (!process.argv.slice(2).length) {
program.outputHelp()
}
function camelize (str) {
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')
}
// commander passes the Command object itself as options,
// extract only actual options into a fresh object.
function cleanArgs (cmd) {
const args = {}
cmd.options.forEach(o => {
const key = camelize(o.long.replace(/^--/, ''))
// if an option is not present and Command has a method with the same name
// it should not be copied
if (typeof cmd[key] !== 'function' && typeof cmd[key] !== 'undefined') {
args[key] = cmd[key]
}
if (cmd.parent && typeof cmd.parent[key] !== 'function' && typeof cmd.parent[key] !== 'undefined') {
args[key] = cmd.parent[key]
}
})
return args
}
function createJSLib (name, cmd, preParamsNum) {
const options = cleanArgs(cmd)
if (minimist(process.argv.slice(preParamsNum))._.length > 1) {
console.log(chalk.yellow('\n Info: You provided more than one argument. The first one will be used as the library\'s name, the rest are ignored.'))
}
// --git makes commander to default git to true
if (process.argv.includes('-g') || process.argv.includes('--git')) {
options.forceGit = true
}
require('../lib/create')(name, options)
}