forked from cadecairos/webmaker-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-webmaker.js
More file actions
executable file
·315 lines (290 loc) · 8.86 KB
/
install-webmaker.js
File metadata and controls
executable file
·315 lines (290 loc) · 8.86 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
/****
*
* This is the installer for the Webmaker suite of projects.
*
* Run "node install", and you should be set.
*
* Use "node update" to bump every project to latest master.
* (note: not implemented yet)
*
* Use "node run" to fire up every project for testing.
* (note: not implemented yet)
*
****/
/**
* Installation script requirements
*/
var batchExec = require("./lib/batch").batchExec,
checkError = require("./lib/batch").checkError,
fs = require("fs"),
runtime;
/**
* This function houses all the installer code
*/
function runInstaller(runtime, commandStrings) {
console.log("Finished bootstrapping.");
console.log("\n===================================================================");
console.log("Starting installation. You might want to go make a cup of coffee...");
console.log("===================================================================");
// Installation requirements
var habitat = (function() {
var habitat = require("habitat");
habitat.load();
return new habitat();
}()),
awsOptions = habitat.get("s3"),
gitOptions = habitat.get("git"),
username,
gitCredentials = (function(options) {
if (!options)
return '';
username = options.username;
if (!username)
return '';
return username;
}(gitOptions)),
repos = require("./lib/repos")(commandStrings);
// Our list of apps that belong to the Webmaker Suite
// This list will become a middleware list instead, so
// that it's easier to manipulate, and easier to require
// in other apps (like for "node run").
var repositories = Object.keys(repos);
/**
* Tweak all .env files that require AWS credentials
* to use the ones that we got from the user.
*/
function setupAWS(repositories, next) {
if (repositories.length === 0) {
return setTimeout(next, 10);
};
var repo = repositories.pop(),
aws = repos[repo].aws;
if (aws) {
process.chdir(repo);
var data = "" + fs.readFileSync(aws), line, i, last,
lines = data.split("\n");
for(i=0, last=lines.length; i<last; i++) {
line = lines[i];
if(line.indexOf("S3_KEY=")>-1) {
lines[i] = "export S3_KEY=\"" + awsOptions.key + "\"";
}
else if(line.indexOf("S3_SECRET=")>-1) {
lines[i] = "export S3_SECRET=\"" + awsOptions.secret + "\"";
}
else if(line.indexOf("S3_BUCKET=")>-1) {
lines[i] = "export S3_BUCKET=\"" + awsOptions.bucket + "\"";
}
}
fs.writeFileSync(aws, lines.join("\n"));
process.chdir("..");
}
setupAWS(repositories, next);
}
/**
* Set up the environment for specific repositories
*/
function setupEnvironment(repositories, next) {
if (repositories.length === 0) {
return setTimeout(next, 10);
};
var repo = repositories.pop(),
env = repos[repo].env;
if (env) {
console.log("setting up " + repo + " environment.");
if (typeof env === "string") {
process.chdir(repo);
batchExec([env], function() {
process.chdir("..");
setupEnvironment(repositories, next);
});
}
else if (typeof env === "function") {
env(repo, fs, habitat);
setupEnvironment(repositories, next);
}
} else { setupEnvironment(repositories, next); }
}
/**
* Set up all the .env files so that all
* repositories point to all the correct
* other repositories.
*/
function setupEnvironments() {
console.log();
setupEnvironment(repositories = Object.keys(repos), function() {
console.log("setting AWS credentials.");
setupAWS(repositories = Object.keys(repos), function() {
console.log("\nInstallation complete.");
process.exit(0);
});
});
};
/**
* Run npm install + npm cache clean for a repository.
*/
function installModule(repositories, next) {
if (repositories.length === 0) {
return setTimeout(next, 10);
};
var repo = repositories.pop();
console.log("resolving module dependencies for "+repo);
process.chdir(repo);
batchExec(repos[repo].install,
function() {
process.chdir("..");
installModule(repositories, next);
}
);
}
/**
* Run npm install + npm cache clean for all repositories.
*/
function installModules() {
if(runtime.skipnpm) {
setupEnvironments();
} else {
console.log();
installModule(repositories = Object.keys(repos), function() {
setupEnvironments();
});
}
}
/**
* When we have processed all repositories,
* link them all up with relevant .env settings.
*/
function tryNext(error, stdout, stderr) {
checkError(error, stdout, stderr);
// done cloning - set up the .env files
if (repositories.length === 0) {
installModules();
}
// clone the next repository
else {
var repo = repositories.pop(),
repoURL = "https://github.com/mozilla/" + repo + ".git",
rm = "rm -rf " + repo,
clone = "git clone " + repoURL,
commands = (runtime.skipclone ? [] : [rm, clone]);
if(!runtime.skipclone) {
console.log("\ncloning " + repo);
}
batchExec(commands, function(error, stdout, stderr) {
checkError(error, stdout, stderr);
process.chdir(repo);
var commands = (runtime.skipclone ? [] : [
"git checkout master",
"git submodule sync",
"git submodule update --init --recursive",
"git remote rename origin mozilla",
"git remote add origin git@github.com:" + username + "/" + repo + ".git",
]);
batchExec(commands, function() {
process.chdir("..");
tryNext();
});
});
}
};
/**
* clone all the repositories
*/
tryNext();
}
/**
* Runtime argument parsing
*/
function getRunTime() {
var argv = require("argv");
argv.option({
name: 'username',
type: 'string',
description: 'Username for git',
example: "'node install --username=username"
});
argv.option({
name: 's3key',
type: 'string',
description: 'API key for Amazon Web Services\' S3',
example: "'node install --s3key=abcdefg'"
});
argv.option({
name: 's3secret',
type: 'string',
description: 'Secret key for Amazon Web Services\' S3',
example: "'node install --s3key=abcdefg --s3secret=123456'"
});
argv.option({
name: 's3bucket',
type: 'string',
description: 'Bucket name to use with Amazon Web Services\' S3',
example: "'node install --s3key=abcdefg --s3secret=123456' --s3bucket=my.bucket.name"
});
argv.option({
name: 'skipclone',
type: 'string',
description: 'Skip all \'git clone\' steps',
example: "'node install --skipclone'"
});
argv.option({
name: 'skipnpm',
type: 'string',
description: 'Skip all \'npm install\' and \'npm cache clean\' steps',
example: "'node install --skipnpm'"
});
return argv.run().options;
}
/**
* Bootstrap and run the installation
*/
(function bootStrap(){
console.log("Bootstrapping installer...");
var commandStrings = require("./lib/commandstrings"),
npm = commandStrings.npm,
commands = [
"rm -rf node_modules",
npm + " install --no-bin-links",
npm + " cache clean"
];
batchExec(commands, function() {
runtime = getRunTime();
// do we need an .env file?
if (!fs.existsSync(".env")) {
console.log("No .env file found.");
/**
* This funcitons writes the installer's .env file
*/
var writeEnv = function (err, result) {
if (err) { return onErr(err); }
// write local .env
var content = [
'export GIT_USERNAME="' + result.username + '"',
'export S3_KEY="' + result.s3key + '"',
'export S3_SECRET="' + result.s3secret + '"',
'export S3_BUCKET="' + result.s3bucket + '"',
''].join("\n");
fs.writeFileSync(".env", content);
console.log(".env file created.");
runInstaller(runtime, commandStrings);
};
// do we still need git username and s3 key/secret combinations?
if (!runtime.username || !runtime.s3key || !runtime.s3secret || !runtime.s3bucket) {
console.log("Please specify your git and AWS credentials:");
var prompt = require("prompt");
prompt.start();
prompt.get(['username', 's3key', 's3secret', 's3bucket'], writeEnv);
}
// we got the user/pass information from the runtime arguments
else {
writeEnv(null, {
username: runtime.username,
s3key: runtime.s3key,
s3secret: runtime.s3secret,
s3bucket: runtime.s3bucket
});
}
}
// we already had an .env file
else { runInstaller(runtime, commandStrings); }
});
}());