-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathplugin.js
70 lines (58 loc) · 2.55 KB
/
plugin.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
'use strict';
let fs = require('fs');
/* eslint-disable no-console */
const log = console.log;
const logDebug = msg => {
if (process.env.PLUGIN_DEBUG) console.debug(msg);
};
const logError = console.error;
/* eslint-enable no-console */
const generateServer = server =>
`<server><id>${server.id}</id><username>${server.username}</username><password>${server.password}</password></server>`;
const generateRepository = type => repo =>
`<${type}><id>${repo.id}</id><name>${repo.name}</name><url>${repo.url}</url><layout>${repo.layout}</layout></${type}>`;
const generateProfile = profile =>
`<profile><id>${profile.id}</id><repositories>${profile.hasOwnProperty('repositories') ? profile.repositories.map(generateRepository('repository')).join('') : ''}</repositories><pluginRepositories>${profile.hasOwnProperty('plugin_repositories') ? profile.plugin_repositories
.map(generateRepository('pluginRepository')).join('') : ''}</pluginRepositories></profile>`;
const generateActiveProfile = profile =>
`<activeProfile>${profile}</activeProfile>`;
const parseParam = param => {
let config = [];
let env = process.env[`PLUGIN_${param.toUpperCase()}`];
if (!env) env = process.env[`MAVEN_${param.toUpperCase()}`];
if (env) {
if (param === 'active_profiles') {
config = env.split(',');
} else {
try {
config = JSON.parse(env);
} catch (ignore) {
logError(`-- Error: cannot parse ${param} data`);
process.exit(1);
}
}
}
logDebug(`-- Found ${param}: ${config.length}`);
return config;
};
module.exports = {
init: () => {
log('-- Preparing Maven for authentication...');
const config = ['servers', 'profiles', 'active_profiles'].reduce((
acc,
val
) => {
acc[val] = parseParam(val);
return acc;
}, {});
const data = `<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><localRepository>${process.env.PWD}/.m2</localRepository><servers>${config.servers.map(generateServer).join('')}</servers><profiles>${config.profiles.map(generateProfile).join('')}</profiles><activeProfiles>${config.active_profiles.map(generateActiveProfile).join('')}</activeProfiles></settings>`;
try {
fs.writeFileSync('settings.xml', data);
log('-- Maven authentication done!');
} catch (ignore) {
logError('-- Error: cannot write file');
return process.exit(1);
}
return process.exit(0);
}
};