-
Notifications
You must be signed in to change notification settings - Fork 6
/
masterpkg.js
executable file
·163 lines (128 loc) · 3.69 KB
/
masterpkg.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
/*jslint node: true */
"use strict";
var argv = require('minimist')(process.argv.slice(2));
var gulp = require('gulp');
var gutil = require('gulp-util');
var chalk = require('chalk');
var prettyTime = require('pretty-hrtime');
var fs = require('fs');
var path = require('path');
require("./gulpfile.js");
function help() {
console.log("master build");
}
// exit with 0 or 1
var failed = false;
process.once('exit', function(code) {
if (code === 0 && failed) {
process.exit(1);
}
});
if ((argv.r) || (argv.release)) {
global.release = true;
}
if (argv._.length === 0 ) {
help();
process.exit(0);
} else if (argv._[0] === "install") {
installService();
} else if (argv._[0] === "uninstall") {
uninstallService();
} else {
logEvents(gulp);
gulp.start(argv._[0], function(err) {
if (err) {
console.log(err.stack);
process.exit(1);
}
});
}
// format orchestrator errors
function formatError(e) {
if (!e.err) {
return e.message;
}
// PluginError
if (typeof e.err.showStack === 'boolean') {
return e.err.toString();
}
// normal error
if (e.err.stack) {
return e.err.stack;
}
// unknown (string, number, etc.)
return new Error(String(e.err)).stack;
}
// Copied from main gulp binary.
function logEvents(gulpInst) {
// total hack due to poor error management in orchestrator
gulp.on('err', function () {
failed = true;
});
gulp.on('task_start', function (e) {
// TODO: batch these
// so when 5 tasks start at once it only logs one time with all 5
gutil.log('Starting', '\'' + chalk.cyan(e.task) + '\'...');
});
gulp.on('task_stop', function (e) {
var time = prettyTime(e.hrDuration);
gutil.log(
'Finished', '\'' + chalk.cyan(e.task) + '\'',
'after', chalk.magenta(time)
);
});
gulp.on('task_err', function (e) {
var msg = formatError(e);
var time = prettyTime(e.hrDuration);
gutil.log(
'\'' + chalk.cyan(e.task) + '\'',
chalk.red('errored after'),
chalk.magenta(time)
);
gutil.log(msg);
});
gulp.on('task_not_found', function (err) {
gutil.log(
chalk.red('Task \'' + err.task + '\' is not in your gulpfile')
);
gutil.log('Please check the documentation for proper gulpfile formatting');
process.exit(1);
});
}
function installService() {
var Service = require('node-windows').Service;
var contents = fs.readFileSync(path.join(process.cwd(), 'master.json' ));
var masterJson = JSON.parse(contents);
console.log(JSON.stringify(masterJson,null,1));
// Create a new service object
var svc = new Service({
name: masterJson.name,
description: masterJson.description || masterJson.name,
script: path.join(process.cwd(), 'app.js' ),
flags: "--max-old-space-size=2000"
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
console.log("Service installed");
});
svc.install();
}
function uninstallService() {
var Service = require('node-windows').Service;
var contents = fs.readFileSync(path.join(process.cwd(), 'master.json' ));
var masterJson = JSON.parse(contents);
console.log(JSON.stringify(masterJson,null,1));
// Create a new service object
var svc = new Service({
name: masterJson.name,
description: masterJson.description || masterJson.name,
script: path.join(process.cwd(), 'app.js' )
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('uninstall',function(){
console.log("Service uninstalled");
});
svc.uninstall();
}