api.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  4. var path = _interopDefault(require('path'));
  5. var minimatch = _interopDefault(require('minimatch'));
  6. var createDebug = _interopDefault(require('debug'));
  7. var objectSchema = require('@humanwhocodes/object-schema');
  8. /**
  9. * @fileoverview ConfigSchema
  10. * @author Nicholas C. Zakas
  11. */
  12. //------------------------------------------------------------------------------
  13. // Helpers
  14. //------------------------------------------------------------------------------
  15. /**
  16. * Assets that a given value is an array.
  17. * @param {*} value The value to check.
  18. * @returns {void}
  19. * @throws {TypeError} When the value is not an array.
  20. */
  21. function assertIsArray(value) {
  22. if (!Array.isArray(value)) {
  23. throw new TypeError('Expected value to be an array.');
  24. }
  25. }
  26. /**
  27. * Assets that a given value is an array containing only strings and functions.
  28. * @param {*} value The value to check.
  29. * @returns {void}
  30. * @throws {TypeError} When the value is not an array of strings and functions.
  31. */
  32. function assertIsArrayOfStringsAndFunctions(value, name) {
  33. assertIsArray(value);
  34. if (value.some(item => typeof item !== 'string' && typeof item !== 'function')) {
  35. throw new TypeError('Expected array to only contain strings.');
  36. }
  37. }
  38. //------------------------------------------------------------------------------
  39. // Exports
  40. //------------------------------------------------------------------------------
  41. /**
  42. * The base schema that every ConfigArray uses.
  43. * @type Object
  44. */
  45. const baseSchema = Object.freeze({
  46. name: {
  47. required: false,
  48. merge() {
  49. return undefined;
  50. },
  51. validate(value) {
  52. if (typeof value !== 'string') {
  53. throw new TypeError('Property must be a string.');
  54. }
  55. }
  56. },
  57. files: {
  58. required: false,
  59. merge() {
  60. return undefined;
  61. },
  62. validate(value) {
  63. // first check if it's an array
  64. assertIsArray(value);
  65. // then check each member
  66. value.forEach(item => {
  67. if (Array.isArray(item)) {
  68. assertIsArrayOfStringsAndFunctions(item);
  69. } else if (typeof item !== 'string' && typeof item !== 'function') {
  70. throw new TypeError('Items must be a string, a function, or an array of strings and functions.');
  71. }
  72. });
  73. }
  74. },
  75. ignores: {
  76. required: false,
  77. merge() {
  78. return undefined;
  79. },
  80. validate: assertIsArrayOfStringsAndFunctions
  81. }
  82. });
  83. /**
  84. * @fileoverview ConfigArray
  85. * @author Nicholas C. Zakas
  86. */
  87. //------------------------------------------------------------------------------
  88. // Helpers
  89. //------------------------------------------------------------------------------
  90. const debug = createDebug('@hwc/config-array');
  91. const MINIMATCH_OPTIONS = {
  92. matchBase: true,
  93. dot: true
  94. };
  95. const CONFIG_TYPES = new Set(['array', 'function']);
  96. /**
  97. * Shorthand for checking if a value is a string.
  98. * @param {any} value The value to check.
  99. * @returns {boolean} True if a string, false if not.
  100. */
  101. function isString(value) {
  102. return typeof value === 'string';
  103. }
  104. /**
  105. * Normalizes a `ConfigArray` by flattening it and executing any functions
  106. * that are found inside.
  107. * @param {Array} items The items in a `ConfigArray`.
  108. * @param {Object} context The context object to pass into any function
  109. * found.
  110. * @param {Array<string>} extraConfigTypes The config types to check.
  111. * @returns {Promise<Array>} A flattened array containing only config objects.
  112. * @throws {TypeError} When a config function returns a function.
  113. */
  114. async function normalize(items, context, extraConfigTypes) {
  115. const allowFunctions = extraConfigTypes.includes('function');
  116. const allowArrays = extraConfigTypes.includes('array');
  117. async function *flatTraverse(array) {
  118. for (let item of array) {
  119. if (typeof item === 'function') {
  120. if (!allowFunctions) {
  121. throw new TypeError('Unexpected function.');
  122. }
  123. item = item(context);
  124. if (item.then) {
  125. item = await item;
  126. }
  127. }
  128. if (Array.isArray(item)) {
  129. if (!allowArrays) {
  130. throw new TypeError('Unexpected array.');
  131. }
  132. yield * flatTraverse(item);
  133. } else if (typeof item === 'function') {
  134. throw new TypeError('A config function can only return an object or array.');
  135. } else {
  136. yield item;
  137. }
  138. }
  139. }
  140. /*
  141. * Async iterables cannot be used with the spread operator, so we need to manually
  142. * create the array to return.
  143. */
  144. const asyncIterable = await flatTraverse(items);
  145. const configs = [];
  146. for await (const config of asyncIterable) {
  147. configs.push(config);
  148. }
  149. return configs;
  150. }
  151. /**
  152. * Normalizes a `ConfigArray` by flattening it and executing any functions
  153. * that are found inside.
  154. * @param {Array} items The items in a `ConfigArray`.
  155. * @param {Object} context The context object to pass into any function
  156. * found.
  157. * @param {Array<string>} extraConfigTypes The config types to check.
  158. * @returns {Array} A flattened array containing only config objects.
  159. * @throws {TypeError} When a config function returns a function.
  160. */
  161. function normalizeSync(items, context, extraConfigTypes) {
  162. const allowFunctions = extraConfigTypes.includes('function');
  163. const allowArrays = extraConfigTypes.includes('array');
  164. function *flatTraverse(array) {
  165. for (let item of array) {
  166. if (typeof item === 'function') {
  167. if (!allowFunctions) {
  168. throw new TypeError('Unexpected function.');
  169. }
  170. item = item(context);
  171. if (item.then) {
  172. throw new TypeError('Async config functions are not supported.');
  173. }
  174. }
  175. if (Array.isArray(item)) {
  176. if (!allowArrays) {
  177. throw new TypeError('Unexpected array.');
  178. }
  179. yield * flatTraverse(item);
  180. } else if (typeof item === 'function') {
  181. throw new TypeError('A config function can only return an object or array.');
  182. } else {
  183. yield item;
  184. }
  185. }
  186. }
  187. return [...flatTraverse(items)];
  188. }
  189. /**
  190. * Determines if a given file path should be ignored based on the given
  191. * matcher.
  192. * @param {Array<string|() => boolean>} ignores The ignore patterns to check.
  193. * @param {string} filePath The absolute path of the file to check.
  194. * @param {string} relativeFilePath The relative path of the file to check.
  195. * @returns {boolean} True if the path should be ignored and false if not.
  196. */
  197. function shouldIgnoreFilePath(ignores, filePath, relativeFilePath) {
  198. let shouldIgnore = false;
  199. for (const matcher of ignores) {
  200. if (typeof matcher === 'function') {
  201. shouldIgnore = shouldIgnore || matcher(filePath);
  202. continue;
  203. }
  204. /*
  205. * If there's a negated pattern, that means anything matching
  206. * must NOT be ignored. To do that, we need to use the `flipNegate`
  207. * option for minimatch to check if the filepath matches the
  208. * pattern specified after the !, and if that result is true,
  209. * then we return false immediately because this file should
  210. * never be ignored.
  211. */
  212. if (matcher.startsWith('!')) {
  213. /*
  214. * The file must already be ignored in order to apply a negated
  215. * pattern, because negated patterns simply remove files that
  216. * would already be ignored.
  217. */
  218. if (shouldIgnore &&
  219. minimatch(relativeFilePath, matcher, {
  220. ...MINIMATCH_OPTIONS,
  221. flipNegate: true
  222. })) {
  223. return false;
  224. }
  225. } else {
  226. shouldIgnore = shouldIgnore || minimatch(relativeFilePath, matcher, MINIMATCH_OPTIONS);
  227. }
  228. }
  229. return shouldIgnore;
  230. }
  231. /**
  232. * Determines if a given file path is matched by a config. If the config
  233. * has no `files` field, then it matches; otherwise, if a `files` field
  234. * is present then we match the globs in `files` and exclude any globs in
  235. * `ignores`.
  236. * @param {string} filePath The absolute file path to check.
  237. * @param {Object} config The config object to check.
  238. * @returns {boolean} True if the file path is matched by the config,
  239. * false if not.
  240. */
  241. function pathMatches(filePath, basePath, config) {
  242. // a config without `files` field always match
  243. if (!config.files) {
  244. return true;
  245. }
  246. /*
  247. * For both files and ignores, functions are passed the absolute
  248. * file path while strings are compared against the relative
  249. * file path.
  250. */
  251. const relativeFilePath = path.relative(basePath, filePath);
  252. // if files isn't an array, throw an error
  253. if (!Array.isArray(config.files) || config.files.length === 0) {
  254. throw new TypeError('The files key must be a non-empty array.');
  255. }
  256. // match both strings and functions
  257. const match = pattern => {
  258. if (isString(pattern)) {
  259. return minimatch(relativeFilePath, pattern, MINIMATCH_OPTIONS);
  260. }
  261. if (typeof pattern === 'function') {
  262. return pattern(filePath);
  263. }
  264. throw new TypeError(`Unexpected matcher type ${pattern}.`);
  265. };
  266. // check for all matches to config.files
  267. let filePathMatchesPattern = config.files.some(pattern => {
  268. if (Array.isArray(pattern)) {
  269. return pattern.every(match);
  270. }
  271. return match(pattern);
  272. });
  273. /*
  274. * If the file path matches the config.files patterns, then check to see
  275. * if there are any files to ignore.
  276. */
  277. if (filePathMatchesPattern && config.ignores) {
  278. filePathMatchesPattern = !shouldIgnoreFilePath(config.ignores, filePath, relativeFilePath);
  279. }
  280. return filePathMatchesPattern;
  281. }
  282. /**
  283. * Ensures that a ConfigArray has been normalized.
  284. * @param {ConfigArray} configArray The ConfigArray to check.
  285. * @returns {void}
  286. * @throws {Error} When the `ConfigArray` is not normalized.
  287. */
  288. function assertNormalized(configArray) {
  289. // TODO: Throw more verbose error
  290. if (!configArray.isNormalized()) {
  291. throw new Error('ConfigArray must be normalized to perform this operation.');
  292. }
  293. }
  294. /**
  295. * Ensures that config types are valid.
  296. * @param {Array<string>} extraConfigTypes The config types to check.
  297. * @returns {void}
  298. * @throws {Error} When the config types array is invalid.
  299. */
  300. function assertExtraConfigTypes(extraConfigTypes) {
  301. if (extraConfigTypes.length > 2) {
  302. throw new TypeError('configTypes must be an array with at most two items.');
  303. }
  304. for (const configType of extraConfigTypes) {
  305. if (!CONFIG_TYPES.has(configType)) {
  306. throw new TypeError(`Unexpected config type "${configType}" found. Expected one of: "object", "array", "function".`);
  307. }
  308. }
  309. }
  310. //------------------------------------------------------------------------------
  311. // Public Interface
  312. //------------------------------------------------------------------------------
  313. const ConfigArraySymbol = {
  314. isNormalized: Symbol('isNormalized'),
  315. configCache: Symbol('configCache'),
  316. schema: Symbol('schema'),
  317. finalizeConfig: Symbol('finalizeConfig'),
  318. preprocessConfig: Symbol('preprocessConfig')
  319. };
  320. // used to store calculate data for faster lookup
  321. const dataCache = new WeakMap();
  322. /**
  323. * Represents an array of config objects and provides method for working with
  324. * those config objects.
  325. */
  326. class ConfigArray extends Array {
  327. /**
  328. * Creates a new instance of ConfigArray.
  329. * @param {Iterable|Function|Object} configs An iterable yielding config
  330. * objects, or a config function, or a config object.
  331. * @param {string} [options.basePath=""] The path of the config file
  332. * @param {boolean} [options.normalized=false] Flag indicating if the
  333. * configs have already been normalized.
  334. * @param {Object} [options.schema] The additional schema
  335. * definitions to use for the ConfigArray schema.
  336. * @param {Array<string>} [options.configTypes] List of config types supported.
  337. */
  338. constructor(configs,
  339. {
  340. basePath = '',
  341. normalized = false,
  342. schema: customSchema,
  343. extraConfigTypes = []
  344. } = {}
  345. ) {
  346. super();
  347. /**
  348. * Tracks if the array has been normalized.
  349. * @property isNormalized
  350. * @type boolean
  351. * @private
  352. */
  353. this[ConfigArraySymbol.isNormalized] = normalized;
  354. /**
  355. * The schema used for validating and merging configs.
  356. * @property schema
  357. * @type ObjectSchema
  358. * @private
  359. */
  360. this[ConfigArraySymbol.schema] = new objectSchema.ObjectSchema({
  361. ...customSchema,
  362. ...baseSchema
  363. });
  364. /**
  365. * The path of the config file that this array was loaded from.
  366. * This is used to calculate filename matches.
  367. * @property basePath
  368. * @type string
  369. */
  370. this.basePath = basePath;
  371. assertExtraConfigTypes(extraConfigTypes);
  372. /**
  373. * The supported config types.
  374. * @property configTypes
  375. * @type Array<string>
  376. */
  377. this.extraConfigTypes = Object.freeze([...extraConfigTypes]);
  378. /**
  379. * A cache to store calculated configs for faster repeat lookup.
  380. * @property configCache
  381. * @type Map
  382. * @private
  383. */
  384. this[ConfigArraySymbol.configCache] = new Map();
  385. // init cache
  386. dataCache.set(this, {});
  387. // load the configs into this array
  388. if (Array.isArray(configs)) {
  389. this.push(...configs);
  390. } else {
  391. this.push(configs);
  392. }
  393. }
  394. /**
  395. * Prevent normal array methods from creating a new `ConfigArray` instance.
  396. * This is to ensure that methods such as `slice()` won't try to create a
  397. * new instance of `ConfigArray` behind the scenes as doing so may throw
  398. * an error due to the different constructor signature.
  399. * @returns {Function} The `Array` constructor.
  400. */
  401. static get [Symbol.species]() {
  402. return Array;
  403. }
  404. /**
  405. * Returns the `files` globs from every config object in the array.
  406. * This can be used to determine which files will be matched by a
  407. * config array or to use as a glob pattern when no patterns are provided
  408. * for a command line interface.
  409. * @returns {Array<string|Function>} An array of matchers.
  410. */
  411. get files() {
  412. assertNormalized(this);
  413. // if this data has been cached, retrieve it
  414. const cache = dataCache.get(this);
  415. if (cache.files) {
  416. return cache.files;
  417. }
  418. // otherwise calculate it
  419. const result = [];
  420. for (const config of this) {
  421. if (config.files) {
  422. config.files.forEach(filePattern => {
  423. result.push(filePattern);
  424. });
  425. }
  426. }
  427. // store result
  428. cache.files = result;
  429. dataCache.set(this, cache);
  430. return result;
  431. }
  432. /**
  433. * Returns ignore matchers that should always be ignored regardless of
  434. * the matching `files` fields in any configs. This is necessary to mimic
  435. * the behavior of things like .gitignore and .eslintignore, allowing a
  436. * globbing operation to be faster.
  437. * @returns {string[]} An array of string patterns and functions to be ignored.
  438. */
  439. get ignores() {
  440. assertNormalized(this);
  441. // if this data has been cached, retrieve it
  442. const cache = dataCache.get(this);
  443. if (cache.ignores) {
  444. return cache.ignores;
  445. }
  446. // otherwise calculate it
  447. const result = [];
  448. for (const config of this) {
  449. if (config.ignores && !config.files) {
  450. result.push(...config.ignores);
  451. }
  452. }
  453. // store result
  454. cache.ignores = result;
  455. dataCache.set(this, cache);
  456. return result;
  457. }
  458. /**
  459. * Indicates if the config array has been normalized.
  460. * @returns {boolean} True if the config array is normalized, false if not.
  461. */
  462. isNormalized() {
  463. return this[ConfigArraySymbol.isNormalized];
  464. }
  465. /**
  466. * Normalizes a config array by flattening embedded arrays and executing
  467. * config functions.
  468. * @param {ConfigContext} context The context object for config functions.
  469. * @returns {Promise<ConfigArray>} The current ConfigArray instance.
  470. */
  471. async normalize(context = {}) {
  472. if (!this.isNormalized()) {
  473. const normalizedConfigs = await normalize(this, context, this.extraConfigTypes);
  474. this.length = 0;
  475. this.push(...normalizedConfigs.map(this[ConfigArraySymbol.preprocessConfig].bind(this)));
  476. this[ConfigArraySymbol.isNormalized] = true;
  477. // prevent further changes
  478. Object.freeze(this);
  479. }
  480. return this;
  481. }
  482. /**
  483. * Normalizes a config array by flattening embedded arrays and executing
  484. * config functions.
  485. * @param {ConfigContext} context The context object for config functions.
  486. * @returns {ConfigArray} The current ConfigArray instance.
  487. */
  488. normalizeSync(context = {}) {
  489. if (!this.isNormalized()) {
  490. const normalizedConfigs = normalizeSync(this, context, this.extraConfigTypes);
  491. this.length = 0;
  492. this.push(...normalizedConfigs.map(this[ConfigArraySymbol.preprocessConfig]));
  493. this[ConfigArraySymbol.isNormalized] = true;
  494. // prevent further changes
  495. Object.freeze(this);
  496. }
  497. return this;
  498. }
  499. /**
  500. * Finalizes the state of a config before being cached and returned by
  501. * `getConfig()`. Does nothing by default but is provided to be
  502. * overridden by subclasses as necessary.
  503. * @param {Object} config The config to finalize.
  504. * @returns {Object} The finalized config.
  505. */
  506. [ConfigArraySymbol.finalizeConfig](config) {
  507. return config;
  508. }
  509. /**
  510. * Preprocesses a config during the normalization process. This is the
  511. * method to override if you want to convert an array item before it is
  512. * validated for the first time. For example, if you want to replace a
  513. * string with an object, this is the method to override.
  514. * @param {Object} config The config to preprocess.
  515. * @returns {Object} The config to use in place of the argument.
  516. */
  517. [ConfigArraySymbol.preprocessConfig](config) {
  518. return config;
  519. }
  520. /**
  521. * Returns the config object for a given file path.
  522. * @param {string} filePath The complete path of a file to get a config for.
  523. * @returns {Object} The config object for this file.
  524. */
  525. getConfig(filePath) {
  526. assertNormalized(this);
  527. // first check the cache to avoid duplicate work
  528. let finalConfig = this[ConfigArraySymbol.configCache].get(filePath);
  529. if (finalConfig) {
  530. return finalConfig;
  531. }
  532. // TODO: Maybe move elsewhere?
  533. const relativeFilePath = path.relative(this.basePath, filePath);
  534. if (shouldIgnoreFilePath(this.ignores, filePath, relativeFilePath)) {
  535. // cache and return result - finalConfig is undefined at this point
  536. this[ConfigArraySymbol.configCache].set(filePath, finalConfig);
  537. return finalConfig;
  538. }
  539. // filePath isn't automatically ignored, so try to construct config
  540. const matchingConfigs = [];
  541. for (const config of this) {
  542. if (pathMatches(filePath, this.basePath, config)) {
  543. debug(`Matching config found for ${filePath}`);
  544. matchingConfigs.push(config);
  545. } else {
  546. debug(`No matching config found for ${filePath}`);
  547. }
  548. }
  549. // if matching both files and ignores, there will be no config to create
  550. if (matchingConfigs.length === 0) {
  551. // cache and return result - finalConfig is undefined at this point
  552. this[ConfigArraySymbol.configCache].set(filePath, finalConfig);
  553. return finalConfig;
  554. }
  555. // otherwise construct the config
  556. finalConfig = matchingConfigs.reduce((result, config) => {
  557. return this[ConfigArraySymbol.schema].merge(result, config);
  558. }, {}, this);
  559. finalConfig = this[ConfigArraySymbol.finalizeConfig](finalConfig);
  560. this[ConfigArraySymbol.configCache].set(filePath, finalConfig);
  561. return finalConfig;
  562. }
  563. /**
  564. * Determines if the given filepath is ignored based on the configs.
  565. * @param {string} filePath The complete path of a file to check.
  566. * @returns {boolean} True if the path is ignored, false if not.
  567. */
  568. isIgnored(filePath) {
  569. return this.getConfig(filePath) === undefined;
  570. }
  571. }
  572. exports.ConfigArray = ConfigArray;
  573. exports.ConfigArraySymbol = ConfigArraySymbol;