@eik/common 4.1.1 → 5.0.0

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 (42) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/lib/classes/custom-error.js +8 -8
  3. package/lib/classes/eik-config.js +148 -150
  4. package/lib/classes/file-mapping.js +25 -25
  5. package/lib/classes/invalid-config-error.js +7 -7
  6. package/lib/classes/local-file-location.js +45 -52
  7. package/lib/classes/missing-config-error.js +7 -7
  8. package/lib/classes/multiple-config-sources-error.js +6 -6
  9. package/lib/classes/no-files-matched-error.js +8 -8
  10. package/lib/classes/read-file.js +25 -25
  11. package/lib/classes/remote-file-location.js +30 -30
  12. package/lib/classes/resolved-files.js +38 -46
  13. package/lib/classes/single-dest-multiple-source-error.js +9 -9
  14. package/lib/helpers/config-store.js +103 -103
  15. package/lib/helpers/get-defaults.js +24 -24
  16. package/lib/helpers/index.js +21 -21
  17. package/lib/helpers/local-assets.js +49 -49
  18. package/lib/helpers/path-slashes.js +8 -10
  19. package/lib/helpers/resolve-files.js +60 -65
  20. package/lib/helpers/type-slug.js +3 -3
  21. package/lib/helpers/type-title.js +4 -4
  22. package/lib/index.js +12 -12
  23. package/lib/schemas/assert.js +41 -41
  24. package/lib/schemas/index.js +7 -7
  25. package/lib/schemas/validate.js +64 -64
  26. package/lib/schemas/validation-error.js +11 -11
  27. package/lib/stream.js +11 -11
  28. package/lib/validators/index.js +37 -37
  29. package/package.json +14 -13
  30. package/types/classes/eik-config.d.ts +1 -1
  31. package/types/classes/file-mapping.d.ts +2 -2
  32. package/types/classes/invalid-config-error.d.ts +1 -1
  33. package/types/classes/missing-config-error.d.ts +1 -1
  34. package/types/classes/multiple-config-sources-error.d.ts +1 -1
  35. package/types/classes/no-files-matched-error.d.ts +1 -1
  36. package/types/classes/resolved-files.d.ts +1 -1
  37. package/types/classes/single-dest-multiple-source-error.d.ts +1 -1
  38. package/types/helpers/config-store.d.ts +1 -1
  39. package/types/helpers/index.d.ts +10 -10
  40. package/types/helpers/resolve-files.d.ts +1 -1
  41. package/types/index.d.ts +6 -6
  42. package/types/schemas/index.d.ts +3 -3
@@ -1,34 +1,34 @@
1
- import { isReadableStream } from '../stream.js';
1
+ import { isReadableStream } from "../stream.js";
2
2
 
3
3
  const ReadFile = class ReadFile {
4
- constructor({ mimeType = '', etag = '' } = {}) {
5
- this._mimeType = mimeType;
6
- // @ts-ignore
7
- this._stream = undefined;
8
- this._etag = etag;
9
- }
4
+ constructor({ mimeType = "", etag = "" } = {}) {
5
+ this._mimeType = mimeType;
6
+ // @ts-ignore
7
+ this._stream = undefined;
8
+ this._etag = etag;
9
+ }
10
10
 
11
- get mimeType() {
12
- return this._mimeType;
13
- }
11
+ get mimeType() {
12
+ return this._mimeType;
13
+ }
14
14
 
15
- set stream(value) {
16
- if (!isReadableStream(value))
17
- throw new Error('Value is not a Readable stream');
18
- this._stream = value;
19
- }
15
+ set stream(value) {
16
+ if (!isReadableStream(value))
17
+ throw new Error("Value is not a Readable stream");
18
+ this._stream = value;
19
+ }
20
20
 
21
- // @ts-ignore
22
- get stream() {
23
- return this._stream;
24
- }
21
+ // @ts-ignore
22
+ get stream() {
23
+ return this._stream;
24
+ }
25
25
 
26
- get etag() {
27
- return this._etag;
28
- }
26
+ get etag() {
27
+ return this._etag;
28
+ }
29
29
 
30
- get [Symbol.toStringTag]() {
31
- return 'ReadFile';
32
- }
30
+ get [Symbol.toStringTag]() {
31
+ return "ReadFile";
32
+ }
33
33
  };
34
34
  export default ReadFile;
@@ -1,41 +1,41 @@
1
1
  /**
2
2
  * @type {(value: unknown, message?: string) => asserts value}
3
3
  */
4
- import assert from 'node:assert';
5
- import { join } from 'node:path';
4
+ import assert from "node:assert";
5
+ import { join } from "node:path";
6
6
 
7
7
  class RemoteFileLocation {
8
- /**
9
- * @param {string} filePathname pathname for file relative to package root eg. /folder/client.js
10
- * @param {string} packagePathname pathname for package root eg. /pkg/my-pack/1.0.0
11
- * @param {string} origin server origin eg. https://server.com
12
- */
13
- constructor(filePathname, packagePathname, origin) {
14
- assert(
15
- typeof filePathname === 'string',
16
- '"filePathname" must be of type "string"',
17
- );
18
- assert(
19
- typeof packagePathname === 'string',
20
- '"packagePathname" must be of type "string"',
21
- );
22
- assert(typeof origin === 'string', '"origin" must be of type "string"');
8
+ /**
9
+ * @param {string} filePathname pathname for file relative to package root eg. /folder/client.js
10
+ * @param {string} packagePathname pathname for package root eg. /pkg/my-pack/1.0.0
11
+ * @param {string} origin server origin eg. https://server.com
12
+ */
13
+ constructor(filePathname, packagePathname, origin) {
14
+ assert(
15
+ typeof filePathname === "string",
16
+ '"filePathname" must be of type "string"',
17
+ );
18
+ assert(
19
+ typeof packagePathname === "string",
20
+ '"packagePathname" must be of type "string"',
21
+ );
22
+ assert(typeof origin === "string", '"origin" must be of type "string"');
23
23
 
24
- /**
25
- * @type {string} pathname to package root
26
- */
27
- this.packagePathname = new URL(packagePathname, origin).pathname;
24
+ /**
25
+ * @type {string} pathname to package root
26
+ */
27
+ this.packagePathname = new URL(packagePathname, origin).pathname;
28
28
 
29
- /**
30
- * @type {string} pathname to file relative to package root
31
- */
32
- this.filePathname = new URL(filePathname, origin).pathname;
29
+ /**
30
+ * @type {string} pathname to file relative to package root
31
+ */
32
+ this.filePathname = new URL(filePathname, origin).pathname;
33
33
 
34
- /**
35
- * @type {URL} WHATWG URL object containing the full remote URL for the file
36
- */
37
- this.url = new URL(join(packagePathname, filePathname), origin);
38
- }
34
+ /**
35
+ * @type {URL} WHATWG URL object containing the full remote URL for the file
36
+ */
37
+ this.url = new URL(join(packagePathname, filePathname), origin);
38
+ }
39
39
  }
40
40
 
41
41
  export default RemoteFileLocation;
@@ -1,58 +1,50 @@
1
1
  /**
2
2
  * @type {(value: unknown, message?: string) => asserts value}
3
3
  */
4
- import assert from 'node:assert';
5
- import LocalFileLocation from './local-file-location.js';
4
+ import assert from "node:assert";
5
+ import LocalFileLocation from "./local-file-location.js";
6
6
  import {
7
- removeLeadingSlash,
8
- removeTrailingSlash,
9
- } from '../helpers/path-slashes.js';
7
+ removeLeadingSlash,
8
+ removeTrailingSlash,
9
+ } from "../helpers/path-slashes.js";
10
10
 
11
- const originalFiles = Symbol('files');
11
+ const originalFiles = Symbol("files");
12
12
 
13
13
  class ResolvedFiles {
14
- /**
15
- * @param {string[]} files
16
- * @param {{definition: [string, string], basePath: string, pattern: string}} meta
17
- */
18
- constructor(files, meta) {
19
- assert(Array.isArray(files), '"files" must be an array');
20
- assert(meta, '"meta" must be defined');
21
- assert(
22
- typeof meta.basePath,
23
- '"meta.basePath" must be a non empty string',
24
- );
25
- assert(
26
- typeof meta.pattern,
27
- '"meta.pattern" must be a non empty string',
28
- );
29
- assert(
30
- Array.isArray(meta.definition),
31
- '"meta.definition" must be an array',
32
- );
33
- assert(
34
- meta.definition.length === 2,
35
- '"meta.definition" must be an array of length 2',
36
- );
14
+ /**
15
+ * @param {string[]} files
16
+ * @param {{definition: [string, string], basePath: string, pattern: string}} meta
17
+ */
18
+ constructor(files, meta) {
19
+ assert(Array.isArray(files), '"files" must be an array');
20
+ assert(meta, '"meta" must be defined');
21
+ assert(typeof meta.basePath, '"meta.basePath" must be a non empty string');
22
+ assert(typeof meta.pattern, '"meta.pattern" must be a non empty string');
23
+ assert(
24
+ Array.isArray(meta.definition),
25
+ '"meta.definition" must be an array',
26
+ );
27
+ assert(
28
+ meta.definition.length === 2,
29
+ '"meta.definition" must be an array of length 2',
30
+ );
37
31
 
38
- const [destination, source] = meta.definition;
39
- this[originalFiles] = files;
40
- this.destination = destination;
41
- this.source = source;
42
- this.basePath = meta.basePath;
43
- this.pattern = meta.pattern;
44
- }
32
+ const [destination, source] = meta.definition;
33
+ this[originalFiles] = files;
34
+ this.destination = destination;
35
+ this.source = source;
36
+ this.basePath = meta.basePath;
37
+ this.pattern = meta.pattern;
38
+ }
45
39
 
46
- *[Symbol.iterator]() {
47
- for (const file of this[originalFiles]) {
48
- const relative = removeTrailingSlash(
49
- removeLeadingSlash(
50
- file.replace(/\\/g, '/').replace(this.basePath, ''),
51
- ),
52
- );
53
- yield new LocalFileLocation(relative, this.basePath);
54
- }
55
- }
40
+ *[Symbol.iterator]() {
41
+ for (const file of this[originalFiles]) {
42
+ const relative = removeTrailingSlash(
43
+ removeLeadingSlash(file.replace(/\\/g, "/").replace(this.basePath, "")),
44
+ );
45
+ yield new LocalFileLocation(relative, this.basePath);
46
+ }
47
+ }
56
48
  }
57
49
 
58
50
  export default ResolvedFiles;
@@ -1,12 +1,12 @@
1
- import CustomError from './custom-error.js';
1
+ import CustomError from "./custom-error.js";
2
2
 
3
3
  export default class SingleDestMultipleSourcesError extends CustomError {
4
- /**
5
- * @param {string} destFilePath
6
- */
7
- constructor(destFilePath) {
8
- super(
9
- `Cannot specify a single file destination for multiple source files. See '${destFilePath}'`,
10
- );
11
- }
4
+ /**
5
+ * @param {string} destFilePath
6
+ */
7
+ constructor(destFilePath) {
8
+ super(
9
+ `Cannot specify a single file destination for multiple source files. See '${destFilePath}'`,
10
+ );
11
+ }
12
12
  }
@@ -1,10 +1,10 @@
1
- import { readFileSync, writeFileSync } from 'node:fs';
2
- import { join, dirname } from 'node:path';
3
- import os from 'node:os';
4
- import EikConfig from '../classes/eik-config.js';
5
- import MissingConfigError from '../classes/missing-config-error.js';
6
- import MultipleConfigSourcesError from '../classes/multiple-config-sources-error.js';
7
- import InvalidConfigError from '../classes/invalid-config-error.js';
1
+ import { readFileSync, writeFileSync } from "node:fs";
2
+ import { join, dirname } from "node:path";
3
+ import os from "node:os";
4
+ import EikConfig from "../classes/eik-config.js";
5
+ import MissingConfigError from "../classes/missing-config-error.js";
6
+ import MultipleConfigSourcesError from "../classes/multiple-config-sources-error.js";
7
+ import InvalidConfigError from "../classes/invalid-config-error.js";
8
8
 
9
9
  const homedir = os.homedir();
10
10
 
@@ -16,104 +16,104 @@ const homedir = os.homedir();
16
16
  * @returns {object}
17
17
  */
18
18
  function readJSONFromDisk(path) {
19
- let fileData;
20
- try {
21
- fileData = readFileSync(path, { encoding: 'utf8' });
22
- // eslint-disable-next-line no-unused-vars
23
- } catch (e) {
24
- // @ts-ignore
25
- return null;
26
- }
27
- return JSON.parse(fileData);
19
+ let fileData;
20
+ try {
21
+ fileData = readFileSync(path, { encoding: "utf8" });
22
+ // eslint-disable-next-line no-unused-vars
23
+ } catch (e) {
24
+ // @ts-ignore
25
+ return null;
26
+ }
27
+ return JSON.parse(fileData);
28
28
  }
29
29
 
30
30
  export default {
31
- /**
32
- * Load the configuration from an exact path and return an EikConfig object
33
- *
34
- * @param {string} configFilePathname
35
- * @param {function} loadJSONFromDisk
36
- *
37
- * @returns {EikConfig}
38
- */
39
- loadFromPath(configFilePathname, loadJSONFromDisk = readJSONFromDisk) {
40
- const eikJSON = loadJSONFromDisk(configFilePathname);
41
- if (!eikJSON) {
42
- throw new MissingConfigError(dirname(configFilePathname));
43
- }
44
- let assets = eikJSON;
45
- // detect package.json
46
- if (eikJSON.eik) {
47
- assets = {
48
- name: eikJSON.name,
49
- version: eikJSON.version,
50
- ...eikJSON.eik,
51
- };
52
- }
53
- const eikrc = loadJSONFromDisk(join(homedir, '.eikrc')) || {};
54
- return new EikConfig(assets, eikrc.tokens, dirname(configFilePathname));
55
- },
56
- /**
57
- * Tries to find the configuration for eik in the provided directory.
58
- *
59
- * @param {string} configRootDir The base directory for the eik project.
60
- * @param {function} [loadJSONFromDisk] The function to use to load the file from disk.
61
- *
62
- * @returns {EikConfig}
63
- */
64
- findInDirectory(configRootDir, loadJSONFromDisk = readJSONFromDisk) {
65
- const pkgJSON = loadJSONFromDisk(join(configRootDir, 'package.json'));
66
- const eikJSON = loadJSONFromDisk(join(configRootDir, 'eik.json'));
67
- if (
68
- pkgJSON != null &&
69
- Object.prototype.hasOwnProperty.call(pkgJSON, 'eik') &&
70
- eikJSON != null
71
- ) {
72
- throw new MultipleConfigSourcesError();
73
- }
74
- let assets;
75
- if (pkgJSON) {
76
- const { name, version, eik } = pkgJSON;
77
- if (eik) {
78
- assets = { name, version, ...pkgJSON.eik };
79
- }
80
- }
81
- if (eikJSON) {
82
- assets = { ...assets, ...eikJSON };
83
- }
84
- if (assets == null) {
85
- throw new MissingConfigError(configRootDir);
86
- }
87
- const eikrc = loadJSONFromDisk(join(homedir, '.eikrc')) || {};
88
- const config = new EikConfig(assets, eikrc.tokens, configRootDir);
89
- try {
90
- config.validate();
91
- } catch (err) {
92
- throw new InvalidConfigError(
93
- // @ts-ignore
94
- `config.findInDirectory operation failed: ${err.message}`,
95
- );
96
- }
97
- return config;
98
- },
31
+ /**
32
+ * Load the configuration from an exact path and return an EikConfig object
33
+ *
34
+ * @param {string} configFilePathname
35
+ * @param {function} loadJSONFromDisk
36
+ *
37
+ * @returns {EikConfig}
38
+ */
39
+ loadFromPath(configFilePathname, loadJSONFromDisk = readJSONFromDisk) {
40
+ const eikJSON = loadJSONFromDisk(configFilePathname);
41
+ if (!eikJSON) {
42
+ throw new MissingConfigError(dirname(configFilePathname));
43
+ }
44
+ let assets = eikJSON;
45
+ // detect package.json
46
+ if (eikJSON.eik) {
47
+ assets = {
48
+ name: eikJSON.name,
49
+ version: eikJSON.version,
50
+ ...eikJSON.eik,
51
+ };
52
+ }
53
+ const eikrc = loadJSONFromDisk(join(homedir, ".eikrc")) || {};
54
+ return new EikConfig(assets, eikrc.tokens, dirname(configFilePathname));
55
+ },
56
+ /**
57
+ * Tries to find the configuration for eik in the provided directory.
58
+ *
59
+ * @param {string} configRootDir The base directory for the eik project.
60
+ * @param {function} [loadJSONFromDisk] The function to use to load the file from disk.
61
+ *
62
+ * @returns {EikConfig}
63
+ */
64
+ findInDirectory(configRootDir, loadJSONFromDisk = readJSONFromDisk) {
65
+ const pkgJSON = loadJSONFromDisk(join(configRootDir, "package.json"));
66
+ const eikJSON = loadJSONFromDisk(join(configRootDir, "eik.json"));
67
+ if (
68
+ pkgJSON != null &&
69
+ Object.prototype.hasOwnProperty.call(pkgJSON, "eik") &&
70
+ eikJSON != null
71
+ ) {
72
+ throw new MultipleConfigSourcesError();
73
+ }
74
+ let assets;
75
+ if (pkgJSON) {
76
+ const { name, version, eik } = pkgJSON;
77
+ if (eik) {
78
+ assets = { name, version, ...pkgJSON.eik };
79
+ }
80
+ }
81
+ if (eikJSON) {
82
+ assets = { ...assets, ...eikJSON };
83
+ }
84
+ if (assets == null) {
85
+ throw new MissingConfigError(configRootDir);
86
+ }
87
+ const eikrc = loadJSONFromDisk(join(homedir, ".eikrc")) || {};
88
+ const config = new EikConfig(assets, eikrc.tokens, configRootDir);
89
+ try {
90
+ config.validate();
91
+ } catch (err) {
92
+ throw new InvalidConfigError(
93
+ // @ts-ignore
94
+ `config.findInDirectory operation failed: ${err.message}`,
95
+ );
96
+ }
97
+ return config;
98
+ },
99
99
 
100
- /**
101
- * Persist config changes to disk as <cwd>/eik.json
102
- *
103
- * @param {import('../classes/eik-config.js')} config
104
- */
105
- persistToDisk(config) {
106
- try {
107
- // @ts-ignore
108
- config.validate();
109
- } catch (err) {
110
- throw new InvalidConfigError(
111
- // @ts-ignore
112
- `config.persistToDisk operation failed: ${err.message}`,
113
- );
114
- }
115
- // @ts-ignore
116
- const dest = join(config.cwd, 'eik.json');
117
- writeFileSync(dest, JSON.stringify(config, null, 2));
118
- },
100
+ /**
101
+ * Persist config changes to disk as <cwd>/eik.json
102
+ *
103
+ * @param {import('../classes/eik-config.js')} config
104
+ */
105
+ persistToDisk(config) {
106
+ try {
107
+ // @ts-ignore
108
+ config.validate();
109
+ } catch (err) {
110
+ throw new InvalidConfigError(
111
+ // @ts-ignore
112
+ `config.persistToDisk operation failed: ${err.message}`,
113
+ );
114
+ }
115
+ // @ts-ignore
116
+ const dest = join(config.cwd, "eik.json");
117
+ writeFileSync(dest, JSON.stringify(config, null, 2));
118
+ },
119
119
  };
@@ -1,7 +1,7 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import configStore from './config-store.js';
4
- import EikConfig from '../classes/eik-config.js';
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import configStore from "./config-store.js";
4
+ import EikConfig from "../classes/eik-config.js";
5
5
  /**
6
6
  * Sets up and returns an object containing a set of default values for the app context.
7
7
  * Default values are fetched from the app's eik.json or package.json file as well as from .eikrc, if present in the users home directory.
@@ -11,24 +11,24 @@ import EikConfig from '../classes/eik-config.js';
11
11
  * @returns {import("../classes/eik-config.js").default} EikConfig
12
12
  */
13
13
  export default function getDefaults(directoryOrFilepath) {
14
- try {
15
- const stats = fs.statSync(directoryOrFilepath);
16
- if (stats.isDirectory()) {
17
- return configStore.findInDirectory(directoryOrFilepath);
18
- } else {
19
- return configStore.loadFromPath(directoryOrFilepath);
20
- }
21
- } catch (error) {
22
- const e = /** @type {Error} */ (error);
23
- if (e.constructor.name === 'MissingConfigError') {
24
- // assume directory
25
- let cwd = directoryOrFilepath;
26
- // detect exact file and get its directory
27
- if (path.extname(directoryOrFilepath)) {
28
- cwd = path.dirname(directoryOrFilepath);
29
- }
30
- return new EikConfig(null, [], cwd);
31
- }
32
- throw e;
33
- }
14
+ try {
15
+ const stats = fs.statSync(directoryOrFilepath);
16
+ if (stats.isDirectory()) {
17
+ return configStore.findInDirectory(directoryOrFilepath);
18
+ } else {
19
+ return configStore.loadFromPath(directoryOrFilepath);
20
+ }
21
+ } catch (error) {
22
+ const e = /** @type {Error} */ (error);
23
+ if (e.constructor.name === "MissingConfigError") {
24
+ // assume directory
25
+ let cwd = directoryOrFilepath;
26
+ // detect exact file and get its directory
27
+ if (path.extname(directoryOrFilepath)) {
28
+ cwd = path.dirname(directoryOrFilepath);
29
+ }
30
+ return new EikConfig(null, [], cwd);
31
+ }
32
+ throw e;
33
+ }
34
34
  }
@@ -1,25 +1,25 @@
1
- import localAssets from './local-assets.js';
2
- import getDefaults from './get-defaults.js';
3
- import configStore from './config-store.js';
4
- import typeSlug from './type-slug.js';
5
- import typeTitle from './type-title.js';
6
- import resolveFiles from './resolve-files.js';
1
+ import localAssets from "./local-assets.js";
2
+ import getDefaults from "./get-defaults.js";
3
+ import configStore from "./config-store.js";
4
+ import typeSlug from "./type-slug.js";
5
+ import typeTitle from "./type-title.js";
6
+ import resolveFiles from "./resolve-files.js";
7
7
  import {
8
- addTrailingSlash,
9
- removeTrailingSlash,
10
- addLeadingSlash,
11
- removeLeadingSlash,
12
- } from './path-slashes.js';
8
+ addTrailingSlash,
9
+ removeTrailingSlash,
10
+ addLeadingSlash,
11
+ removeLeadingSlash,
12
+ } from "./path-slashes.js";
13
13
 
14
14
  export default {
15
- localAssets,
16
- getDefaults,
17
- configStore,
18
- typeSlug,
19
- typeTitle,
20
- addTrailingSlash,
21
- removeTrailingSlash,
22
- addLeadingSlash,
23
- removeLeadingSlash,
24
- resolveFiles,
15
+ localAssets,
16
+ getDefaults,
17
+ configStore,
18
+ typeSlug,
19
+ typeTitle,
20
+ addTrailingSlash,
21
+ removeTrailingSlash,
22
+ addLeadingSlash,
23
+ removeLeadingSlash,
24
+ resolveFiles,
25
25
  };