This repository has been archived by the owner on Dec 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
181 lines (151 loc) · 5.78 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
const fs = require('fs');
const parser = require('xml2json');
const path = require('path');
const AdmZip = require('adm-zip');
const rimraf = require("rimraf");
let project;
function updateProject() {
const processesPath = path.join(getExtractedProjectPath(), 'processes');
const processes = fs.readdirSync(processesPath);
processes.forEach(process => {
if (process && path.extname(process) === '.xml') {
const processFileName = process;
const extensionsFileName = process.replace('.bpmn20.xml',
'-extensions.json');
updateVariableMappingRepresentation(processFileName,
extensionsFileName);
}
});
}
function retrieveMappableEntitiesIds(xmlParsedFile, xmlNameSpace) {
xmlProcessElements = xmlParsedFile[`${xmlNameSpace}definitions`][`${xmlNameSpace}process`];
const mappableEntityTypes = ['userTask', 'serviceTask', 'callActivity'];
const entityIds = [];
for (let [key] of Object.entries(xmlProcessElements)) {
var entity = `${key}`.replace(xmlNameSpace, '');
if (mappableEntityTypes.includes(entity)) {
//entities of the same kind get parsed together as an array
if (Array.isArray(xmlProcessElements[key])) {
xmlProcessElements[key].forEach((item) => {
entityIds.push(item.id);
});
} else {
entityIds.push(xmlProcessElements[key].id);
}
}
}
return entityIds;
}
function updateVariableMappingRepresentation(processName, extensionsFileName) {
try {
const processContent = fs.readFileSync(
path.join(getExtractedProjectPath(), 'processes', processName));
const xmlParsedFile = JSON.parse(
parser.toJson(processContent, {reversible: true}));
const xmlNameSpace = extractNameSpace(xmlParsedFile);
const processId = xmlParsedFile[`${xmlNameSpace}definitions`][`${xmlNameSpace}process`].id;
const entityIds = retrieveMappableEntitiesIds(xmlParsedFile,
xmlNameSpace);
updateProcessExtensions(extensionsFileName, processId, entityIds);
} catch (error) {
console.log(error);
}
}
function extractNameSpace(xmlParsedFile) {
let xmlNameSpaceArray = Object.keys(xmlParsedFile)[0].split(':');
let xmlNameSpace;
if (xmlNameSpaceArray.length == 2) {
xmlNameSpace = xmlNameSpaceArray[0] + ':';
} else {
xmlNameSpace = '';
}
return xmlNameSpace;
}
function updateProcessExtensions(processExtensionsName, processId, entityIds) {
try {
const fileContent = fs.readFileSync(getExtractedProjectPath() + '/processes/' + processExtensionsName);
const processExtensions = JSON.parse(fileContent);
console.log('\n\t* Update of file ' + "'" + processExtensionsName + "'" + ' started');
console.log('\n\t\t- Current variable mapping representation: ', processExtensions.extensions[processId].mappings);
//make the 'Send all variables' cases explicit
entityIds.forEach((entityId) => {
if (processExtensions.extensions[processId].mappings.hasOwnProperty(
entityId) === false) {
processExtensions.extensions[processId].mappings[entityId] = {
"mappingType": "MAP_ALL"
};
}
});
//make the 'Don't send variables' use case implicit
const mappingToDelete = {
"inputs": {},
"outputs": {}
};
allMappings = processExtensions.extensions[processId].mappings;
for (let [key] of Object.entries(allMappings)) {
if (JSON.stringify(allMappings[key]) === JSON.stringify(mappingToDelete)) {
delete allMappings[key];
}
}
console.log('\n\t\t- Updated variable mapping representation: ', allMappings);
const updatedProcessExtensions = JSON.stringify(processExtensions);
try {
fs.writeFileSync(path.join(getExtractedProjectPath(), 'processes', processExtensionsName), updatedProcessExtensions);
console.log('\n\t* Update of file ' + "'" + processExtensionsName + "'" + ' successfully completed');
} catch (error) {
console.log('\n\t* Failed to update file: ' + processExtensionsName);
console.log(error);
}
} catch (e) {
console.log(e);
}
}
function extractProject() {
if (path.extname(project) === '.zip') {
const zip = new AdmZip(project);
try {
zip.extractAllTo(getExtractedProjectPath(), true);
} catch (error) {
console.log(error);
}
}
}
function compressNewProject() {
const zip = new AdmZip();
try {
zip.addLocalFolder(getExtractedProjectPath());
} catch (error) {
console.log(error);
}
const updatedProjectName = getProjectName() + '_updated.zip';
try {
zip.writeZip(path.join(getProjectDirectoryPath(), updatedProjectName));
} catch (error) {
console.log(error);
}
}
function removeProjectFolder() {
rimraf.sync(getExtractedProjectPath());
}
function getProjectName() {
return path.basename(project).replace('.zip', '');
}
function getProjectDirectoryPath() {
return project.substring(0, project.lastIndexOf('/'));
}
function getExtractedProjectPath() {
return project.replace('.zip', '');
}
function initScript() {
project = process.argv[2];
console.log(`\n1. Extracting folder: ${project}`);
extractProject();
console.log(`\n2. Updating ${getProjectName()} project`);
updateProject();
console.log(`\n3. Compressing folder`);
compressNewProject();
console.log(`\n4. Cleaning up`);
removeProjectFolder();
console.log(`\nProject updated successfully!`);
}
initScript();