/
utils.js
116 lines (96 loc) · 2.56 KB
/
utils.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
import {normalizeInput} from 'embark-utils';
function dirname() {
const path = require('path');
return path.dirname.apply(path.dirname, arguments);
}
function filesMatchingPattern(files) {
const globule = require('globule');
return globule.find(files, {nonull: true});
}
function fileMatchesPattern(patterns, intendedPath) {
const globule = require('globule');
return globule.isMatch(patterns, intendedPath);
}
function cd(folder) {
const shelljs = require('shelljs');
shelljs.cd(folder);
}
function sed(file, pattern, replace) {
const shelljs = require('shelljs');
shelljs.sed('-i', pattern, replace, file);
}
function extractTar(filename, packageDirectory, cb) {
const o_fs = require('fs-extra');
const tar = require('tar');
o_fs.createReadStream(filename).pipe(
tar.x({
strip: 1,
C: packageDirectory
}).on('end', function () {
cb();
})
);
}
function extractZip(filename, packageDirectory, opts, cb) {
const decompress = require('decompress');
decompress(filename, packageDirectory, opts).then((_files) => {
cb();
});
}
function isValidDomain(v) {
// from: https://github.com/miguelmota/is-valid-domain
if (typeof v !== 'string') return false;
var parts = v.split('.');
if (parts.length <= 1) return false;
var tld = parts.pop();
var tldRegex = /^(?:xn--)?[a-zA-Z0-9]+$/gi;
if (!tldRegex.test(tld)) return false;
var isValid = parts.every(function(host) {
var hostRegex = /^(?!:\/\/)([a-zA-Z0-9]+|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])$/gi;
return hostRegex.test(host);
});
return isValid;
}
function interceptLogs(consoleContext, logger) {
let context = {};
context.console = consoleContext;
context.console.log = function () {
logger.info(normalizeInput(arguments));
};
context.console.warn = function () {
logger.warn(normalizeInput(arguments));
};
context.console.info = function () {
logger.info(normalizeInput(arguments));
};
context.console.debug = function () {
// TODO: ue JSON.stringify
logger.debug(normalizeInput(arguments));
};
context.console.trace = function () {
logger.trace(normalizeInput(arguments));
};
context.console.dir = function () {
logger.dir(normalizeInput(arguments));
};
}
function getWindowSize() {
const windowSize = require('window-size');
if (windowSize) {
return windowSize.get();
}
return {width: 240, height: 75};
}
module.exports = {
dirname,
filesMatchingPattern,
fileMatchesPattern,
isValidDomain,
cd,
sed,
extractTar,
extractZip,
normalizeInput,
interceptLogs,
getWindowSize
};