UNPKG

5.01 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const download = require("../lib/download-git-repo");
4const program = require("commander");
5const exists = require("fs").existsSync;
6const os = require("os");
7const path = require("path");
8const rm = require("rimraf").sync;
9const uid = require("uid");
10const ora = require("ora");
11const inquirer = require("inquirer");
12const logger = require("../lib/logger");
13const generate = require("../lib/generate");
14const checkVersion = require("../lib/check-version");
15
16var template = "zhuanti";
17
18/**
19 * Usage.
20 */
21
22program
23 .usage("<template-name> [project-name]")
24 .option("-c, --clone", "use git clone");
25program.parse(process.argv);
26
27inquirer.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 * Settings.
83 */
84
85
86
87const hasSlash = template.indexOf("/") > -1;
88
89var rawName = "";
90let inPlace = false;
91let name = "";
92let to = "";
93let clone = false;
94
95function createZhuanti() {
96 // const myDate = new Date();
97 // const yy = myDate.getFullYear();
98 // const month = myDate.getMonth() + 1;
99 // const mm = month >= 10 ? month : `0${month}`;
100 // const date = myDate.getDate();
101 // const dd = date >= 10 ? date : `0${date}`;
102
103 // var template = program.args[0]
104 // var hasSlash = template.indexOf('/') > -1
105 // var rawName = program.args[1] + '_'+yy+mm+dd;
106
107 inquirer.prompt([{
108 type: "input",
109 message: "请输入要创建的专题文件夹名称",
110 name: "inputMsg"
111 }]).then((answers) => {
112 if (answers.inputMsg) {
113 rawName = answers.inputMsg; // + '_'+yy+mm+dd;
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 * Check, download and generate the project.
142 */
143
144function run() {
145
146
147 // check if template is local
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 // use official templates
165
166 // const repo = true ? "cbd-template/" : "skdream/";
167 const repo = "cbd-template/";
168 const officialTemplate = repo + template; // 'cbd-templates/' + template
169 if (template.indexOf("#") !== -1) {
170 downloadAndGenerate(officialTemplate);
171 } else {
172 // warnings.v2BranchIsNowDefault(template, inPlace ? '' : name)
173 downloadAndGenerate(officialTemplate);
174 }
175 } else {
176 downloadAndGenerate(template);
177 }
178 });
179 }
180}
181
182/**
183 * Download a generate from a template repo.
184 *
185 * @param {String} template
186 */
187
188function 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 // console.log(3434)
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