@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 +13 -10
- package/cjs/index.d.ts +14 -7
- package/cjs/index.js +15 -10
- package/esm/index.d.ts +14 -7
- package/esm/index.js +14 -9
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://app.travis-ci.com/staticpagesjs/core)
|
|
4
4
|
[](https://coveralls.io/github/staticpagesjs/core?branch=master)
|
|
5
5
|

|
|
6
|
-

|
|
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
|
|
74
|
-
|
|
75
|
-
Each
|
|
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)
|
|
82
|
-
controller
|
|
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
|
|
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:
|
|
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
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
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
|
-
|
|
10
|
-
|
|
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(`
|
|
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('
|
|
14
|
+
throw new Error('Argument type mismatch, \'from\' exptects \'iterable\' or \'asyncIterable\'.');
|
|
16
15
|
if (typeof to !== 'function')
|
|
17
|
-
throw new Error(`
|
|
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(`
|
|
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 =
|
|
42
|
+
exports.default = staticPages;
|
package/esm/index.d.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
export declare namespace staticPages {
|
|
2
|
-
type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
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
|
|
7
|
-
|
|
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(`
|
|
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('
|
|
11
|
+
throw new Error('Argument type mismatch, \'from\' exptects \'iterable\' or \'asyncIterable\'.');
|
|
13
12
|
if (typeof to !== 'function')
|
|
14
|
-
throw new Error(`
|
|
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(`
|
|
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": "
|
|
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": "^
|
|
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": "^
|
|
56
|
-
"jest": "^
|
|
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
|
}
|