-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (118 loc) · 3.28 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
'use strict';
var JAVA_SOURCES = 'src/main/java';
var JAVA_TEST_SOURCES = 'src/test/java';
var JAVA_RESOURCES = 'src/main/resources';
var JAVA_TEST_RESOURCES = 'src/test/resources';
var JAVA_CLASSES = 'target/classes';
var color = require('kleur');
var spawn = require('child_process').spawn;
var path = require('path');
var chokidar = require('chokidar'),
cp = require('cp'),
mkdirp = require('mkdirp');
var prefix = '[AMVN]';
function log(msg = '', ...args) {
console.log(color.yellow(`${prefix} ${msg}`), ...args);
}
function logError(msg, ...args) {
if (msg instanceof Error) {
console.error(msg, ...args);
} else {
console.error(color.red(`${prefix} ${msg}`), ...args);
}
}
function now() {
return new Date().getTime();
}
function AwesomeMaven(mvnPath, options) {
if (!(this instanceof AwesomeMaven)) {
return new AwesomeMaven(mvnPath, options);
}
function isTest() {
return options.mvnArgs.some(arg => arg.includes('test'));
}
var mvn;
var starting = 0;
function runMaven(cleanStart) {
starting = starting || now();
if (!cleanStart) {
log('restarting mvn...');
}
mvn = spawn(mvnPath, options.mvnArgs, { stdio: 'inherit' });
if (cleanStart) {
mvn.on('exit', function(code) {
if (code && code !== 143) {
logError('mvn exited unexpectedly (code=%s)', code);
process.exit(1);
}
});
}
starting = 0;
}
function reloadMaven() {
// already starting
if (starting) {
return;
}
starting = now();
var timer = setInterval(function() {
var killed = !mvn;
try {
options.verbose && log('sending KILL to mvn...');
killed || process.kill(mvn.pid);
} catch (e) {
options.verbose && log('received <' + e.message + '>. Already dead?');
killed = true;
}
if (!killed) {
return;
}
// clear timer
clearInterval(timer);
// restart
runMaven();
}, 500);
}
function copyResource(srcPath) {
var relativePath = path.relative(JAVA_RESOURCES, srcPath);
var targetPath = path.join(JAVA_CLASSES, relativePath);
var targetDirectory = path.dirname(targetPath);
log('%s changed, updating in %s', srcPath, JAVA_CLASSES);
mkdirp.sync(targetDirectory);
cp.sync(srcPath, targetPath);
}
function watchResources() {
var watcher = chokidar.watch([], { usePolling: options.poll });
watcher.on('change', copyResource);
log('watching %s for changes...', JAVA_RESOURCES);
watcher.add(JAVA_RESOURCES + '/**/*');
if (isTest()) {
log('watching %s for changes...', JAVA_TEST_RESOURCES);
watcher.add(JAVA_TEST_RESOURCES + '/**/*');
}
return watcher;
}
function watchSources() {
var watcher = chokidar.watch([], { usePolling: options.poll });
watcher.on('change', reloadMaven);
log('watching %s for changes...', JAVA_SOURCES);
watcher.add(JAVA_SOURCES + '/**/*');
if (isTest()) {
log('watching %s for changes...', JAVA_TEST_SOURCES);
watcher.add(JAVA_TEST_SOURCES + '/**/*');
}
return watcher;
}
if (options.watch) {
watchResources();
}
if (options.reload) {
watchSources();
}
runMaven(true);
}
module.exports = {
log,
logError,
AwesomeMaven
};