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.

README.md 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # Config Array
  2. by [Nicholas C. Zakas](https://humanwhocodes.com)
  3. If you find this useful, please consider supporting my work with a [donation](https://humanwhocodes.com/donate).
  4. ## Description
  5. A config array is a way of managing configurations that are based on glob pattern matching of filenames. Each config array contains the information needed to determine the correct configuration for any file based on the filename.
  6. ## Background
  7. In 2019, I submitted an [ESLint RFC](https://github.com/eslint/rfcs/pull/9) proposing a new way of configuring ESLint. The goal was to streamline what had become an increasingly complicated configuration process. Over several iterations, this proposal was eventually born.
  8. The basic idea is that all configuration, including overrides, can be represented by a single array where each item in the array is a config object. Config objects appearing later in the array override config objects appearing earlier in the array. You can calculate a config for a given file by traversing all config objects in the array to find the ones that match the filename. Matching is done by specifying glob patterns in `files` and `ignores` properties on each config object. Here's an example:
  9. ```js
  10. export default [
  11. // match all JSON files
  12. {
  13. name: "JSON Handler",
  14. files: ["**/*.json"],
  15. handler: jsonHandler
  16. },
  17. // match only package.json
  18. {
  19. name: "package.json Handler",
  20. files: ["package.json"],
  21. handler: packageJsonHandler
  22. }
  23. ];
  24. ```
  25. In this example, there are two config objects: the first matches all JSON files in all directories and the second matches just `package.json` in the base path directory (all the globs are evaluated as relative to a base path that can be specified). When you retrieve a configuration for `foo.json`, only the first config object matches so `handler` is equal to `jsonHandler`; when you retrieve a configuration for `package.json`, `handler` is equal to `packageJsonHandler` (because both config objects match, the second one wins).
  26. ## Installation
  27. You can install the package using npm or Yarn:
  28. ```bash
  29. npm install @humanwhocodes/config-array --save
  30. # or
  31. yarn add @humanwhocodes/config-array
  32. ```
  33. ## Usage
  34. First, import the `ConfigArray` constructor:
  35. ```js
  36. import { ConfigArray } from "@humanwhocodes/config-array";
  37. // or using CommonJS
  38. const { ConfigArray } = require("@humanwhocodes/config-array");
  39. ```
  40. When you create a new instance of `ConfigArray`, you must pass in two arguments: an array of configs and an options object. The array of configs is most likely read in from a configuration file, so here's a typical example:
  41. ```js
  42. const configFilename = path.resolve(process.cwd(), "my.config.js");
  43. const { default: rawConfigs } = await import(configFilename);
  44. const configs = new ConfigArray(rawConfigs, {
  45. // the path to match filenames from
  46. basePath: process.cwd(),
  47. // additional items in each config
  48. schema: mySchema
  49. });
  50. ```
  51. This example reads in an object or array from `my.config.js` and passes it into the `ConfigArray` constructor as the first argument. The second argument is an object specifying the `basePath` (the directoy in which `my.config.js` is found) and a `schema` to define the additional properties of a config object beyond `files`, `ignores`, and `name`.
  52. ### Specifying a Schema
  53. The `schema` option is required for you to use additional properties in config objects. The schema is object that follows the format of an [`ObjectSchema`](https://npmjs.com/package/@humanwhocodes/object-schema). The schema specifies both validation and merge rules that the `ConfigArray` instance needs to combine configs when there are multiple matches. Here's an example:
  54. ```js
  55. const configFilename = path.resolve(process.cwd(), "my.config.js");
  56. const { default: rawConfigs } = await import(configFilename);
  57. const mySchema = {
  58. // define the handler key in configs
  59. handler: {
  60. required: true,
  61. merge(a, b) {
  62. if (!b) return a;
  63. if (!a) return b;
  64. },
  65. validate(value) {
  66. if (typeof value !== "function") {
  67. throw new TypeError("Function expected.");
  68. }
  69. }
  70. }
  71. };
  72. const configs = new ConfigArray(rawConfigs, {
  73. // the path to match filenames from
  74. basePath: process.cwd(),
  75. // additional items in each config
  76. schema: mySchema
  77. });
  78. ```
  79. ### Config Arrays
  80. Config arrays can be multidimensional, so it's possible for a config array to contain another config array, such as:
  81. ```js
  82. export default [
  83. // JS config
  84. {
  85. files: ["**/*.js"],
  86. handler: jsHandler
  87. },
  88. // JSON configs
  89. [
  90. // match all JSON files
  91. {
  92. name: "JSON Handler",
  93. files: ["**/*.json"],
  94. handler: jsonHandler
  95. },
  96. // match only package.json
  97. {
  98. name: "package.json Handler",
  99. files: ["package.json"],
  100. handler: packageJsonHandler
  101. }
  102. ],
  103. // filename must match function
  104. {
  105. files: [ filePath => filePath.endsWith(".md") ],
  106. handler: markdownHandler
  107. },
  108. // filename must match all patterns in subarray
  109. {
  110. files: [ ["*.test.*", "*.js"] ],
  111. handler: jsTestHandler
  112. },
  113. // filename must not match patterns beginning with !
  114. {
  115. name: "Non-JS files",
  116. files: ["!*.js"],
  117. settings: {
  118. js: false
  119. }
  120. }
  121. ];
  122. ```
  123. In this example, the array contains both config objects and a config array. When a config array is normalized (see details below), it is flattened so only config objects remain. However, the order of evaluation remains the same.
  124. If the `files` array contains a function, then that function is called with the absolute path of the file and is expected to return `true` if there is a match and `false` if not. (The `ignores` array can also contain functions.)
  125. If the `files` array contains an item that is an array of strings and functions, then all patterns must match in order for the config to match. In the preceding examples, both `*.test.*` and `*.js` must match in order for the config object to be used.
  126. If a pattern in the files array begins with `!` then it excludes that pattern. In the preceding example, any filename that doesn't end with `.js` will automatically getting a `settings.js` property set to `false`.
  127. ### Config Functions
  128. Config arrays can also include config functions. A config function accepts a single parameter, `context` (defined by you), and must return either a config object or a config array (it cannot return another function). Config functions allow end users to execute code in the creation of appropriate config objects. Here's an example:
  129. ```js
  130. export default [
  131. // JS config
  132. {
  133. files: ["**/*.js"],
  134. handler: jsHandler
  135. },
  136. // JSON configs
  137. function (context) {
  138. return [
  139. // match all JSON files
  140. {
  141. name: context.name + " JSON Handler",
  142. files: ["**/*.json"],
  143. handler: jsonHandler
  144. },
  145. // match only package.json
  146. {
  147. name: context.name + " package.json Handler",
  148. files: ["package.json"],
  149. handler: packageJsonHandler
  150. }
  151. ];
  152. }
  153. ];
  154. ```
  155. When a config array is normalized, each function is executed and replaced in the config array with the return value.
  156. **Note:** Config functions cannot be async. This will be added in a future version.
  157. ### Normalizing Config Arrays
  158. Once a config array has been created and loaded with all of the raw config data, it must be normalized before it can be used. The normalization process goes through and flattens the config array as well as executing all config functions to get their final values.
  159. To normalize a config array, call the `normalize()` method and pass in a context object:
  160. ```js
  161. await configs.normalize({
  162. name: "MyApp"
  163. });
  164. ```
  165. The `normalize()` method returns a promise, so be sure to use the `await` operator. The config array instance is normalized in-place, so you don't need to create a new variable.
  166. **Important:** Once a `ConfigArray` is normalized, it cannot be changed further. You can, however, create a new `ConfigArray` and pass in the normalized instance to create an unnormalized copy.
  167. ### Getting Config for a File
  168. To get the config for a file, use the `getConfig()` method on a normalized config array and pass in the filename to get a config for:
  169. ```js
  170. // pass in absolute filename
  171. const fileConfig = configs.getConfig(path.resolve(process.cwd(), "package.json"));
  172. ```
  173. The config array always returns an object, even if there are no configs matching the given filename. You can then inspect the returned config object to determine how to proceed.
  174. A few things to keep in mind:
  175. * You must pass in the absolute filename to get a config for.
  176. * The returned config object never has `files`, `ignores`, or `name` properties; the only properties on the object will be the other configuration options specified.
  177. * The config array caches configs, so subsequent calls to `getConfig()` with the same filename will return in a fast lookup rather than another calculation.
  178. ## Acknowledgements
  179. The design of this project was influenced by feedback on the ESLint RFC, and incorporates ideas from:
  180. * Teddy Katz (@not-an-aardvark)
  181. * Toru Nagashima (@mysticatea)
  182. * Kai Cataldo (@kaicataldo)
  183. ## License
  184. Apache 2.0