utils.js 2.9 KB

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