1 | const path = require("path");
|
2 | const fs = require("fs");
|
3 | const question = [
|
4 | {
|
5 | name: "to",
|
6 | type: "input",
|
7 | message: `请输入模板保存路径(相对项目根目录):`
|
8 | }
|
9 | ];
|
10 |
|
11 | module.exports = async context => {
|
12 | const { template, cwd } = context;
|
13 | const inquirer = require("inquirer");
|
14 |
|
15 | inquirer.prompt(question).then(answers => {
|
16 | const { to } = answers;
|
17 | const target = path.join(cwd, to);
|
18 | const exists = fs.existsSync(target);
|
19 | if (exists) {
|
20 | generator(template, target);
|
21 | } else {
|
22 | console.log("目录不存在".red);
|
23 | }
|
24 | });
|
25 | };
|
26 |
|
27 | function generator(template, target) {
|
28 | const download = require("download-git-repo");
|
29 | const ora = require("ora");
|
30 |
|
31 | const spinner = ora("loading...").start();
|
32 | download(`liqiang0335/template-${template}`, target, err => {
|
33 | spinner.stop();
|
34 | if (err) {
|
35 | console.log(err);
|
36 | return;
|
37 | }
|
38 | console.log("OK".green);
|
39 | });
|
40 | }
|