1 | #!/usr/bin/env node
|
2 |
|
3 | const download = require("../lib/download-git-repo");
|
4 | const program = require("commander");
|
5 | const exists = require("fs").existsSync;
|
6 | const os = require("os");
|
7 | const path = require("path");
|
8 | const rm = require("rimraf").sync;
|
9 | const uid = require("uid");
|
10 | const ora = require("ora");
|
11 | const inquirer = require("inquirer");
|
12 | const logger = require("../lib/logger");
|
13 | const generate = require("../lib/generate");
|
14 | const checkVersion = require("../lib/check-version");
|
15 |
|
16 | var template = "zhuanti";
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | program
|
23 | .usage("<template-name> [project-name]")
|
24 | .option("-c, --clone", "use git clone");
|
25 | program.parse(process.argv);
|
26 |
|
27 | inquirer.prompt([{
|
28 | type: "list",
|
29 | message: "请选择要创建的项目类型",
|
30 | name: "tplType",
|
31 | choices: [{
|
32 | value: "0",
|
33 | name: "zhuanti : 一键创建专题模板",
|
34 | short: "zhuanti",
|
35 | },
|
36 | {
|
37 | value: "1",
|
38 | name: "zhuanti-es : 一键创建es6专题模板",
|
39 | short: "zhuanti-es",
|
40 | },
|
41 | {
|
42 | value: "4",
|
43 | name: "zhuanti-es6-multi-page : 一键创建es6多页面专题模板",
|
44 | short: "zhuanti-es6-multi-page",
|
45 | },
|
46 | {
|
47 | value: "2",
|
48 | name: "react-hybrid : 基于react的纯混合app项目",
|
49 | short: "react-hybrid",
|
50 | },
|
51 | {
|
52 | value: "3",
|
53 | name: "react-2in1 : 基于react的二合一项目",
|
54 | short: "react-2in1",
|
55 | },
|
56 | ],
|
57 | }]).then((answers) => {
|
58 | switch (answers.tplType) {
|
59 | case "1":
|
60 | template = "zhuanti-es";
|
61 | createZhuanti();
|
62 | break;
|
63 | case "4":
|
64 | template = "zhuanti-es6-multi-page";
|
65 | createZhuanti();
|
66 | break;
|
67 | case "2":
|
68 | case "3":
|
69 | console.log("模板正在完善中...");
|
70 | break;
|
71 | case "0":
|
72 | default:
|
73 | template = "zhuanti";
|
74 | createZhuanti();
|
75 | break;
|
76 | }
|
77 | }).catch((err) => {
|
78 | console.log(err);
|
79 | });
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | const hasSlash = template.indexOf("/") > -1;
|
88 |
|
89 | var rawName = "";
|
90 | let inPlace = false;
|
91 | let name = "";
|
92 | let to = "";
|
93 | let clone = false;
|
94 |
|
95 | function createZhuanti() {
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | inquirer.prompt([{
|
108 | type: "input",
|
109 | message: "请输入要创建的专题文件夹名称",
|
110 | name: "inputMsg"
|
111 | }]).then((answers) => {
|
112 | if (answers.inputMsg) {
|
113 | rawName = answers.inputMsg;
|
114 | inPlace = !rawName || rawName === ".";
|
115 | name = inPlace ? path.relative("../", process.cwd()) : rawName;
|
116 | to = path.resolve(rawName || ".");
|
117 | clone = program.clone || false;
|
118 |
|
119 | if (exists(to)) {
|
120 | inquirer.prompt([{
|
121 | type: "confirm",
|
122 | message: inPlace ?
|
123 | "在当前目录创建项目?" : "目标目录已存在,是否继续?",
|
124 | name: "ok",
|
125 | }]).then((answers) => {
|
126 | if (answers.ok) {
|
127 | run();
|
128 | }
|
129 | });
|
130 | } else {
|
131 | to = path.resolve(rawName || ".");
|
132 | run();
|
133 | }
|
134 | }
|
135 | });
|
136 |
|
137 | }
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function run() {
|
145 |
|
146 |
|
147 |
|
148 | if (/^[./]|(\w:)/.test(template)) {
|
149 | const templatePath = template.charAt(0) === "/" || /^\w:/.test(template) ?
|
150 | template :
|
151 | path.normalize(path.join(process.cwd(), template));
|
152 | if (exists(templatePath)) {
|
153 | generate(name, templatePath, to, (err) => {
|
154 | if (err) logger.fatal(err);
|
155 | console.log();
|
156 | logger.success("Generated \"%s\".", name);
|
157 | });
|
158 | } else {
|
159 | logger.fatal("Local template \"%s\" not found.", template);
|
160 | }
|
161 | } else {
|
162 | checkVersion(() => {
|
163 | if (!hasSlash) {
|
164 |
|
165 |
|
166 |
|
167 | const repo = "cbd-template/";
|
168 | const officialTemplate = repo + template;
|
169 | if (template.indexOf("#") !== -1) {
|
170 | downloadAndGenerate(officialTemplate);
|
171 | } else {
|
172 |
|
173 | downloadAndGenerate(officialTemplate);
|
174 | }
|
175 | } else {
|
176 | downloadAndGenerate(template);
|
177 | }
|
178 | });
|
179 | }
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | function downloadAndGenerate(template) {
|
189 | const tmp = `${os.tmpdir()}/cbd-template-${uid()}`;
|
190 | const spinner = ora("downloading template");
|
191 | spinner.start();
|
192 | download(template, tmp, {
|
193 | clone
|
194 | }, (err) => {
|
195 | spinner.stop();
|
196 | process.on("exit", () => {
|
197 | rm(tmp);
|
198 | });
|
199 | if (err) logger.fatal(`Failed to download repo ${template}: ${err.message.trim()}`);
|
200 |
|
201 |
|
202 | generate(name, tmp, to, (err) => {
|
203 | if (err) logger.fatal(err);
|
204 | console.log();
|
205 | logger.success("Generated \"%s\".", name);
|
206 | });
|
207 | });
|
208 | } |
\ | No newline at end of file |