Created
January 21, 2018 04:17
-
-
Save YaoKaiLun/a8101f47dd579247dea255c676d7641d to your computer and use it in GitHub Desktop.
use vue cli to init a project template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const download = require('download-git-repo') | |
const program = require('commander') | |
const exists = require('fs').existsSync | |
const path = require('path') | |
const ora = require('ora') | |
const home = require('user-home') | |
const tildify = require('tildify') | |
const chalk = require('chalk') | |
const inquirer = require('inquirer') | |
const rm = require('rimraf').sync | |
const logger = require('../lib/logger') | |
const generate = require('../lib/generate') | |
const checkVersion = require('../lib/check-version') | |
const warnings = require('../lib/warnings') | |
const localPath = require('../lib/local-path') | |
const isLocalPath = localPath.isLocalPath | |
const getTemplatePath = localPath.getTemplatePath | |
/** | |
* Usage. | |
*/ | |
program | |
.usage('<template-name> [project-name]') | |
.option('-c, --clone', 'use git clone') | |
.option('--offline', 'use cached template') | |
/** | |
* Help. | |
*/ | |
program.on('--help', () => { | |
console.log(' Examples:') | |
console.log() | |
console.log(chalk.gray(' # create a new project with an official template')) | |
console.log(' $ vue init webpack my-project') | |
console.log() | |
console.log(chalk.gray(' # create a new project straight from a github template')) | |
console.log(' $ vue init username/repo my-project') | |
console.log() | |
}) | |
/** | |
* Help. | |
*/ | |
function help () { | |
program.parse(process.argv) | |
if (program.args.length < 1) return program.help() | |
} | |
help() | |
/** | |
* Settings. | |
*/ | |
let template = program.args[0] | |
const hasSlash = template.indexOf('/') > -1 | |
const rawName = program.args[1] | |
const inPlace = !rawName || rawName === '.' | |
const name = inPlace ? path.relative('../', process.cwd()) : rawName | |
const to = path.resolve(rawName || '.') | |
const clone = program.clone || false | |
const tmp = path.join(home, '.vue-templates', template.replace(/\//g, '-')) | |
if (program.offline) { | |
console.log(`> Use cached template at ${chalk.yellow(tildify(tmp))}`) | |
template = tmp | |
} | |
/** | |
* Padding. | |
*/ | |
console.log() | |
process.on('exit', () => { | |
console.log() | |
}) | |
if (exists(to)) { | |
inquirer.prompt([{ | |
type: 'confirm', | |
message: inPlace | |
? 'Generate project in current directory?' | |
: 'Target directory exists. Continue?', | |
name: 'ok' | |
}]).then(answers => { | |
if (answers.ok) { | |
run() | |
} | |
}).catch(logger.fatal) | |
} else { | |
run() | |
} | |
/** | |
* Check, download and generate the project. | |
*/ | |
function run () { | |
// check if template is local | |
if (isLocalPath(template)) { | |
const templatePath = getTemplatePath(template) | |
if (exists(templatePath)) { | |
generate(name, templatePath, to, err => { | |
if (err) logger.fatal(err) | |
console.log() | |
logger.success('Generated "%s".', name) | |
}) | |
} else { | |
logger.fatal('Local template "%s" not found.', template) | |
} | |
} else { | |
checkVersion(() => { | |
if (!hasSlash) { | |
// use official templates | |
const officialTemplate = 'vuejs-templates/' + template | |
if (template.indexOf('#') !== -1) { | |
downloadAndGenerate(officialTemplate) | |
} else { | |
if (template.indexOf('-2.0') !== -1) { | |
warnings.v2SuffixTemplatesDeprecated(template, inPlace ? '' : name) | |
return | |
} | |
// warnings.v2BranchIsNowDefault(template, inPlace ? '' : name) | |
downloadAndGenerate(officialTemplate) | |
} | |
} else { | |
downloadAndGenerate(template) | |
} | |
}) | |
} | |
} | |
/** | |
* Download a generate from a template repo. | |
* | |
* @param {String} template | |
*/ | |
function downloadAndGenerate (template) { | |
const spinner = ora('downloading template') | |
spinner.start() | |
// Remove if local template exists | |
if (exists(tmp)) rm(tmp) | |
download(template, tmp, { clone }, err => { | |
spinner.stop() | |
if (err) logger.fatal('Failed to download repo ' + template + ': ' + err.message.trim()) | |
generate(name, tmp, to, err => { | |
if (err) logger.fatal(err) | |
console.log() | |
logger.success('Generated "%s".', name) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment