embark-area-51/lib/utils/utils.js

148 lines
3.2 KiB
JavaScript
Raw Normal View History

let path = require('path');
let globule = require('globule');
let merge = require('merge');
let http = require('follow-redirects').http;
let https = require('follow-redirects').https;
let shelljs = require('shelljs');
2017-12-16 17:23:02 +00:00
var tar = require('tar');
2017-12-19 19:07:48 +00:00
var propose = require('propose');
2017-02-18 19:10:01 +00:00
2017-12-16 02:53:11 +00:00
//let fs = require('../core/fs.js');
let o_fs = require('fs-extra');
2017-02-18 19:10:01 +00:00
function joinPath() {
return path.join.apply(path.join, arguments);
}
2017-02-18 19:45:57 +00:00
function filesMatchingPattern(files) {
2017-02-19 05:00:01 +00:00
return globule.find(files, {nonull: true});
2017-02-18 19:37:07 +00:00
}
function fileMatchesPattern(patterns, intendedPath) {
2017-02-19 05:00:01 +00:00
return globule.isMatch(patterns, intendedPath);
2017-02-18 19:37:07 +00:00
}
2017-02-18 19:45:57 +00:00
function recursiveMerge(target, source) {
return merge.recursive(target, source);
}
2017-02-18 20:27:08 +00:00
function checkIsAvailable(url, callback) {
2017-12-05 23:14:46 +00:00
http.get(url, function (_res) {
callback(true);
2017-12-05 23:14:46 +00:00
}).on('error', function (_res) {
callback(false);
2017-02-18 20:27:08 +00:00
});
}
2017-12-16 03:15:24 +00:00
function httpGetRequest(httpObj, url, callback) {
httpObj.get(url, function(res) {
2017-12-16 03:11:55 +00:00
let body = '';
res.on('data', function (d) {
body += d;
});
res.on('end', function () {
callback(null, body);
});
}).on('error', function (err) {
callback(err);
2017-12-16 03:11:55 +00:00
});
2017-04-02 18:40:10 +00:00
}
2017-12-16 03:15:24 +00:00
function httpGet(url, callback) {
httpGetRequest(http, url, callback);
}
function httpsGet(url, callback) {
2017-12-16 03:15:24 +00:00
httpGetRequest(https, url, callback);
}
2017-12-31 02:44:59 +00:00
function httpGetJson(url, callback) {
httpGetRequest(http, url, function(err, body) {
try {
let parsed = JSON.parse(body);
return callback(err, parsed);
} catch(e) {
return callback(e);
}
});
}
function runCmd(cmd, options) {
let result = shelljs.exec(cmd, options || {silent: true});
if (result.code !== 0) {
console.log("error doing.. " + cmd);
console.log(result.output);
if (result.stderr !== undefined) {
console.log(result.stderr);
}
exit();
}
}
function cd(folder) {
shelljs.cd(folder);
}
function sed(file, pattern, replace) {
shelljs.sed('-i', pattern, replace, file);
}
function exit(code) {
process.exit(code);
}
2017-12-16 02:53:11 +00:00
function downloadFile(url, dest, cb) {
var file = o_fs.createWriteStream(dest);
(url.substring(0,5) === 'https' ? https : http).get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb);
});
}).on('error', function(err) {
o_fs.unlink(dest);
if (cb) cb(err.message);
});
}
2017-12-16 17:23:02 +00:00
function extractTar(filename, packageDirectory, cb) {
o_fs.createReadStream(filename).pipe(
tar.x({
strip: 1,
C: packageDirectory
}).on('end', function() {
cb();
})
);
}
2017-12-19 19:07:48 +00:00
function proposeAlternative(word, _dictionary, _exceptions) {
let exceptions = _exceptions || [];
let dictionary = _dictionary.filter((entry) => {
return exceptions.indexOf(entry) < 0;
});
return propose(word, dictionary, {threshold: 0.3});
}
function pwd() {
2018-03-26 19:26:37 +00:00
return process.env.PWD || process.cwd();
}
2017-02-18 19:10:01 +00:00
module.exports = {
2017-02-18 19:45:57 +00:00
joinPath: joinPath,
filesMatchingPattern: filesMatchingPattern,
fileMatchesPattern: fileMatchesPattern,
2017-02-18 20:27:08 +00:00
recursiveMerge: recursiveMerge,
checkIsAvailable: checkIsAvailable,
2017-04-02 18:40:10 +00:00
httpGet: httpGet,
httpsGet: httpsGet,
2017-12-31 02:44:59 +00:00
httpGetJson: httpGetJson,
runCmd: runCmd,
cd: cd,
sed: sed,
2017-12-16 02:53:11 +00:00
exit: exit,
2017-12-16 17:23:02 +00:00
downloadFile: downloadFile,
2017-12-19 19:07:48 +00:00
extractTar: extractTar,
proposeAlternative: proposeAlternative,
pwd: pwd
2017-02-18 19:10:01 +00:00
};