-
Notifications
You must be signed in to change notification settings - Fork 7
/
shared.js
206 lines (189 loc) · 7.58 KB
/
shared.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
const fetch = require('node-fetch')
const dotenv = require('dotenv')
const fs = require('fs')
const path = require('path')
const Zip = require('adm-zip');
function readConfig() {
const env = dotenv.config().parsed;
const configStr = fs.readFileSync('config.json', 'utf-8');
const config = JSON.parse(configStr);
config.authToken = env['AUTH_TOKEN'];
// Add defaults if they don't exist
config.csp = config.csp || {};
config.csp['style-src'] = config.csp['style-src'] || [];
config.csp['connect-src'] = config.csp['connect-src'] || [];
config.csp.patch_preload_bundles = config.csp.patch_preload_bundles || false;
config.one_page = config.one_page || {};
config.one_page.patch_xhr_out = config.one_page.patch_xhr_out || false;
config.one_page.inline_game_scripts = config.one_page.inline_game_scripts || false;
config.one_page.mraid_support = config.one_page.mraid_support || false;
config.one_page.snapchat_cta = config.one_page.snapchat_cta || false;
// Mon 17 May 2021: Backwards compatibility when this used to be a boolean
// and convert to an object
var onePageExternFiles = config.one_page.extern_files;
if (onePageExternFiles) {
if (typeof onePageExternFiles === 'boolean') {
onePageExternFiles = {
enabled: onePageExternFiles
}
}
}
config.one_page.compress_engine = config.one_page.compress_engine || '';
onePageExternFiles = onePageExternFiles || { enabled: false };
onePageExternFiles.folder_name = onePageExternFiles.folder_name || '';
onePageExternFiles.external_url_prefix = onePageExternFiles.external_url_prefix || '';
config.one_page.extern_files = onePageExternFiles;
return config;
}
function pollJob(config, jobId) {
var self = this;
return new Promise((resolve, reject) => {
console.log("↪️ Polling job ", jobId)
fetch('https://playcanvas.com/api/jobs/' + jobId, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + config.authToken
}
})
.then(res => res.json())
.then((json) => {
if (json.status == "complete") {
console.log("✔️ Job complete!",)
resolve(json.data)
} else if (json.status == "error") {
console.log(" job error ", json.messages)
reject(new Error(json.messages.join(';')))
} else if (json.status == "running") {
console.log(" job still running");
return waitAndRetry(config, jobId, resolve);
}
})
});
}
function waitAndRetry(config, jobId, callback) {
return new Promise(resolve => {
console.log(" will wait 1s and then retry")
sleep(1000)
.then(() => pollJob(config, jobId))
.then(callback); // nested promises anyone?
})
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function downloadProject(config, directory) {
return new Promise((resolve, reject) => {
console.log("✔️ Requested build from Playcanvas")
fetch('https://playcanvas.com/api/apps/download', {
method: 'POST',
body: JSON.stringify({
"project_id": parseInt(config.playcanvas.project_id),
"name": config.playcanvas.name,
"scenes": config.playcanvas.scenes,
"branch_id": config.playcanvas.branch_id,
"description": config.playcanvas.description,
"preload_bundle": config.playcanvas.preload_bundle,
"version": config.playcanvas.version,
"release_notes": config.playcanvas.release_notes,
"scripts_concatenate": config.playcanvas.scripts_concatenate,
"scripts_minify": config.playcanvas.scripts_minify,
"optimize_scene_format": config.playcanvas.optimize_scene_format,
"engine_version": config.playcanvas.engine_version
}),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + config.authToken
}
})
.then(res => {
if (res.status !== 201) {
throw new Error("Error: status code " + res.status);
}
return res.json();
})
.then(buildJob => pollJob(config, buildJob.id))
.then(json => {
console.log("✔ Downloading zip", json.download_url);
return fetch(json.download_url, {method: 'GET'})
})
.then(res => res.buffer())
.then(buffer => {
let output = path.resolve(__dirname, directory + "/" + config.playcanvas.name + '_Download.zip');
if (!fs.existsSync(path.dirname(output))) {
fs.mkdirSync(path.dirname(output), {recursive:true});
}
fs.writeFileSync(output, buffer, 'binary')
resolve(output);
})
.catch(reject);
});
}
function archiveProject(config, branchName, branchId, directory) {
return new Promise((resolve, reject) => {
console.log("✔️ Requested archive from Playcanvas")
fetch('https://playcanvas.com/api/projects/' + config.playcanvas.project_id + '/export', {
method: 'POST',
body: JSON.stringify({
"branch_id": branchId
}),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + config.authToken
}
})
.then(res => {
if (res.status !== 200) {
throw new Error("Error: status code " + res.status);
}
return res.json();
})
.then(buildJob => pollJob(config, buildJob.id))
.then(json => {
console.log("✔ Downloading zip", json.url);
return fetch(json.url, {method: 'GET'})
})
.then(res => res.buffer())
.then(buffer => {
let output = path.resolve(__dirname, directory + "/" + config.playcanvas.name + '_Archive_' + branchName + '.zip');
if (!fs.existsSync(path.dirname(output))) {
fs.mkdirSync(path.dirname(output), {recursive:true});
}
fs.writeFileSync(output, buffer, 'binary')
resolve(output);
})
.catch(reject);
});
}
function unzipProject(zipLocation, unzipFolderName) {
return new Promise((resolve, reject) => {
console.log('✔️ Unzipping ', zipLocation);
var zipFile = new Zip(zipLocation);
try {
var tempFolder = path.resolve(path.dirname(zipLocation), unzipFolderName);
if (fs.existsSync(tempFolder)) {
fs.rmSync(tempFolder, {recursive:true});
}
fs.mkdirSync(tempFolder);
zipFile.extractAllTo(tempFolder, true);
resolve(tempFolder);
} catch (e) {
reject(e);
}
});
}
function zipProject(rootFolder, targetLocation) {
return new Promise((resolve, reject) => {
console.log("✔️ Zipping it all back again")
let output = path.resolve(__dirname, targetLocation);
var zip = new Zip();
zip.addLocalFolder(rootFolder);
if (!fs.existsSync(path.dirname(output))) {
fs.mkdirSync(path.dirname(output));
}
zip.writeZip(output);
fs.rmSync(rootFolder, {recursive:true});
resolve(output);
});
}
module.exports = { readConfig, sleep, downloadProject, archiveProject, unzipProject, zipProject};