| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | const {
|
|---|
| 4 | resolve
|
|---|
| 5 | } = require('path');
|
|---|
| 6 |
|
|---|
| 7 | const {
|
|---|
| 8 | statSync
|
|---|
| 9 | } = require('fs');
|
|---|
| 10 |
|
|---|
| 11 | const normalizePath = require('normalize-path');
|
|---|
| 12 | /**
|
|---|
| 13 | * @template T
|
|---|
| 14 | * @param {T} value
|
|---|
| 15 | * @return {
|
|---|
| 16 | T extends (null | undefined)
|
|---|
| 17 | ? []
|
|---|
| 18 | : T extends string
|
|---|
| 19 | ? [string]
|
|---|
| 20 | : T extends readonly unknown[]
|
|---|
| 21 | ? T
|
|---|
| 22 | : T extends Iterable<infer T>
|
|---|
| 23 | ? T[]
|
|---|
| 24 | : [T]
|
|---|
| 25 | }
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | /* istanbul ignore next */
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | function arrify(value) {
|
|---|
| 32 | // eslint-disable-next-line no-undefined
|
|---|
| 33 | if (value === null || value === undefined) {
|
|---|
| 34 | // @ts-ignore
|
|---|
| 35 | return [];
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | if (Array.isArray(value)) {
|
|---|
| 39 | // @ts-ignore
|
|---|
| 40 | return value;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | if (typeof value === 'string') {
|
|---|
| 44 | // @ts-ignore
|
|---|
| 45 | return [value];
|
|---|
| 46 | } // @ts-ignore
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | if (typeof value[Symbol.iterator] === 'function') {
|
|---|
| 50 | // @ts-ignore
|
|---|
| 51 | return [...value];
|
|---|
| 52 | } // @ts-ignore
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | return [value];
|
|---|
| 56 | }
|
|---|
| 57 | /**
|
|---|
| 58 | * @param {string|string[]} files
|
|---|
| 59 | * @param {string} context
|
|---|
| 60 | * @returns {string[]}
|
|---|
| 61 | */
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | function parseFiles(files, context) {
|
|---|
| 65 | return arrify(files).map((
|
|---|
| 66 | /** @type {string} */
|
|---|
| 67 | file) => normalizePath(resolve(context, file)));
|
|---|
| 68 | }
|
|---|
| 69 | /**
|
|---|
| 70 | * @param {string|string[]} patterns
|
|---|
| 71 | * @param {string|string[]} extensions
|
|---|
| 72 | * @returns {string[]}
|
|---|
| 73 | */
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 | function parseFoldersToGlobs(patterns, extensions = []) {
|
|---|
| 77 | const extensionsList = arrify(extensions);
|
|---|
| 78 | const [prefix, postfix] = extensionsList.length > 1 ? ['{', '}'] : ['', ''];
|
|---|
| 79 | const extensionsGlob = extensionsList.map((
|
|---|
| 80 | /** @type {string} */
|
|---|
| 81 | extension) => extension.replace(/^\./u, '')).join(',');
|
|---|
| 82 | return arrify(patterns).map((
|
|---|
| 83 | /** @type {string} */
|
|---|
| 84 | pattern) => {
|
|---|
| 85 | try {
|
|---|
| 86 | // The patterns are absolute because they are prepended with the context.
|
|---|
| 87 | const stats = statSync(pattern);
|
|---|
| 88 | /* istanbul ignore else */
|
|---|
| 89 |
|
|---|
| 90 | if (stats.isDirectory()) {
|
|---|
| 91 | return pattern.replace(/[/\\]*?$/u, `/**${extensionsGlob ? `/*.${prefix + extensionsGlob + postfix}` : ''}`);
|
|---|
| 92 | }
|
|---|
| 93 | } catch (_) {// Return the pattern as is on error.
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | return pattern;
|
|---|
| 97 | });
|
|---|
| 98 | }
|
|---|
| 99 | /**
|
|---|
| 100 | * @param {string} _ key, but unused
|
|---|
| 101 | * @param {any} value
|
|---|
| 102 | */
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | const jsonStringifyReplacerSortKeys = (_, value) => {
|
|---|
| 106 | /**
|
|---|
| 107 | * @param {{ [x: string]: any; }} sorted
|
|---|
| 108 | * @param {string | number} key
|
|---|
| 109 | */
|
|---|
| 110 | const insert = (sorted, key) => {
|
|---|
| 111 | // eslint-disable-next-line no-param-reassign
|
|---|
| 112 | sorted[key] = value[key];
|
|---|
| 113 | return sorted;
|
|---|
| 114 | };
|
|---|
| 115 |
|
|---|
| 116 | return value instanceof Object && !(value instanceof Array) ? Object.keys(value).sort().reduce(insert, {}) : value;
|
|---|
| 117 | };
|
|---|
| 118 |
|
|---|
| 119 | module.exports = {
|
|---|
| 120 | arrify,
|
|---|
| 121 | parseFiles,
|
|---|
| 122 | parseFoldersToGlobs,
|
|---|
| 123 | jsonStringifyReplacerSortKeys
|
|---|
| 124 | }; |
|---|