@static-pages/core 4.0.0 → 5.0.1

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.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://app.travis-ci.com/staticpagesjs/core.svg?branch=master)](https://app.travis-ci.com/staticpagesjs/core)
4
4
  [![Coverage Status](https://coveralls.io/repos/github/staticpagesjs/core/badge.svg?branch=master)](https://coveralls.io/github/staticpagesjs/core?branch=master)
5
5
  ![npms.io (quality)](https://img.shields.io/npms-io/quality-score/@static-pages/core?label=quality)
6
- ![Maintenance](https://img.shields.io/maintenance/yes/2022)
6
+ ![Maintenance](https://img.shields.io/maintenance/yes/2023)
7
7
 
8
8
  This package contains only the core; this means it does not provide CLI support or readers and writers.
9
9
  You can import this library to your JS project then add your own controllers, readers and writers.
@@ -37,7 +37,7 @@ import markdownReader from '@static-pages/markdown-reader';
37
37
  import yamlReader from '@static-pages/yaml-reader';
38
38
  import twigWriter from '@static-pages/twig-writer';
39
39
 
40
- staticPages([{
40
+ staticPages({
41
41
  from: markdownReader({
42
42
  pattern: "pages/**/*.md"
43
43
  }),
@@ -63,29 +63,32 @@ staticPages([{
63
63
  data.commitHash = yourGetCommitHashFn();
64
64
  return data;
65
65
  }
66
- }])
66
+ })
67
67
  .catch(error => {
68
68
  console.error('Error:', error);
69
69
  console.error(error.stack);
70
70
  });
71
71
  ```
72
72
 
73
- ## `staticPages(routes: Route | Route[])`
74
- The function expects the `routes` parameter to be an array. It is wrapped into an array automatically when other type is provided.
75
- Each item in this array should be an object containing a `from`, `to` and optionally a `controller` property matching the definition below.
73
+ ## `staticPages(...routes: Route[])`
74
+
75
+ Each route consists of a `from`, `to` and optionally a `controller` property matching the definition below.
76
76
 
77
77
  ```ts
78
78
  type Data = Record<string, unknown>;
79
79
  type Route = {
80
80
  from: Iterable<Data> | AsyncIterable<Data>;
81
- to: (data: Data) => void | Promise<void>;
82
- controller?: (data: Data) => undefined | Data | Data[] | Promise<undefined | Data | Data[]>;
81
+ to: { (data: Data): void | Promise<void>; teardown?(): void | Promise<void>; };
82
+ controller?(data: Data): void | Data | Data[] | Promise<void | Data | Data[]>;
83
83
  };
84
84
  ```
85
85
 
86
- > Tip: Controllers may return an array of `Data` objects. Each data object will be rendered as a separate page.
86
+ > Tip: Controllers may return an array of `Data` objects; each will be rendered as a separate page.
87
+ > Alternatively it may return `void` to prevent the rendering of the current data object.
87
88
 
88
- > Tip: Controllers may return `undefined` to prevent the rendering of the current data object.
89
+ > Tip: To schedule cleanup after writers you can define a `.teardown()` member on the writer call.
90
+ > This callback will be run after the last page is processed. If more writers provide the same callback
91
+ > its only executed once.
89
92
 
90
93
  ## Missing a feature?
91
94
  Create an issue describing your needs. If it fits the scope of the project I will implement it or you can implement it your own and submit a pull request.
package/cjs/index.d.ts CHANGED
@@ -1,11 +1,18 @@
1
1
  export declare namespace staticPages {
2
- type Data = Record<string, unknown>;
3
- type Controller = (data: staticPages.Data) => undefined | staticPages.Data | staticPages.Data[] | Promise<undefined | staticPages.Data | staticPages.Data[]>;
4
- type Route = {
5
- from: Iterable<staticPages.Data> | AsyncIterable<staticPages.Data>;
6
- to: (data: staticPages.Data) => void | Promise<void>;
7
- controller?: staticPages.Controller;
2
+ type Route<R extends Record<string, unknown> = Record<string, unknown>, W extends Record<string, unknown> = Record<string, unknown>> = {
3
+ from: Iterable<R> | AsyncIterable<R>;
4
+ to: {
5
+ (data: W): void | Promise<void>;
6
+ teardown?(): void | Promise<void>;
7
+ };
8
+ controller?(data: R): void | W | W[] | Promise<void | W | W[]>;
8
9
  };
9
10
  }
10
- export declare const staticPages: (routes: staticPages.Route | staticPages.Route[]) => Promise<void>;
11
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>]): Promise<void>;
12
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>]): Promise<void>;
13
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>, R3 extends Record<string, unknown> = Record<string, unknown>, W3 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>, staticPages.Route<R3, W3>]): Promise<void>;
14
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>, R3 extends Record<string, unknown> = Record<string, unknown>, W3 extends Record<string, unknown> = Record<string, unknown>, R4 extends Record<string, unknown> = Record<string, unknown>, W4 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>, staticPages.Route<R3, W3>, staticPages.Route<R4, W4>]): Promise<void>;
15
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>, R3 extends Record<string, unknown> = Record<string, unknown>, W3 extends Record<string, unknown> = Record<string, unknown>, R4 extends Record<string, unknown> = Record<string, unknown>, W4 extends Record<string, unknown> = Record<string, unknown>, R5 extends Record<string, unknown> = Record<string, unknown>, W5 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>, staticPages.Route<R3, W3>, staticPages.Route<R4, W4>, staticPages.Route<R5, W5>]): Promise<void>;
16
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>, R3 extends Record<string, unknown> = Record<string, unknown>, W3 extends Record<string, unknown> = Record<string, unknown>, R4 extends Record<string, unknown> = Record<string, unknown>, W4 extends Record<string, unknown> = Record<string, unknown>, R5 extends Record<string, unknown> = Record<string, unknown>, W5 extends Record<string, unknown> = Record<string, unknown>, R6 extends Record<string, unknown> = Record<string, unknown>, W6 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>, staticPages.Route<R3, W3>, staticPages.Route<R4, W4>, staticPages.Route<R5, W5>, staticPages.Route<R6, W6>]): Promise<void>;
17
+ export declare function staticPages(...routes: staticPages.Route[]): Promise<void>;
11
18
  export default staticPages;
package/cjs/index.js CHANGED
@@ -2,21 +2,23 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.staticPages = void 0;
4
4
  const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof x;
5
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
5
  const isIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.iterator]) === 'function';
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
6
  const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
9
- const staticPages = async (routes) => {
10
- for (const route of Array.isArray(routes) ? routes : [routes]) {
7
+ async function staticPages(...routes) {
8
+ const teardown = new Set();
9
+ for (const route of routes) {
11
10
  if (typeof route !== 'object' || !route)
12
- throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
11
+ throw new Error(`Argument type mismatch, expected 'object', got '${getType(route)}'.`);
13
12
  const { from, to, controller } = route;
14
13
  if (!isIterable(from) && !isAsyncIterable(from))
15
- throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
14
+ throw new Error('Argument type mismatch, \'from\' exptects \'iterable\' or \'asyncIterable\'.');
16
15
  if (typeof to !== 'function')
17
- throw new Error(`Route 'to' type mismatch, expected 'function', got '${getType(to)}'.`);
16
+ throw new Error(`Argument type mismatch, 'to' expects 'function', got '${getType(to)}'.`);
18
17
  if (typeof controller !== 'undefined' && typeof controller !== 'function')
19
- throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
18
+ throw new Error(`Argument type mismatch, 'controller' expects 'function', got '${getType(controller)}'.`);
19
+ if (typeof to.teardown === 'function') {
20
+ teardown.add(to.teardown);
21
+ }
20
22
  const isController = typeof controller === 'function';
21
23
  for await (const data of from) {
22
24
  const results = isController ? await controller(data) : data;
@@ -32,6 +34,9 @@ const staticPages = async (routes) => {
32
34
  }
33
35
  }
34
36
  }
35
- };
37
+ for (const fn of teardown) {
38
+ await fn();
39
+ }
40
+ }
36
41
  exports.staticPages = staticPages;
37
- exports.default = exports.staticPages;
42
+ exports.default = staticPages;
package/esm/index.d.ts CHANGED
@@ -1,11 +1,18 @@
1
1
  export declare namespace staticPages {
2
- type Data = Record<string, unknown>;
3
- type Controller = (data: staticPages.Data) => undefined | staticPages.Data | staticPages.Data[] | Promise<undefined | staticPages.Data | staticPages.Data[]>;
4
- type Route = {
5
- from: Iterable<staticPages.Data> | AsyncIterable<staticPages.Data>;
6
- to: (data: staticPages.Data) => void | Promise<void>;
7
- controller?: staticPages.Controller;
2
+ type Route<R extends Record<string, unknown> = Record<string, unknown>, W extends Record<string, unknown> = Record<string, unknown>> = {
3
+ from: Iterable<R> | AsyncIterable<R>;
4
+ to: {
5
+ (data: W): void | Promise<void>;
6
+ teardown?(): void | Promise<void>;
7
+ };
8
+ controller?(data: R): void | W | W[] | Promise<void | W | W[]>;
8
9
  };
9
10
  }
10
- export declare const staticPages: (routes: staticPages.Route | staticPages.Route[]) => Promise<void>;
11
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>]): Promise<void>;
12
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>]): Promise<void>;
13
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>, R3 extends Record<string, unknown> = Record<string, unknown>, W3 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>, staticPages.Route<R3, W3>]): Promise<void>;
14
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>, R3 extends Record<string, unknown> = Record<string, unknown>, W3 extends Record<string, unknown> = Record<string, unknown>, R4 extends Record<string, unknown> = Record<string, unknown>, W4 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>, staticPages.Route<R3, W3>, staticPages.Route<R4, W4>]): Promise<void>;
15
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>, R3 extends Record<string, unknown> = Record<string, unknown>, W3 extends Record<string, unknown> = Record<string, unknown>, R4 extends Record<string, unknown> = Record<string, unknown>, W4 extends Record<string, unknown> = Record<string, unknown>, R5 extends Record<string, unknown> = Record<string, unknown>, W5 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>, staticPages.Route<R3, W3>, staticPages.Route<R4, W4>, staticPages.Route<R5, W5>]): Promise<void>;
16
+ export declare function staticPages<R1 extends Record<string, unknown> = Record<string, unknown>, W1 extends Record<string, unknown> = Record<string, unknown>, R2 extends Record<string, unknown> = Record<string, unknown>, W2 extends Record<string, unknown> = Record<string, unknown>, R3 extends Record<string, unknown> = Record<string, unknown>, W3 extends Record<string, unknown> = Record<string, unknown>, R4 extends Record<string, unknown> = Record<string, unknown>, W4 extends Record<string, unknown> = Record<string, unknown>, R5 extends Record<string, unknown> = Record<string, unknown>, W5 extends Record<string, unknown> = Record<string, unknown>, R6 extends Record<string, unknown> = Record<string, unknown>, W6 extends Record<string, unknown> = Record<string, unknown>>(...routes: [staticPages.Route<R1, W1>, staticPages.Route<R2, W2>, staticPages.Route<R3, W3>, staticPages.Route<R4, W4>, staticPages.Route<R5, W5>, staticPages.Route<R6, W6>]): Promise<void>;
17
+ export declare function staticPages(...routes: staticPages.Route[]): Promise<void>;
11
18
  export default staticPages;
package/esm/index.js CHANGED
@@ -1,19 +1,21 @@
1
1
  const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof x;
2
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
2
  const isIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.iterator]) === 'function';
4
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
3
  const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
6
- export const staticPages = async (routes) => {
7
- for (const route of Array.isArray(routes) ? routes : [routes]) {
4
+ export async function staticPages(...routes) {
5
+ const teardown = new Set();
6
+ for (const route of routes) {
8
7
  if (typeof route !== 'object' || !route)
9
- throw new Error(`Route type mismatch, expected 'object', got '${getType(route)}'.`);
8
+ throw new Error(`Argument type mismatch, expected 'object', got '${getType(route)}'.`);
10
9
  const { from, to, controller } = route;
11
10
  if (!isIterable(from) && !isAsyncIterable(from))
12
- throw new Error('Route \'from\' is not an \'iterable\' or an \'asyncIterable\'.');
11
+ throw new Error('Argument type mismatch, \'from\' exptects \'iterable\' or \'asyncIterable\'.');
13
12
  if (typeof to !== 'function')
14
- throw new Error(`Route 'to' type mismatch, expected 'function', got '${getType(to)}'.`);
13
+ throw new Error(`Argument type mismatch, 'to' expects 'function', got '${getType(to)}'.`);
15
14
  if (typeof controller !== 'undefined' && typeof controller !== 'function')
16
- throw new Error(`Route 'controller' type mismatch, expected 'function', got '${getType(controller)}'.`);
15
+ throw new Error(`Argument type mismatch, 'controller' expects 'function', got '${getType(controller)}'.`);
16
+ if (typeof to.teardown === 'function') {
17
+ teardown.add(to.teardown);
18
+ }
17
19
  const isController = typeof controller === 'function';
18
20
  for await (const data of from) {
19
21
  const results = isController ? await controller(data) : data;
@@ -29,5 +31,8 @@ export const staticPages = async (routes) => {
29
31
  }
30
32
  }
31
33
  }
32
- };
34
+ for (const fn of teardown) {
35
+ await fn();
36
+ }
37
+ }
33
38
  export default staticPages;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@static-pages/core",
3
- "version": "4.0.0",
3
+ "version": "5.0.1",
4
4
  "description": "General purpose static pages renderer.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.js",
@@ -45,15 +45,15 @@
45
45
  },
46
46
  "homepage": "https://staticpagesjs.github.io/",
47
47
  "devDependencies": {
48
- "@types/jest": "^28.1.6",
48
+ "@types/jest": "^29.2.3",
49
49
  "@types/node": "^14.18.9",
50
50
  "@typescript-eslint/eslint-plugin": "^5.3.0",
51
51
  "@typescript-eslint/parser": "^5.3.0",
52
52
  "coveralls": "^3.1.1",
53
53
  "cross-env": "^7.0.3",
54
54
  "eslint": "^8.1.0",
55
- "eslint-plugin-jest": "^26.6.0",
56
- "jest": "^28.1.3",
55
+ "eslint-plugin-jest": "^27.1.5",
56
+ "jest": "^29.3.1",
57
57
  "rimraf": "^3.0.2",
58
58
  "typescript": "^4.4.4"
59
59
  }