dybox-module-create
Version:
diyi a vue module project
99 lines (94 loc) • 3.18 kB
JavaScript
const program = require("commander");
const path = require("path");
const fs = require("fs");
const inquirer = require("inquirer");
const download = require("download-git-repo");
const ora = require("ora");
const chalk = require("chalk");
program.version("0.0.1");
const loading = ora("模板下载中,请稍后……");
const query = process.argv[process.argv.length-1]
const gitUrl = "http://192.168.1.184:dyWeb/ErpDyTinyWeb/vue-module-template#"+query;
program.option("-i, --init [name]", "init a project", "myapp");
program.parse(process.argv);
if (program.init) {
var projectPath = path.resolve(program.init);
var projectName = path.basename(projectPath);
const questions = [
{
type: "input",
name: "name",
message: "递易项目模块名称",
default: projectName,
filter: value => value.trim(),
validate: value => {
const validate = value.trim().split(" ").length === 1;
return validate || "项目名称不允许有空格!";
},
transformer: value => `:${value}`
},
{
type: "input",
name: "description",
message: "递易项目模块描述",
default: "a admincraft project",
validate: () => {
return true;
},
transformer: value => `:${value}`
},
{
type: "input",
name: "author",
message: "递易项目模块作者",
default: "unnamed",
validate(val) {
return true;
},
transformer: value => `:${value}`
}
];
inquirer.prompt(questions).then(({ name, description, author }) => {
loading.start();
console.log(name, description, author )
download(gitUrl, `./${name}`, { clone: true }, function(err) {
if (err) {
loading.fail("模板下载失败!");
console.log(chalk.red(err));
process.exit();
} else {
fs.readFile(`./${name}/package.json`, "utf8", (err, data) => {
if (err) {
console.log(chalk.red(err));
process.exit();
}
const packageJson = JSON.parse(data);
packageJson.name = name;
packageJson.description = description;
packageJson.author = author;
packageString = JSON.stringify(packageJson, null, 2);
fs.writeFile(`./${name}/package.json`, packageString, "utf8", err => {
if (err) {
console.log(chalk.red(err));
process.exit();
}
loading.succeed("递易项目模板准备就绪!请进一步操作。");
console.log(`
${chalk.bgWhite.black(" 进一步操作 ")}
${chalk.yellow(`cd ${name}`)}
${chalk.yellow("yarn 或 npm install")}
${chalk.yellow("yarn serve 或 npm run serve")}
`);
});
});
}
});
});
}
program.on("--help", function() {
console.log(" Examples:");
console.log("");
console.log(" this is an example");
console.log("");
});