forked from finn-no/maven-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
251 lines (209 loc) · 8.18 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var fs = require('fs');
var path = require('path');
var walk = require('fs-walk');
var JSZip = require('jszip');
var extend = require('util-extend');
var exec = require('child_process').exec;
var defineOpts = require('define-options');
var semver = require('semver');
var isBinaryFile = require('isbinaryfile');
var validateConfig, validateRepos, validateRepo, userConfig;
const DEFAULT_CONFIG = {
artifactId: '{name}',
buildDir: 'dist',
finalName: '{name}',
type: 'war',
fileEncoding: 'utf-8',
version: '{version}',
generatePom: true,
semver: null
};
validateConfig = defineOpts({
groupId : 'string - the Maven group id.',
artifactId : '?|string - the Maven artifact id. default "' + DEFAULT_CONFIG.artifactId + '".',
classifier : '?|string - the Maven optional classifier.',
buildDir : '?|string - build directory. default "' + DEFAULT_CONFIG.buildDir + '".',
finalName : '?|string - the final name of the file created when the built project is packaged. default "' +
DEFAULT_CONFIG.finalName + '"',
type : '?|string - "jar" or "war". default "' + DEFAULT_CONFIG.type + '".',
fileEncoding : '?|string - valid file encoding. default "' + DEFAULT_CONFIG.fileEncoding + '"',
generatePom : '?|boolean - "true" or "false". default "' + DEFAULT_CONFIG.generatePom + '".',
pomFile : '?|string - filename of an existing pom.xml to use, should be used with generatePom set to "false".'
});
validateRepos = defineOpts({
repositories : 'object[] - array of repositories, each with id and url to a Maven repository'
});
validateRepo = defineOpts({
id : 'string - the Maven repository id',
url : 'string - URL to the Maven repository'
});
function convertPathIntoUnixLike (path) {
return path.replace(/\\/g, '/');
}
function readPackageJSON (encoding) {
return JSON.parse(fs.readFileSync('package.json', encoding));
}
function filterConfig (configTmpl, pkg) {
// create a config object from the config template
// replace {key} with the key's value in package.json
var obj = extend({}, configTmpl);
Object.keys(obj).forEach(function (key) {
var value = obj[key];
if (typeof value != 'string') { return; }
obj[key] = value.replace(/{([^}]+)}/g, function (org, key) {
if (pkg[key] === undefined) { return org; }
return pkg[key];
});
});
return obj;
}
function archivePath (isSnapshot) {
var conf = getConfig(isSnapshot);
return path.join(conf.buildDir, conf.finalName + '.' + conf.type);
}
function mvnArgs (repoId, isSnapshot, file) {
var conf = getConfig(isSnapshot);
var args = {
packaging : conf.type,
file : file,
groupId : conf.groupId,
artifactId : conf.artifactId,
classifier : conf.classifier,
version : conf.version,
generatePom : conf.generatePom,
pomFile : conf.pomFile
};
if (repoId) {
var repos = conf.repositories, l = repos.length;
for (var i=0; i<l; i++) {
if (repos[i].id !== repoId) { continue; }
args.repositoryId = repos[i].id;
args.url = repos[i].url;
break;
}
}
return Object.keys(args).filter(function (key) {
return typeof args[key] !== 'undefined';
}).reduce(function (arr, key) {
return arr.concat('"-D' + key + '=' + args[key] + '"');
}, []);
}
function check (cmd, err, stdout, stderr) {
if (err) {
if (err.code === 'ENOENT') {
console.error(cmd + ' command not found. Do you have it in your PATH?');
} else {
console.error(stdout);
console.error(stderr);
}
exit();
}
}
function command (cmd, done) {
console.log('Executing command: ' + cmd);
exec(cmd, function (err, stdout, stderr) {
check(cmd, err, stdout, stderr);
if (done) { done(err, stdout, stderr); }
});
}
function getConfig (isSnapshot) {
var configTmpl = extend({}, DEFAULT_CONFIG);
if (userConfig) { configTmpl = extend(configTmpl, userConfig); }
var pkg = readPackageJSON(configTmpl.fileEncoding);
if (!configTmpl.semver && isSnapshot) {
//for backwards compat inc 'patch' version when making snapshots
configTmpl.semver = 'patch';
}
if (configTmpl.semver) {
if (Array.isArray(configTmpl.semver)) {
//interpret semver config as params to semver.inc
pkg.version = semver.inc.apply(null, [pkg.version].concat(configTmpl.semver));
}
else if (semver.valid(configTmpl.semver)) {
//semver is a valid version to use
pkg.version = configTmpl.semver
}
else {
//interpret semver as the second option to semver.inc
pkg.version = semver.inc(pkg.version, configTmpl.semver);
}
}
//convert prerelease components into proper qualifiers
var components = semver.prerelease(pkg.version);
if (components) {
//check if we should drop the last build number component
if (isSnapshot && isFinite(components[components.length - 1])) {
components.pop();
}
//replace the '.' between prerelease components with '-' to be proper qualifiers
if (components.length > 0) {
pkg.version = [semver.major(pkg.version), semver.minor(pkg.version), semver.patch(pkg.version)].join('.') + '-' + components.join('-');
}
else {
//should only get here if we popped off the last component, so we don't want to put an extra '-'
pkg.version = [semver.major(pkg.version), semver.minor(pkg.version), semver.patch(pkg.version)].join('.')
}
}
//pkg version should be modified above but still need to add snapshot when appropriate
if (isSnapshot) {
pkg.version += '-SNAPSHOT';
}
return filterConfig(configTmpl, pkg);
}
function package (isSnapshot, done) {
if (typeof isSnapshot == 'function') { done = isSnapshot; isSnapshot = false; }
var archive = new JSZip();
var conf = getConfig(isSnapshot);
walk.walkSync(conf.buildDir, function (base, file, stat) {
if (stat.isDirectory() || file.indexOf(conf.finalName + '.' + conf.type) === 0) {
return;
}
var filePath = path.join(base, file);
var data;
if(isBinaryFile.sync(filePath)) {
data = fs.readFileSync(filePath);
} else {
data = fs.readFileSync(filePath, {'encoding': conf.fileEncoding});
}
archive.file(convertPathIntoUnixLike(path.relative(conf.buildDir, filePath)), data, {createFolders: true});
});
var buffer = archive.generate({type:'nodebuffer', compression:'DEFLATE'});
var arPath = archivePath(isSnapshot);
console.log('archive path', arPath);
fs.writeFileSync(arPath, buffer);
if (done) { done(); }
return arPath;
}
function mvn (args, repoId, isSnapshot, file, done) {
if (!file) { file = package(isSnapshot); }
// check if file exists
fs.statSync(file);
command('mvn -B ' + args.concat(mvnArgs(repoId, isSnapshot, file)).join(' '), done);
}
function exit(){
process.exit(1);
}
function setUserConfig (_userConfig) {
validateConfig(_userConfig);
userConfig = _userConfig;
}
var maven = {
config: setUserConfig,
package: package,
install: function (file, done) {
if (typeof file == 'function') { done = file; file = undefined; }
mvn(['install:install-file'], null, true, file, done);
},
deploy: function (repoId, file, isSnapshot, done) {
var conf = getConfig();
if (file && typeof file != 'string') { done = isSnapshot; isSnapshot = file; file = undefined; }
if (isSnapshot && typeof isSnapshot != 'boolean') { done = isSnapshot; isSnapshot = false; }
validateRepos(conf);
if (conf.repositories.length === 0) {
throw new Error('Maven repositories have to include at least one repository with ‘id’ and ‘url’.');
}
conf.repositories.forEach(validateRepo);
mvn(['deploy:deploy-file'], repoId, isSnapshot, file, done);
}
};
module.exports = maven;