1 | let nodepath = require("path");
|
2 | let config = require("map-path")("Services/config.js");
|
3 | let cwd = process.cwd();
|
4 | let fs = require("fs-extra");
|
5 | let utils = require("map-path")("Utils");
|
6 | let inquirer = require("inquirer");
|
7 | let configUrl = config.configUrl;
|
8 |
|
9 | let readConfig = function() {
|
10 | return fs.readJson(configUrl);
|
11 | };
|
12 |
|
13 | let readCmds = function() {
|
14 | let { store } = this;
|
15 | let configUrl = store.config.path;
|
16 | let cmdUrl = nodepath.join(configUrl, "cmds");
|
17 |
|
18 | return new Promise((resolve, reject) => {
|
19 | fs.readdir(cmdUrl, (err, files) => {
|
20 | if (err) {
|
21 | reject(err);
|
22 | return;
|
23 | }
|
24 | resolve(files);
|
25 | });
|
26 | });
|
27 | };
|
28 |
|
29 | let exportCmds = function() {
|
30 | let { store } = this;
|
31 | let configUrl = store.config.path;
|
32 | let { data: files } = this;
|
33 |
|
34 | return new Promise((resolve, reject) => {
|
35 | let out = [];
|
36 | if (Array.isArray(files)) {
|
37 | files.forEach((name, ind) => {
|
38 | let cmdUrl = nodepath.join(configUrl, "cmds", name);
|
39 | out.push({
|
40 | callback: function() {
|
41 | return new Promise((resolve, reject) => {
|
42 | fs.stat(cmdUrl, (err, stats) => {
|
43 | if (err) {
|
44 | reject(err);
|
45 | return;
|
46 | }
|
47 | if (!stats.isFile()) {
|
48 | reject(`${cmdUrl} is not file format`);
|
49 | return;
|
50 | }
|
51 |
|
52 | try {
|
53 | let config = require(cmdUrl);
|
54 | resolve(config);
|
55 | } catch (e) {
|
56 | reject(e);
|
57 | return;
|
58 | }
|
59 | });
|
60 | });
|
61 | }
|
62 | });
|
63 | });
|
64 | }
|
65 | if (out.length === 0) {
|
66 | resolve(out);
|
67 | return;
|
68 | }
|
69 | utils
|
70 | .branch(out)
|
71 | .then(({ store }) => {
|
72 | let out = [];
|
73 | for (let i in store) {
|
74 | out.push(store[i].name);
|
75 | }
|
76 | resolve(out);
|
77 | })
|
78 | .catch(err => {
|
79 | utils.consoleNo(err);
|
80 | reject();
|
81 | });
|
82 | });
|
83 | };
|
84 |
|
85 | let validCommand = function(commandName, url) {
|
86 | let { store, data } = this;
|
87 | let configUrl = store.config.path;
|
88 |
|
89 | return new Promise((resolve, reject) => {
|
90 | if (data.indexOf(commandName) !== -1) {
|
91 | inquirer
|
92 | .createPromptModule()([
|
93 | {
|
94 | name: "confirm",
|
95 | type: "input",
|
96 | message: `${commandName} is exit , cover it?`
|
97 | }
|
98 | ])
|
99 | .then(ans => {
|
100 | let conf = ans.confirm;
|
101 | if (conf === "yes") {
|
102 | resolve();
|
103 | } else {
|
104 | reject();
|
105 | }
|
106 | });
|
107 | } else {
|
108 | resolve();
|
109 | }
|
110 | });
|
111 | };
|
112 |
|
113 | let validContent = function(commandName, url) {
|
114 | return new Promise((resolve, reject) => {
|
115 | fs.stat(url, (err, stats) => {
|
116 | if (err) {
|
117 | reject(err);
|
118 | return;
|
119 | }
|
120 | if (!stats.isFile()) {
|
121 | reject(`task ${url} must file`);
|
122 | return;
|
123 | }
|
124 | resolve();
|
125 | });
|
126 | });
|
127 | };
|
128 |
|
129 | let createCmd = function(commandName, optionArr = []) {
|
130 | let { store } = this;
|
131 | let configUrl = store.config.path;
|
132 |
|
133 | let newConfigUrl = configUrl.replace(/\\/gm, () => {
|
134 | return "\\" + "\\";
|
135 | });
|
136 |
|
137 | let cmd = `
|
138 | let nodepath=require('path');
|
139 | module.exports={
|
140 | options:${JSON.stringify(optionArr)},
|
141 | name:"${commandName}",
|
142 | action:function(arg,options){
|
143 | let url=nodepath.join('${newConfigUrl}','tasks',"${commandName}","index.js");
|
144 | return new Promise((resolve,reject)=>{
|
145 |
|
146 | try{
|
147 | require(url)(arg,options)
|
148 | .then((data)=>{
|
149 | resolve(data);
|
150 | })
|
151 |
|
152 | }
|
153 | catch(e){
|
154 | reject(e);
|
155 | }
|
156 |
|
157 | })
|
158 | }
|
159 | };
|
160 | `;
|
161 |
|
162 | let url = nodepath.join(configUrl, "cmds", commandName) + ".js";
|
163 | let { data: names } = this;
|
164 | return new Promise((resolve, reject) => {
|
165 | fs.writeFile(url, cmd, err => {
|
166 | if (err) {
|
167 | reject(err);
|
168 | return;
|
169 | }
|
170 | resolve();
|
171 | });
|
172 | });
|
173 | };
|
174 |
|
175 | let createTask = function(commandName, url) {
|
176 | let { store } = this;
|
177 | let configUrl = store.config.path;
|
178 |
|
179 |
|
180 | return new Promise((resolve, reject) => {
|
181 | let copyUrl = nodepath.join(configUrl, "tasks", commandName, "index.js");
|
182 | fs.copy(url, copyUrl)
|
183 | .then(() => {
|
184 | resolve();
|
185 | })
|
186 | .catch(err => {
|
187 | reject(err);
|
188 | });
|
189 | });
|
190 | };
|
191 |
|
192 | function sub(arg, allOption) {
|
193 | let { file, option } = allOption;
|
194 | let commandName = arg;
|
195 |
|
196 | return new Promise((resolve, reject) => {
|
197 | if (!commandName) {
|
198 | reject(`must sub a command name`);
|
199 | return;
|
200 | }
|
201 | let arr = [];
|
202 |
|
203 | if (typeof option === "string") {
|
204 | arr = option.split("+");
|
205 | }
|
206 |
|
207 | utils
|
208 | .branch([
|
209 | { callback: readConfig, name: "config" },
|
210 | { callback: readCmds },
|
211 | { callback: exportCmds },
|
212 | { callback: validCommand, props: [commandName, arr] },
|
213 | { callback: validContent, props: [commandName, file] },
|
214 | { callback: createCmd, props: [commandName, arr] },
|
215 | { callback: createTask, props: [commandName, file] }
|
216 | ])
|
217 | .then(data => {
|
218 | resolve(`${commandName} is configed`);
|
219 | })
|
220 | .catch(err => {
|
221 | if (err) {
|
222 | utils.consoleNo(err);
|
223 | }
|
224 | });
|
225 | });
|
226 | }
|
227 |
|
228 | module.exports = sub;
|