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
/
Copy pathindex.js
136 lines (119 loc) · 4.58 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
const fs = require('fs');
const shortid = require('shortid');
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 processId = 'Process_' + shortid();
const processFileName = process;
const extensionsFileName = process.replace('.bpmn20.xml', '-extensions.json');
updateProcessXML(processFileName, processId);
updateProcessExtensionsJSON(extensionsFileName, processId);
}
});
}
function updateProcessXML(processName, processId) {
try {
const processContent = fs.readFileSync(path.join(getExtractedProjectPath(), 'processes', processName));
const xmlParsedFile = JSON.parse(parser.toJson(processContent, { reversible: true }));
const xmlNameSpace = extractNameSpace(xmlParsedFile);
xmlParsedFile[`${xmlNameSpace}definitions`].id = xmlParsedFile[`${xmlNameSpace}definitions`][`${xmlNameSpace}process`].id.replace('process', 'model');
xmlParsedFile[`${xmlNameSpace}definitions`][`${xmlNameSpace}process`].id = processId;
xmlParsedFile[`${xmlNameSpace}definitions`]['bpmndi:BPMNDiagram']['bpmndi:BPMNPlane'].bpmnElement = processId;
const updatedXML = parser.toXml(JSON.stringify(xmlParsedFile), { sanitize: true});
try {
fs.writeFileSync(path.join(getExtractedProjectPath(), 'processes', processName), updatedXML);
console.log('\t-> Process file: ' + processName + ' successfully updated');
} catch (error) {
console.log('\t-> Failed to update file: ' + processName );
console.log(error);
}
} 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 updateProcessExtensionsJSON(processExtensionsName, processId) {
try {
const processContent = fs.readFileSync(getExtractedProjectPath() + '/processes/' + processExtensionsName);
const processExtensions = JSON.parse(processContent);
const oldProcessExtensions = processExtensions.extensions;
processExtensions.extensions = {
[processId]: oldProcessExtensions
}
const updatedProcessExtensions = JSON.stringify(processExtensions);
try {
fs.writeFileSync(path.join(getExtractedProjectPath(), 'processes', processExtensionsName), updatedProcessExtensions);
console.log('\t-> Extensions file: ' + processExtensionsName + ' successfully updated');
} catch (error) {
console.log('\t-> Failed to update file: ' + processExtensionsName );
console.log(error);
}
} catch (error) {
console.log(error);
}
}
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()}`);
updateProject();
console.log(`\n3. Compressing folder`);
compressNewProject();
console.log(`\n4. Cleaning up`);
removeProjectFolder();
console.log(`\nProject updated successfully!`);
}
initScript();