脚手架

参考:

https://blog.csdn.net/weixin_38788347/article/details/81239527

https://www.cnblogs.com/zhangycun/p/12097752.html

https://www.npmjs.com/package/download-git-repo

下载自己的仓库是,遇到报错:

✖ Downloading...
✖ Error: 'git clone' failed with status 128

原因是:

download('https://github.com/LiFangCoder/vue-template-one#master', name, { clone: true }, (err) => {})

应该写成:

download('https://github.com:LiFangCoder/vue-template-one#master', name, { clone: true }, (err) => {

 

index.js文件内容:

#!/usr/bin/env node

// 处理用户输入的命令

const program = require('commander');

// 下载模板

const download = require('download-git-repo');

// 问题交互

const inquirer = require('inquirer');

// node 文件模块

const fs = require('fs');

// 填充信息至文件

const handlebars = require('handlebars');

// 动画效果

const ora = require('ora');

// 字体加颜色

const chalk = require('chalk');

// 显示提示图标

const symbols = require('log-symbols');

// 命令行操作

var shell = require("shelljs");

 

program.version('1.0.1', '-v, --version')

.command('init <name>')

.action((name) => {

if (!fs.existsSync(name)) {

inquirer.prompt([

{

name: 'description',

message: 'Input the object description'

},

{

name: 'author',

message: 'Input the object author'

}

]).then((answers) => {

const spinner = ora('Downloading...');

spinner.start();

download('https://github.com:LiFangCoder/vue-template-one#main', name, { clone: true }, (err) => {

if (err) {

spinner.fail();

console.log(symbols.error, chalk.red(err));

} else {

spinner.succeed();

const fileName = `${name}/package.json`;

const meta = {

name,

description: answers.description,

author: answers.author

}

if (fs.existsSync(fileName)) {

const content = fs.readFileSync(fileName).toString();

const result = handlebars.compile(content)(meta);

fs.writeFileSync(fileName, result);

}

console.log(symbols.success, chalk.green('The vue object has downloaded successfully!'));

inquirer.prompt([

{

type: 'confirm',

name: 'ifInstall',

message: 'Are you want to install dependence now?',

default: true

}

]).then((answers) => {

if (answers.ifInstall) {

inquirer.prompt([

{

type: 'list',

name: 'installWay',

message: 'Choose the tool to install',

choices: [

'npm', 'cnpm'

]

}

]).then(ans => {

if (ans.installWay === 'npm') {

let spinner = ora('Installing...');

spinner.start();

// 命令行操作安装依赖

shell.exec("cd " + name + " && npm i", function (err, stdout, stderr) {

if (err) {

spinner.fail();

console.log(symbols.error, chalk.red(err));

}

else {

spinner.succeed();

console.log(symbols.success, chalk.green('The object has installed dependence successfully!'));

}

});

} else {

let spinner = ora('Installing...');

spinner.start();

shell.exec("cd " + name + " && cnpm i", function (err, stdout, stderr) {

if (err) {

spinner.fail();

console.log(symbols.error, chalk.red(err));

}

else {

spinner.succeed();

console.log(symbols.success, chalk.green('The object has installed dependence successfully!'));

}

})

}

})

} else {

console.log(symbols.success, chalk.green('You should install the dependence by yourself!'));

}

})

}

})

})

} else {

// 错误提示项目已存在,避免覆盖原有项目

console.log(symbols.error, chalk.red('The object has exist'));

}

});

program.parse(process.argv);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值