@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,9 +1,9 @@
1
1
  /**
2
2
  * @type {(value: unknown, message?: string) => asserts value}
3
3
  */
4
- import assert from 'node:assert';
5
- import fs from 'node:fs';
6
- import configStore from './config-store.js';
4
+ import assert from "node:assert";
5
+ import fs from "node:fs";
6
+ import configStore from "./config-store.js";
7
7
 
8
8
  /**
9
9
  * Sets up asset routes for local development. Mounted paths match those on Eik server and values are read from projects eik.json file.
@@ -12,52 +12,52 @@ import configStore from './config-store.js';
12
12
  * @param {string} rootEikDirectory - (optional) path to folder where eik configuration file can be found
13
13
  */
14
14
  async function localAssets(app, rootEikDirectory = process.cwd()) {
15
- assert(
16
- // @ts-ignore
17
- app.decorateReply || app.name === 'app' || app.route,
18
- 'App must be an Express, Fastify or Hapi app instance',
19
- );
20
- assert(
21
- typeof rootEikDirectory === 'string' && rootEikDirectory,
22
- 'Path to folder for eik config must be provided and must be of type string',
23
- );
24
- // ensure eik.json only loaded 1x
25
- const eik = configStore.findInDirectory(rootEikDirectory);
15
+ assert(
16
+ // @ts-ignore
17
+ app.decorateReply || app.name === "app" || app.route,
18
+ "App must be an Express, Fastify or Hapi app instance",
19
+ );
20
+ assert(
21
+ typeof rootEikDirectory === "string" && rootEikDirectory,
22
+ "Path to folder for eik config must be provided and must be of type string",
23
+ );
24
+ // ensure eik.json only loaded 1x
25
+ const eik = configStore.findInDirectory(rootEikDirectory);
26
26
 
27
- (await eik.mappings()).forEach((mapping) => {
28
- const { pathname } = mapping.destination.url;
29
- const { contentType, absolute: path } = mapping.source;
30
- // @ts-ignore
31
- if (app.get) {
32
- // @ts-ignore
33
- app.get(pathname, (req, res) => {
34
- if (res.set) {
35
- // express
36
- res.set('Access-Control-Allow-Origin', '*');
37
- res.set('content-type', contentType);
38
- fs.createReadStream(path).pipe(res);
39
- } else if (res.type) {
40
- // fastify
41
- res.header('Access-Control-Allow-Origin', '*');
42
- res.type(contentType);
43
- res.send(fs.createReadStream(path));
44
- }
45
- });
46
- } else {
47
- // hapi
48
- // @ts-ignore
49
- app.route({
50
- method: 'GET',
51
- path: pathname,
52
- // @ts-ignore
53
- handler(req, h) {
54
- return h
55
- .response(fs.createReadStream(path))
56
- .header('Access-Control-Allow-Origin', '*')
57
- .type(contentType);
58
- },
59
- });
60
- }
61
- });
27
+ (await eik.mappings()).forEach((mapping) => {
28
+ const { pathname } = mapping.destination.url;
29
+ const { contentType, absolute: path } = mapping.source;
30
+ // @ts-ignore
31
+ if (app.get) {
32
+ // @ts-ignore
33
+ app.get(pathname, (req, res) => {
34
+ if (res.set) {
35
+ // express
36
+ res.set("Access-Control-Allow-Origin", "*");
37
+ res.set("content-type", contentType);
38
+ fs.createReadStream(path).pipe(res);
39
+ } else if (res.type) {
40
+ // fastify
41
+ res.header("Access-Control-Allow-Origin", "*");
42
+ res.type(contentType);
43
+ res.send(fs.createReadStream(path));
44
+ }
45
+ });
46
+ } else {
47
+ // hapi
48
+ // @ts-ignore
49
+ app.route({
50
+ method: "GET",
51
+ path: pathname,
52
+ // @ts-ignore
53
+ handler(req, h) {
54
+ return h
55
+ .response(fs.createReadStream(path))
56
+ .header("Access-Control-Allow-Origin", "*")
57
+ .type(contentType);
58
+ },
59
+ });
60
+ }
61
+ });
62
62
  }
63
63
  export default localAssets;
@@ -1,5 +1,5 @@
1
- import { sep } from 'path';
2
- import { platform } from 'os';
1
+ import { sep } from "path";
2
+ import { platform } from "os";
3
3
 
4
4
  /**
5
5
  * Add a trailing slash to a path if necessary
@@ -8,7 +8,7 @@ import { platform } from 'os';
8
8
  *
9
9
  * @returns {string}
10
10
  */
11
- export const addTrailingSlash = (val) => (val.endsWith('/') ? val : `${val}/`);
11
+ export const addTrailingSlash = (val) => (val.endsWith("/") ? val : `${val}/`);
12
12
 
13
13
  /**
14
14
  * Remove a trailing slash from a path if necessary
@@ -18,10 +18,8 @@ export const addTrailingSlash = (val) => (val.endsWith('/') ? val : `${val}/`);
18
18
  * @returns {string}
19
19
  */
20
20
  export const removeTrailingSlash = (val) =>
21
- // this is also used to trim from config files, which may now always use the OS's sep value. Look for both.
22
- val.endsWith('/') || val.endsWith('\\')
23
- ? val.substr(0, val.length - 1)
24
- : val;
21
+ // this is also used to trim from config files, which may now always use the OS's sep value. Look for both.
22
+ val.endsWith("/") || val.endsWith("\\") ? val.substr(0, val.length - 1) : val;
25
23
 
26
24
  /**
27
25
  * Add a leading slash to a path if necessary, but not on Windows
@@ -31,7 +29,7 @@ export const removeTrailingSlash = (val) =>
31
29
  * @returns {string}
32
30
  */
33
31
  export const addLeadingSlash = (val) =>
34
- val.startsWith('/') || platform() === 'win32' ? val : `/${val}`;
32
+ val.startsWith("/") || platform() === "win32" ? val : `/${val}`;
35
33
 
36
34
  /**
37
35
  * Remove a leading slash from a path if necessary
@@ -41,7 +39,7 @@ export const addLeadingSlash = (val) =>
41
39
  * @returns {string}
42
40
  */
43
41
  export const removeLeadingSlash = (val) =>
44
- val.startsWith('/') || val.startsWith('\\') ? val.substr(1) : val;
42
+ val.startsWith("/") || val.startsWith("\\") ? val.substr(1) : val;
45
43
 
46
44
  /**
47
45
  * Replaces a path string's separators (/ or \) with the current OS's sep value from node:path.
@@ -55,4 +53,4 @@ export const ensureOsSep = (val) => val.replace(/\/\\/g, sep);
55
53
  * @param {string} val
56
54
  * @returns {string}
57
55
  */
58
- export const ensurePosix = (val) => val.replace(/\\/g, '/');
56
+ export const ensurePosix = (val) => val.replace(/\\/g, "/");
@@ -1,14 +1,14 @@
1
- import { extname, join, isAbsolute, basename, sep, normalize } from 'node:path';
2
- import isGlob from 'is-glob';
3
- import { glob } from 'glob';
1
+ import { extname, join, isAbsolute, basename, sep, normalize } from "node:path";
2
+ import isGlob from "is-glob";
3
+ import { glob } from "glob";
4
4
  import {
5
- removeTrailingSlash,
6
- addLeadingSlash,
7
- removeLeadingSlash,
8
- ensureOsSep,
9
- ensurePosix,
10
- } from './path-slashes.js';
11
- import ResolvedFiles from '../classes/resolved-files.js';
5
+ removeTrailingSlash,
6
+ addLeadingSlash,
7
+ removeLeadingSlash,
8
+ ensureOsSep,
9
+ ensurePosix,
10
+ } from "./path-slashes.js";
11
+ import ResolvedFiles from "../classes/resolved-files.js";
12
12
 
13
13
  /**
14
14
  * Create a new path from a path string preceeding a glob or the whole path if no glob is found
@@ -17,16 +17,16 @@ import ResolvedFiles from '../classes/resolved-files.js';
17
17
  * @returns {string} modified path
18
18
  */
19
19
  const pathUntilGlob = (path) => {
20
- const segments = (path || '').split(sep);
21
- const segmentsToKeep = [];
22
- for (const segment of segments) {
23
- if (segment === '.') continue;
24
- if (segment === '') continue;
25
- if (isGlob(segment)) break;
26
- segmentsToKeep.push(segment);
27
- }
20
+ const segments = (path || "").split(sep);
21
+ const segmentsToKeep = [];
22
+ for (const segment of segments) {
23
+ if (segment === ".") continue;
24
+ if (segment === "") continue;
25
+ if (isGlob(segment)) break;
26
+ segmentsToKeep.push(segment);
27
+ }
28
28
 
29
- return addLeadingSlash(normalize(segmentsToKeep.join(sep)));
29
+ return addLeadingSlash(normalize(segmentsToKeep.join(sep)));
30
30
  };
31
31
 
32
32
  /**
@@ -38,60 +38,55 @@ const pathUntilGlob = (path) => {
38
38
  * @returns {Promise<ResolvedFiles[]>}
39
39
  */
40
40
  const resolveFiles = async (files, cwd) =>
41
- Promise.all(
42
- Object.entries(files).map(async (definition) => {
43
- let [, source] = definition;
41
+ Promise.all(
42
+ Object.entries(files).map(async (definition) => {
43
+ let [, source] = definition;
44
44
 
45
- // The config may not always match the OS's separator.
46
- // Convert the input to OS-specific if necessary.
47
- // We convert back to always using forward slashes for glob,
48
- // but for calculating the paths relative to cwd we'd like
49
- // these to be OS-specific for now.
50
- source = ensureOsSep(source);
45
+ // The config may not always match the OS's separator.
46
+ // Convert the input to OS-specific if necessary.
47
+ // We convert back to always using forward slashes for glob,
48
+ // but for calculating the paths relative to cwd we'd like
49
+ // these to be OS-specific for now.
50
+ source = ensureOsSep(source);
51
51
 
52
- // normalise to absolute path
53
- let pattern = isAbsolute(source) ? source : join(cwd, source);
52
+ // normalise to absolute path
53
+ let pattern = isAbsolute(source) ? source : join(cwd, source);
54
54
 
55
- // append glob if folder
56
- if (
57
- extname(pattern) === '' &&
58
- isGlob(ensurePosix(pattern)) === false
59
- ) {
60
- pattern = `${pattern}${sep}**${sep}*`;
61
- }
55
+ // append glob if folder
56
+ if (extname(pattern) === "" && isGlob(ensurePosix(pattern)) === false) {
57
+ pattern = `${pattern}${sep}**${sep}*`;
58
+ }
62
59
 
63
- // trim off any glob
64
- let basePath = pathUntilGlob(pattern);
60
+ // trim off any glob
61
+ let basePath = pathUntilGlob(pattern);
65
62
 
66
- // fix basePath if file
67
- if (extname(pattern) !== '') {
68
- basePath = removeTrailingSlash(
69
- basePath.replace(basename(pattern), ''),
70
- );
71
- }
63
+ // fix basePath if file
64
+ if (extname(pattern) !== "") {
65
+ basePath = removeTrailingSlash(basePath.replace(basename(pattern), ""));
66
+ }
72
67
 
73
- // convert glob pattern to forward slash separators
74
- // https://www.npmjs.com/package/glob#windows
75
- basePath = ensurePosix(basePath);
76
- pattern = ensurePosix(pattern);
68
+ // convert glob pattern to forward slash separators
69
+ // https://www.npmjs.com/package/glob#windows
70
+ basePath = ensurePosix(basePath);
71
+ pattern = ensurePosix(pattern);
77
72
 
78
- // process glob pattern into a list of existing files
79
- const resolvedFiles = await glob(pattern, {
80
- cwd: basePath,
81
- nodir: true,
82
- });
73
+ // process glob pattern into a list of existing files
74
+ const resolvedFiles = await glob(pattern, {
75
+ cwd: basePath,
76
+ nodir: true,
77
+ });
83
78
 
84
- // trim off the basePath to create a relative pathed pattern
85
- pattern = removeTrailingSlash(
86
- removeLeadingSlash(pattern.replace(basePath, '')),
87
- );
79
+ // trim off the basePath to create a relative pathed pattern
80
+ pattern = removeTrailingSlash(
81
+ removeLeadingSlash(pattern.replace(basePath, "")),
82
+ );
88
83
 
89
- return new ResolvedFiles(resolvedFiles, {
90
- definition,
91
- basePath,
92
- pattern,
93
- });
94
- }),
95
- );
84
+ return new ResolvedFiles(resolvedFiles, {
85
+ definition,
86
+ basePath,
87
+ pattern,
88
+ });
89
+ }),
90
+ );
96
91
 
97
92
  export default resolveFiles;
@@ -5,7 +5,7 @@
5
5
  * @returns {string} the Eik package type
6
6
  */
7
7
  export default (type) => {
8
- if (type === 'package') return 'pkg';
9
- if (type === 'image') return 'img';
10
- return type;
8
+ if (type === "package") return "pkg";
9
+ if (type === "image") return "img";
10
+ return type;
11
11
  };
@@ -4,8 +4,8 @@
4
4
  * @param {"PACKAGE" | "NPM" | "MAP"} type
5
5
  */
6
6
  export default (type) => {
7
- if (type === 'package') return 'PACKAGE';
8
- if (type === 'npm') return 'NPM';
9
- if (type === 'image') return 'IMAGE';
10
- return 'MAP';
7
+ if (type === "package") return "PACKAGE";
8
+ if (type === "npm") return "NPM";
9
+ if (type === "image") return "IMAGE";
10
+ return "MAP";
11
11
  };
package/lib/index.js CHANGED
@@ -1,16 +1,16 @@
1
- import * as validators from './validators/index.js';
2
- import ReadFile from './classes/read-file.js';
3
- import EikConfig from './classes/eik-config.js';
4
- import schemas from './schemas/index.js';
5
- import * as stream from './stream.js';
6
- import helpers from './helpers/index.js';
1
+ import * as validators from "./validators/index.js";
2
+ import ReadFile from "./classes/read-file.js";
3
+ import EikConfig from "./classes/eik-config.js";
4
+ import schemas from "./schemas/index.js";
5
+ import * as stream from "./stream.js";
6
+ import helpers from "./helpers/index.js";
7
7
 
8
8
  export default {
9
- validators,
10
- ReadFile,
11
- EikConfig,
12
- schemas,
13
- stream,
14
- helpers,
9
+ validators,
10
+ ReadFile,
11
+ EikConfig,
12
+ schemas,
13
+ stream,
14
+ helpers,
15
15
  };
16
16
  export { validators, ReadFile, EikConfig, schemas, stream, helpers };
@@ -1,14 +1,14 @@
1
1
  import {
2
- eikJSON,
3
- name,
4
- version,
5
- type,
6
- server,
7
- files,
8
- importMap,
9
- out,
10
- } from './validate.js';
11
- import ValidationError from './validation-error.js';
2
+ eikJSON,
3
+ name,
4
+ version,
5
+ type,
6
+ server,
7
+ files,
8
+ importMap,
9
+ out,
10
+ } from "./validate.js";
11
+ import ValidationError from "./validation-error.js";
12
12
 
13
13
  /**
14
14
  * @template T
@@ -25,38 +25,38 @@ import ValidationError from './validation-error.js';
25
25
  * @returns {SchemaAssert<T>}
26
26
  */
27
27
  const assert = (validate, message) => (value) => {
28
- const valid = validate(value);
29
- if (valid.error) {
30
- const errorMessage = valid.error
31
- // @ts-ignore
32
- .map((err) => {
33
- let msg = err.message;
34
- if (err.params && err.params.allowedValues) {
35
- msg += ` ("${err.params.allowedValues.join('", "')}")`;
36
- }
37
- return msg;
38
- })
39
- .join(',');
40
- // @ts-expect-error Maybe some toString magic happens here?
41
- throw new ValidationError(`${message}: ${errorMessage}`, valid.error);
42
- }
28
+ const valid = validate(value);
29
+ if (valid.error) {
30
+ const errorMessage = valid.error
31
+ // @ts-ignore
32
+ .map((err) => {
33
+ let msg = err.message;
34
+ if (err.params && err.params.allowedValues) {
35
+ msg += ` ("${err.params.allowedValues.join('", "')}")`;
36
+ }
37
+ return msg;
38
+ })
39
+ .join(",");
40
+ // @ts-expect-error Maybe some toString magic happens here?
41
+ throw new ValidationError(`${message}: ${errorMessage}`, valid.error);
42
+ }
43
43
  };
44
44
 
45
45
  export default {
46
- /** Asserts the given [eik.json](https://eik.dev/docs/reference/eik-json) includes required fields that are valid */
47
- eikJSON: assert(eikJSON, 'Invalid eik.json schema'),
48
- /** Asserts the given [name](https://eik.dev/docs/reference/eik-json#name) value is valid*/
49
- name: assert(name, 'Parameter "name" is not valid'),
50
- /** Asserts the given [type](https://eik.dev/docs/reference/eik-json#type) value is valid*/
51
- type: assert(type, 'Parameter "type" is not valid'),
52
- /** Asserts the given [version](https://eik.dev/docs/reference/eik-json#version) value is valid*/
53
- version: assert(version, 'Parameter "version" is not valid'),
54
- /** Asserts the given [server](https://eik.dev/docs/reference/eik-json#server) value is valid*/
55
- server: assert(server, 'Parameter "server" is not valid'),
56
- /** Asserts the given [files](https://eik.dev/docs/reference/eik-json#files) value is valid */
57
- files: assert(files, 'Parameter "files" is not valid'),
58
- /** Asserts the given [import-map](https://eik.dev/docs/reference/eik-json#import-map) value is valid */
59
- importMap: assert(importMap, 'Parameter "import-map" is not valid'),
60
- /** Asserts the given [out](https://eik.dev/docs/reference/eik-json#out) value is valid */
61
- out: assert(out, 'Parameter "out" is not valid'),
46
+ /** Asserts the given [eik.json](https://eik.dev/docs/reference/eik-json) includes required fields that are valid */
47
+ eikJSON: assert(eikJSON, "Invalid eik.json schema"),
48
+ /** Asserts the given [name](https://eik.dev/docs/reference/eik-json#name) value is valid*/
49
+ name: assert(name, 'Parameter "name" is not valid'),
50
+ /** Asserts the given [type](https://eik.dev/docs/reference/eik-json#type) value is valid*/
51
+ type: assert(type, 'Parameter "type" is not valid'),
52
+ /** Asserts the given [version](https://eik.dev/docs/reference/eik-json#version) value is valid*/
53
+ version: assert(version, 'Parameter "version" is not valid'),
54
+ /** Asserts the given [server](https://eik.dev/docs/reference/eik-json#server) value is valid*/
55
+ server: assert(server, 'Parameter "server" is not valid'),
56
+ /** Asserts the given [files](https://eik.dev/docs/reference/eik-json#files) value is valid */
57
+ files: assert(files, 'Parameter "files" is not valid'),
58
+ /** Asserts the given [import-map](https://eik.dev/docs/reference/eik-json#import-map) value is valid */
59
+ importMap: assert(importMap, 'Parameter "import-map" is not valid'),
60
+ /** Asserts the given [out](https://eik.dev/docs/reference/eik-json#out) value is valid */
61
+ out: assert(out, 'Parameter "out" is not valid'),
62
62
  };
@@ -1,15 +1,15 @@
1
- import { readFileSync } from 'node:fs';
2
- import { join, dirname } from 'node:path';
3
- import * as validate from './validate.js';
4
- import assert from './assert.js';
5
- import ValidationError from './validation-error.js';
6
- import { fileURLToPath } from 'node:url';
1
+ import { readFileSync } from "node:fs";
2
+ import { join, dirname } from "node:path";
3
+ import * as validate from "./validate.js";
4
+ import assert from "./assert.js";
5
+ import ValidationError from "./validation-error.js";
6
+ import { fileURLToPath } from "node:url";
7
7
 
8
8
  const __filename = fileURLToPath(import.meta.url);
9
9
  const __dirname = dirname(__filename);
10
10
 
11
11
  const schema = JSON.parse(
12
- readFileSync(join(__dirname, './eikjson.schema.json'), 'utf8'),
12
+ readFileSync(join(__dirname, "./eikjson.schema.json"), "utf8"),
13
13
  );
14
14
 
15
15
  export default { schema, validate, assert, ValidationError };