-
Notifications
You must be signed in to change notification settings - Fork 0
/
summon
executable file
·127 lines (109 loc) · 4.2 KB
/
summon
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
#!/usr/bin/env node
// Please don't edit this file directly. It is compiled from the CoffeeScript
// in summon.coffee. If you edit the CoffeeScript source and compile its changes,
// please keep the commits to this file separate. See the README for instructions
// on setting up a pre-commit hook to help with that.
var async, exec, getPlatformInstallers, nopt, options, packageName, readline, runInstallCommand, testCommand, tryToInstall, userInput;
exec = require('child_process').exec;
readline = require('readline');
async = require('async');
nopt = require('nopt');
options = nopt({
testCommand: [String],
manager: [Array, String]
});
userInput = readline.createInterface({
input: process.stdin,
output: process.stdout
});
if (!(options.argv.remain.length > 0)) {
console.log('Pass the package name you want to install as the first argument.');
console.log('Usage: ./summon [packageName]');
console.log('Example: ./summon git');
process.exit(1);
}
packageName = options.argv.remain[0];
testCommand = options.testCommand ? options.testCommand : packageName;
exec("command -v " + testCommand).on('exit', function(code) {
if (code === 0) {
process.exit(0);
}
console.log("You are missing " + packageName + ", which is a required dependency.");
return tryToInstall();
});
tryToInstall = function() {
return userInput.question("Do you want to try to install " + packageName + " automatically? (Y/n) ", function(answer) {
var installers;
if (/^n/i.test(answer)) {
console.log("\nPlease install " + packageName + " and then re-run this installer.");
process.exit(1);
}
console.log("Okay, let's try to install " + packageName + ".\n");
installers = getPlatformInstallers();
return async.filter(installers, function(installer, callback) {
return exec("command -v " + installer).on('exit', function(code) {
return callback(code === 0);
});
}, function(installers) {
var index, installer, _i, _len;
if (installers.length === 0) {
console.log("We couldn't find any package managers.");
console.log("Please install " + packageName + " and then re-run this installer.");
process.exit(1);
}
console.log("We looked for package managers and found:");
for (index = _i = 0, _len = installers.length; _i < _len; index = ++_i) {
installer = installers[index];
console.log("" + (index + 1) + ") " + installer);
}
return userInput.question("\nDo you want to use one of these? Or let us know what command you prefer. (1/#/[command]) ", function(choice) {
if (/^\d+$/.test(choice)) {
installer = installers[choice - 1];
} else if (choice) {
installer = choice;
} else {
installer = installers[0];
console.log("Using the default installer: " + installer);
}
if (!installer) {
console.log('\nOops, not a valid choice.');
process.exit(1);
}
return runInstallCommand(installer, packageName);
});
});
});
};
runInstallCommand = function(installer, command) {
var _package;
_package = options[installer] ? options[installer] : packageName;
command = "" + installer + " install " + _package;
console.log("\nOkay, going to try running: " + command + "\n");
return userInput.question('Good to go? (Y/n) ', function(answer) {
if (answer && /^n/i.test(answer)) {
console.log("\nOkay, we won't do it. You're on your own for installing " + packageName + ".");
process.exit(0);
}
return exec(command).on('exit', function(code) {
if (code === 0) {
console.log('\nAwesome, that worked! Enjoy!');
return process.exit(0);
} else {
console.log("\nUh oh, looks like that didn't work.");
console.log("Please install " + packageName + " and then re-run this installer.");
return process.exit(code);
}
});
});
};
getPlatformInstallers = function() {
if (process.platform === "linux" && process.arch === "x64") {
return ["apt-get", "yum"];
} else if (process.platform === "darwin") {
return ["brew", "port", "fink"];
} else if (process.platform === "win32") {
return ["apt-cyg"];
} else {
return [];
}
};