UNPKG

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