forked from grumpishmermaids/grump
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.js
More file actions
453 lines (366 loc) · 12 KB
/
utils.js
File metadata and controls
453 lines (366 loc) · 12 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
var path = require('path');
var fs = require('fs.extra');
var http = require('http');
var https = require("follow-redirects").https;
var mkdirp = require('mkdirp');
var exec = require('child-process-promise').exec;
var spawn = require('child_process').spawn;
var prompt = require('prompt');
var _ = require('lodash');
var serverApiUrl = "http://localhost:3000/api/lib/";
// Local directory - wrap this around paths accessing the local
// node module folder and not the CWD grump is being executed from
var lodir = function(dir) {
var newArgs = [__dirname];
var args = Array.prototype.slice.call(arguments);
newArgs = newArgs.concat(args);
return path.join.apply(this, newArgs);
};
// Fetch and update config
var config = function() {
return JSON.parse(fs.readFileSync(lodir('config.json'), 'utf-8'));
};
var updateConfig = function(obj) {
fs.writeFileSync(lodir('config.json'), JSON.stringify(obj));
};
// Quick check to know if verbose messages should be displayed
var isVerbose = function() {
return config().verbose === "true";
};
// Make the lib dir if it doesn't exist
var initialRun = function() {
try {
fs.mkdirSync(lodir('lib'));
} catch (e) {}
};
// Scan lib directory for installed grumps
var getInstalledGrumps = function() {
var grumps;
try {
var data = fs.readFileSync(lodir('lib/grumpTable.json'), 'utf-8');
return JSON.parse(data);
}
catch(e) {
return {};
}
};
/*=============================
= NOTES =
=============================*/
/**
*
* validLocalGrump needs to be fixed
*
*/
// Check if grump ( specific (keith/hello) or general (hello) ) exists
var validLocalGrump = function(grump, installedGrumps) {
return installedGrumps.hasOwnProperty(grump);
};
// Query Grumpjs server for grump
var queryServer = function(grump, cb) {
if (isVerbose()) console.log("Querying grumpjs server for " + grump.cyan);
http.get(serverApiUrl + grump, function (res) {
if (isVerbose()) console.log("Received statusCode " + res.statusCode.toString().green + " from server.");
if (res.statusCode === 404) {
cb("Error".red + ": " + grump.cyan + " was not found on the server.", null);
} else if (res.statusCode > 500) {
cb("Error".red + ": Something went wrong on grumpjs. Please try again later.", null);
} else if (res.statusCode === 200) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
body = JSON.parse(body);
if (body.grumps.length === 0) {
cb("Error".red + ": No matches for '" + grump.cyan + "' were found on server.", body);
} else {
cb(null, body);
}
});
res.on('error', function(err){
cb(err, null);
});
}
})
.on('error', function(err) {
cb(err, null);
});
};
var install = function(repo, installedGrumps, isUpdate) {
var repoName = repo.repoName;
var author = repo.author;
if (isVerbose()) { console.log("Installing " + author.green + "/" + repoName.cyan + "..."); }
if(isUpdate) {
fs.rmrfSync(lodir("lib", repoName, author));
}
mkdirp.sync(lodir("lib", repoName, author));
// Recursively create command and author directory
// Clone from github
var gitCloneCommand = 'git clone ' + repo.cloneUrl + ' ' + lodir("lib", repoName, author);
exec(gitCloneCommand)
.fail(function (err) {
console.log("Error".red + ": Something went wrong while attempting to clone " + grump.cyan + ".");
})
.then(function () {
//get grump.json file
var grumpjson = JSON.parse(fs.readFileSync(lodir("lib", repoName, author, "grump.json"), 'utf-8'));
//get all of the commands in the grump.json file, and add it to our table entry
for(var key in grumpjson.commands) {
installScript(key);
}
if(isUpdate){
trimTable(grumpjson);
}
fs.writeFileSync(lodir('lib', 'grumpTable.json'), JSON.stringify(installedGrumps), 'utf8');
var successMessage = isUpdate ? "updated." : "installed.";
console.log("Grump ", author.green + "/".green + repoName.green, " successfully " + successMessage);
})
.fail(function (err) {
console.log("error".red, err);
});
function trimTable (grumpjson) {
for(var key in installedGrumps) {
installedGrumps[key] = _.filter(installedGrumps[key], function (commandObj) {
if(commandObj.author === author && commandObj.repoName === repoName) {
var commandKey = commandObj.command;
if(!grumpjson.commands[commandKey]) {
return false;
}
}
return true;
});
if(installedGrumps[key].length === 0) {
delete installedGrumps[key];
}
}
}
function installScript (command) {
var authorKey = author + ":" + command;
var repoKey = repoName + ":" + command;
var authorRepoKey = author + "/" + repoName + ":" + command;
var commandKey = command;
var grumps;
var value = {
repoName: repoName,
author: author,
command: command,
path: lodir('lib', repoName, author)
};
var keys = [authorKey, repoKey, authorRepoKey, commandKey];
for (var i = 0; i < keys.length; i++) {
installedGrumps[keys[i]] = installedGrumps[keys[i]] || [];
var grumpIndex = checkPackage(installedGrumps[keys[i]], value);
if(grumpIndex !== -1) {
installedGrumps[keys[i]][grumpIndex] = value;
} else {
installedGrumps[keys[i]].push(value);
}
}
}
function checkPackage (grumps, grump) {
var pack;
for(var i = 0; i < grumps.length; i++) {
pack = grumps[i];
if (pack.author === grump.author && pack.repoName === grump.repoName) {
return i;
}
}
return -1;
}
};
var run = function(data, args) {
var repoName = data.repoName;
var author = data.author;
var path = data.path;
var command = data.command;
var grumpjson;
try {
grumpjson = JSON.parse(fs.readFileSync(path + '/grump.json'));
}
catch(e) {
console.err('Error: could not find grump.json');
}
// Extract default command config
var file = grumpjson.commands[command].path;
var grumpPath = path + '/' + file;
// var grumpPath = lodir("lib", command, author, file);
// Determine how to run the script
var cmd = grumpjson.commands[command].run;
// Set up arguments to be passed into spawn
args.unshift(grumpPath);
// Make backup of original script for variable injection
copyGrump(grumpPath, function() {
// Prompt for variables and store persistant values if first run
checkVars(function() {
// Finally, run the grump
runGrump(cmd, args, function() {
// And now, we can move the .bak file back to the original
fs.copy(grumpPath + ".bak", grumpPath, { replace: true }, function(err) {
if (err) {
console.log("Error".red + ": Something went wrong while copying backup of grump.");
}
fs.unlink(grumpPath + ".bak", function(err) {
if (err) {
console.log("Error".red + ": Something went wrong while deleting backup of grump.");
}
});
});
});
});
});
function copyGrump(grump, cb) {
var from = grump;
var to = grump + ".bak";
// Check if .bak file exists
fs.stat(to, function(err, stat) {
// Copy was never deleted which means execution was canceled half way through
if (err === null) {
// Swap which file to copy from/to since .bak holds the original
// We don't need to make a backup...we need to restore the back up now.
var tmp = from
from = to;
to = tmp;
}
// Perform copy
fs.copy(from, to, { replace: true }, function(err) {
if (err) {
console.log("Error".red + ": Something went wrong while making copy of grump.");
}
cb();
});
});
}
function firstRun(cb) {
if (grumpjson.stats === undefined) {
//console.log("first run.");
cb(true);
} else {
//console.log(grumpjson.stats.executions + " runs.");
cb(false);
}
}
function checkVars(cb) {
var init;
if (grumpjson.stats === undefined) {
//console.log("first run.");
init = true;
} else {
//console.log(grumpjson.stats.executions + " runs.");
init = false;
}
promptVars(init, function(vars) {
injectVariables(vars, function() {
cb();
});
});
}
function promptVars(init, cb) {
// Sort persist/non-persist vars
var persist = {};
var non_persist = {};
var persist_keys = [];
var non_persist_keys = [];
_.each(grumpjson.commands[command].vars, function(variable, key) {
if (variable.persist && variable.persist.toString() === "true") {
persist[key] = variable;
persist_keys.push(key);
} else {
non_persist[key] = variable;
non_persist_keys.push(key);
}
});
var prompts = [];
// Also ask persistant variables since this is the 1st time running this grump
if (init) {
var prompts = prompts.concat(persist_keys);
}
var prompts = prompts.concat(non_persist_keys);
if (prompts.length === 0) {
cb({}); // Pass in empty variables object since no vars are available
} else {
var variables = {};
// If not first time, there are vars to fetch from grump.json
if (!init) {
variables = grumpjson.commands[command].persist_vars;
}
prompt.message = author.green + "/" + commandName.cyan;
prompt.start();
prompt.get(prompts, function (err, results) {
// First time, so extract persist vars and save them to grump json
if (init) {
// Create new object to hold persist vars
grumpjson.commands[command].persist_vars = {};
_.each(results, function(result, key) {
// Found a persist var
if (persist_keys.indexOf(key) !== -1) {
grumpjson.commands[command].persist_vars[key] = result;
}
});
}
variables = _.merge(_.clone(variables), _.clone(results));
cb(variables);
});
}
}
function runGrump(cmd, args, cb) {
var childProcess = spawn(cmd, args, {stdio: [
0, // use parents stdin for child
'pipe', // pipe child's stdout to parent
]});
// Update grump stats
updateStats();
// Save grump.json file changes
updateGrumpJSON(grumpjson);
childProcess.stdout.on('data', function (data) {
process.stdout.write(data);
});
childProcess.stderr.on('data', function (data) {
process.stderr.write(data);
});
childProcess.on('close', function (code) {
cb();
//console.log('process exit code ' + code);
});
function updateStats() {
if (grumpjson.stats === undefined) {
grumpjson.stats = {};
grumpjson.stats.executions = 1;
} else {
grumpjson.stats.executions += 1;
}
}
}
function updateGrumpJSON(obj) {
fs.writeFileSync(lodir("lib", repoName, author, "grump.json"), JSON.stringify(obj));
}
function injectVariables(vars, cb) {
// Read in file
var contents = fs.readFile(grumpPath, 'utf-8', function(err, contents) {
if (err) {
console.log("Error".red + ": Something went wrong while attempting to read the grump file.");
}
// replace all occurances of variables with their values
_.each(vars, function(value, key) {
contents = contents.replace("grump_" + key, value);
});
// Write the new file
fs.writeFile(grumpPath, contents, function(err) {
if (err) {
console.log("Error".red + ": Something went wrong while attempting to write the grump file.");
}
cb();
});
});
}
};
exports.install = install;
exports.run = run;
exports.queryServer = queryServer;
exports.isVerbose = isVerbose;
exports.lodir = lodir;
exports.validLocalGrump = validLocalGrump;
exports.initialRun = initialRun;
exports.getInstalledGrumps = getInstalledGrumps;
exports.config = config();
exports.updateConfig = updateConfig;