2017-12-04 20:16:14 +02:00
|
|
|
const toolUtils = {};
|
|
|
|
|
|
|
|
toolUtils.execCommand = function(command) {
|
2019-07-30 09:35:42 +02:00
|
|
|
const exec = require('child_process').exec;
|
2017-12-04 20:16:14 +02:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-09-13 00:16:42 +02:00
|
|
|
exec(command, (error, stdout) => {
|
2017-12-04 20:16:14 +02:00
|
|
|
if (error) {
|
|
|
|
if (error.signal == 'SIGTERM') {
|
|
|
|
resolve('Process was killed');
|
|
|
|
} else {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolve(stdout.trim());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2017-12-04 20:16:14 +02:00
|
|
|
|
|
|
|
toolUtils.downloadFile = function(url, targetPath) {
|
|
|
|
const https = require('https');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const file = fs.createWriteStream(targetPath);
|
2019-07-30 09:35:42 +02:00
|
|
|
https.get(url, function(response) {
|
2019-09-19 23:51:18 +02:00
|
|
|
if (response.statusCode !== 200) reject(new Error(`HTTP error ${response.statusCode}`));
|
2017-12-04 20:16:14 +02:00
|
|
|
response.pipe(file);
|
|
|
|
file.on('finish', function() {
|
2019-10-09 21:35:13 +02:00
|
|
|
// file.close();
|
2017-12-04 20:16:14 +02:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}).on('error', (error) => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2017-12-04 20:16:14 +02:00
|
|
|
|
|
|
|
toolUtils.fileSha256 = function(filePath) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const fs = require('fs');
|
|
|
|
const algo = 'sha256';
|
|
|
|
const shasum = crypto.createHash(algo);
|
|
|
|
|
|
|
|
const s = fs.ReadStream(filePath);
|
|
|
|
s.on('data', function(d) { shasum.update(d); });
|
|
|
|
s.on('end', function() {
|
|
|
|
const d = shasum.digest('hex');
|
|
|
|
resolve(d);
|
|
|
|
});
|
|
|
|
s.on('error', function(error) {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2017-12-04 20:16:14 +02:00
|
|
|
|
|
|
|
toolUtils.unlinkForce = async function(filePath) {
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fs.unlink(filePath);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ENOENT') return;
|
|
|
|
throw error;
|
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2017-12-04 20:16:14 +02:00
|
|
|
|
|
|
|
toolUtils.fileExists = async function(filePath) {
|
2018-10-13 12:09:03 +02:00
|
|
|
const fs = require('fs-extra');
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2017-12-04 20:16:14 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-09-13 00:16:42 +02:00
|
|
|
fs.stat(filePath, function(err) {
|
2017-12-04 20:16:14 +02:00
|
|
|
if (err == null) {
|
|
|
|
resolve(true);
|
2019-10-09 21:35:13 +02:00
|
|
|
} else if (err.code == 'ENOENT') {
|
2017-12-04 20:16:14 +02:00
|
|
|
resolve(false);
|
|
|
|
} else {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2017-12-04 20:16:14 +02:00
|
|
|
|
2018-02-04 19:42:33 +02:00
|
|
|
toolUtils.githubOauthToken = async function() {
|
2018-02-04 19:51:42 +02:00
|
|
|
const fs = require('fs-extra');
|
2019-09-19 23:51:18 +02:00
|
|
|
const r = await fs.readFile(`${__dirname}/github_oauth_token.txt`);
|
2018-02-04 19:42:33 +02:00
|
|
|
return r.toString();
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2018-02-04 19:42:33 +02:00
|
|
|
|
2019-01-12 00:07:23 +02:00
|
|
|
toolUtils.githubRelease = async function(project, tagName, options = null) {
|
|
|
|
options = Object.assign({}, {
|
|
|
|
isDraft: false,
|
|
|
|
isPreRelease: false,
|
|
|
|
}, options);
|
|
|
|
|
2018-02-04 19:51:42 +02:00
|
|
|
const fetch = require('node-fetch');
|
2018-02-04 19:55:22 +02:00
|
|
|
|
2018-02-05 19:27:38 +02:00
|
|
|
const oauthToken = await toolUtils.githubOauthToken();
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
const response = await fetch(`https://api.github.com/repos/laurent22/${project}/releases`, {
|
2019-07-30 09:35:42 +02:00
|
|
|
method: 'POST',
|
2018-02-04 19:42:33 +02:00
|
|
|
body: JSON.stringify({
|
|
|
|
tag_name: tagName,
|
|
|
|
name: tagName,
|
2019-01-12 00:07:23 +02:00
|
|
|
draft: options.isDraft,
|
|
|
|
prerelease: options.isPreRelease,
|
2018-02-04 19:42:33 +02:00
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2019-09-19 23:51:18 +02:00
|
|
|
'Authorization': `token ${oauthToken}`,
|
2018-02-04 19:42:33 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const responseText = await response.text();
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
if (!response.ok) throw new Error(`Cannot create GitHub release: ${responseText}`);
|
2018-02-04 19:42:33 +02:00
|
|
|
|
|
|
|
const responseJson = JSON.parse(responseText);
|
2019-09-19 23:51:18 +02:00
|
|
|
if (!responseJson.url) throw new Error(`No URL for release: ${responseText}`);
|
2018-02-04 19:42:33 +02:00
|
|
|
|
|
|
|
return responseJson;
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2018-02-04 19:42:33 +02:00
|
|
|
|
2019-06-15 19:58:09 +02:00
|
|
|
toolUtils.readline = question => {
|
2019-09-13 00:16:42 +02:00
|
|
|
return new Promise((resolve) => {
|
2019-06-15 19:58:09 +02:00
|
|
|
const readline = require('readline');
|
|
|
|
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
2019-07-30 09:35:42 +02:00
|
|
|
output: process.stdout,
|
2019-06-15 19:58:09 +02:00
|
|
|
});
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
rl.question(`${question} `, answer => {
|
2019-06-15 19:58:09 +02:00
|
|
|
resolve(answer);
|
|
|
|
rl.close();
|
|
|
|
});
|
|
|
|
});
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2019-06-15 19:58:09 +02:00
|
|
|
|
2018-10-13 00:25:11 +02:00
|
|
|
toolUtils.isLinux = () => {
|
|
|
|
return process && process.platform === 'linux';
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2018-10-13 00:25:11 +02:00
|
|
|
|
|
|
|
toolUtils.isWindows = () => {
|
|
|
|
return process && process.platform === 'win32';
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2018-10-13 00:25:11 +02:00
|
|
|
|
|
|
|
toolUtils.isMac = () => {
|
|
|
|
return process && process.platform === 'darwin';
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2018-10-13 00:25:11 +02:00
|
|
|
|
2019-09-30 00:11:36 +02:00
|
|
|
toolUtils.insertContentIntoFile = async function(filePath, markerOpen, markerClose, contentToInsert) {
|
2019-07-18 19:36:29 +02:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
let content = await fs.readFile(filePath, 'utf-8');
|
|
|
|
// [^]* matches any character including new lines
|
2019-09-19 23:51:18 +02:00
|
|
|
const regex = new RegExp(`${markerOpen}[^]*?${markerClose}`);
|
2019-07-18 19:36:29 +02:00
|
|
|
content = content.replace(regex, markerOpen + contentToInsert + markerClose);
|
|
|
|
await fs.writeFile(filePath, content);
|
2019-07-30 09:35:42 +02:00
|
|
|
};
|
2019-07-18 19:36:29 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
module.exports = toolUtils;
|