1 | 'use strict';
|
2 |
|
3 | const inquirer = require('inquirer'),
|
4 | userHome = require('user-home'),
|
5 | _ = require('lodash'),
|
6 | T = require('../lib/tools'),
|
7 | LoadingORA = require('../lib/loadProgress').LoadingORA,
|
8 | templates = require('../templates');
|
9 |
|
10 | const prompt = inquirer.createPromptModule();
|
11 |
|
12 | T.isInSourcePath() && T.setCWD(T.Path.resolve(process.cwd(), '../'));
|
13 |
|
14 | function deleteProject() {
|
15 | let message = T.msg.red('提醒:') + T.msg.yellow('此操作会将项目目录从本地磁盘中删除,您确定要删除吗{ 谨慎执行(u_u) } ');
|
16 |
|
17 | let name = T.getProjectName();
|
18 | let path = T.Path.resolve(process.cwd(), `${name}`);
|
19 |
|
20 | if (!T.fs.existsSync(path)) {
|
21 | T.log.red('× 项目不存在');
|
22 | return false;
|
23 | }
|
24 |
|
25 | prompt([{
|
26 | type: 'confirm',
|
27 | name: 'ok',
|
28 | message: message
|
29 | }]).then(awn => awn.ok && _delete(path));
|
30 | }
|
31 |
|
32 | function _delete(path) {
|
33 | const removePrg = new LoadingORA();
|
34 | removePrg.start('正在删除项目,请稍后......');
|
35 | T.fsa.remove(path, err => {
|
36 | if (err) T.log.red(`× ${err}`);
|
37 | else removePrg.stop('√ 项目删除成功!');
|
38 | });
|
39 | }
|
40 |
|
41 | function addTemplate() {
|
42 | let [action, name, url, vs] = T.argv._;
|
43 | vs && (vs = '^' + String(vs).replace(/^\^/, ''));
|
44 | name = String(name);
|
45 | if (!name || !url) {
|
46 | T.log.red(`× 必须指定模板名称<templateName>和模板地址<templateGitUrl(username/repo)>
|
47 | \n 如:gupack addTemplate vue_browserify RodeyManager/vue_browserify`);
|
48 | return false;
|
49 | }
|
50 | templates[name] = {
|
51 | url,
|
52 | vs
|
53 | };
|
54 |
|
55 | let template = templates[name];
|
56 | require('./download')(template)
|
57 | .then(result => {
|
58 | T.log.end(T.msg.green(result.message + ' \n√ Path: ' + result.tempPath));
|
59 | templateWT();
|
60 | })
|
61 | .catch(err => T.log.red(err.message));
|
62 | }
|
63 |
|
64 | function removeTemplate() {
|
65 | let [action, name] = T.argv._;
|
66 |
|
67 | if (!name) {
|
68 | T.log.red(`× 必须指定需要删除模板名称<templateName>
|
69 | \n 如:gupack removeTemplate vue_browserify`);
|
70 | return false;
|
71 | }
|
72 | if (!(name in templates)) {
|
73 | T.log.red(`× 未找到当前指定模板,当前已存在的模板:${Object.keys(templates).join('、')}`);
|
74 | return false;
|
75 | }
|
76 |
|
77 | prompt([{
|
78 | type: 'confirm',
|
79 | name: 'ok',
|
80 | message: `您确定要删除该(${name})模板吗?`
|
81 | }]).then(awn => awn.ok && _removeTemplate(name));
|
82 |
|
83 | return false;
|
84 | }
|
85 |
|
86 | function _removeTemplate(name) {
|
87 | const loading = new LoadingORA();
|
88 | loading.start(T.msg.yellow('start remove ......'));
|
89 | T.fsa.removeSync(T.Path.resolve(userHome, '.gupack-templates', templates[name]['url']));
|
90 | delete templates[name];
|
91 | loading.stop();
|
92 | loading.stop(T.msg.green(`√ 你也可以使用 gupack new myapp --template ${name} --skip-cache 来创建项目并下载最新的模板了(╯﹏╰)!`));
|
93 | }
|
94 |
|
95 | function listTemplate() {
|
96 | let tks = Object.keys(templates);
|
97 | let sl = tks.map(tk => tk.length);
|
98 | let maxLen = _.max(sl);
|
99 | if (tks.length > 0) {
|
100 | let s = '',
|
101 | template;
|
102 | tks.forEach((k, i) => {
|
103 | template = templates[k];
|
104 | s += `${T.msg.green(T.fillAlign(tks[i], maxLen))}: ${T.msg.gray('https://github.com/' + template.url)}, ${template.vs || ''} \n`;
|
105 | });
|
106 | T.log(s);
|
107 | } else {
|
108 | T.log.red(' 暂无模板,您可以添加模板, 如: gupack addTemplate GitHub_username/repo ');
|
109 | }
|
110 | }
|
111 |
|
112 |
|
113 | function templateWT() {
|
114 | T.fsa.writeJSONSync(T.Path.resolve(__dirname, '../templates.json'), templates, 'utf8');
|
115 | T.log.green(`√ 模板添加成功! 你可以使用 gupack new myapp --template ${name} 来创建项目了!`);
|
116 | T.log.green(`! 你可以使用 gupack new myapp --template ${name} --skip-cache 来创建项目并下载最新的模板了(╯﹏╰)!`);
|
117 | }
|
118 |
|
119 | module.exports = {
|
120 | remove: deleteProject,
|
121 | addTemplate,
|
122 | removeTemplate,
|
123 | listTemplate
|
124 | }; |
\ | No newline at end of file |