@eik/cli 3.1.3 → 3.1.5

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 (84) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/classes/alias.js +109 -119
  3. package/classes/index.js +58 -58
  4. package/classes/integrity.js +89 -97
  5. package/classes/login.js +47 -47
  6. package/classes/meta.js +75 -80
  7. package/classes/ping.js +53 -53
  8. package/classes/publish/map.js +90 -92
  9. package/classes/publish/package/index.js +130 -130
  10. package/classes/publish/package/tasks/check-bundle-sizes.js +19 -21
  11. package/classes/publish/package/tasks/check-if-already-published.js +67 -72
  12. package/classes/publish/package/tasks/cleanup.js +17 -17
  13. package/classes/publish/package/tasks/create-temp-directory.js +17 -17
  14. package/classes/publish/package/tasks/create-zip-file.js +60 -65
  15. package/classes/publish/package/tasks/dry-run.js +15 -15
  16. package/classes/publish/package/tasks/save-metafile.js +17 -17
  17. package/classes/publish/package/tasks/task.js +7 -7
  18. package/classes/publish/package/tasks/upload-files.js +52 -56
  19. package/classes/publish/package/tasks/validate-input.js +24 -24
  20. package/classes/version.js +152 -163
  21. package/commands/alias.js +61 -95
  22. package/commands/init.js +89 -109
  23. package/commands/integrity.js +42 -55
  24. package/commands/login.js +80 -101
  25. package/commands/map-alias.js +62 -82
  26. package/commands/map.js +69 -83
  27. package/commands/meta.js +36 -53
  28. package/commands/npm-alias.js +55 -80
  29. package/commands/package-alias.js +58 -90
  30. package/commands/ping.js +19 -30
  31. package/commands/publish.js +117 -127
  32. package/commands/version.js +87 -95
  33. package/formatters/alias.js +60 -63
  34. package/formatters/artifact.js +77 -77
  35. package/formatters/file.js +25 -25
  36. package/formatters/index.js +4 -4
  37. package/formatters/version.js +49 -51
  38. package/index.js +76 -72
  39. package/package.json +5 -4
  40. package/readme.md +1 -1
  41. package/types/classes/alias.d.ts +3 -3
  42. package/types/classes/integrity.d.ts +1 -1
  43. package/types/classes/login.d.ts +1 -1
  44. package/types/classes/meta.d.ts +1 -1
  45. package/types/classes/ping.d.ts +1 -1
  46. package/types/classes/publish/map.d.ts +1 -1
  47. package/types/classes/publish/package/index.d.ts +11 -11
  48. package/types/classes/publish/package/tasks/check-bundle-sizes.d.ts +1 -1
  49. package/types/classes/publish/package/tasks/check-if-already-published.d.ts +1 -1
  50. package/types/classes/publish/package/tasks/cleanup.d.ts +1 -1
  51. package/types/classes/publish/package/tasks/create-temp-directory.d.ts +1 -1
  52. package/types/classes/publish/package/tasks/create-zip-file.d.ts +1 -1
  53. package/types/classes/publish/package/tasks/dry-run.d.ts +1 -1
  54. package/types/classes/publish/package/tasks/save-metafile.d.ts +1 -1
  55. package/types/classes/publish/package/tasks/task.d.ts +1 -1
  56. package/types/classes/publish/package/tasks/upload-files.d.ts +1 -1
  57. package/types/classes/publish/package/tasks/validate-input.d.ts +1 -1
  58. package/types/classes/version.d.ts +3 -3
  59. package/types/utils/defaults.d.ts +22 -0
  60. package/types/utils/error.d.ts +19 -0
  61. package/types/utils/hash/index.d.ts +3 -3
  62. package/types/utils/http/index.d.ts +4 -4
  63. package/types/utils/index.d.ts +5 -5
  64. package/types/utils/json/index.d.ts +3 -3
  65. package/utils/command-handler.js +72 -0
  66. package/utils/defaults.js +79 -0
  67. package/utils/error.js +42 -0
  68. package/utils/hash/file.js +4 -4
  69. package/utils/hash/files.js +9 -9
  70. package/utils/hash/index.js +3 -3
  71. package/utils/http/index.js +4 -4
  72. package/utils/http/integrity.js +18 -18
  73. package/utils/http/latest-version.js +37 -37
  74. package/utils/http/request.js +42 -42
  75. package/utils/http/versions.js +20 -20
  76. package/utils/index.js +6 -5
  77. package/utils/json/index.js +3 -3
  78. package/utils/json/read.js +26 -26
  79. package/utils/json/write-eik.js +17 -17
  80. package/utils/json/write.js +26 -28
  81. package/utils/logger.js +43 -43
  82. package/utils/type-title.js +3 -3
  83. package/utils/url.js +2 -2
  84. package/commands/index.js +0 -27
@@ -0,0 +1,72 @@
1
+ import ora from "ora";
2
+ import logger from "./logger.js";
3
+ import { EikCliError } from "./error.js";
4
+ import { getArgsOrDefaults } from "./defaults.js";
5
+
6
+ /**
7
+ * @typedef {ReturnType<import('./defaults.js').getArgsOrDefaults>} Argv
8
+ * @typedef {ReturnType<import('./logger.js').default>} Logger
9
+ * @typedef {import('ora').Ora} Spinner
10
+ */
11
+
12
+ /**
13
+ * @template [T=Record<string, unknown>]
14
+ * @callback HandlerFunction
15
+ * @param {Argv & T} argv
16
+ * @param {Logger} log
17
+ * @param {Spinner} spinner Can we remove this?
18
+ * @returns {Promise<void>}
19
+ */
20
+
21
+ /**
22
+ * @param {{ command: string; options?: string[] }} opts
23
+ * @param {HandlerFunction} handlerFunction
24
+ * @returns {import('yargs').CommandModule["handler"]}
25
+ */
26
+ export function commandHandler(opts, handlerFunction) {
27
+ return async (argv) => {
28
+ const spinner = ora({ stream: process.stdout }).start();
29
+ const log = logger(spinner, argv.debug);
30
+
31
+ try {
32
+ const args = getArgsOrDefaults(argv, opts);
33
+
34
+ if (argv.debug) {
35
+ log.debug(`command inputs:
36
+ ${JSON.stringify(args, null, 2)}`);
37
+ }
38
+
39
+ await handlerFunction(args, log, spinner);
40
+
41
+ spinner.text = "";
42
+ spinner.stopAndPersist();
43
+ } catch (e) {
44
+ if (e instanceof EikCliError) {
45
+ log.error(e.message);
46
+
47
+ if (argv.debug) {
48
+ log.debug(e.stack);
49
+ if (e.cause) {
50
+ log.debug(`Caused by ${e.cause.message}`);
51
+ log.debug(e.cause.stack);
52
+ }
53
+ }
54
+
55
+ spinner.text = "";
56
+ spinner.stopAndPersist();
57
+ return process.exit(e.exitCode);
58
+ }
59
+
60
+ const error = /** @type {Error} */ (e);
61
+ log.error(`${error.name}: ${error.message}`);
62
+
63
+ if (argv.debug) {
64
+ log.debug(error.stack);
65
+ }
66
+
67
+ spinner.text = "";
68
+ spinner.stopAndPersist();
69
+ process.exit(1);
70
+ }
71
+ };
72
+ }
@@ -0,0 +1,79 @@
1
+ import fs from "fs";
2
+ import { join, isAbsolute } from "path";
3
+ import { helpers } from "@eik/common";
4
+ import { EikCliError, errors } from "./error.js";
5
+
6
+ const defaults = {
7
+ name: "",
8
+ type: "package",
9
+ version: "1.0.0",
10
+ server: "",
11
+ out: "./.eik",
12
+ files: "./public",
13
+ "import-map": [],
14
+ };
15
+
16
+ /**
17
+ * Get defaults for things like server, name and version from Eik config.
18
+ * If a specific argument is given for it, that takes precedence.
19
+ * @template [T=Record<string, unknown>]
20
+ * @param {any} argv
21
+ * @param {{ command: string; options?: string[] }} opts
22
+ * @returns {import('@eik/common').EikConfig & typeof defaults & T}
23
+ */
24
+ export function getArgsOrDefaults(argv, opts) {
25
+ let { cwd, config: configPath } = argv;
26
+ if (!cwd) {
27
+ cwd = process.cwd();
28
+ }
29
+
30
+ let config = {};
31
+ if (!opts.command.startsWith("init")) {
32
+ let path = cwd;
33
+ if (configPath) {
34
+ path = isAbsolute(configPath) ? configPath : join(cwd, configPath);
35
+ }
36
+ try {
37
+ const stats = fs.statSync(path);
38
+ if (stats.isDirectory()) {
39
+ config = helpers.configStore.findInDirectory(path).toJSON();
40
+ } else {
41
+ config = helpers.configStore.loadFromPath(path).toJSON();
42
+ }
43
+ } catch (error) {
44
+ const e = /** @type {Error} */ (error);
45
+ if (e.constructor.name === "MissingConfigError") {
46
+ if (!hasOptionsOnArgv(argv, opts.options)) {
47
+ throw new EikCliError(
48
+ errors.ERR_MISSING_CONFIG,
49
+ `No eik.json or package.json with eik configuration in ${cwd}, and did not get required fields as options`,
50
+ error,
51
+ );
52
+ }
53
+ }
54
+ }
55
+ }
56
+
57
+ const result = {
58
+ ...defaults,
59
+ ...config,
60
+ ...argv,
61
+ cwd,
62
+ };
63
+
64
+ return result;
65
+ }
66
+
67
+ /**
68
+ * @param {any} argv
69
+ * @param {string[]} options
70
+ * @returns {boolean}
71
+ */
72
+ function hasOptionsOnArgv(argv, options = []) {
73
+ for (const arg of options) {
74
+ if (typeof argv[arg] === "undefined") {
75
+ return false;
76
+ }
77
+ }
78
+ return true;
79
+ }
package/utils/error.js ADDED
@@ -0,0 +1,42 @@
1
+ export const errors = {
2
+ ERR_MISSING_CONFIG: "ERR_MISSING_CONFIG",
3
+ ERR_WRONG_TYPE: "ERR_WRONG_TYPE",
4
+ ERR_VERSION_EXISTS: "ERR_VERSION_EXISTS",
5
+ ERR_NOT_GIT: "ERR_NOT_GIT",
6
+ ERR_GIT_COMMIT: "ERR_GIT_COMMIT",
7
+ };
8
+
9
+ export class EikCliError extends Error {
10
+ #errorCode;
11
+ #exitCode;
12
+
13
+ /**
14
+ * @param {string} errorCode
15
+ * @param {string} message
16
+ * @param {Error} [cause]
17
+ */
18
+ constructor(errorCode, message, cause) {
19
+ super(message);
20
+
21
+ this.name = "EikCliError";
22
+ this.cause = cause;
23
+
24
+ this.#errorCode = errorCode;
25
+
26
+ switch (errorCode) {
27
+ case errors.ERR_VERSION_EXISTS:
28
+ this.#exitCode = 0;
29
+ break;
30
+ default:
31
+ this.#exitCode = 1;
32
+ }
33
+ }
34
+
35
+ get errorCode() {
36
+ return this.#errorCode;
37
+ }
38
+
39
+ get exitCode() {
40
+ return this.#exitCode;
41
+ }
42
+ }
@@ -1,5 +1,5 @@
1
- import ssri from 'ssri';
2
- import fs from 'fs';
1
+ import ssri from "ssri";
2
+ import fs from "fs";
3
3
 
4
4
  /**
5
5
  * Reads a file from a given path and produces and returns an integrity hash from its contents
@@ -11,6 +11,6 @@ import fs from 'fs';
11
11
  * @example hash.file('/path/to/file.js');
12
12
  */
13
13
  export default async (path) => {
14
- const integrity = await ssri.fromStream(fs.createReadStream(path));
15
- return integrity.toString();
14
+ const integrity = await ssri.fromStream(fs.createReadStream(path));
15
+ return integrity.toString();
16
16
  };
@@ -1,5 +1,5 @@
1
- import ssri from 'ssri';
2
- import fileHash from './file.js';
1
+ import ssri from "ssri";
2
+ import fileHash from "./file.js";
3
3
 
4
4
  /**
5
5
  * Reads files from given paths and produces and returns an integrity hash from all files contents
@@ -11,11 +11,11 @@ import fileHash from './file.js';
11
11
  * @example hash.files(['/path/to/file1.js', '/path/to/file2.js']);
12
12
  */
13
13
  export default async (files) => {
14
- const hashes = await Promise.all(files.map(fileHash));
15
- const hasher = ssri.create();
16
- for (const hash of hashes.sort()) {
17
- hasher.update(hash);
18
- }
19
- const integrity = hasher.digest();
20
- return integrity.toString();
14
+ const hashes = await Promise.all(files.map(fileHash));
15
+ const hasher = ssri.create();
16
+ for (const hash of hashes.sort()) {
17
+ hasher.update(hash);
18
+ }
19
+ const integrity = hasher.digest();
20
+ return integrity.toString();
21
21
  };
@@ -1,5 +1,5 @@
1
- import file from './file.js';
2
- import files from './files.js';
3
- import compare from './compare.js';
1
+ import file from "./file.js";
2
+ import files from "./files.js";
3
+ import compare from "./compare.js";
4
4
 
5
5
  export default { file, files, compare };
@@ -1,7 +1,7 @@
1
- import latestVersion from './latest-version.js';
2
- import versions from './versions.js';
3
- import integrity from './integrity.js';
4
- import request from './request.js';
1
+ import latestVersion from "./latest-version.js";
2
+ import versions from "./versions.js";
3
+ import integrity from "./integrity.js";
4
+ import request from "./request.js";
5
5
 
6
6
  export default { latestVersion, versions, integrity, request };
7
7
  export { latestVersion, versions, integrity, request };
@@ -1,4 +1,4 @@
1
- import { join } from 'path';
1
+ import { join } from "path";
2
2
 
3
3
  /**
4
4
  * Fetches package integrity string by name and version from a given Eik asset server.
@@ -12,24 +12,24 @@ import { join } from 'path';
12
12
  * @throws Error
13
13
  */
14
14
  export default async (server, type, name, version) => {
15
- const url = new URL(join(type, name, version), server);
16
- url.search = `?t=${Date.now()}`;
15
+ const url = new URL(join(type, name, version), server);
16
+ url.search = `?t=${Date.now()}`;
17
17
 
18
- const res = await fetch(url);
18
+ const res = await fetch(url);
19
19
 
20
- if (!res.ok) {
21
- if (res.status === 404) {
22
- return null;
23
- }
24
- throw new Error('Server responded with non 200 status code.');
25
- }
20
+ if (!res.ok) {
21
+ if (res.status === 404) {
22
+ return null;
23
+ }
24
+ throw new Error("Server responded with non 200 status code.");
25
+ }
26
26
 
27
- try {
28
- const body = await res.json();
29
- return body.integrity;
30
- } catch (err) {
31
- throw new Error(
32
- 'An error occurred while attempting to parse json response from server.',
33
- );
34
- }
27
+ try {
28
+ const body = await res.json();
29
+ return body.integrity;
30
+ } catch (err) {
31
+ throw new Error(
32
+ "An error occurred while attempting to parse json response from server.",
33
+ );
34
+ }
35
35
  };
@@ -1,4 +1,4 @@
1
- import { join } from 'path';
1
+ import { join } from "path";
2
2
 
3
3
  /**
4
4
  * Fetches the latest version from an Eik server of a package by name, optionally restricting the lookup to a specified semver major version
@@ -12,44 +12,44 @@ import { join } from 'path';
12
12
  * @throws Error
13
13
  */
14
14
  export default async (server, type, name, major) => {
15
- const url = new URL(`${join(type, name)}?t=${Date.now()}`, server);
16
- const res = await fetch(url);
17
- if (!res.ok) {
18
- if (res.status === 404) {
19
- return null;
20
- }
21
- throw new Error('Server responded with non 200 status code.');
22
- }
15
+ const url = new URL(`${join(type, name)}?t=${Date.now()}`, server);
16
+ const res = await fetch(url);
17
+ if (!res.ok) {
18
+ if (res.status === 404) {
19
+ return null;
20
+ }
21
+ throw new Error("Server responded with non 200 status code.");
22
+ }
23
23
 
24
- let body;
25
- try {
26
- body = await res.json();
27
- } catch (err) {
28
- throw new Error(
29
- 'An error occurred while attempting to parse json response from server.',
30
- );
31
- }
24
+ let body;
25
+ try {
26
+ body = await res.json();
27
+ } catch (err) {
28
+ throw new Error(
29
+ "An error occurred while attempting to parse json response from server.",
30
+ );
31
+ }
32
32
 
33
- let versions;
34
- try {
35
- versions = new Map(body.versions);
36
- } catch (err) {
37
- throw new Error(
38
- 'An error occurred while attempting to create an internal versions map. The JSON returned from the server is most likely invalid.',
39
- );
40
- }
33
+ let versions;
34
+ try {
35
+ versions = new Map(body.versions);
36
+ } catch (err) {
37
+ throw new Error(
38
+ "An error occurred while attempting to create an internal versions map. The JSON returned from the server is most likely invalid.",
39
+ );
40
+ }
41
41
 
42
- const highestMajor = Math.max(...versions.keys());
43
- if (Number.isNaN(highestMajor)) {
44
- throw new Error(
45
- 'An error occurred while attempting to get the highest major version from the internal versions map.',
46
- );
47
- }
42
+ const highestMajor = Math.max(...versions.keys());
43
+ if (Number.isNaN(highestMajor)) {
44
+ throw new Error(
45
+ "An error occurred while attempting to get the highest major version from the internal versions map.",
46
+ );
47
+ }
48
48
 
49
- try {
50
- const entry = versions.get(Number(major || highestMajor));
51
- return entry.version;
52
- } catch (err) {
53
- return null;
54
- }
49
+ try {
50
+ const entry = versions.get(Number(major || highestMajor));
51
+ return entry.version;
52
+ } catch (err) {
53
+ return null;
54
+ }
55
55
  };
@@ -1,4 +1,4 @@
1
- import { readFile } from 'node:fs/promises';
1
+ import { readFile } from "node:fs/promises";
2
2
 
3
3
  /**
4
4
  * @typedef {object} RequestOptions
@@ -21,54 +21,54 @@ import { readFile } from 'node:fs/promises';
21
21
  * @throws Error
22
22
  */
23
23
  async function request(options) {
24
- const { method = 'POST', host, pathname, data, file, map, token } = options;
25
- const body = new FormData();
26
- const headers = new Headers();
24
+ const { method = "POST", host, pathname, data, file, map, token } = options;
25
+ const body = new FormData();
26
+ const headers = new Headers();
27
27
 
28
- if (data) {
29
- for (const [key, value] of Object.entries(data)) {
30
- body.set(key, value);
31
- }
32
- }
28
+ if (data) {
29
+ for (const [key, value] of Object.entries(data)) {
30
+ body.set(key, value);
31
+ }
32
+ }
33
33
 
34
- if (file) {
35
- const fileData = await readFile(file);
36
- body.set('package', new Blob([fileData]));
37
- }
34
+ if (file) {
35
+ const fileData = await readFile(file);
36
+ body.set("package", new Blob([fileData]));
37
+ }
38
38
 
39
- if (map) {
40
- const mapData = await readFile(map);
41
- body.set('map', new Blob([mapData]));
42
- }
39
+ if (map) {
40
+ const mapData = await readFile(map);
41
+ body.set("map", new Blob([mapData]));
42
+ }
43
43
 
44
- if (token) {
45
- headers.set('Authorization', `Bearer ${token}`);
46
- }
44
+ if (token) {
45
+ headers.set("Authorization", `Bearer ${token}`);
46
+ }
47
47
 
48
- try {
49
- const url = new URL(pathname, host);
50
- url.search = `?t=${Date.now()}`;
48
+ try {
49
+ const url = new URL(pathname, host);
50
+ url.search = `?t=${Date.now()}`;
51
51
 
52
- const res = await fetch(url, { method, body, headers });
52
+ const res = await fetch(url, { method, body, headers });
53
53
 
54
- if (!res.ok) {
55
- const err = new Error(
56
- `Server responded with a non 200 ok status code. Response: ${res.status}`,
57
- );
58
- // @ts-ignore
59
- err.statusCode = res.status;
60
- throw err;
61
- }
62
- if (res?.headers?.get('content-type')?.includes('application/json')) {
63
- return { message: await res.json(), status: res.status };
64
- }
65
- return { message: await res.text(), status: res.status };
66
- } catch (err) {
67
- if (!err.statusCode) {
68
- err.statusCode = 500;
69
- }
70
- throw err;
71
- }
54
+ if (!res.ok) {
55
+ const err = new Error(
56
+ `Server responded with a non 200 ok status code. Response: ${res.status}`,
57
+ );
58
+ // @ts-ignore
59
+ err.statusCode = res.status;
60
+ throw err;
61
+ }
62
+ if (res?.headers?.get("content-type")?.includes("application/json")) {
63
+ return { message: await res.json(), status: res.status };
64
+ }
65
+ return { message: await res.text(), status: res.status };
66
+ } catch (err) {
67
+ if (!err.statusCode) {
68
+ err.statusCode = 500;
69
+ }
70
+ throw err;
71
+ }
72
72
  }
73
73
 
74
74
  export default request;
@@ -1,4 +1,4 @@
1
- import { join } from 'path';
1
+ import { join } from "path";
2
2
 
3
3
  /**
4
4
  * Fetches package versions by name from a given Eik asset server.
@@ -11,27 +11,27 @@ import { join } from 'path';
11
11
  * @throws Error
12
12
  */
13
13
  export default async (server, type, name) => {
14
- const pkg = join(type, name);
15
- const url = new URL(pkg, server);
16
- url.search = `?t=${Date.now()}`;
14
+ const pkg = join(type, name);
15
+ const url = new URL(pkg, server);
16
+ url.search = `?t=${Date.now()}`;
17
17
 
18
- const res = await fetch(url);
18
+ const res = await fetch(url);
19
19
 
20
- if (!res.ok) {
21
- if (res.status === 404) {
22
- return null;
23
- }
24
- throw new Error('Server responded with non 200 status code.');
25
- }
20
+ if (!res.ok) {
21
+ if (res.status === 404) {
22
+ return null;
23
+ }
24
+ throw new Error("Server responded with non 200 status code.");
25
+ }
26
26
 
27
- let body;
28
- try {
29
- body = await res.json();
30
- } catch (err) {
31
- throw new Error(
32
- 'An error occurred while attempting to parse json response from server.',
33
- );
34
- }
27
+ let body;
28
+ try {
29
+ body = await res.json();
30
+ } catch (err) {
31
+ throw new Error(
32
+ "An error occurred while attempting to parse json response from server.",
33
+ );
34
+ }
35
35
 
36
- return body.versions;
36
+ return body.versions;
37
37
  };
package/utils/index.js CHANGED
@@ -1,7 +1,8 @@
1
- import { helpers } from '@eik/common';
2
- import logger from './logger.js';
3
- import typeTitle from './type-title.js';
1
+ import { helpers } from "@eik/common";
2
+ import logger from "./logger.js";
3
+ import typeTitle from "./type-title.js";
4
+ import { getArgsOrDefaults } from "./defaults.js";
4
5
 
5
- const { getDefaults, typeSlug } = helpers;
6
+ const { typeSlug } = helpers;
6
7
 
7
- export { logger, getDefaults, typeSlug, typeTitle };
8
+ export { logger, getArgsOrDefaults, typeSlug, typeTitle };
@@ -1,5 +1,5 @@
1
- import read from './read.js';
2
- import write from './write.js';
3
- import writeEik from './write-eik.js';
1
+ import read from "./read.js";
2
+ import write from "./write.js";
3
+ import writeEik from "./write-eik.js";
4
4
 
5
5
  export default { read, write, writeEik };
@@ -1,6 +1,6 @@
1
- import assert from 'assert';
2
- import fs from 'node:fs/promises';
3
- import { join, isAbsolute } from 'path';
1
+ import assert from "assert";
2
+ import fs from "node:fs/promises";
3
+ import { join, isAbsolute } from "path";
4
4
 
5
5
  /**
6
6
  * Reads a file at a given location, assumes the contents to be JSON and then deserializes into a JavaScript object
@@ -17,27 +17,27 @@ import { join, isAbsolute } from 'path';
17
17
  * @example json.read({ filename: './relative/path/to/file.json', cwd: '/path/to/cwd });
18
18
  */
19
19
  export default async (location) => {
20
- if (typeof location !== 'string') {
21
- assert(
22
- location.filename,
23
- 'When "location" is not of type "string" then it must be an "object" with property "filename"',
24
- );
25
- }
26
- let cwd = process.cwd();
27
- let filename = '';
28
- if (typeof location === 'string') {
29
- filename = location;
30
- } else {
31
- filename = location.filename;
32
- if (location.cwd) {
33
- cwd = location.cwd;
34
- }
35
- }
36
- const path = isAbsolute(filename) ? filename : join(cwd, filename);
37
- try {
38
- const meta = await fs.readFile(path, 'utf8');
39
- return JSON.parse(meta);
40
- } catch (err) {
41
- return {};
42
- }
20
+ if (typeof location !== "string") {
21
+ assert(
22
+ location.filename,
23
+ 'When "location" is not of type "string" then it must be an "object" with property "filename"',
24
+ );
25
+ }
26
+ let cwd = process.cwd();
27
+ let filename = "";
28
+ if (typeof location === "string") {
29
+ filename = location;
30
+ } else {
31
+ filename = location.filename;
32
+ if (location.cwd) {
33
+ cwd = location.cwd;
34
+ }
35
+ }
36
+ const path = isAbsolute(filename) ? filename : join(cwd, filename);
37
+ try {
38
+ const meta = await fs.readFile(path, "utf8");
39
+ return JSON.parse(meta);
40
+ } catch (err) {
41
+ return {};
42
+ }
43
43
  };