Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lintSource.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use strict';
  2. const lintPostcssResult = require('./lintPostcssResult');
  3. const path = require('path');
  4. /** @typedef {import('stylelint').StylelintInternalApi} StylelintInternalApi */
  5. /** @typedef {import('stylelint').GetLintSourceOptions} Options */
  6. /** @typedef {import('postcss').Result} Result */
  7. /** @typedef {import('stylelint').PostcssResult} PostcssResult */
  8. /** @typedef {import('stylelint').StylelintPostcssResult} StylelintPostcssResult */
  9. /**
  10. * Run stylelint on a PostCSS Result, either one that is provided
  11. * or one that we create
  12. * @param {StylelintInternalApi} stylelint
  13. * @param {Options} options
  14. * @returns {Promise<PostcssResult>}
  15. */
  16. module.exports = function lintSource(stylelint, options = {}) {
  17. if (!options.filePath && options.code === undefined && !options.existingPostcssResult) {
  18. return Promise.reject(new Error('You must provide filePath, code, or existingPostcssResult'));
  19. }
  20. const isCodeNotFile = options.code !== undefined;
  21. const inputFilePath = isCodeNotFile ? options.codeFilename : options.filePath;
  22. if (inputFilePath !== undefined && !path.isAbsolute(inputFilePath)) {
  23. if (isCodeNotFile) {
  24. return Promise.reject(new Error('codeFilename must be an absolute path'));
  25. }
  26. return Promise.reject(new Error('filePath must be an absolute path'));
  27. }
  28. const getIsIgnored = stylelint.isPathIgnored(inputFilePath).catch((err) => {
  29. if (isCodeNotFile && err.code === 'ENOENT') return false;
  30. throw err;
  31. });
  32. return getIsIgnored.then((isIgnored) => {
  33. if (isIgnored) {
  34. /** @type {PostcssResult} */
  35. return options.existingPostcssResult
  36. ? Object.assign(options.existingPostcssResult, {
  37. stylelint: createEmptyStylelintPostcssResult(),
  38. })
  39. : createEmptyPostcssResult(inputFilePath);
  40. }
  41. const configSearchPath = stylelint._options.configFile || inputFilePath;
  42. const getConfig = stylelint.getConfigForFile(configSearchPath).catch((err) => {
  43. if (isCodeNotFile && err.code === 'ENOENT') return stylelint.getConfigForFile(process.cwd());
  44. throw err;
  45. });
  46. return getConfig.then((result) => {
  47. if (!result) {
  48. throw new Error('Config file not found');
  49. }
  50. const config = result.config;
  51. const existingPostcssResult = options.existingPostcssResult;
  52. const stylelintResult = {
  53. ruleSeverities: {},
  54. customMessages: {},
  55. disabledRanges: {},
  56. };
  57. if (existingPostcssResult) {
  58. const stylelintPostcssResult = Object.assign(existingPostcssResult, {
  59. stylelint: stylelintResult,
  60. });
  61. return lintPostcssResult(stylelint._options, stylelintPostcssResult, config).then(
  62. () => stylelintPostcssResult,
  63. );
  64. }
  65. return stylelint
  66. ._getPostcssResult({
  67. code: options.code,
  68. codeFilename: options.codeFilename,
  69. filePath: inputFilePath,
  70. codeProcessors: config.codeProcessors,
  71. })
  72. .then((postcssResult) => {
  73. const stylelintPostcssResult = Object.assign(postcssResult, {
  74. stylelint: stylelintResult,
  75. });
  76. return lintPostcssResult(stylelint._options, stylelintPostcssResult, config).then(
  77. () => stylelintPostcssResult,
  78. );
  79. });
  80. });
  81. });
  82. };
  83. /**
  84. * @returns {StylelintPostcssResult}
  85. */
  86. function createEmptyStylelintPostcssResult() {
  87. return {
  88. ruleSeverities: {},
  89. customMessages: {},
  90. disabledRanges: {},
  91. ignored: true,
  92. stylelintError: false,
  93. };
  94. }
  95. /**
  96. * @param {string} [filePath]
  97. * @returns {PostcssResult}
  98. */
  99. function createEmptyPostcssResult(filePath) {
  100. return {
  101. root: {
  102. source: {
  103. input: { file: filePath },
  104. },
  105. },
  106. messages: [],
  107. opts: undefined,
  108. stylelint: createEmptyStylelintPostcssResult(),
  109. warn: () => {},
  110. };
  111. }