utils.js
2.35 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parseFiles = parseFiles;
exports.replaceBackslashes = replaceBackslashes;
exports.parseFoldersToGlobs = parseFoldersToGlobs;
exports.jsonStringifyReplacerSortKeys = void 0;
var _fs = require("fs");
var _arrify = _interopRequireDefault(require("arrify"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()*?[\]{|}]|^!|[!+@](?=\())/g;
/**
* @param {string|string[]} files
* @param {string} context
* @returns {string[]}
*/
function parseFiles(files, context) {
return (0, _arrify.default)(files).map(file => `${replaceBackslashes(context).replace(UNESCAPED_GLOB_SYMBOLS_RE, '\\$2')}/${replaceBackslashes(file)}`);
}
/**
* @param {string} str
* @returns {string}
*/
function replaceBackslashes(str) {
return str.replace(/\\/g, '/');
}
/**
* @param {string|string[]} patterns
* @param {string|string[]} extensions
* @returns {string[]}
*/
function parseFoldersToGlobs(patterns, extensions = []) {
const extensionsList = (0, _arrify.default)(extensions);
const [prefix, postfix] = extensionsList.length > 1 ? ['{', '}'] : ['', ''];
const extensionsGlob = extensionsList.map(extension => extension.replace(/^\./u, '')).join(',');
return (0, _arrify.default)(patterns).map(pattern => replaceBackslashes(pattern)).map(pattern => {
try {
// The patterns are absolute because they are prepended with the context.
const stats = (0, _fs.statSync)(pattern);
/* istanbul ignore else */
if (stats.isDirectory()) {
return pattern.replace(/[/\\]*?$/u, `/**${extensionsGlob ? `/*.${prefix + extensionsGlob + postfix}` : ''}`);
}
} catch (_) {// Return the pattern as is on error.
}
return pattern;
});
}
/**
*
* @param {string} _ key, but unused
* @param {any} value
*/
const jsonStringifyReplacerSortKeys = (_, value) => {
/**
* @param {{ [x: string]: any; }} sorted
* @param {string | number} key
*/
const insert = (sorted, key) => {
// eslint-disable-next-line no-param-reassign
sorted[key] = value[key];
return sorted;
};
return value instanceof Object && !(value instanceof Array) ? Object.keys(value).sort().reduce(insert, {}) : value;
};
exports.jsonStringifyReplacerSortKeys = jsonStringifyReplacerSortKeys;