-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
120 lines (120 loc) · 3.53 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env node
import inquirer from "inquirer";
import download from "download-git-repo";
import handlebars from "handlebars";
import chalk from "chalk";
import ora from "ora";
import fs from "fs";
import path from "path";
const spinnerDownloadTemplate = ora("正在下载模板, 请稍后...");
inquirer
.prompt([
{
name: "description",
message: "请输入主题描述",
default: "",
},
{
name: "name",
message: "请输入主题名称",
default: "theme-name",
},
{
name: "author",
message: "请输入主题作者",
validate: (i) => {
if (i != "") {
return true;
} else {
return false;
}
},
},
{
name: "repoURL",
message: "请输入主题仓库URL",
validate: (i) => {
if (i == "") {
return false;
}
try {
// tslint:disable-next-line: no-unused-expression
new URL(i);
} catch (err) {
return "\nURL无效\n";
}
let res = "\n";
if (!i.startsWith("https://github.com/")) res += "https://github.com/ 开头是必要的\n";
if (i.endsWith(".git")) res += "不能是以 .git 结尾\n";
if (res != "\n") {
return res;
} else {
return true;
}
},
},
{
name: "template",
type: "rawlist",
message: "请选择主题模板",
choices: ["simple", "full"],
default: "simple",
},
])
.then((answers) => {
// console.log(answers)
spinnerDownloadTemplate.start();
const downloadPath = path.join(process.cwd(), "out", answers.name);
download(
`bitbucket:hi-windom/sili-t-${answers.template}#main`,
downloadPath,
{ clone: false },
function (err) {
if (err) {
spinnerDownloadTemplate.fail(err.toString());
console.log(chalk.red(err));
}
{
spinnerDownloadTemplate.succeed("Success");
const packagePath = path.join(downloadPath, "package.json");
// 判断是否有package.json, 要把输入的数据回填到模板中
if (fs.existsSync(packagePath)) {
const content = fs.readFileSync(packagePath).toString();
// handlebars 模板处理引擎
const template = handlebars.compile(content);
const param = {
name: answers.name,
description: answers.description,
author: answers.author,
repoURL: answers.repoURL,
};
const result = template(param);
fs.writeFileSync(packagePath, result);
console.log(chalk.green("package.json初始化成功!"));
}
const themeJsonPath = path.join(downloadPath, "theme.json");
if (fs.existsSync(themeJsonPath)) {
const content = fs.readFileSync(themeJsonPath).toString();
// handlebars 模板处理引擎
const template = handlebars.compile(content);
const param = {
name: answers.name,
author: answers.author,
repoURL: answers.repoURL,
};
const result = template(param);
fs.writeFileSync(themeJsonPath, result);
console.log(chalk.green("theme.json初始化成功!"));
}
}
}
);
})
.catch((error) => {
console.error(error);
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else went wrong
}
});