-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (83 loc) · 2.46 KB
/
Copy pathindex.js
File metadata and controls
94 lines (83 loc) · 2.46 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
// Require a the few dependencies we will need: A promise wrapper, package
// manager, log implementation and REPL loop for console
var Promise = require('bluebird'),
Launcher = require('./lib/launcher'),
path = require('path'),
log = require('npmlog'),
repl = require('repl'),
rc = require('rc'),
util = require('./lib/util'),
nodeUtil = require('util');
function loadConfig(fileName) {
var defaults = {
logging: {
prefix: 'init'
},
npm: {
spin: false
}
};
return new Promise(function(resolve, reject) {
// Search for a config from several locations & environment variables;
// see https://github.com/dominictarr/rc
// Then construct a loader with the given configuration
var loadedConfig = rc(fileName, defaults),
parsedConfig;
// Resolve the env. variables nested within the config
parsedConfig = util.mapNested(loadedConfig, util.envReplace);
resolve(parsedConfig);
});
}
function createLauncher(config) {
// Curry the logger with the particular prefix
var logger = {
info: log.info.bind(log, config.logging.prefix),
error: log.error.bind(log, config.logging.prefix),
warn: log.warn.bind(log, config.logging.prefix)
};
return new Promise(function(resolve, reject) {
resolve(new Launcher(config, logger));
});
}
function startPackages(launcher) {
return function(packages) {
var promises = packages.map(launcher.start.bind(launcher))
return Promise.all(promises);
}
}
function startREPL(context) {
// Callback that is invoked when REPL dies
return new Promise(function(resolve, reject) {
var cli = repl.start({
prompt: 'jsos> ',
input: process.stdin,
output: process.stdout
})
// Pass the loader to the context
for (prop in context) {
if (context.hasOwnProperty(prop)) {
cli.context[prop] = context[prop];
}
}
// Handle the exist function
cli.on('exit', function() {
return resolve();
})
});
}
// Initialize the loader
var promise = loadConfig('jsos-init')
.then(createLauncher)
.then(function(launcher) {
// Then start all the packages found, and finally start the REPL
// Note: All the launcher specifics need to be executed in Launcher context.
return launcher.init().bind(launcher)
.then(launcher.list)
.then(startPackages(launcher))
.done(startREPL({ launcher: launcher }))
})
.catch(function(err) {
var message = nodeUtil.format('Initialization failed: %s', err.message);
log.error(message);
console.log(err.stack);
});