mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-04-22 04:18:33 -05:00
refactor bin
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
# @vue/cli-build
|
||||
|
||||
> `vue build` command addon for `@vue/cli`
|
||||
@@ -0,0 +1,3 @@
|
||||
# @vue/cli-service-global
|
||||
|
||||
Installing this package globally allows you to run `vue serve` and `vue build` directly without any local dependencies.
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@vue/cli-build",
|
||||
"name": "@vue/cli-service-global",
|
||||
"version": "0.1.0",
|
||||
"description": "build addon for vue-cli",
|
||||
"description": "vue-cli-service global addon for vue-cli",
|
||||
"main": "index.js",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
@@ -1,9 +1,71 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('commander')
|
||||
const chalk = require('chalk')
|
||||
const semver = require('semver')
|
||||
const { error } = require('@vue/cli-shared-utils')
|
||||
const requiredVersion = require('../package.json').engines.node
|
||||
|
||||
if (!semver.satisfies(process.version, requiredVersion)) {
|
||||
error(
|
||||
`You are using Node ${process.version}, but this version of vue-cli ` +
|
||||
`requires Node ${requiredVersion}.\nPlease upgrade your Node version.`
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const program = require('commander')
|
||||
const loadCommand = require('../lib/util/loadCommand')
|
||||
|
||||
program
|
||||
.version(require('../package').version)
|
||||
.usage('<command> [options]')
|
||||
.command('create', 'create a new project powered by vue-cli-service')
|
||||
.command('init', 'generate a project from a remote template')
|
||||
.command('build', 'build a .js or .vue file with zero config')
|
||||
.parse(process.argv)
|
||||
|
||||
program
|
||||
.command('create <app-name>')
|
||||
.description('create a new project powered by vue-cli-service')
|
||||
.action(require('../lib/create'))
|
||||
|
||||
program
|
||||
.command('init <template> <app-name>')
|
||||
.description('generate a project from a remote template (requires @vue/cli-init)')
|
||||
.action(() => {
|
||||
loadCommand('init', '@vue/cli-init')
|
||||
})
|
||||
|
||||
program
|
||||
.command('serve [filename]')
|
||||
.description('serve a .js or vue file in development mode with zero config')
|
||||
.action(filename => {
|
||||
loadCommand('serve', '@vue/cli-service-global').serve(filename)
|
||||
})
|
||||
|
||||
program
|
||||
.command('build [filename]')
|
||||
.description('build a .js or .vue file in production mode with zero config')
|
||||
.action(filename => {
|
||||
loadCommand('build', '@vue/cli-service-global').build(filename)
|
||||
})
|
||||
|
||||
program.on('--help', () => {
|
||||
console.log()
|
||||
})
|
||||
|
||||
// customize missing arg messages
|
||||
const formatArgs = ({ name, required }) => {
|
||||
return `${required ? '<' : '['}${name}${required ? '>' : ']'}`
|
||||
}
|
||||
|
||||
program.Command.prototype.missingArgument = function (argName) {
|
||||
console.log()
|
||||
console.log(` Missing required argument ${chalk.yellow(`<${argName}>`)}.`)
|
||||
console.log()
|
||||
console.log(` Usage: vue ${this._name} ${this._args.map(formatArgs).join(' ')}`)
|
||||
console.log()
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
program.parse(process.argv)
|
||||
|
||||
if (!process.argv.slice(2).length) {
|
||||
program.outputHelp()
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('../lib/util/loadCommand')('build')
|
||||
@@ -1,70 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const {
|
||||
warn,
|
||||
error,
|
||||
stopSpinner
|
||||
} = require('@vue/cli-shared-utils')
|
||||
const semver = require('semver')
|
||||
const requiredVersion = require('../package.json').engines.node
|
||||
|
||||
if (!semver.satisfies(process.version, requiredVersion)) {
|
||||
error(
|
||||
`You are using Node ${process.version}, but this version of vue-cli ` +
|
||||
`requires Node ${requiredVersion}.\nPlease upgrade your Node version.`
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const chalk = require('chalk')
|
||||
const rimraf = require('rimraf')
|
||||
const inquirer = require('inquirer')
|
||||
const program = require('commander')
|
||||
const Creator = require('../lib/Creator')
|
||||
const clearConsole = require('../lib/util/clearConsole')
|
||||
|
||||
async function create () {
|
||||
program
|
||||
.usage('<app-name>')
|
||||
.parse(process.argv)
|
||||
|
||||
const projectName = program.args[0]
|
||||
if (!projectName) {
|
||||
warn(`\n Please provide an app name.`)
|
||||
program.outputHelp()
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const targetDir = path.resolve(process.cwd(), projectName)
|
||||
if (fs.existsSync(targetDir)) {
|
||||
clearConsole()
|
||||
const { action } = await inquirer.prompt([
|
||||
{
|
||||
name: 'action',
|
||||
type: 'list',
|
||||
message: `Target directory ${chalk.cyan(targetDir)} already exists. Pick an action:`,
|
||||
choices: [
|
||||
{ name: 'Overwrite', value: 'overwrite' },
|
||||
{ name: 'Merge', value: 'merge' },
|
||||
{ name: 'Cancel', value: false }
|
||||
]
|
||||
}
|
||||
])
|
||||
if (!action) {
|
||||
return
|
||||
} else if (action === 'overwrite') {
|
||||
rimraf.sync(targetDir)
|
||||
}
|
||||
}
|
||||
|
||||
const creator = new Creator(projectName, targetDir)
|
||||
await creator.create()
|
||||
}
|
||||
|
||||
create().catch(err => {
|
||||
stopSpinner(false) // do not persist
|
||||
error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('../lib/util/loadCommand')('init')
|
||||
@@ -0,0 +1,43 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const chalk = require('chalk')
|
||||
const rimraf = require('rimraf')
|
||||
const inquirer = require('inquirer')
|
||||
const Creator = require('./Creator')
|
||||
const clearConsole = require('./util/clearConsole')
|
||||
const { error, warn, stopSpinner } = require('@vue/cli-shared-utils')
|
||||
|
||||
async function create (projectName) {
|
||||
const targetDir = path.resolve(process.cwd(), projectName)
|
||||
if (fs.existsSync(targetDir)) {
|
||||
clearConsole()
|
||||
const { action } = await inquirer.prompt([
|
||||
{
|
||||
name: 'action',
|
||||
type: 'list',
|
||||
message: `Target directory ${chalk.cyan(targetDir)} already exists. Pick an action:`,
|
||||
choices: [
|
||||
{ name: 'Overwrite', value: 'overwrite' },
|
||||
{ name: 'Merge', value: 'merge' },
|
||||
{ name: 'Cancel', value: false }
|
||||
]
|
||||
}
|
||||
])
|
||||
if (!action) {
|
||||
return
|
||||
} else if (action === 'overwrite') {
|
||||
rimraf.sync(targetDir)
|
||||
}
|
||||
}
|
||||
|
||||
const creator = new Creator(projectName, targetDir)
|
||||
await creator.create()
|
||||
}
|
||||
|
||||
module.exports = projectName => {
|
||||
create(projectName).catch(err => {
|
||||
stopSpinner(false) // do not persist
|
||||
error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
module.exports = function loadCommand (name) {
|
||||
const moduleName = `@vue/cli-${name}`
|
||||
module.exports = function loadCommand (commandName, moduleName) {
|
||||
const isNotFoundError = err => {
|
||||
return err.message.match(/Cannot find module/)
|
||||
}
|
||||
@@ -8,7 +7,7 @@ module.exports = function loadCommand (name) {
|
||||
} catch (err) {
|
||||
if (isNotFoundError(err)) {
|
||||
try {
|
||||
require('import-global')(`@vue/cli-${name}`)
|
||||
return require('import-global')(moduleName)
|
||||
} catch (err2) {
|
||||
if (isNotFoundError(err2)) {
|
||||
const chalk = require('chalk')
|
||||
@@ -16,10 +15,11 @@ module.exports = function loadCommand (name) {
|
||||
const installCommand = hasYarn ? `yarn global add` : `npm install -g`
|
||||
console.log()
|
||||
console.log(
|
||||
` Command ${chalk.cyan(`vue ${name}`)} requires a global addon to be installed.\n` +
|
||||
` Command ${chalk.cyan(`vue ${commandName}`)} requires a global addon to be installed.\n` +
|
||||
` Please run ${chalk.cyan(`${installCommand} ${moduleName}`)} and try again.`
|
||||
)
|
||||
console.log()
|
||||
process.exit(1)
|
||||
} else {
|
||||
throw err2
|
||||
}
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
"version": "3.0.0-alpha.1",
|
||||
"description": "Command line interface for rapid Vue.js development",
|
||||
"bin": {
|
||||
"vue": "bin/vue",
|
||||
"vue-create": "bin/vue-create",
|
||||
"vue-init": "bin/vue-init"
|
||||
"vue": "bin/vue"
|
||||
},
|
||||
"files": [
|
||||
"bin",
|
||||
|
||||
Reference in New Issue
Block a user