getStylelint.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getStylelint;
  6. var _os = require("os");
  7. var _jestWorker = require("jest-worker");
  8. var _worker = require("./worker");
  9. var _utils = require("./utils");
  10. var _options = require("./options");
  11. // @ts-ignore
  12. /** @type {{[key: string]: any}} */
  13. const cache = {};
  14. /** @typedef {import('stylelint')} Stylelint */
  15. /** @typedef {import('stylelint').LintResult} LintResult */
  16. /** @typedef {import('./options').Options} Options */
  17. /** @typedef {() => Promise<void>} AsyncTask */
  18. /** @typedef {(files: string|string[]) => Promise<LintResult[]>} LintTask */
  19. /** @typedef {JestWorker & {lintFiles: LintTask}} Worker */
  20. /** @typedef {{stylelint: Stylelint, lintFiles: LintTask, cleanup: AsyncTask, threads: number, }} Linter */
  21. /**
  22. * @param {Options} options
  23. * @returns {Linter}
  24. */
  25. function loadStylelint(options) {
  26. const stylelint = (0, _worker.setup)(options, (0, _options.getStylelintOptions)(options));
  27. return {
  28. stylelint,
  29. lintFiles: _worker.lintFiles,
  30. cleanup: async () => {},
  31. threads: 1
  32. };
  33. }
  34. /**
  35. * @param {string|undefined} key
  36. * @param {number} poolSize
  37. * @param {Options} options
  38. * @returns {Linter}
  39. */
  40. function loadStylelintThreaded(key, poolSize, options) {
  41. const cacheKey = getCacheKey(key, options);
  42. const source = require.resolve('./worker');
  43. const workerOptions = {
  44. enableWorkerThreads: true,
  45. numWorkers: poolSize,
  46. setupArgs: [options, (0, _options.getStylelintOptions)(options)]
  47. };
  48. const local = loadStylelint(options);
  49. /** @type {Worker?} */
  50. // prettier-ignore
  51. let worker =
  52. /** @type {Worker} */
  53. new _jestWorker.Worker(source, workerOptions);
  54. /** @type {Linter} */
  55. const context = { ...local,
  56. threads: poolSize,
  57. lintFiles: async files =>
  58. /* istanbul ignore next */
  59. worker ? worker.lintFiles(files) : local.lintFiles(files),
  60. cleanup: async () => {
  61. cache[cacheKey] = local;
  62. context.lintFiles = files => local.lintFiles(files);
  63. /* istanbul ignore next */
  64. if (worker) {
  65. worker.end();
  66. worker = null;
  67. }
  68. }
  69. };
  70. return context;
  71. }
  72. /**
  73. * @param {string|undefined} key
  74. * @param {Options} options
  75. * @returns {Linter}
  76. */
  77. function getStylelint(key, {
  78. threads,
  79. ...options
  80. }) {
  81. const max = typeof threads !== 'number' ? threads ? (0, _os.cpus)().length - 1 : 1 : threads;
  82. const cacheKey = getCacheKey(key, {
  83. threads,
  84. ...options
  85. });
  86. if (!cache[cacheKey]) {
  87. cache[cacheKey] = max > 1 ? loadStylelintThreaded(key, max, options) : loadStylelint(options);
  88. }
  89. return cache[cacheKey];
  90. }
  91. /**
  92. * @param {string|undefined} key
  93. * @param {Options} options
  94. * @returns {string}
  95. */
  96. function getCacheKey(key, options) {
  97. return JSON.stringify({
  98. key,
  99. options
  100. }, _utils.jsonStringifyReplacerSortKeys);
  101. }