-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathproject.js
More file actions
377 lines (290 loc) · 10.7 KB
/
project.js
File metadata and controls
377 lines (290 loc) · 10.7 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
var path = require('path');
var fs = require('fs');
var _ = require('underscore');
var Dependencies = require('./dependencies/dependencies');
var Config = require('./config');
var Meteor = require('./meteor');
var Command = require('./command');
var wrench = require('wrench');
var exec = require('child_process').exec;
var exit = require('exit');
var detectEol = require('./eol').detectEol;
// The project is the current directory's personal version of meteor,
// complete with its own set of packages.
// it installs into ./meteor/meteorite
Project = function(root, meteorArgs) {
// Figure out all the paths we'll need to know
this.root = root;
this.meteorArgs = meteorArgs;
this.smartJsonPath = path.join(this.root, 'smart.json');
this.smartLockPath = path.join(this.root, 'smart.lock');
this.packagesRoot = path.join(this.root, 'packages');
var data = '';
if (fs.existsSync(this.smartLockPath))
data += fs.readFileSync(this.smartLockPath).toString();
if (fs.existsSync(this.smartJsonPath))
data += fs.readFileSync(this.smartJsonPath).toString();
this.EOL = detectEol(data);
// set a base meteor if it's specified in the args (or a default one if not)
this.meteor = new Meteor(meteorArgs);
};
// read the config from smart.lock if it exists
Project.prototype.initFromLock = function() {
var self = this;
if (fs.existsSync(this.smartLockPath)) {
var data = fs.readFileSync(this.smartLockPath).toString();
var lockData = JSON.parse(data);
// embed the root path in all this data
lockData.meteor.root = self.root;
_.each(lockData.dependencies.basePackages, function(pkg) {
pkg.root = self.root;
});
_.each(lockData.dependencies.packages, function(pkg) {
pkg.root = self.root;
});
this.meteor = new Meteor(lockData.meteor);
this.dependencies = Dependencies.newFromLockJson(lockData.dependencies);
}
};
// have a look in smart.json, see if it's different to what we have from smart.lock
Project.prototype.checkSmartJson = function(forceUpdate) {
// this is the config specified in smart.json
var config = new Config(this.root);
var newMeteor = new Meteor(config.meteor);
// when running in the context of a package, we are the only required package
if (config.name) {
var specifier = {};
specifier[config.name] = {path: "."};
var newDeps = new Dependencies(specifier);
} else {
var newDeps = new Dependencies(config.packages);
}
if (forceUpdate || !this.meteor.equals(newMeteor) || !this.dependencies || !this.dependencies.equalBase(newDeps)) {
if (!forceUpdate && this.dependencies)
console.log('smart.json changed.. installing from smart.json');
this.lockChanged = true;
this.meteor = newMeteor;
this.dependencies = newDeps;
}
};
// FIXME - this doesn't actually fetch the packages.
// we only fetch them when we are about to install them, or we need to
// take a look inside them. I think this is reasonable.
Project.prototype.fetch = function(fn, forceUpdate) {
var self = this;
// prepare dependencies and meteor
self.initFromLock();
self.checkSmartJson(forceUpdate);
// Ensure the right version of meteor has been fetched
var buildDevBundle = !! self.meteorArgs['build-dev-bundle'];
self.meteor.prepare(buildDevBundle, function() {
// resolving dependencies fetches them. We need to check otherwise
if (!self.dependencies.resolved()) {
console.verbose('Resolving dependency tree');
self.dependencies.resolve(self.meteorArgs.force, function(err, conflicts) {
_.each(conflicts, function(conflict, name) {
console.log(('Problem installing ' + name.bold).red);
console.log((" ✘ " + conflict).red);
});
if (err) {
console.log(err.message.red);
exit(1);
return;
}
fn();
});
} else {
fn();
}
});
};
Project.prototype.uninstall = function() {
var self = this;
// for now, remove anything in packages/ that is a symlink
if (fs.existsSync(this.packagesRoot)) {
var dirs = fs.readdirSync(this.packagesRoot);
_.each(dirs, function(dir) {
var dirPath = path.join(self.packagesRoot, dir);
if (fs.lstatSync(dirPath).isSymbolicLink())
fs.unlink(dirPath)
});
}
};
// either install from smart.lock or prepare smart.lock and do so
Project.prototype.install = function(fn, forceUpdate) {
var self = this;
self._optimizeFS();
// Fetch everything the project needs
self.fetch(function() {
// ensure that the installRoot exists
if (! self.dependencies.isEmpty())
wrench.mkdirSyncRecursive(self.packagesRoot);
// Link each package into installRoot
self.dependencies.installInto(self, function(packagesInstalled) {
console.log();
console.log('Done installing smart packages'.bold);
// install the smart.lock file
if (fs.existsSync(self.smartJsonPath) && (self.lockChanged || !fs.existsSync(self.smartLockPath)))
self.writeLockFile();
fn();
});
}, forceUpdate);
};
// if there's no dependencies we don't have to install
Project.prototype.needsToInstall = function() {
return !this.dependencies.isEmpty();
}
// prepare a new smart.lock, then install
Project.prototype.update = function(fn) {
this.install(fn, true);
};
Project.prototype.execute = function(args, fn) {
var self = this;
if (self.meteorArgs.version)
console.suppress();
console.log();
console.log("Stand back while Meteorite does its thing".bold);
// TODO -- what do we do here if not installed? I'm not sure we just go ahead
// and install, we should probably abort and tell them
self.install(function() {
console.log();
console.log("Ok, everything's ready. Here comes Meteor!".green);
console.log();
if (self.meteorArgs.version)
console.unsuppress();
self.meteor.execute(args, self.packagesRoot, fn);
});
};
// assumes that we are installed.
Project.prototype.isUsing = function(packageName, fn) {
var self = this;
self.install(function() {
return self.meteor.isUsing(packageName, fn);
});
}
// ensure a named package is installed
//
// NOTE: Right now, if the package is already available (included in meteor, already in smart.json)
// we ignore the version, and just stick with what we have
//
// TODO: In the future a version # would override anything in meteor + rewrite smart.json
// but right now it's TBH to overwrite meteor's packages.
Project.prototype.installPackage = function(pkgName, version, fn) {
var self = this;
// first ensure we are fetched, so we know _all_ the packages that are available
self.fetch(function() {
self.hasPackage(pkgName, function(check) {
// if we have the package already
if (check)
return fn();
// better check that the package exists on atmosphere
Atmosphere.package(pkgName, function(atmosphere_defn) {
if (!atmosphere_defn)
throw("Package named " + pkgName + " doesn't exist in your meteor installation, smart.json, or on atmosphere");
// ok, it's not installed. So we need to add it (permanently) to the smart.json
// and clear our dependencies
var smartJson = self.readSmartJson();
var defn = {}
if (version)
defn.version = version;
smartJson.packages = smartJson.packages || {};
smartJson.packages[pkgName] = defn;
self.writeSmartJson(smartJson);
// maybe a hack to read it back out from disk, but not a big deal I don't think
self.checkSmartJson(true);
fn();
});
});
});
}
// remove a package that is listed in smart.json (if it is indeed listed there)
Project.prototype.uninstallPackage = function(pkgName, fn) {
var self = this;
// we need to be fetched so we have all the information
// self.fetch(function() {
var pkg = self.dependencies.basePackages[pkgName];
if (pkg) {
// remove that bad boy from smart.json and reset our dependencies
var smartJson = self.readSmartJson();
delete smartJson.packages[pkgName];
self.writeSmartJson(smartJson);
// maybe a hack to read it back out from disk, but not a big deal I don't think
self.checkSmartJson(true);
// now bring everything in line with smart.json
self.install(function() {
// finally remove package from packages/
pkg.removeFrom(self, fn);
});
} else {
fn();
}
}
// Is the package part of the meteor install, or is it a dependency?
//
// NOTE: assumes we have fetched. FIXME: figure out a better / systematic way
// to write code that has this sort of assumption
Project.prototype.hasPackage = function(pkgName, fn) {
if (this.dependencies.packages[pkgName]) {
return fn(true);
}
this.meteor.hasPackage(pkgName, fn)
};
// very simple version of what config does
Project.prototype.readSmartJson = function() {
try {
var rawConfig = fs.readFileSync(path.join(this.root, 'smart.json')).toString();
return JSON.parse(rawConfig);
} catch (err) {
return {};
}
};
Project.prototype.smartJson = function() {
var data = {};
if (!this.meteor.defaultMeteor)
data.meteor = this.meteor.toJson();
if (this.dependencies)
data.packages = this.dependencies.toJson().basePackages;
else
data.packages = {};
return data;
};
Project.prototype.writeSmartJson = function(json) {
json = json || this.smartJson();
// Make a nicely formated default json string
var smartJsonString = JSON.stringify(json, null, 2) + "\n";
// Write to disk
if (fs.existsSync(this.root))
fs.writeFileSync(this.smartJsonPath, smartJsonString.replace(/\n/g, this.EOL));
};
Project.prototype.lockJson = function() {
return {
meteor: this.meteor.toJson(true),
dependencies: this.dependencies.toJson(true)
};
};
// write out into smart.lock
Project.prototype.writeLockFile = function() {
var smartJsonString = JSON.stringify(this.lockJson(), null, 2) + "\n";
fs.writeFileSync(this.smartLockPath, smartJsonString.replace(/\n/g, this.EOL));
};
Project.prototype._optimizeFS = function() {
var self = this;
var deletable = [];
// remove old .meteor/meteorite directory
var oldInstallRoot = path.join(this.root, '.meteor', 'meteorite')
if (fs.existsSync(oldInstallRoot)) {
deletable.push(oldInstallRoot);
}
if (deletable.length > 0)
console.log("Yay! We're optimizing your installation!".yellow.bold);
_.each(deletable, function(filePath) {
console.log(" ✘ ".red + ("Deleting " + filePath).grey);
if (fs.lstatSync(filePath).isDirectory())
wrench.rmdirSyncRecursive(filePath);
else
fs.unlink(filePath);
});
};
module.exports = Project;
// var _debug = require('./debug');
// _.debugClass('Project');