This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·117 lines (99 loc) · 3.43 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
/**
* Convert config object to string with xml
* @param config
* @returns {string}
*/
function makeConfigString(config) {
const serverName = config.serverName; // eslint-disable-line prefer-destructuring
const autoUpload = config.autoUpload || false;
const autoUploadExternalChanges = config.autoUploadExternalChanges || false;
const notifyRemoteChanges = config.notifyRemoteChanges || true;
const excludedLocal = [];
const excludedServer = [];
const mappings = [];
if (config.excludedLocal) {
config.excludedLocal.forEach((item) => {
excludedLocal.push(`<excludedPath local="true" path="${item}" />`);
});
}
if (config.excludedServer) {
config.excludedServer.forEach((item) => {
excludedServer.push(`<excludedPath path="${item}" />`);
});
}
if (config.mappings) {
config.mappings.forEach((item) => {
mappings.push(`<mapping deploy="${item.deploy}" local="${item.local}" web="${item.web || ''}" />`);
});
}
// 0.1.1 deprecation warning
if (config.serverPath && config.localPath) {
console.warn('Options "serverPath" and "localPath" is deprecated! Use mappings options.');
mappings.push(`<mapping deploy="${config.serverPath}" local="${config.localPath}" web="" />`);
}
if (!serverName) {
console.warn('Option "serverName" is not specified.');
}
if (mappings.length === 0) {
console.warn('Mappings are not specified.');
}
return `<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" promptOnRemoteOverwrite="CHECK_CONTENT" serverName="${serverName}" autoUpload="${autoUpload}" autoUploadExternalChanges="${autoUploadExternalChanges}" notifyRemoteChanges="${notifyRemoteChanges}">
<serverData>
<paths name="${serverName}">
<serverdata>
<mappings>
${mappings.join('\n')}
</mappings>
<excludedPaths>
${excludedLocal.join('\n')}
${excludedServer.join('\n')}
</excludedPaths>
</serverdata>
</paths>
</serverData>
</component>
</project>
`;
}
/**
* Run
* idea-deploy-config env [config]
*/
function cli() {
const fs = require('fs');
const args = process.argv.slice(2);
const configSrc = args[1] || `${process.cwd()}/deploy-config.json`;
let env = args[0];
if (env === 'init') {
console.log('Create empty deployment configuration file');
fs.createReadStream(`${__dirname}/deploy-config-example.json`).pipe(fs.createWriteStream(`${process.cwd()}/deploy-config.json`));
return;
}
fs.readFile(configSrc, 'utf8', (err, data) => {
if (err) throw err;
let config = JSON.parse(data);
if (!env) {
env = Object.keys(config)[0]; // eslint-disable-line prefer-destructuring
console.log(`No environment set, use "${env}"`);
config = config[env];
} else {
config = config[env];
}
if (!config) {
throw new Error(`Configuration for ${env} could not be found`);
}
const configString = makeConfigString(config);
const configFolder = `${process.cwd()}/.idea`;
const configFile = `${configFolder}/deployment.xml`;
fs.stat(configFolder, (err) => {
if (err) throw err;
fs.writeFile(configFile, configString, { flag: 'w' }, (err) => {
if (err) throw err;
console.log(`IDE deployment configuration set for "${env}"`);
});
});
});
}
exports.cli = cli;