forked from finn-no/maven-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (121 loc) · 4.32 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
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 config = {
buildDir: 'dist',
finalName: '{name}',
type: 'war',
fileEncoding: 'utf-8'
},
validateConfig = defineOpts({
groupId : 'string - the Maven group id.',
buildDir : '?|string - build directory. default "' + config.buildDir + '".',
finalName : '?|string - the final name of the file created when the built project is packaged. default "' +
config.finalName + '"',
type : '?|string - "jar" or "war". default "' + config.type + '".',
fileEncoding : '?|string - valid file encoding. default "' + config.fileEncoding + '"'
}),
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'
}),
pkg = JSON.parse(fs.readFileSync('./package.json', config.fileEncoding));
function filterConfig () {
Object.keys(config).forEach(function (key) {
var value = config[key];
if (typeof value != 'string') { return; }
config[key] = value.replace(/{([^}]+)}/g, function (org, key) {
if (pkg[key] === undefined) { return org; }
return pkg[key];
});
});
}
function warPath () {
return path.join(config.buildDir, config.finalName + '.' + config.type);
}
function mvnArgs (repoId, isSnapshot) {
var args = {
packaging : config.type,
file : warPath(),
groupId : config.groupId,
artifactId : pkg.name,
version : pkg.version
};
if (repoId) {
var repos = config.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;
}
}
if (isSnapshot) {
args.version = semver.inc(args.version, 'patch') + '-SNAPSHOT';
}
return Object.keys(args).reduce(function (arr, key) {
return arr.concat('-D' + key + '=' + args[key]);
}, []);
}
function check (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);
}
process.exit(1);
}
}
function command (cmd, done) {
console.log('Executing command: ' + cmd);
exec(cmd, function (err, stdout, stderr) {
check(err, stdout, stderr);
if (done) { done(err, stdout, stderr); }
});
}
function mvn (args, repoId, isSnapshot, done) {
command('mvn ' + args.concat(mvnArgs(repoId, isSnapshot)).join(' '), done);
}
var maven = {
config: function (c) {
validateConfig(c);
extend(config, c);
filterConfig();
},
package: function (done) {
var war = new JSZip();
walk.walkSync(config.buildDir, function (base, file, stat) {
if (stat.isDirectory() || file.indexOf(config.finalName + '.' + config.type) === 0) {
return;
}
var filePath = path.join(base, file);
var data = fs.readFileSync(filePath, {'encoding': config.fileEncoding});
war.file(path.relative(config.buildDir, filePath), data);
});
var buffer = war.generate({type:"nodebuffer", compression:'DEFLATE'});
fs.writeFileSync(warPath(), buffer);
if (done) { done(); }
},
install: function (done) {
this.package();
mvn(['install:install-file'], null, true, done);
},
deploy: function (repoId, isSnapshot, done) {
if (typeof isSnapshot == 'function') { done = isSnapshot; isSnapshot = false; }
validateRepos(config);
config.repositories.forEach(validateRepo);
this.package();
mvn(['deploy:deploy-file'], repoId, isSnapshot, done);
}
};
module.exports = maven;