-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·414 lines (315 loc) · 9.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"use strict";
const os = require('os'),
path = require('path'),
fs = require('fs-extra'),
chalk = require('chalk'),
_ = require('lodash'),
spawn = require('cross-spawn'),
globalModules = require('global-modules');
function pluginUtils(pluginName) {
this.pluginPrefix = "steamer-plugin-";
this.pluginName = pluginName || "";
// plugin config object
this.config = null;
this.isWindows = (os.type() === "Windows_NT");
// global home directory, usually for global config
this.globalHome = this._getGlobalHome();
this.globalNodeModules = this._getNodePath() || '';
}
/**
* get NODE_PATH
* @return {String} [NODE_PATH]
*/
pluginUtils.prototype._getNodePath = function() {
var env = process.env;
if (env.NODE_PATH) {
return env.NODE_PATH;
}
if (fs.existsSync(globalModules)) {
return globalModules;
}
// No Longer Support Compatible Mode
return null;
// global node module path
// var ps = spawn.sync('npm', ['root', '-g'], {}),
// npmRoot = (ps && ps.stdout) ? ps.stdout.toString().replace(/[\n\t\r]/g,"") : "";
// return npmRoot;
};
/**
* get home directory
* @return {String} [os home directory]
*/
pluginUtils.prototype._getGlobalHome = function() {
return os.homedir() || process.cwd();
};
/**
* add nodejs require path
* @param {String} requirePath [new require path]
* @param {String} targetPath [target require path]
*/
pluginUtils.prototype.addRequirePath = function(requirePath, targetPath) {
var targetPath = targetPath || require.main.paths;
if (!_.includes(targetPath, requirePath)) {
targetPath.push(requirePath);
}
};
/**
* Create config file
* @param {Object} config [config object]
* @param {Object} option [options]
*/
pluginUtils.prototype.createConfig = function(config, option) {
var config = config || "",
option = option || {};
// config file path: [folder]./steamer/[filename].[extension]
var folder = (option.isGlobal) ? this.globalHome : (option.folder || process.cwd()),
filename = option.filename || this.pluginName,
extension = option.extension || "js",
overwrite = option.overwrite || false; // overwrite the config file or not
var configFile = path.resolve(path.join(folder, ".steamer/" + filename + "." + extension));
if (!overwrite && fs.existsSync(configFile)) {
throw new Error(configFile + " exists");
}
this._writeFile(configFile, filename, config);
this.config = null;
};
/**
* read config file, local config extends global config
* @param {Object} config [config object]
* @param {Object} option [options]
*/
pluginUtils.prototype.readConfig = function(option) {
var option = option || {};
var folder = option.folder || process.cwd(),
filename = option.filename || this.pluginName,
extension = option.extension || "js";
var globalConfigFile = path.resolve(path.join(this.globalHome, ".steamer/" + filename + "." + extension)),
localConfigFile = path.resolve(path.join(folder, ".steamer/" + filename + "." + extension));
var globalConfig = this._readFile(globalConfigFile),
localConfig = this._readFile(localConfigFile);
this.config = _.merge({}, globalConfig, localConfig);
return this.config;
};
/**
* read steamerjs config, local config extensd global config
* @return {Object} [steamer config]
*/
pluginUtils.prototype.readSteamerConfig = function() {
var globalConfig = this.readSteamerLocalConfig(),
localConfig = this.readSteamerGlobalConfig();
var config = _.merge({}, globalConfig, localConfig);
return config;
};
/**
* read steamerjs local config
* @return {Object} [steamer local config]
*/
pluginUtils.prototype.readSteamerLocalConfig = function() {
var localConfigFile = path.join(process.cwd(), ".steamer/steamer.js");
var localConfig = this._readFile(localConfigFile);
return localConfig;
};
/**
* read steamerjs global config
* @return {Object} [steamer global config]
*/
pluginUtils.prototype.readSteamerGlobalConfig = function() {
var globalConfigFile = path.join(this.globalHome, ".steamer/steamer.js");
var globalConfig = this._readFile(globalConfigFile);
return globalConfig;
};
/**
* create steamerjs config
*/
pluginUtils.prototype.createSteamerConfig = function(config, options) {
var config = config || {},
options = options || {};
var folder = (options.isGlobal) ? this.globalHome : process.cwd(),
overwrite = options.overwrite || false;
var configFile = path.join(folder, ".steamer/steamer.js");
try {
if (!overwrite && fs.existsSync(configFile)) {
throw new Error(configFile + " exists");
}
this._writeFile(configFile, "steamerjs", config);
}
catch(e) {
this.error(e.stack);
throw e;
}
};
/**
* read config file
* @param {String} filepath [file path]
* @return {Object} [config object]
*/
pluginUtils.prototype._readFile = function(filepath) {
var extension = path.extname(filepath);
var isJs = extension === ".js",
config = {};
try {
if (isJs) {
if (require.cache[filepath]) {
delete require.cache[filepath];
}
config = require(filepath) || {};
}
else {
config = JSON.parse(fs.readFileSync(filepath, "utf-8")) || {};
}
config = config.config;
}
catch(e) {
return config;
}
return config;
};
/**
* write config file
* @param {String} filepath [config file path]
* @param {Object|String} [config content]
*/
pluginUtils.prototype._writeFile = function(filepath, plugin, config) {
var extension = path.extname(filepath);
var isJs = extension === ".js",
newConfig = {
plugin: plugin,
config: config,
},
contentPrefix = (isJs) ? "module.exports = " : "",
contentPostfix = (isJs) ? ";" : "",
content = contentPrefix + JSON.stringify(newConfig, null, 4) + contentPostfix;
try {
fs.ensureFileSync(filepath);
fs.writeFileSync(filepath, content, 'utf-8');
}
catch(e) {
throw e;
}
};
/**
* print error message
* @param {String} str [message]
* @return {String} [msg]
*/
pluginUtils.prototype.error = function(str) {
this.log(str, 'red');
};
/**
* print information
* @param {String} str [message]
* @return {String} [msg]
*/
pluginUtils.prototype.info = function(str) {
this.log(str, 'cyan');
};
/**
* print warning message
* @param {String} str [message]
* @return {String} [msg]
*/
pluginUtils.prototype.warn = function(str) {
this.log(str, 'yellow');
};
/**
* print success message
* @param {String} str [message]
* @return {String} [msg]
*/
pluginUtils.prototype.success = function(str) {
this.log(str, 'green');
};
/**
* print title message
* @param {String} color [color name]
* @return {String} [msg with color]
*/
pluginUtils.prototype.printTitle = function(str, color) {
var msg = "",
color = color || 'white',
str = " " + str + " ",
len = str.length,
maxLen = process.stdout.columns || 84;
var padding = "=".repeat(Math.floor((maxLen - len) / 2));
msg += padding + str + padding;
return this.log(msg, color);
};
/**
* print end message
* @param {String} color [color name]
* @return {String} [msg with color]
*/
pluginUtils.prototype.printEnd = function(color) {
var msg = "",
color = color || 'white',
maxLen = process.stdout.columns || 84;
msg += "=".repeat(maxLen);
return this.log(msg, color);
};
/**
* print command usage
* @param {String} description [description of the command]
* @param {String} cmd [command name]
* @return {String} [message]
*/
pluginUtils.prototype.printUsage = function(description, cmd) {
var msg = chalk.green("\nusage: \n"),
cmd = cmd || this.pluginName.replace(this.pluginPrefix, "");
msg += "steamer " + cmd + " " + description + "\n";
console.log(msg);
};
/**
* print command option
* @param {Array} options [array of options]
- option {String} full option
- alias {String} option alias
- value {String} option alias
- description {String} option description
* @return {String} [message]
*/
pluginUtils.prototype.printOption = function(options) {
var options = options || [];
var maxColumns = process.stdout.columns || 84,
maxOptionLength = 0;
var msg = chalk.green("options: \n");
options.map((item) => {
let option = item.option || '',
alias = item.alias || '',
value = item.value || '';
let msg = " --" + option;
msg += (alias) ? ", -" + alias : "";
msg += (value) ? " " + value : "";
item.msg = msg;
let msgLen = msg.length;
maxOptionLength = (msgLen > maxOptionLength) ? msgLen : maxOptionLength;
return item;
});
options.map((item) => {
let length = item.msg.length;
let space = " ".repeat(maxOptionLength - length);
item.msg = item.msg + space + " ";
return item;
});
options.map((item) => {
item.msg += item.description + "\n";
msg += item.msg;
});
console.log(msg);
};
/**
* pring message
* @param {String} str [message]
* @param {String} color [color name]
* @return {String} [message with color]
*/
pluginUtils.prototype.log = function(str, color) {
str = str || '';
str = _.isObject(str) ? JSON.stringify(str) : str;
let msg = chalk[color](str);
console.log(msg);
return msg;
};
pluginUtils.prototype.fs = fs;
pluginUtils.prototype.chalk = chalk;
pluginUtils.prototype._ = _;
module.exports = pluginUtils;