-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkolibri.js
100 lines (81 loc) · 2.64 KB
/
kolibri.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
const { spawn } = require('child_process');
const path = require('path');
const Defer = require('promise-be-deferred');
const yauzl = require('yauzl');
class Kolibri
{
constructor(pexFile, options) {
this._pexFile = pexFile;
this._options = options;
}
getHome(child = true) {
const home = path.resolve(this._options.homeDirectory, path.basename(this._pexFile, '.pex'));
return child
? path.resolve(home, `${this._options.python}-${this._options.port}`)
: home;
}
getInfo() {
const read = new Defer();
const buf = [];
yauzl.open(this._pexFile, { lazyEntries: true }, (err, zipFile) => {
if (err) throw err;
zipFile.on('entry', (entry) => {
if (entry.fileName !== 'PEX-INFO') {
return process.nextTick(() => zipFile.readEntry());
}
zipFile.openReadStream(entry, (err, pexInfo) => {
pexInfo.on('data', data => buf.push(data));
pexInfo.on('end', () => {
zipFile.close();
read.resolve(JSON.parse(Buffer.concat(buf).toString()));
});
});
});
zipFile.readEntry();
});
return read;
}
spawn(command, args = []) {
const { logger } = this._options;
args.unshift(this._pexFile, command);
const contentDirectory = this._options.clean
? 'content'
: this._options.contentDirectory;
logger.verbose(`KOLIBRI_HOME=${this.getHome()}`);
logger.verbose(`KOLIBRI_CONTENT_DIR=${contentDirectory}`);
logger.verbose(`KOLIBRI_RUN_MODE=${this._options.runMode}`);
logger.verbose(`KOLIBRI_HTTP_PORT=${this._options.port}`);
return spawn(this._options.python, args, {
env: Object.assign({} , process.env, {
KOLIBRI_HOME: this.getHome(),
KOLIBRI_CONTENT_DIR: contentDirectory,
KOLIBRI_RUN_MODE: this._options.runMode,
KOLIBRI_HTTP_PORT: this._options.port,
}),
detached: true,
windowsHide: true,
});
}
manage(args) {
const { logger } = this._options;
logger.verbose(`Invoking management command: ${args.join(', ')}`);
const child = this.spawn('manage', args);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
return new Promise(resolve => {
child.on('exit', () => resolve(null));
});
}
}
Kolibri.VERSION_REGEX = /^v([ab\d\.]+)$/
Kolibri.build = function(argv)
{
if (!argv.pex) {
return null;
}
let pexFile = Kolibri.VERSION_REGEX.test(argv.pex)
? path.resolve(argv.homeDirectory, argv.pex.replace(Kolibri.VERSION_REGEX, "kolibri-$1.pex"))
: path.resolve(process.cwd(), argv.pex);
return new Kolibri(pexFile, argv);
};
module.exports = Kolibri;