forked from thingworx-field-work/BMCoreUIWidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGulpfile.js
250 lines (210 loc) · 9.32 KB
/
Gulpfile.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// @ts-check
const path = require('path');
const fs = require('fs');
const xml2js = require('xml2js');
const del = require('del');
const deleteEmpty = require('delete-empty');
const { series, parallel, src, dest } = require('gulp');
const zip = require('gulp-zip');
const concat = require('gulp-concat');
const terser = require('gulp-terser');
const babel = require('gulp-babel');
const { spawn } = require('child_process');
const request = require('request');
const packageJson = require('./package.json');
let zipName = `${packageJson.packageName}-min-${packageJson.version}.zip`;
const subProjects = {
'BMCoreUI': '../BMCoreUI',
'BMCollectionView': '../BMCollectionViewTypescript',
'BMCodeHost': '../BMCodeHost',
'BMMenu': '../BMMenu',
'BMPresentationController': '../BMPresentationController',
'BMView': '../BMView'
}
function zipNameDebug(cb) {
zipName = `${packageJson.packageName}-dev-${packageJson.version}.zip`;
cb();
}
/**
* Cleans the build directory.
*/
async function cleanBuildDir() {
await del('build');
await del('zip');
fs.mkdirSync('build');
fs.mkdirSync('build/ui');
fs.mkdirSync('build/ui/BMCoreUIWidgets');
fs.mkdirSync('zip');
}
/**
* Builds all subprojects.
*/
async function buildAll() {
await buildAllWithCommand('build');
}
/**
* Builds all subprojects without optimization.
*/
async function buildAllDebug() {
await buildAllWithCommand('buildDebug');
}
async function buildAllWithCommand(command) {
// Build core ui first, if specified
for (const key in subProjects) {
if (key == 'BMCoreUI') {
console.log(`Building sub-project ${key}...`);
await new Promise(resolve => {
spawn('npm', ['run', command], {cwd: subProjects[key], stdio: 'inherit', shell: true}).on('close', resolve);
});
break;
}
}
// Then build everything else in parallel
const promises = [];
for (const key in subProjects) {
if (key == 'BMCoreUI') continue;
console.log(`Building sub-project ${key}...`);
promises.push(new Promise(resolve => {
spawn('npm', ['run', command], {cwd: subProjects[key], stdio: 'inherit', shell: true}).on('close', resolve);
}));
}
await Promise.all(promises);
}
/**
* Copies the files from all subprojects into the build folder.
* Also copies the metadata.xml file into the build folder.
*/
async function copyAll() {
const folders = Object.keys(subProjects).map(key => `${subProjects[key]}/build/ui/${key}/**/*`);
await new Promise(resolve => src(folders).pipe(dest('build/ui/BMCoreUIWidgets')).on('end', resolve));
await new Promise(resolve => src('metadata.xml').pipe(dest('build/').on('end', resolve)));
}
/**
* Merges the subproject files into a single file per type. The order is determined by the key order in the `subProjects` map and then the file
* order as defined in the metadata xml file.
*/
async function mergeAll() {
const metadataKeys = Object.keys(subProjects);
const metadataXMLs = await Promise.all(metadataKeys.map(key => `${subProjects[key]}/build/metadata.xml`).map(file => fs.readFileSync(file, 'utf8')).map(xml => xml2js.parseStringPromise(xml)));
const metadatas = metadataKeys.map((k, i) => ({key: k, xml: metadataXMLs[i]}));
const IDEJS = [], IDECSS = [], runtimeJS = [], runtimeCSS = [];
// Run through each of the metadata defined files and add their contents to the specific groups
for (const metadata of metadatas) {
for (const fileResource of metadata.xml.Entities.Widgets[0].Widget[0].UIResources[0].FileResource) {
/** @type {string} */ let filename = fileResource.$.file;
// Webpack bundles are renamed upon zipping, so they don't match their metadata names in the build folder
if (filename.endsWith('ide.bundle.js')) filename = 'widgetIde.bundle.js';
if (filename.endsWith('runtime.bundle.js')) filename = 'widgetRuntime.bundle.js';
fileResource.content = fs.readFileSync(`${subProjects[metadata.key]}/build/ui/${metadata.key}/${filename}`, 'utf8');
// The file will have been moved into the build folder by the copy all action, but it is no longer needed
await del(`build/ui/BMCoreUIWidgets/${filename}`);
// TODO: Replace references to extension specific folders
const extensionPackageName = metadata.xml.Entities.ExtensionPackages[0].ExtensionPackage[0].$.name;
fileResource.content = fileResource.content.replace(new RegExp(`Common\\/extensions\\/${extensionPackageName}\\/ui\\/${metadata.key}`, 'g'), 'Common/extensions/BMCoreUIWidgets/ui/BMCoreUIWidgets')
// Note that while the type is fixed, a file can be both a development and runtime resource, but only has a single type
if (fileResource.$.isDevelopment == 'true') {
if (fileResource.$.type == 'CSS') {
IDECSS.push(fileResource.content);
}
else {
IDEJS.push(fileResource.content);
}
}
if (fileResource.$.isRuntime == 'true') {
if (fileResource.$.type == 'CSS') {
runtimeCSS.push(fileResource.content);
}
else {
runtimeJS.push(fileResource.content);
}
}
}
}
// The JS resources are wrapped in an IIFE
IDEJS.unshift('\n;(function() {\n');
IDEJS.push('\n})();\n');
runtimeJS.unshift('\n;(function() {\n');
runtimeJS.push('\n})();\n');
// Combine and write out the files
fs.writeFileSync('build/ui/BMCoreUIWidgets/ide.css', IDECSS.join('\n'), 'utf8');
fs.writeFileSync('build/ui/BMCoreUIWidgets/runtime.css', runtimeCSS.join('\n'), 'utf8');
fs.writeFileSync('build/ui/BMCoreUIWidgets/ide.js', IDEJS.join('\n'), 'utf8');
fs.writeFileSync('build/ui/BMCoreUIWidgets/runtime.js', runtimeJS.join('\n'), 'utf8');
}
async function createZip() {
// Create a zip of the build directory
const zipStream = src('build/**')
.pipe(zip(zipName))
.pipe(dest('zip'));
await new Promise(resolve => zipStream.on('end', resolve));
}
async function upload() {
const host = packageJson.thingworxServer;
const user = packageJson.thingworxUser;
const password = packageJson.thingworxPassword;
return new Promise((resolve, reject) => {
request.post({
url: `${host}/Thingworx/Subsystems/PlatformSubsystem/Services/DeleteExtensionPackage`,
headers: {
'X-XSRF-TOKEN': 'TWX-XSRF-TOKEN-VALUE',
Accept: 'application/json',
'Content-Type': 'application/json',
'X-THINGWORX-SESSION': 'true'
},
body: {packageName: packageJson.packageName},
json: true
},
function (err, httpResponse, body) {
// load the file from the zip folder
let formData = {
file: fs.createReadStream(
path.join('zip', zipName)
)
};
// POST request to the ExtensionPackageUploader servlet
request
.post(
{
url: `${host}/Thingworx/ExtensionPackageUploader?purpose=import`,
headers: {
'X-XSRF-TOKEN': 'TWX-XSRF-TOKEN-VALUE'
},
formData: formData
},
function (err, httpResponse, body) {
if (err) {
console.error("Failed to upload widget to thingworx");
reject(err);
return;
}
if (httpResponse.statusCode != 200) {
reject(`Failed to upload widget to thingworx. We got status code ${httpResponse.statusCode} (${httpResponse.statusMessage})
body:
${httpResponse.body}`);
} else {
console.log(`Uploaded widget version ${packageJson.version} to Thingworx!`);
resolve();
}
}
)
.auth(user, password);
if (err) {
console.error("Failed to delete widget from thingworx");
return;
}
if (httpResponse.statusCode != 200) {
console.log(`Failed to delete widget from thingworx. We got status code ${httpResponse.statusCode} (${httpResponse.statusMessage})
body:
${httpResponse.body}`);
} else {
console.log(`Deleted previous version of ${packageJson.packageName} from Thingworx!`);
}
})
.auth(user, password);
})
}
exports.default = series(cleanBuildDir, buildAll, copyAll, mergeAll, createZip);
exports.buildDebug = series(zipNameDebug, cleanBuildDir, buildAllDebug, copyAll, mergeAll, createZip);
exports.merge = series(cleanBuildDir, copyAll, mergeAll, createZip);
exports.upload = series(cleanBuildDir, buildAll, copyAll, mergeAll, createZip, upload);
exports.uploadDebug = series(zipNameDebug, cleanBuildDir, buildAllDebug, copyAll, mergeAll, createZip, upload);