/
index.js
219 lines (188 loc) · 6.16 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
var util = require('util');
var path = require('path');
var EE = require('events').EventEmitter;
var extend = require('extend');
var resolve = require('resolve');
var flaggedRespawn = require('flagged-respawn');
var isPlainObject = require('is-plain-object');
var mapValues = require('object.map');
var fined = require('fined');
var findCwd = require('./lib/find_cwd');
var findConfig = require('./lib/find_config');
var fileSearch = require('./lib/file_search');
var parseOptions = require('./lib/parse_options');
var silentRequire = require('./lib/silent_require');
var buildConfigName = require('./lib/build_config_name');
var registerLoader = require('./lib/register_loader');
var getNodeFlags = require('./lib/get_node_flags');
function Liftoff(opts) {
EE.call(this);
extend(this, parseOptions(opts));
}
util.inherits(Liftoff, EE);
Liftoff.prototype.requireLocal = function(module, basedir) {
try {
this.emit('preload:before', module);
var result = require(resolve.sync(module, { basedir: basedir }));
this.emit('preload:success', module, result);
return result;
} catch (e) {
this.emit('preload:failure', module, e);
}
};
Liftoff.prototype.buildEnvironment = function(opts) {
opts = opts || {};
// get modules we want to preload
var preload = opts.preload || [];
// ensure items to preload is an array
if (!Array.isArray(preload)) {
preload = [preload];
}
// make a copy of search paths that can be mutated for this run
var searchPaths = this.searchPaths.slice();
// calculate current cwd
var cwd = findCwd(opts);
// if cwd was provided explicitly, only use it for searching config
if (opts.cwd) {
searchPaths = [cwd];
} else {
// otherwise just search in cwd first
searchPaths.unshift(cwd);
}
// calculate the regex to use for finding the config file
var configNameSearch = buildConfigName({
configName: this.configName,
extensions: Object.keys(this.extensions),
});
// calculate configPath
var configPath = findConfig({
configNameSearch: configNameSearch,
searchPaths: searchPaths,
configPath: opts.configPath,
});
// if we have a config path, save the directory it resides in.
var configBase;
if (configPath) {
configBase = path.dirname(configPath);
// if cwd wasn't provided explicitly, it should match configBase
if (!opts.cwd) {
cwd = configBase;
}
}
// TODO: break this out into lib/
// locate local module and package next to config or explicitly provided cwd
/* eslint one-var: 0 */
var modulePath, modulePackage;
try {
var delim = path.delimiter;
var paths = (process.env.NODE_PATH ? process.env.NODE_PATH.split(delim) : []);
modulePath = resolve.sync(this.moduleName, { basedir: configBase || cwd, paths: paths });
modulePackage = silentRequire(fileSearch('package.json', [modulePath]));
} catch (e) {}
// if we have a configuration but we failed to find a local module, maybe
// we are developing against ourselves?
if (!modulePath && configPath) {
// check the package.json sibling to our config to see if its `name`
// matches the module we're looking for
var modulePackagePath = fileSearch('package.json', [configBase]);
modulePackage = silentRequire(modulePackagePath);
if (modulePackage && modulePackage.name === this.moduleName) {
// if it does, our module path is `main` inside package.json
modulePath = path.join(path.dirname(modulePackagePath), modulePackage.main || 'index.js');
cwd = configBase;
} else {
// clear if we just required a package for some other project
modulePackage = {};
}
}
var exts = this.extensions;
var eventEmitter = this;
var configFiles = {};
if (isPlainObject(this.configFiles)) {
var notfound = { path: null };
configFiles = mapValues(this.configFiles, function(prop, name) {
var defaultObj = { name: name, cwd: cwd, extensions: exts };
return mapValues(prop, function(pathObj) {
var found = fined(pathObj, defaultObj) || notfound;
if (isPlainObject(found.extension)) {
registerLoader(eventEmitter, found.extension, found.path, cwd);
}
return found.path;
});
});
}
return {
cwd: cwd,
preload: preload,
configNameSearch: configNameSearch,
configPath: configPath,
configBase: configBase,
modulePath: modulePath,
modulePackage: modulePackage || {},
configFiles: configFiles,
};
};
Liftoff.prototype.handleFlags = function(cb) {
if (typeof this.v8flags === 'function') {
this.v8flags(function(err, flags) {
if (err) {
cb(err);
} else {
cb(null, flags);
}
});
} else {
process.nextTick(function() {
cb(null, this.v8flags);
}.bind(this));
}
};
Liftoff.prototype.prepare = function(opts, fn) {
if (typeof fn !== 'function') {
throw new Error('You must provide a callback function.');
}
process.title = this.processTitle;
var completion = opts.completion;
if (completion && this.completions) {
return this.completions(completion);
}
var env = this.buildEnvironment(opts);
fn.call(this, env);
};
Liftoff.prototype.execute = function(env, forcedFlags, fn) {
if (typeof forcedFlags === 'function') {
fn = forcedFlags;
forcedFlags = undefined;
}
if (typeof fn !== 'function') {
throw new Error('You must provide a callback function.');
}
this.handleFlags(function(err, flags) {
if (err) {
throw err;
}
flags = flags || [];
flaggedRespawn(flags, process.argv, forcedFlags, execute.bind(this));
function execute(ready, child, argv) {
if (child !== process) {
var execArgv = getNodeFlags.fromReorderedArgv(argv);
this.emit('respawn', execArgv, child);
}
if (ready) {
preloadModules(this, env);
registerLoader(this, this.extensions, env.configPath, env.cwd);
fn.call(this, env, argv);
}
}
}.bind(this));
};
function preloadModules(inst, env) {
var basedir = env.cwd;
env.preload.filter(toUnique).forEach(function(module) {
inst.requireLocal(module, basedir);
});
}
function toUnique(elem, index, array) {
return array.indexOf(elem) === index;
}
module.exports = Liftoff;