1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const program = require('commander');
|
11 | const path = require('path');
|
12 | const util = require('./util');
|
13 | const memFs = require('./util/file');
|
14 | const inquirer = require('inquirer');
|
15 | const EventEmitter = require('events');
|
16 | const readPkgUp = require('read-pkg-up');
|
17 | const findUp = require('find-up');
|
18 | const makeDir = require('make-dir');
|
19 | const fs = require('fs');
|
20 | const _ = require('lodash');
|
21 | const chalk = require('chalk');
|
22 | const log = require('./util/log');
|
23 | const config = require('../bin/config');
|
24 | const debug = require('debug')('dingtalk-cli-pro:core');
|
25 |
|
26 |
|
27 | const SYSTEM_PLUGIN_LIST = config.SYSTEM_PLUGIN_LIST
|
28 |
|
29 | const SYS_COMMAND_LIST = [
|
30 | {
|
31 | name: 'init',
|
32 | path: path.resolve(__dirname, './commands', './init')
|
33 | },
|
34 | {
|
35 | name: 'dev',
|
36 | path: path.resolve(__dirname, './commands', './dev')
|
37 | },
|
38 | {
|
39 | name: 'must',
|
40 | path: path.resolve(__dirname, './commands', './must')
|
41 | },
|
42 | {
|
43 | name: 'test',
|
44 | path: path.resolve(__dirname, './commands', './test')
|
45 | },
|
46 | {
|
47 | name: 'publish',
|
48 | path: path.resolve(__dirname, './commands', './publish')
|
49 | },
|
50 | {
|
51 | name: 'build',
|
52 | path: path.resolve(__dirname, './commands', './build')
|
53 | },
|
54 | {
|
55 | name: 'generator',
|
56 | path: path.resolve(__dirname, './commands', './generator')
|
57 | }
|
58 | ]
|
59 |
|
60 | const COMMAND_MAP = new Map();
|
61 |
|
62 | class Client extends EventEmitter {
|
63 | constructor(options) {
|
64 | super(options);
|
65 | this.fs = memFs;
|
66 | this.program = program;
|
67 | this.log = log;
|
68 | this.context = {
|
69 | plugins: []
|
70 | }
|
71 | }
|
72 |
|
73 | prompt(question) {
|
74 | return inquirer.prompt(question)
|
75 | }
|
76 |
|
77 | registryCommand(cmdString) {
|
78 | if (COMMAND_MAP.has(cmdString)) {
|
79 | this.log.error(`${cmdString} command aleady has be registried, please rename it`)
|
80 | return null;
|
81 | } else {
|
82 | COMMAND_MAP.set(cmdString, 1);
|
83 | return this.program.command(cmdString)
|
84 | }
|
85 | }
|
86 |
|
87 | hasCommand(cmdString) {
|
88 | return COMMAND_MAP.has(cmdString);
|
89 | }
|
90 |
|
91 | executeSystemPlugin() {
|
92 | this.context.plugins = this.context.plugins.concat(SYS_COMMAND_LIST);
|
93 | this.executePlugin(SYS_COMMAND_LIST);
|
94 | }
|
95 |
|
96 | loaderInstalledPlugin(root) {
|
97 | const plugins = util.getPluginNameAndPath(root) || [];
|
98 | const context = this.context;
|
99 |
|
100 | const reducePlugins = plugins.filter(plugin => {
|
101 | if (context.plugins.map(item => item.name).indexOf(plugin.name) === -1) {
|
102 | return true;
|
103 | } else {
|
104 | if (SYSTEM_PLUGIN_LIST.indexOf(plugin.name) === -1) {
|
105 | this.log.error(`${plugin.name} is registried, please rename it`)
|
106 | }
|
107 |
|
108 | return false
|
109 | }
|
110 | });
|
111 |
|
112 | if (reducePlugins.length) {
|
113 | context.plugins = context.plugins.concat(reducePlugins);
|
114 | this.executePlugin(reducePlugins);
|
115 | }
|
116 | }
|
117 |
|
118 | loaderConfigPluginDir(dir) {
|
119 | fs.readdirSync(dir).forEach(file => {
|
120 | if (/\.js?/.test(file)) {
|
121 | require(path.join(dir, file)).call(this, this);
|
122 | }
|
123 | })
|
124 | }
|
125 | loaderConfigPluginFiles(list, dir) {
|
126 | debug(`configpluinfiles: %o, ${dir}`, list);
|
127 | list.forEach(item => {
|
128 | const file = item[0];
|
129 | const options = item[1];
|
130 | if (/\.js?/.test(file)) {
|
131 | if (path.isAbsolute(file)) {
|
132 | require(file).call(this, this, options)
|
133 | } else {
|
134 | require(path.join(dir, file)).call(this, this, options);
|
135 | }
|
136 | }
|
137 | })
|
138 | }
|
139 | executePlugin(plugins){
|
140 | plugins.forEach(plugin => {
|
141 | require(plugin.path).call(this, this)
|
142 | })
|
143 | }
|
144 |
|
145 | pluginIsExist(name) {
|
146 | const context = this.context;
|
147 | return context.plugins.filter(plugin => plugin.name === name).length ? true : false;
|
148 | }
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 | rootGeneratorName() {
|
155 |
|
156 | const pkg = readPkgUp.sync().package;
|
157 | return pkg ? pkg.name : '*';
|
158 | }
|
159 |
|
160 | readPackageInfo() {
|
161 | return readPkgUp.sync();
|
162 | }
|
163 |
|
164 | |
165 |
|
166 |
|
167 |
|
168 | rootGeneratorVersion() {
|
169 | const pkg = readPkgUp.sync().package;
|
170 | return pkg ? pkg.version : '0.0.0';
|
171 | }
|
172 |
|
173 | |
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | destinationRoot(rootPath) {
|
181 | let _destinationRoot = null
|
182 | if (typeof rootPath === 'string') {
|
183 | _destinationRoot = path.resolve(rootPath);
|
184 |
|
185 | if (!fs.existsSync(rootPath)) {
|
186 | makeDir.sync(rootPath);
|
187 | }
|
188 |
|
189 | process.chdir(rootPath);
|
190 | } else {
|
191 | _destinationRoot = path.resolve(rootPath);
|
192 | }
|
193 |
|
194 |
|
195 | return _destinationRoot;
|
196 | }
|
197 | |
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | sourceRoot(rootPath) {
|
204 | let _sourceRoot = null;
|
205 | if (typeof rootPath === 'string') {
|
206 | _sourceRoot = path.resolve(rootPath);
|
207 | } else {
|
208 | _sourceRoot = path.resolve('./templates');
|
209 | }
|
210 |
|
211 | return _sourceRoot;
|
212 | }
|
213 |
|
214 | |
215 |
|
216 |
|
217 |
|
218 |
|
219 | templatePath(rootPath, ...args) {
|
220 | let filepath = path.join.apply(path, args);
|
221 |
|
222 | if (!path.isAbsolute(filepath)) {
|
223 | filepath = path.join(rootPath, filepath);
|
224 | }
|
225 |
|
226 | return filepath;
|
227 | }
|
228 |
|
229 | |
230 |
|
231 |
|
232 |
|
233 |
|
234 | destinationPath(rootPath, ...args) {
|
235 | let filepath = path.join.apply(path, args);
|
236 |
|
237 | if (!path.isAbsolute(filepath)) {
|
238 | filepath = path.join(rootPath, filepath);
|
239 | }
|
240 |
|
241 | return filepath;
|
242 | }
|
243 |
|
244 | async createGenerator(options) {
|
245 | let config = {};
|
246 |
|
247 | const _config = async () => {
|
248 | const sourcePath = this.sourceRoot(config.origin);
|
249 | const targetPath = this.destinationRoot(config.target);
|
250 | this.templatePath = this.templatePath.bind(this, sourcePath);
|
251 | this.destinationPath = this.destinationPath.bind(this, targetPath);
|
252 | }
|
253 |
|
254 | const { configuring = async () => {}, writing, install = async () => {}, end = async () => {}} = options
|
255 | try {
|
256 | config = await configuring();
|
257 |
|
258 | await _config();
|
259 | await writing.call(this);
|
260 | await install.call(this);
|
261 | await end.call(this);
|
262 | } catch(err) {
|
263 | throw err;
|
264 | }
|
265 |
|
266 |
|
267 |
|
268 | }
|
269 | }
|
270 |
|
271 | _.extend(Client.prototype, util);
|
272 | _.extend(Client.prototype, require('./util/user'));
|
273 | _.extend(Client.prototype, require('./util/install'));
|
274 | _.extend(Client.prototype, require('./util/spawn-command'));
|
275 | _.extend(Client.prototype, require('./util/emoji'));
|
276 |
|
277 |
|
278 |
|
279 | module.exports = new Client({}); |
\ | No newline at end of file |