This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
coreUtils.js
87 lines (81 loc) · 3.23 KB
/
coreUtils.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
const shell = require('shelljs');
const https = require('https');
module.exports.buildCore = function (corePath, coreDest) {
shell.exec('npm pack');
let tarballName = shell.ls('-l', 'hadouken-js-adapter-*').map(a => a).sort((a, b) => { b.mtimeMs - a.mtimeMs })[0].name;
let adapterDir = shell.pwd().stdout;
if (corePath) {
shell.echo(`Buliding core found at ${corePath}`);
shell.cd(corePath);
} else {
shell.rm('-rf', 'core');
shell.echo('Pulling develop core from GitHub');
shell.exec('git clone https://github.com/HadoukenIO/core.git');
shell.cd('core');
}
shell.exec('npm install');
shell.cp(`${adapterDir}/${tarballName}`, `./`);
shell.exec(`npm install ${tarballName}`);
shell.exec('npm install openfin-sign');
shell.exec('npm run build');
shell.cd('out');
if (coreDest) {
shell.exec('npm run deploy -- --force --target=' + coreDest, function (code, stdout, stderr) {
if (!stderr) {
shell.echo(`Finished! .asar was created in ${shell.pwd().stdout} and copied into ${coreDest}`);
} else {
shell.echo(`\r\nDeploy failed with error above and exit code: ${code} \r\n`);
}
});
} else {
shell.echo('Finished! .asar was created in ' + shell.pwd().stdout);
}
shell.cd(adapterDir);
};
module.exports.resolveRuntimeVersion = async function (versionOrChannel) {
const splitVersion = versionOrChannel.split('.');
const isVersion = splitVersion.length > 1 && splitVersion.every(x => x === '*' || /^\d+$/.test(x));
if (isVersion) {
const mustMatch = takeWhile(splitVersion, (x) => x !== '*');
if (4 - mustMatch.length > 0) {
// tslint:disable-next-line:no-backbone-get-set-outside-model
const res = await get('https://cdn.openfin.co/release/runtimeVersions');
const versions = res.split('\r\n');
const match = versions.find((v) => v.split('.').slice(0, mustMatch.length).join('.') === mustMatch.join('.'));
if (match) {
return match;
}
} else {
return versionOrChannel;
}
}
try {
// tslint:disable-next-line:no-backbone-get-set-outside-model
return await get(`https://cdn.openfin.co/release/runtime/${versionOrChannel}`);
} catch (err) {
console.log(err);
throw Error('Could not resolve runtime version');
}
}
async function get(url) {
return new Promise((resolve, reject) => {
const request = https.get(url, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}
const body = [];
response.on('data', (chunk) => {
body.push(chunk);
});
response.on('end', () => resolve(body.join('')));
});
request.on('error', (err) => reject(err));
});
}
function takeWhile(arr, func) {
return arr.reduce(({ take, vals }, x, i, r) => take && func(x, i, r)
? { take: true, vals: [...vals, x] }
: { take: false, vals },
{ take: true, vals: [] })
.vals;
}