mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
84 lines
1.8 KiB
JavaScript
84 lines
1.8 KiB
JavaScript
const toolUtils = {};
|
|
|
|
toolUtils.execCommand = function(command) {
|
|
const exec = require('child_process').exec
|
|
|
|
return new Promise((resolve, reject) => {
|
|
let childProcess = exec(command, (error, stdout, stderr) => {
|
|
if (error) {
|
|
if (error.signal == 'SIGTERM') {
|
|
resolve('Process was killed');
|
|
} else {
|
|
reject(error);
|
|
}
|
|
} else {
|
|
resolve(stdout.trim());
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
toolUtils.downloadFile = function(url, targetPath) {
|
|
const https = require('https');
|
|
const fs = require('fs');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const file = fs.createWriteStream(targetPath);
|
|
const request = https.get(url, function(response) {
|
|
if (response.statusCode !== 200) reject(new Error('HTTP error ' + response.statusCode));
|
|
response.pipe(file);
|
|
file.on('finish', function() {
|
|
file.close();
|
|
resolve();
|
|
});
|
|
}).on('error', (error) => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
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);
|
|
});
|
|
});
|
|
}
|
|
|
|
toolUtils.unlinkForce = async function(filePath) {
|
|
const fs = require('fs-extra');
|
|
|
|
try {
|
|
await fs.unlink(filePath);
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') return;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
toolUtils.fileExists = async function(filePath) {
|
|
return new Promise((resolve, reject) => {
|
|
fs.stat(filePath, function(err, stat) {
|
|
if (err == null) {
|
|
resolve(true);
|
|
} else if(err.code == 'ENOENT') {
|
|
resolve(false);
|
|
} else {
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = toolUtils; |