@docusaurus/utils 2.0.0-beta.16 → 2.0.0-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/lib/constants.d.ts +51 -1
  2. package/lib/constants.d.ts.map +1 -1
  3. package/lib/constants.js +57 -10
  4. package/lib/constants.js.map +1 -1
  5. package/lib/dataFileUtils.d.ts +38 -2
  6. package/lib/dataFileUtils.d.ts.map +1 -1
  7. package/lib/dataFileUtils.js +39 -13
  8. package/lib/dataFileUtils.js.map +1 -1
  9. package/lib/emitUtils.d.ts +32 -0
  10. package/lib/emitUtils.d.ts.map +1 -0
  11. package/lib/emitUtils.js +80 -0
  12. package/lib/emitUtils.js.map +1 -0
  13. package/lib/gitUtils.d.ts +54 -5
  14. package/lib/gitUtils.d.ts.map +1 -1
  15. package/lib/gitUtils.js +17 -14
  16. package/lib/gitUtils.js.map +1 -1
  17. package/lib/globUtils.d.ts +28 -0
  18. package/lib/globUtils.d.ts.map +1 -1
  19. package/lib/globUtils.js +36 -13
  20. package/lib/globUtils.js.map +1 -1
  21. package/lib/hashUtils.d.ts +5 -4
  22. package/lib/hashUtils.d.ts.map +1 -1
  23. package/lib/hashUtils.js +7 -6
  24. package/lib/hashUtils.js.map +1 -1
  25. package/lib/i18nUtils.d.ts +51 -0
  26. package/lib/i18nUtils.d.ts.map +1 -0
  27. package/lib/i18nUtils.js +69 -0
  28. package/lib/i18nUtils.js.map +1 -0
  29. package/lib/index.d.ts +10 -54
  30. package/lib/index.d.ts.map +1 -1
  31. package/lib/index.js +56 -256
  32. package/lib/index.js.map +1 -1
  33. package/lib/jsUtils.d.ts +45 -0
  34. package/lib/jsUtils.d.ts.map +1 -0
  35. package/lib/jsUtils.js +94 -0
  36. package/lib/jsUtils.js.map +1 -0
  37. package/lib/markdownLinks.d.ts +48 -5
  38. package/lib/markdownLinks.d.ts.map +1 -1
  39. package/lib/markdownLinks.js +29 -13
  40. package/lib/markdownLinks.js.map +1 -1
  41. package/lib/markdownUtils.d.ts +112 -0
  42. package/lib/markdownUtils.d.ts.map +1 -0
  43. package/lib/markdownUtils.js +271 -0
  44. package/lib/markdownUtils.js.map +1 -0
  45. package/lib/pathUtils.d.ts +2 -1
  46. package/lib/pathUtils.d.ts.map +1 -1
  47. package/lib/pathUtils.js +16 -7
  48. package/lib/pathUtils.js.map +1 -1
  49. package/lib/slugger.d.ts +10 -0
  50. package/lib/slugger.d.ts.map +1 -1
  51. package/lib/slugger.js +6 -2
  52. package/lib/slugger.js.map +1 -1
  53. package/lib/tags.d.ts +42 -10
  54. package/lib/tags.d.ts.map +1 -1
  55. package/lib/tags.js +40 -26
  56. package/lib/tags.js.map +1 -1
  57. package/lib/urlUtils.d.ts +57 -0
  58. package/lib/urlUtils.d.ts.map +1 -1
  59. package/lib/urlUtils.js +132 -6
  60. package/lib/urlUtils.js.map +1 -1
  61. package/lib/webpackUtils.d.ts +5 -0
  62. package/lib/webpackUtils.d.ts.map +1 -1
  63. package/lib/webpackUtils.js +8 -5
  64. package/lib/webpackUtils.js.map +1 -1
  65. package/package.json +11 -11
  66. package/src/constants.ts +65 -9
  67. package/src/dataFileUtils.ts +44 -12
  68. package/src/emitUtils.ts +99 -0
  69. package/src/gitUtils.ts +77 -17
  70. package/src/globUtils.ts +34 -13
  71. package/src/hashUtils.ts +6 -5
  72. package/src/i18nUtils.ts +115 -0
  73. package/src/index.ts +43 -307
  74. package/src/jsUtils.ts +102 -0
  75. package/src/markdownLinks.ts +71 -28
  76. package/src/markdownUtils.ts +354 -0
  77. package/src/pathUtils.ts +15 -7
  78. package/src/slugger.ts +13 -1
  79. package/src/tags.ts +53 -28
  80. package/src/urlUtils.ts +145 -7
  81. package/src/webpackUtils.ts +11 -4
  82. package/lib/markdownParser.d.ts +0 -32
  83. package/lib/markdownParser.d.ts.map +0 -1
  84. package/lib/markdownParser.js +0 -161
  85. package/lib/markdownParser.js.map +0 -1
  86. package/src/markdownParser.ts +0 -201
@@ -4,17 +4,67 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
+ /** Node major version, directly read from env. */
7
8
  export declare const NODE_MAJOR_VERSION: number;
9
+ /** Node minor version, directly read from env. */
8
10
  export declare const NODE_MINOR_VERSION: number;
11
+ /** Docusaurus core version. */
12
+ export declare const DOCUSAURUS_VERSION: any;
13
+ /**
14
+ * Can be overridden with cli option `--out-dir`. Code should generally use
15
+ * `context.outDir` instead (which is always absolute and localized).
16
+ */
9
17
  export declare const DEFAULT_BUILD_DIR_NAME = "build";
18
+ /**
19
+ * Can be overridden with cli option `--config`. Code should generally use
20
+ * `context.siteConfigPath` instead (which is always absolute).
21
+ */
10
22
  export declare const DEFAULT_CONFIG_FILE_NAME = "docusaurus.config.js";
23
+ /** Can be absolute or relative to site directory. */
11
24
  export declare const BABEL_CONFIG_FILE_NAME: string;
25
+ /**
26
+ * Can be absolute or relative to site directory. Code should generally use
27
+ * `context.generatedFilesDir` instead (which is always absolute).
28
+ */
12
29
  export declare const GENERATED_FILES_DIR_NAME: string;
30
+ /**
31
+ * We would assume all of the site's JS code lives in here and not outside.
32
+ * Relative to the site directory.
33
+ */
13
34
  export declare const SRC_DIR_NAME = "src";
14
- export declare const STATIC_DIR_NAME = "static";
35
+ /**
36
+ * Can be overridden with `config.staticDirectories`. Code should use
37
+ * `context.siteConfig.staticDirectories` instead (which is always absolute).
38
+ */
39
+ export declare const DEFAULT_STATIC_DIR_NAME = "static";
40
+ /**
41
+ * Files here are handled by webpack, hashed (can be cached aggressively).
42
+ * Relative to the build output folder.
43
+ */
15
44
  export declare const OUTPUT_STATIC_ASSETS_DIR_NAME = "assets";
45
+ /**
46
+ * Components in this directory will receive the `@theme` alias and be able to
47
+ * shadow default theme components.
48
+ */
16
49
  export declare const THEME_PATH: string;
50
+ /**
51
+ * All translation-related data live here, relative to site directory. Content
52
+ * will be namespaced by locale.
53
+ */
54
+ export declare const I18N_DIR_NAME = "i18n";
55
+ /**
56
+ * Translations for React code.
57
+ */
58
+ export declare const CODE_TRANSLATIONS_FILE_NAME = "code.json";
59
+ /** Dev server opens on this port by default. */
17
60
  export declare const DEFAULT_PORT = 3000;
61
+ /** Default plugin ID. */
18
62
  export declare const DEFAULT_PLUGIN_ID = "default";
63
+ /**
64
+ * Allow overriding the limit after which the url loader will no longer inline
65
+ * assets.
66
+ *
67
+ * @see https://github.com/facebook/docusaurus/issues/5493
68
+ */
19
69
  export declare const WEBPACK_URL_LOADER_LIMIT: string | number;
20
70
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,eAAO,MAAM,kBAAkB,QAG9B,CAAC;AACF,eAAO,MAAM,kBAAkB,QAG9B,CAAC;AAGF,eAAO,MAAM,sBAAsB,UAAU,CAAC;AAG9C,eAAO,MAAM,wBAAwB,yBAAyB,CAAC;AAE/D,eAAO,MAAM,sBAAsB,QACiC,CAAC;AAErE,eAAO,MAAM,wBAAwB,QAC6B,CAAC;AAEnE,eAAO,MAAM,YAAY,QAAQ,CAAC;AAClC,eAAO,MAAM,eAAe,WAAW,CAAC;AACxC,eAAO,MAAM,6BAA6B,WAAW,CAAC;AACtD,eAAO,MAAM,UAAU,QAA0B,CAAC;AAClD,eAAO,MAAM,YAAY,OAAO,CAAC;AACjC,eAAO,MAAM,iBAAiB,YAAY,CAAC;AAG3C,eAAO,MAAM,wBAAwB,iBACU,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,kDAAkD;AAClD,eAAO,MAAM,kBAAkB,QAG9B,CAAC;AACF,kDAAkD;AAClD,eAAO,MAAM,kBAAkB,QAG9B,CAAC;AAEF,+BAA+B;AAE/B,eAAO,MAAM,kBAAkB,KAAqC,CAAC;AAErE;;;GAGG;AACH,eAAO,MAAM,sBAAsB,UAAU,CAAC;AAE9C;;;GAGG;AACH,eAAO,MAAM,wBAAwB,yBAAyB,CAAC;AAE/D,qDAAqD;AACrD,eAAO,MAAM,sBAAsB,QACiC,CAAC;AAErE;;;GAGG;AACH,eAAO,MAAM,wBAAwB,QAC6B,CAAC;AAEnE;;;GAGG;AACH,eAAO,MAAM,YAAY,QAAQ,CAAC;AAElC;;;GAGG;AACH,eAAO,MAAM,uBAAuB,WAAW,CAAC;AAEhD;;;GAGG;AACH,eAAO,MAAM,6BAA6B,WAAW,CAAC;AAEtD;;;GAGG;AACH,eAAO,MAAM,UAAU,QAA0B,CAAC;AAElD;;;GAGG;AACH,eAAO,MAAM,aAAa,SAAS,CAAC;AAEpC;;GAEG;AACH,eAAO,MAAM,2BAA2B,cAAc,CAAC;AAEvD,gDAAgD;AAChD,eAAO,MAAM,YAAY,OAAO,CAAC;AAEjC,yBAAyB;AACzB,eAAO,MAAM,iBAAiB,YAAY,CAAC;AAE3C;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,iBACU,CAAC"}
package/lib/constants.js CHANGED
@@ -5,23 +5,70 @@
5
5
  * This source code is licensed under the MIT license found in the
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
- var _a;
9
8
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.WEBPACK_URL_LOADER_LIMIT = exports.DEFAULT_PLUGIN_ID = exports.DEFAULT_PORT = exports.THEME_PATH = exports.OUTPUT_STATIC_ASSETS_DIR_NAME = exports.STATIC_DIR_NAME = exports.SRC_DIR_NAME = exports.GENERATED_FILES_DIR_NAME = exports.BABEL_CONFIG_FILE_NAME = exports.DEFAULT_CONFIG_FILE_NAME = exports.DEFAULT_BUILD_DIR_NAME = exports.NODE_MINOR_VERSION = exports.NODE_MAJOR_VERSION = void 0;
9
+ exports.WEBPACK_URL_LOADER_LIMIT = exports.DEFAULT_PLUGIN_ID = exports.DEFAULT_PORT = exports.CODE_TRANSLATIONS_FILE_NAME = exports.I18N_DIR_NAME = exports.THEME_PATH = exports.OUTPUT_STATIC_ASSETS_DIR_NAME = exports.DEFAULT_STATIC_DIR_NAME = exports.SRC_DIR_NAME = exports.GENERATED_FILES_DIR_NAME = exports.BABEL_CONFIG_FILE_NAME = exports.DEFAULT_CONFIG_FILE_NAME = exports.DEFAULT_BUILD_DIR_NAME = exports.DOCUSAURUS_VERSION = exports.NODE_MINOR_VERSION = exports.NODE_MAJOR_VERSION = void 0;
10
+ /** Node major version, directly read from env. */
11
11
  exports.NODE_MAJOR_VERSION = parseInt(process.versions.node.split('.')[0], 10);
12
+ /** Node minor version, directly read from env. */
12
13
  exports.NODE_MINOR_VERSION = parseInt(process.versions.node.split('.')[1], 10);
13
- // Can be overridden with cli option --out-dir
14
+ /** Docusaurus core version. */
15
+ // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
16
+ exports.DOCUSAURUS_VERSION = require('../package.json').version;
17
+ /**
18
+ * Can be overridden with cli option `--out-dir`. Code should generally use
19
+ * `context.outDir` instead (which is always absolute and localized).
20
+ */
14
21
  exports.DEFAULT_BUILD_DIR_NAME = 'build';
15
- // Can be overridden with cli option --config
22
+ /**
23
+ * Can be overridden with cli option `--config`. Code should generally use
24
+ * `context.siteConfigPath` instead (which is always absolute).
25
+ */
16
26
  exports.DEFAULT_CONFIG_FILE_NAME = 'docusaurus.config.js';
17
- exports.BABEL_CONFIG_FILE_NAME = process.env.DOCUSAURUS_BABEL_CONFIG_FILE_NAME || 'babel.config.js';
18
- exports.GENERATED_FILES_DIR_NAME = process.env.DOCUSAURUS_GENERATED_FILES_DIR_NAME || '.docusaurus';
27
+ /** Can be absolute or relative to site directory. */
28
+ exports.BABEL_CONFIG_FILE_NAME = process.env.DOCUSAURUS_BABEL_CONFIG_FILE_NAME ?? 'babel.config.js';
29
+ /**
30
+ * Can be absolute or relative to site directory. Code should generally use
31
+ * `context.generatedFilesDir` instead (which is always absolute).
32
+ */
33
+ exports.GENERATED_FILES_DIR_NAME = process.env.DOCUSAURUS_GENERATED_FILES_DIR_NAME ?? '.docusaurus';
34
+ /**
35
+ * We would assume all of the site's JS code lives in here and not outside.
36
+ * Relative to the site directory.
37
+ */
19
38
  exports.SRC_DIR_NAME = 'src';
20
- exports.STATIC_DIR_NAME = 'static';
21
- exports.OUTPUT_STATIC_ASSETS_DIR_NAME = 'assets'; // files handled by webpack, hashed (can be cached aggressively)
39
+ /**
40
+ * Can be overridden with `config.staticDirectories`. Code should use
41
+ * `context.siteConfig.staticDirectories` instead (which is always absolute).
42
+ */
43
+ exports.DEFAULT_STATIC_DIR_NAME = 'static';
44
+ /**
45
+ * Files here are handled by webpack, hashed (can be cached aggressively).
46
+ * Relative to the build output folder.
47
+ */
48
+ exports.OUTPUT_STATIC_ASSETS_DIR_NAME = 'assets';
49
+ /**
50
+ * Components in this directory will receive the `@theme` alias and be able to
51
+ * shadow default theme components.
52
+ */
22
53
  exports.THEME_PATH = `${exports.SRC_DIR_NAME}/theme`;
54
+ /**
55
+ * All translation-related data live here, relative to site directory. Content
56
+ * will be namespaced by locale.
57
+ */
58
+ exports.I18N_DIR_NAME = 'i18n';
59
+ /**
60
+ * Translations for React code.
61
+ */
62
+ exports.CODE_TRANSLATIONS_FILE_NAME = 'code.json';
63
+ /** Dev server opens on this port by default. */
23
64
  exports.DEFAULT_PORT = 3000;
65
+ /** Default plugin ID. */
24
66
  exports.DEFAULT_PLUGIN_ID = 'default';
25
- // Temporary fix for https://github.com/facebook/docusaurus/issues/5493
26
- exports.WEBPACK_URL_LOADER_LIMIT = (_a = process.env.WEBPACK_URL_LOADER_LIMIT) !== null && _a !== void 0 ? _a : 10000;
67
+ /**
68
+ * Allow overriding the limit after which the url loader will no longer inline
69
+ * assets.
70
+ *
71
+ * @see https://github.com/facebook/docusaurus/issues/5493
72
+ */
73
+ exports.WEBPACK_URL_LOADER_LIMIT = process.env.WEBPACK_URL_LOADER_LIMIT ?? 10000;
27
74
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEU,QAAA,kBAAkB,GAAG,QAAQ,CACxC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EACnC,EAAE,CACH,CAAC;AACW,QAAA,kBAAkB,GAAG,QAAQ,CACxC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EACnC,EAAE,CACH,CAAC;AAEF,8CAA8C;AACjC,QAAA,sBAAsB,GAAG,OAAO,CAAC;AAE9C,6CAA6C;AAChC,QAAA,wBAAwB,GAAG,sBAAsB,CAAC;AAElD,QAAA,sBAAsB,GACjC,OAAO,CAAC,GAAG,CAAC,iCAAiC,IAAI,iBAAiB,CAAC;AAExD,QAAA,wBAAwB,GACnC,OAAO,CAAC,GAAG,CAAC,mCAAmC,IAAI,aAAa,CAAC;AAEtD,QAAA,YAAY,GAAG,KAAK,CAAC;AACrB,QAAA,eAAe,GAAG,QAAQ,CAAC;AAC3B,QAAA,6BAA6B,GAAG,QAAQ,CAAC,CAAC,gEAAgE;AAC1G,QAAA,UAAU,GAAG,GAAG,oBAAY,QAAQ,CAAC;AACrC,QAAA,YAAY,GAAG,IAAI,CAAC;AACpB,QAAA,iBAAiB,GAAG,SAAS,CAAC;AAE3C,uEAAuE;AAC1D,QAAA,wBAAwB,GACnC,MAAA,OAAO,CAAC,GAAG,CAAC,wBAAwB,mCAAI,KAAK,CAAC"}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,kDAAkD;AACrC,QAAA,kBAAkB,GAAG,QAAQ,CACxC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,EACpC,EAAE,CACH,CAAC;AACF,kDAAkD;AACrC,QAAA,kBAAkB,GAAG,QAAQ,CACxC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,EACpC,EAAE,CACH,CAAC;AAEF,+BAA+B;AAC/B,8EAA8E;AACjE,QAAA,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC,OAAO,CAAC;AAErE;;;GAGG;AACU,QAAA,sBAAsB,GAAG,OAAO,CAAC;AAE9C;;;GAGG;AACU,QAAA,wBAAwB,GAAG,sBAAsB,CAAC;AAE/D,qDAAqD;AACxC,QAAA,sBAAsB,GACjC,OAAO,CAAC,GAAG,CAAC,iCAAiC,IAAI,iBAAiB,CAAC;AAErE;;;GAGG;AACU,QAAA,wBAAwB,GACnC,OAAO,CAAC,GAAG,CAAC,mCAAmC,IAAI,aAAa,CAAC;AAEnE;;;GAGG;AACU,QAAA,YAAY,GAAG,KAAK,CAAC;AAElC;;;GAGG;AACU,QAAA,uBAAuB,GAAG,QAAQ,CAAC;AAEhD;;;GAGG;AACU,QAAA,6BAA6B,GAAG,QAAQ,CAAC;AAEtD;;;GAGG;AACU,QAAA,UAAU,GAAG,GAAG,oBAAY,QAAQ,CAAC;AAElD;;;GAGG;AACU,QAAA,aAAa,GAAG,MAAM,CAAC;AAEpC;;GAEG;AACU,QAAA,2BAA2B,GAAG,WAAW,CAAC;AAEvD,gDAAgD;AACnC,QAAA,YAAY,GAAG,IAAI,CAAC;AAEjC,yBAAyB;AACZ,QAAA,iBAAiB,GAAG,SAAS,CAAC;AAE3C;;;;;GAKG;AACU,QAAA,wBAAwB,GACnC,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,KAAK,CAAC"}
@@ -6,19 +6,55 @@
6
6
  */
7
7
  import type { ContentPaths } from './markdownLinks';
8
8
  declare type DataFileParams = {
9
+ /** Path to the potential data file, relative to `contentPaths` */
9
10
  filePath: string;
11
+ /**
12
+ * Includes the base path and localized path, both of which are eligible for
13
+ * sourcing data files. Both paths should be absolute.
14
+ */
10
15
  contentPaths: ContentPaths;
11
16
  };
17
+ /**
18
+ * Looks for a data file in the potential content paths; loads a localized data
19
+ * file in priority.
20
+ *
21
+ * @returns An absolute path to the data file, or `undefined` if there isn't one.
22
+ */
12
23
  export declare function getDataFilePath({ filePath, contentPaths, }: DataFileParams): Promise<string | undefined>;
13
24
  /**
14
- * Looks up for a data file in the content paths, returns the normalized object.
15
- * Throws when validation fails; returns undefined when file not found
25
+ * Looks up for a data file in the content paths, returns the object validated
26
+ * and normalized according to the `validate` callback.
27
+ *
28
+ * @returns `undefined` when file not found
29
+ * @throws Throws when validation fails, displaying a helpful context message.
16
30
  */
17
31
  export declare function getDataFileData<T>(params: DataFileParams & {
32
+ /** Used for the "The X file looks invalid" message. */
18
33
  fileType: string;
19
34
  }, validate: (content: unknown) => T): Promise<T | undefined>;
35
+ /**
36
+ * Takes the `contentPaths` data structure and returns an ordered path list
37
+ * indicating their priorities. For all data, we look in the localized folder
38
+ * in priority.
39
+ */
20
40
  export declare function getContentPathList(contentPaths: ContentPaths): string[];
41
+ /**
42
+ * @param folderPaths a list of absolute paths.
43
+ * @param relativeFilePath file path relative to each `folderPaths`.
44
+ * @returns the first folder path in which the file exists, or `undefined` if
45
+ * none is found.
46
+ */
21
47
  export declare function findFolderContainingFile(folderPaths: string[], relativeFilePath: string): Promise<string | undefined>;
48
+ /**
49
+ * Fail-fast alternative to `findFolderContainingFile`.
50
+ *
51
+ * @param folderPaths a list of absolute paths.
52
+ * @param relativeFilePath file path relative to each `folderPaths`.
53
+ * @returns the first folder path in which the file exists.
54
+ * @throws Throws if no file can be found. You should use this method only when
55
+ * you actually know the file exists (e.g. when the `relativeFilePath` is read
56
+ * with a glob and you are just trying to localize it)
57
+ */
22
58
  export declare function getFolderContainingFile(folderPaths: string[], relativeFilePath: string): Promise<string>;
23
59
  export {};
24
60
  //# sourceMappingURL=dataFileUtils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dataFileUtils.d.ts","sourceRoot":"","sources":["../src/dataFileUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAGlD,aAAK,cAAc,GAAG;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,YAAY,CAAC;CAC5B,CAAC;AAEF,wBAAsB,eAAe,CAAC,EACpC,QAAQ,EACR,YAAY,GACb,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAU9C;AAED;;;GAGG;AACH,wBAAsB,eAAe,CAAC,CAAC,EACrC,MAAM,EAAE,cAAc,GAAG;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAC,EAC3C,QAAQ,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,CAAC,GAChC,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAcxB;AAGD,wBAAgB,kBAAkB,CAAC,YAAY,EAAE,YAAY,GAAG,MAAM,EAAE,CAEvE;AAGD,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,EAAE,EACrB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAI7B;AAED,wBAAsB,uBAAuB,CAC3C,WAAW,EAAE,MAAM,EAAE,EACrB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,CAcjB"}
1
+ {"version":3,"file":"dataFileUtils.d.ts","sourceRoot":"","sources":["../src/dataFileUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,iBAAiB,CAAC;AAGlD,aAAK,cAAc,GAAG;IACpB,kEAAkE;IAClE,QAAQ,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,YAAY,EAAE,YAAY,CAAC;CAC5B,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,eAAe,CAAC,EACpC,QAAQ,EACR,YAAY,GACb,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAS9C;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,CAAC,EACrC,MAAM,EAAE,cAAc,GAAG;IACvB,uDAAuD;IACvD,QAAQ,EAAE,MAAM,CAAC;CAClB,EACD,QAAQ,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,CAAC,GAChC,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAaxB;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,YAAY,EAAE,YAAY,GAAG,MAAM,EAAE,CAEvE;AAED;;;;;GAKG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,EAAE,EACrB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAI7B;AAED;;;;;;;;;GASG;AACH,wBAAsB,uBAAuB,CAC3C,WAAW,EAAE,MAAM,EAAE,EACrB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,CAYjB"}
@@ -8,23 +8,31 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.getFolderContainingFile = exports.findFolderContainingFile = exports.getContentPathList = exports.getDataFileData = exports.getDataFilePath = void 0;
10
10
  const tslib_1 = require("tslib");
11
- const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
12
- const js_yaml_1 = (0, tslib_1.__importDefault)(require("js-yaml"));
13
- const path_1 = (0, tslib_1.__importDefault)(require("path"));
11
+ const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
12
+ const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
13
+ const path_1 = tslib_1.__importDefault(require("path"));
14
14
  const index_1 = require("./index");
15
- const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
15
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
16
+ /**
17
+ * Looks for a data file in the potential content paths; loads a localized data
18
+ * file in priority.
19
+ *
20
+ * @returns An absolute path to the data file, or `undefined` if there isn't one.
21
+ */
16
22
  async function getDataFilePath({ filePath, contentPaths, }) {
17
- // Loads a localized data file in priority
18
23
  const contentPath = await findFolderContainingFile(getContentPathList(contentPaths), filePath);
19
24
  if (contentPath) {
20
- return path_1.default.join(contentPath, filePath);
25
+ return path_1.default.resolve(contentPath, filePath);
21
26
  }
22
27
  return undefined;
23
28
  }
24
29
  exports.getDataFilePath = getDataFilePath;
25
30
  /**
26
- * Looks up for a data file in the content paths, returns the normalized object.
27
- * Throws when validation fails; returns undefined when file not found
31
+ * Looks up for a data file in the content paths, returns the object validated
32
+ * and normalized according to the `validate` callback.
33
+ *
34
+ * @returns `undefined` when file not found
35
+ * @throws Throws when validation fails, displaying a helpful context message.
28
36
  */
29
37
  async function getDataFileData(params, validate) {
30
38
  const filePath = await getDataFilePath(params);
@@ -37,27 +45,45 @@ async function getDataFileData(params, validate) {
37
45
  return validate(unsafeContent);
38
46
  }
39
47
  catch (err) {
40
- // TODO replace later by error cause, see https://v8.dev/features/error-cause
41
48
  logger_1.default.error `The ${params.fileType} file at path=${filePath} looks invalid.`;
42
49
  throw err;
43
50
  }
44
51
  }
45
52
  exports.getDataFileData = getDataFileData;
46
- // Order matters: we look in priority in localized folder
53
+ /**
54
+ * Takes the `contentPaths` data structure and returns an ordered path list
55
+ * indicating their priorities. For all data, we look in the localized folder
56
+ * in priority.
57
+ */
47
58
  function getContentPathList(contentPaths) {
48
59
  return [contentPaths.contentPathLocalized, contentPaths.contentPath];
49
60
  }
50
61
  exports.getContentPathList = getContentPathList;
51
- // return the first folder path in which the file exists in
62
+ /**
63
+ * @param folderPaths a list of absolute paths.
64
+ * @param relativeFilePath file path relative to each `folderPaths`.
65
+ * @returns the first folder path in which the file exists, or `undefined` if
66
+ * none is found.
67
+ */
52
68
  async function findFolderContainingFile(folderPaths, relativeFilePath) {
53
69
  return (0, index_1.findAsyncSequential)(folderPaths, (folderPath) => fs_extra_1.default.pathExists(path_1.default.join(folderPath, relativeFilePath)));
54
70
  }
55
71
  exports.findFolderContainingFile = findFolderContainingFile;
72
+ /**
73
+ * Fail-fast alternative to `findFolderContainingFile`.
74
+ *
75
+ * @param folderPaths a list of absolute paths.
76
+ * @param relativeFilePath file path relative to each `folderPaths`.
77
+ * @returns the first folder path in which the file exists.
78
+ * @throws Throws if no file can be found. You should use this method only when
79
+ * you actually know the file exists (e.g. when the `relativeFilePath` is read
80
+ * with a glob and you are just trying to localize it)
81
+ */
56
82
  async function getFolderContainingFile(folderPaths, relativeFilePath) {
57
83
  const maybeFolderPath = await findFolderContainingFile(folderPaths, relativeFilePath);
58
- // should never happen, as the source was read from the FS anyway...
59
84
  if (!maybeFolderPath) {
60
- throw new Error(`File "${relativeFilePath}" does not exist in any of these folders:\n- ${folderPaths.join('\n- ')}]`);
85
+ throw new Error(`File "${relativeFilePath}" does not exist in any of these folders:
86
+ - ${folderPaths.join('\n- ')}`);
61
87
  }
62
88
  return maybeFolderPath;
63
89
  }
@@ -1 +1 @@
1
- {"version":3,"file":"dataFileUtils.js","sourceRoot":"","sources":["../src/dataFileUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,qEAA0B;AAC1B,mEAA2B;AAC3B,6DAAwB;AACxB,mCAA4C;AAE5C,6EAAwC;AAOjC,KAAK,UAAU,eAAe,CAAC,EACpC,QAAQ,EACR,YAAY,GACG;IACf,0CAA0C;IAC1C,MAAM,WAAW,GAAG,MAAM,wBAAwB,CAChD,kBAAkB,CAAC,YAAY,CAAC,EAChC,QAAQ,CACT,CAAC;IACF,IAAI,WAAW,EAAE;QACf,OAAO,cAAI,CAAC,IAAI,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;KACzC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAbD,0CAaC;AAED;;;GAGG;AACI,KAAK,UAAU,eAAe,CACnC,MAA2C,EAC3C,QAAiC;IAEjC,MAAM,QAAQ,GAAG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AAC,CAAC;AACvE,CAAC;AAFD,gDAEC;AAED;;;;;GAKG;AACI,KAAK,UAAU,wBAAwB,CAC5C,WAAqB,EACrB,gBAAwB;IAExB,OAAO,IAAA,2BAAmB,EAAC,WAAW,EAAE,CAAC,UAAU,EAAE,EAAE,CACrD,kBAAE,CAAC,UAAU,CAAC,cAAI,CAAC,IAAI,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC,CACvD,CAAC;AACJ,CAAC;AAPD,4DAOC;AAED;;;;;;;;;GASG;AACI,KAAK,UAAU,uBAAuB,CAC3C,WAAqB,EACrB,gBAAwB;IAExB,MAAM,eAAe,GAAG,MAAM,wBAAwB,CACpD,WAAW,EACX,gBAAgB,CACjB,CAAC;IACF,IAAI,CAAC,eAAe,EAAE;QACpB,MAAM,IAAI,KAAK,CACb,SAAS,gBAAgB;IAC3B,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CACzB,CAAC;KACH;IACD,OAAO,eAAe,CAAC;AACzB,CAAC;AAfD,0DAeC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ /// <reference types="node" />
8
+ /**
9
+ * Outputs a file to the generated files directory. Only writes files if content
10
+ * differs from cache (for hot reload performance).
11
+ *
12
+ * @param generatedFilesDir Absolute path.
13
+ * @param file Path relative to `generatedFilesDir`. File will always be
14
+ * outputted; no need to ensure directory exists.
15
+ * @param content String content to write.
16
+ * @param skipCache If `true` (defaults as `true` for production), file is
17
+ * force-rewritten, skipping cache.
18
+ */
19
+ export declare function generate(generatedFilesDir: string, file: string, content: string, skipCache?: boolean): Promise<void>;
20
+ /**
21
+ * @param permalink The URL that the HTML file corresponds to, without base URL
22
+ * @param outDir Full path to the output directory
23
+ * @param trailingSlash The site config option. If provided, only one path will
24
+ * be read.
25
+ * @returns This returns a buffer, which you have to decode string yourself if
26
+ * needed. (Not always necessary since the output isn't for human consumption
27
+ * anyways, and most HTML manipulation libs accept buffers)
28
+ * @throws Throws when the HTML file is not found at any of the potential paths.
29
+ * This should never happen as it would lead to a 404.
30
+ */
31
+ export declare function readOutputHTMLFile(permalink: string, outDir: string, trailingSlash: boolean | undefined): Promise<Buffer>;
32
+ //# sourceMappingURL=emitUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"emitUtils.d.ts","sourceRoot":"","sources":["../src/emitUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;;AASH;;;;;;;;;;GAUG;AACH,wBAAsB,QAAQ,CAC5B,iBAAiB,EAAE,MAAM,EACzB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,SAAS,GAAE,OAA+C,GACzD,OAAO,CAAC,IAAI,CAAC,CAgCf;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,kBAAkB,CACtC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,aAAa,EAAE,OAAO,GAAG,SAAS,GACjC,OAAO,CAAC,MAAM,CAAC,CAmBjB"}
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.readOutputHTMLFile = exports.generate = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const path_1 = tslib_1.__importDefault(require("path"));
12
+ const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
13
+ const crypto_1 = require("crypto");
14
+ const jsUtils_1 = require("./jsUtils");
15
+ const fileHash = new Map();
16
+ /**
17
+ * Outputs a file to the generated files directory. Only writes files if content
18
+ * differs from cache (for hot reload performance).
19
+ *
20
+ * @param generatedFilesDir Absolute path.
21
+ * @param file Path relative to `generatedFilesDir`. File will always be
22
+ * outputted; no need to ensure directory exists.
23
+ * @param content String content to write.
24
+ * @param skipCache If `true` (defaults as `true` for production), file is
25
+ * force-rewritten, skipping cache.
26
+ */
27
+ async function generate(generatedFilesDir, file, content, skipCache = process.env.NODE_ENV === 'production') {
28
+ const filepath = path_1.default.resolve(generatedFilesDir, file);
29
+ if (skipCache) {
30
+ await fs_extra_1.default.outputFile(filepath, content);
31
+ // Cache still needs to be reset, otherwise, writing "A", "B", and "A" where
32
+ // "B" skips cache will cause the last "A" not be able to overwrite as the
33
+ // first "A" remains in cache. But if the file never existed in cache, no
34
+ // need to register it.
35
+ if (fileHash.get(filepath)) {
36
+ fileHash.set(filepath, (0, crypto_1.createHash)('md5').update(content).digest('hex'));
37
+ }
38
+ return;
39
+ }
40
+ let lastHash = fileHash.get(filepath);
41
+ // If file already exists but it's not in runtime cache yet, we try to
42
+ // calculate the content hash and then compare. This is to avoid unnecessary
43
+ // overwriting and we can reuse old file.
44
+ if (!lastHash && (await fs_extra_1.default.pathExists(filepath))) {
45
+ const lastContent = await fs_extra_1.default.readFile(filepath, 'utf8');
46
+ lastHash = (0, crypto_1.createHash)('md5').update(lastContent).digest('hex');
47
+ fileHash.set(filepath, lastHash);
48
+ }
49
+ const currentHash = (0, crypto_1.createHash)('md5').update(content).digest('hex');
50
+ if (lastHash !== currentHash) {
51
+ await fs_extra_1.default.outputFile(filepath, content);
52
+ fileHash.set(filepath, currentHash);
53
+ }
54
+ }
55
+ exports.generate = generate;
56
+ /**
57
+ * @param permalink The URL that the HTML file corresponds to, without base URL
58
+ * @param outDir Full path to the output directory
59
+ * @param trailingSlash The site config option. If provided, only one path will
60
+ * be read.
61
+ * @returns This returns a buffer, which you have to decode string yourself if
62
+ * needed. (Not always necessary since the output isn't for human consumption
63
+ * anyways, and most HTML manipulation libs accept buffers)
64
+ * @throws Throws when the HTML file is not found at any of the potential paths.
65
+ * This should never happen as it would lead to a 404.
66
+ */
67
+ async function readOutputHTMLFile(permalink, outDir, trailingSlash) {
68
+ const withTrailingSlashPath = path_1.default.join(outDir, permalink, 'index.html');
69
+ const withoutTrailingSlashPath = path_1.default.join(outDir, `${permalink.replace(/\/$/, '')}.html`);
70
+ const HTMLPath = await (0, jsUtils_1.findAsyncSequential)([
71
+ trailingSlash !== false && withTrailingSlashPath,
72
+ trailingSlash !== true && withoutTrailingSlashPath,
73
+ ].filter((p) => Boolean(p)), fs_extra_1.default.pathExists);
74
+ if (!HTMLPath) {
75
+ throw new Error(`Expected output HTML file to be found at ${withTrailingSlashPath}.`);
76
+ }
77
+ return fs_extra_1.default.readFile(HTMLPath);
78
+ }
79
+ exports.readOutputHTMLFile = readOutputHTMLFile;
80
+ //# sourceMappingURL=emitUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"emitUtils.js","sourceRoot":"","sources":["../src/emitUtils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,wDAAwB;AACxB,gEAA0B;AAC1B,mCAAkC;AAClC,uCAA8C;AAE9C,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;AAE3C;;;;;;;;;;GAUG;AACI,KAAK,UAAU,QAAQ,CAC5B,iBAAyB,EACzB,IAAY,EACZ,OAAe,EACf,YAAqB,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;IAE1D,MAAM,QAAQ,GAAG,cAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE,IAAI,CAAC,CAAC;IAEvD,IAAI,SAAS,EAAE;QACb,MAAM,kBAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACvC,4EAA4E;QAC5E,0EAA0E;QAC1E,yEAAyE;QACzE,uBAAuB;QACvB,IAAI,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;YAC1B,QAAQ,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAA,mBAAU,EAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;SACzE;QACD,OA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AAI,IAAI,wBAAwB;KACnD,CAAC,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EACxC,kBAAE,CAAC,UAAU,CACd,CAAC;IACF,IAAI,CAAC,QAAQ,EAAE;QACb,MAAM,IAAI,KAAK,CACb,4CAA4C,qBAAqB,GAAG,CACrE,CAAC;KACH;IACD,OAAO,kBAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAC/B,CAAC;AAvBD,gDAuBC"}
package/lib/gitUtils.d.ts CHANGED
@@ -4,14 +4,63 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
+ /** Custom error thrown when git is not found in `PATH`. */
7
8
  export declare class GitNotFoundError extends Error {
8
9
  }
9
- export declare const getFileCommitDate: (file: string, { age, includeAuthor, }: {
10
- age?: "oldest" | "newest" | undefined;
11
- includeAuthor?: boolean | undefined;
12
- }) => {
10
+ /** Custom error thrown when the current file is not tracked by git. */
11
+ export declare class FileNotTrackedError extends Error {
12
+ }
13
+ /**
14
+ * Fetches the git history of a file and returns a relevant commit date.
15
+ * It gets the commit date instead of author date so that amended commits
16
+ * can have their dates updated.
17
+ *
18
+ * @throws {@link GitNotFoundError} If git is not found in `PATH`.
19
+ * @throws {@link FileNotTrackedError} If the current file is not tracked by git.
20
+ * @throws Also throws when `git log` exited with non-zero, or when it outputs
21
+ * unexpected text.
22
+ */
23
+ export declare function getFileCommitDate(
24
+ /** Absolute path to the file. */
25
+ file: string, args: {
26
+ /**
27
+ * `"oldest"` is the commit that added the file, following renames;
28
+ * `"newest"` is the last commit that edited the file.
29
+ */
30
+ age?: 'oldest' | 'newest';
31
+ /** Use `includeAuthor: true` to get the author information as well. */
32
+ includeAuthor?: false;
33
+ }): {
34
+ /** Relevant commit date. */
35
+ date: Date;
36
+ /** Timestamp in **seconds**, as returned from git. */
37
+ timestamp: number;
38
+ };
39
+ /**
40
+ * Fetches the git history of a file and returns a relevant commit date.
41
+ * It gets the commit date instead of author date so that amended commits
42
+ * can have their dates updated.
43
+ *
44
+ * @throws {@link GitNotFoundError} If git is not found in `PATH`.
45
+ * @throws {@link FileNotTrackedError} If the current file is not tracked by git.
46
+ * @throws Also throws when `git log` exited with non-zero, or when it outputs
47
+ * unexpected text.
48
+ */
49
+ export declare function getFileCommitDate(
50
+ /** Absolute path to the file. */
51
+ file: string, args: {
52
+ /**
53
+ * `"oldest"` is the commit that added the file, following renames;
54
+ * `"newest"` is the last commit that edited the file.
55
+ */
56
+ age?: 'oldest' | 'newest';
57
+ includeAuthor: true;
58
+ }): {
59
+ /** Relevant commit date. */
13
60
  date: Date;
61
+ /** Timestamp in **seconds**, as returned from git. */
14
62
  timestamp: number;
15
- author?: string | undefined;
63
+ /** The author's name, as returned from git. */
64
+ author: string;
16
65
  };
17
66
  //# sourceMappingURL=gitUtils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"gitUtils.d.ts","sourceRoot":"","sources":["../src/gitUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,qBAAa,gBAAiB,SAAQ,KAAK;CAAG;AAE9C,eAAO,MAAM,iBAAiB,SACtB,MAAM;;;;UASN,IAAI;eACC,MAAM;;CAqElB,CAAC"}
1
+ {"version":3,"file":"gitUtils.d.ts","sourceRoot":"","sources":["../src/gitUtils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,2DAA2D;AAC3D,qBAAa,gBAAiB,SAAQ,KAAK;CAAG;AAE9C,uEAAuE;AACvE,qBAAa,mBAAoB,SAAQ,KAAK;CAAG;AAEjD;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB;AAC/B,iCAAiC;AACjC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE;IACJ;;;OAGG;IACH,GAAG,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAC;IAC1B,uEAAuE;IACvE,aAAa,CAAC,EAAE,KAAK,CAAC;CACvB,GACA;IACD,4BAA4B;IAC5B,IAAI,EAAE,IAAI,CAAC;IACX,sDAAsD;IACtD,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AACF;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB;AAC/B,iCAAiC;AACjC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE;IACJ;;;OAGG;IACH,GAAG,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAC;IAC1B,aAAa,EAAE,IAAI,CAAC;CACrB,GACA;IACD,4BAA4B;IAC5B,IAAI,EAAE,IAAI,CAAC;IACX,sDAAsD;IACtD,SAAS,EAAE,MAAM,CAAC;IAClB,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC"}
package/lib/gitUtils.js CHANGED
@@ -6,22 +6,25 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.getFileCommitDate = exports.GitNotFoundError = void 0;
9
+ exports.getFileCommitDate = exports.FileNotTrackedError = exports.GitNotFoundError = void 0;
10
10
  const tslib_1 = require("tslib");
11
- const path_1 = (0, tslib_1.__importDefault)(require("path"));
12
- const shelljs_1 = (0, tslib_1.__importDefault)(require("shelljs"));
11
+ const path_1 = tslib_1.__importDefault(require("path"));
12
+ const shelljs_1 = tslib_1.__importDefault(require("shelljs"));
13
+ /** Custom error thrown when git is not found in `PATH`. */
13
14
  class GitNotFoundError extends Error {
14
15
  }
15
16
  exports.GitNotFoundError = GitNotFoundError;
16
- const getFileCommitDate = (file, { age = 'oldest', includeAuthor = false, }) => {
17
+ /** Custom error thrown when the current file is not tracked by git. */
18
+ class FileNotTrackedError extends Error {
19
+ }
20
+ exports.FileNotTrackedError = FileNotTrackedError;
21
+ function getFileCommitDate(file, { age = 'oldest', includeAuthor = false, }) {
17
22
  if (!shelljs_1.default.which('git')) {
18
23
  throw new GitNotFoundError(`Failed to retrieve git history for "${file}" because git is not installed.`);
19
24
  }
20
25
  if (!shelljs_1.default.test('-f', file)) {
21
26
  throw new Error(`Failed to retrieve git history for "${file}" because the file does not exist.`);
22
27
  }
23
- const fileBasename = path_1.default.basename(file);
24
- const fileDirname = path_1.default.dirname(file);
25
28
  let formatArg = '--format=%ct';
26
29
  if (includeAuthor) {
27
30
  formatArg += ',%an';
@@ -32,9 +35,9 @@ const getFileCommitDate = (file, { age = 'oldest', includeAuthor = false, }) =>
32
35
  // --diff-filter=A ensures we only get the commit which (A)dded the file
33
36
  extraArgs += ' --follow --diff-filter=A';
34
37
  }
35
- const result = shelljs_1.default.exec(`git log ${extraArgs} ${formatArg} -- "${fileBasename}"`, {
36
- // cwd is important, see: https://github.com/facebook/docusaurus/pull/5048
37
- cwd: fileDirname,
38
+ const result = shelljs_1.default.exec(`git log ${extraArgs} ${formatArg} -- "${path_1.default.basename(file)}"`, {
39
+ // Setting cwd is important, see: https://github.com/facebook/docusaurus/pull/5048
40
+ cwd: path_1.default.dirname(file),
38
41
  silent: true,
39
42
  });
40
43
  if (result.code !== 0) {
@@ -45,11 +48,11 @@ const getFileCommitDate = (file, { age = 'oldest', includeAuthor = false, }) =>
45
48
  regex = /^(?<timestamp>\d+),(?<author>.+)$/;
46
49
  }
47
50
  const output = result.stdout.trim();
51
+ if (!output) {
52
+ throw new FileNotTrackedError(`Failed to retrieve the git history for file "${file}" because the file is not tracked by git.`);
53
+ }
48
54
  const match = output.match(regex);
49
- if (!match ||
50
- !match.groups ||
51
- !match.groups.timestamp ||
52
- (includeAuthor && !match.groups.author)) {
55
+ if (!match) {
53
56
  throw new Error(`Failed to retrieve the git history for file "${file}" with unexpected output: ${output}`);
54
57
  }
55
58
  const timestamp = Number(match.groups.timestamp);
@@ -58,6 +61,6 @@ const getFileCommitDate = (file, { age = 'oldest', includeAuthor = false, }) =>
58
61
  return { date, timestamp, author: match.groups.author };
59
62
  }
60
63
  return { date, timestamp };
61
- };
64
+ }
62
65
  exports.getFileCommitDate = getFileCommitDate;
63
66
  //# sourceMappingURL=gitUtils.js.map
@@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