@vuepress-reco/theme-cli
Version:
A cli for vuepress-theme-reco
122 lines (121 loc) • 4.63 kB
JavaScript
var program = require('commander');
var download = require('download-git-repo');
var chalk = require('chalk');
var ora = require('ora');
var fs = require('fs');
var handleInquirer = require('./inquirer.js');
var spinner = ora();
var stepNum = 3;
var currentStep = 1;
program
.version('1.0.0')
.option('-i, init [name]', '初始化 vuepress-theme-reco 主题博客')
.parse(process.argv);
program.on('--help', function () {
console.log(' Examples:');
console.log();
console.log(chalk.gray(' # create a new project with an official template'));
console.log(' $ reco-cli init my-blog');
console.log();
});
if (program.init) {
var isString = typeof program.init === 'string';
handleInquirer(isString, program.init)
.then(function (choices) {
var style = choices.style, newDir = choices.newDir;
program.init = newDir;
stepNum = style === '2.x' ? 1 : style === 'doc style for 1.x' ? 2 : 3;
var branchName = style === '2.x' ? '2.x' : '1.x';
var gitBranch = "recoluan/vuepress-theme-reco-demo#demo/" + branchName;
spinner.start(chalk.blue("[" + currentStep + "/" + stepNum + "] Load file from git"));
download(gitBranch, program.init, function (err) {
if (!err) {
spinner.succeed(chalk.blue("[" + currentStep + "/" + stepNum + "] Load file from git"));
currentStep++;
handleDownload(choices);
}
else {
spinner.fail(chalk.redBright("[" + currentStep + "/" + stepNum + "] Load file from git"));
console.info(err);
spinner.stop();
}
});
})["catch"](function (err) {
console.info(chalk.redBright(err));
});
}
function handleDownload(choices) {
if (choices.style === '2.x') {
handleEnd();
return;
}
if (choices.style === 'doc style for 1.x') {
changePackage(choices).then(function () {
handleEnd();
});
return;
}
Promise.all([changePackage(choices), changeConfig(choices)]).then(function () {
handleEnd();
});
}
function handleEnd() {
spinner.stop();
console.log();
console.info(chalk.greenBright('Load successful, enjoy it!'));
console.log();
if (program.init !== './') {
console.log(chalk.gray('# Inter your blog'));
console.log("$ cd " + program.init);
}
console.log(chalk.gray('# Install package'));
console.log('$ yarn & npm install');
}
function changePackage(choices) {
spinner.start(chalk.blue("[" + currentStep + "/" + stepNum + "] Edit package.json"));
return new Promise(function (resolve) {
fs.readFile(process.cwd() + "/" + program.init + "/package.json", function (err, data) {
if (err)
throw err;
var _data = JSON.parse(data.toString());
_data.name = choices.title;
_data.description = choices.description;
_data.version = '1.0.0';
var str = JSON.stringify(_data, null, 2);
fs.writeFile(process.cwd() + "/" + program.init + "/package.json", str, function (err) {
if (!err) {
spinner.succeed(chalk.blue("[" + currentStep + "/" + stepNum + "] Edit package.json"));
currentStep++;
resolve();
}
else {
spinner.fail(chalk.blue("[" + currentStep + "/" + stepNum + "] Edit package.json"));
throw err;
}
});
});
});
}
function changeConfig(choices) {
spinner.start(chalk.blue("[" + currentStep + "/" + stepNum + "] Edit config.js"));
return new Promise(function (resolve) {
var _data = require(process.cwd() + "/" + program.init + "/.vuepress/config.js");
_data.themeConfig.type = 'blog';
_data.themeConfig.author = choices.author;
_data.title = choices.title;
_data.description = choices.description;
var str = "module.exports = " + JSON.stringify(_data, null, 2);
fs.writeFile(process.cwd() + "/" + program.init + "/.vuepress/config.js", str, function (err) {
if (!err) {
spinner.succeed(chalk.blue("[" + currentStep + "/" + stepNum + "] Edit config.js"));
currentStep++;
resolve();
}
else {
spinner.fail(chalk.blue("[" + currentStep + "/" + stepNum + "] Edit config.js"));
throw err;
}
});
});
}