@static-pages/core 7.0.0-alpha.3 → 7.0.0-alpha.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.
- package/CHANGELOG.md +6 -6
- package/README.md +20 -41
- package/cjs/create-writer.d.ts +1 -1
- package/cjs/create-writer.js +31 -35
- package/cjs/static-pages.d.ts +1 -1
- package/cjs/static-pages.js +11 -12
- package/esm/create-writer.d.ts +1 -1
- package/esm/create-writer.js +32 -36
- package/esm/static-pages.d.ts +1 -1
- package/esm/static-pages.js +10 -11
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
-
## 7.0.0
|
|
4
|
-
- New `CreateReader` and `CreateWriter` utilities available as `from` and `to` prop values.
|
|
5
|
-
-
|
|
3
|
+
## 7.0.0
|
|
4
|
+
- New `CreateReader` and `CreateWriter` utilities available as `route.from` and `route.to` prop values.
|
|
5
|
+
- Reverted `route.to` callbacks to work as it was in `v4.0.0`. The function is called per doucument, recieving the current page data.
|
|
6
6
|
- Type defintion improvements.
|
|
7
7
|
- Test suite switched from tap to mocha for now.
|
|
8
8
|
- Coverage reports switched from nyc to c8.
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
## 6.0.0
|
|
12
|
-
-
|
|
13
|
-
- Removed the `.teardown()` call on the
|
|
12
|
+
- Use JS iterator protocol for the `route.to` callbacks.
|
|
13
|
+
- Removed the `.teardown()` call on the `route.to` when iteration finished.
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
## 5.0.3
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
- Updated maintenance badge in README.md.
|
|
24
24
|
|
|
25
25
|
## 5.0.0
|
|
26
|
-
- Added a `.teardown()` call on the
|
|
26
|
+
- Added a `.teardown()` call on the `route.to` which is executed when iteration finished.
|
|
27
27
|
- Provided better type definitions.
|
|
28
28
|
|
|
29
29
|
|
package/README.md
CHANGED
|
@@ -93,8 +93,8 @@ Each route consists of a `from`, `to` and a `controller` property matching the d
|
|
|
93
93
|
|
|
94
94
|
```ts
|
|
95
95
|
interface Route<F, T> {
|
|
96
|
-
from?: Iterable<F> | AsyncIterable<F> |
|
|
97
|
-
to?: { (data:
|
|
96
|
+
from?: Iterable<F> | AsyncIterable<F> | createReader.Options<F>;
|
|
97
|
+
to?: { (data: T): MaybePromise<void>; } | createWriter.Options<T>;
|
|
98
98
|
controller?(data: F): MaybePromise<undefined | T | Iterable<T> | AsyncIterable<T>>;
|
|
99
99
|
}
|
|
100
100
|
|
|
@@ -128,66 +128,35 @@ interface CreateWriterOptions<T> {
|
|
|
128
128
|
onError?(error: unknown): MaybePromise<void>;
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
interface Stats {
|
|
132
|
-
isFile(): boolean;
|
|
133
|
-
isDirectory(): boolean;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
interface Dirent {
|
|
137
|
-
name: string;
|
|
138
|
-
path: string;
|
|
139
|
-
isFile(): boolean;
|
|
140
|
-
isDirectory(): boolean;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
131
|
interface Filesystem {
|
|
144
|
-
|
|
132
|
+
stat(
|
|
145
133
|
path: string | URL,
|
|
146
|
-
|
|
147
|
-
encoding: 'utf8';
|
|
148
|
-
withFileTypes: false;
|
|
149
|
-
recursive: boolean;
|
|
150
|
-
},
|
|
151
|
-
callback: (err: Error | null, files: string[]) => void,
|
|
134
|
+
callback: (err: Error | null, stats: { isFile(): boolean; isDirectory(): boolean; }) => void
|
|
152
135
|
): void;
|
|
153
136
|
|
|
154
137
|
readdir(
|
|
155
138
|
path: string | URL,
|
|
156
139
|
options: {
|
|
157
140
|
encoding: 'utf8';
|
|
158
|
-
withFileTypes:
|
|
141
|
+
withFileTypes: false;
|
|
159
142
|
recursive: boolean;
|
|
160
143
|
},
|
|
161
|
-
callback: (err: Error | null, files:
|
|
144
|
+
callback: (err: Error | null, files: string[]) => void,
|
|
162
145
|
): void;
|
|
163
146
|
|
|
164
|
-
|
|
147
|
+
mkdir(
|
|
165
148
|
path: string | URL,
|
|
166
149
|
options: {
|
|
167
|
-
|
|
150
|
+
recursive: true;
|
|
168
151
|
},
|
|
169
|
-
callback: (err: Error | null,
|
|
152
|
+
callback: (err: Error | null, path?: string) => void
|
|
170
153
|
): void;
|
|
171
154
|
|
|
172
155
|
readFile(
|
|
173
156
|
path: string | URL,
|
|
174
|
-
options: null,
|
|
175
157
|
callback: (err: Error | null, data: Uint8Array) => void
|
|
176
158
|
): void;
|
|
177
159
|
|
|
178
|
-
stat(
|
|
179
|
-
path: string | URL,
|
|
180
|
-
callback: (err: Error | null, stats: Stats) => void
|
|
181
|
-
): void;
|
|
182
|
-
|
|
183
|
-
mkdir(
|
|
184
|
-
path: string | URL,
|
|
185
|
-
options: {
|
|
186
|
-
recursive: true;
|
|
187
|
-
},
|
|
188
|
-
callback: (err: Error | null, path?: string) => void
|
|
189
|
-
): void;
|
|
190
|
-
|
|
191
160
|
writeFile(
|
|
192
161
|
path: string | URL,
|
|
193
162
|
data: string | Uint8Array,
|
|
@@ -203,7 +172,7 @@ When you use the `createReader` and `createWriter` interfaces to read and write
|
|
|
203
172
|
### `CreateReaderOptions` default parameters
|
|
204
173
|
- `fs`: the nodejs `fs` module
|
|
205
174
|
- `cwd`: `'pages'`
|
|
206
|
-
- `parse`:
|
|
175
|
+
- `parse`: *see About the default `parse` function*
|
|
207
176
|
- `onError`: `(err) => { throw err; }`
|
|
208
177
|
|
|
209
178
|
### `CreateWriterOptions` default parameters
|
|
@@ -213,6 +182,16 @@ When you use the `createReader` and `createWriter` interfaces to read and write
|
|
|
213
182
|
- `render`: `(data) => data.content`
|
|
214
183
|
- `onError`: `(err) => { throw err; }`
|
|
215
184
|
|
|
185
|
+
### About the default `parse` function
|
|
186
|
+
|
|
187
|
+
When using the default parser, a file type will be guessed by the file extension.
|
|
188
|
+
These could be `json`, `yaml`, `yml`, `md` or `markdown`.
|
|
189
|
+
- `json` will be parsed with `JSON.parse`
|
|
190
|
+
- `yaml` and `yml` will be parsed with the `yaml` package
|
|
191
|
+
- `md` and `markdown` will be parsed with the `gray-matter` package
|
|
192
|
+
|
|
193
|
+
When the document does not contain an `url` property, this function will create one containing the filename without extension.
|
|
194
|
+
|
|
216
195
|
|
|
217
196
|
## `staticPages.with(defaults: Partial<Route>): { (...routes: Partial<Route>[]): Promise<void>; }`
|
|
218
197
|
|
package/cjs/create-writer.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare namespace createWriter {
|
|
|
8
8
|
onError?(error: unknown): MaybePromise<void>;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
-
export declare function createWriter<T>({ fs, cwd, name, render, onError, }?: createWriter.Options<T>): (
|
|
11
|
+
export declare function createWriter<T>({ fs, cwd, name, render, onError, }?: createWriter.Options<T>): (data: T) => Promise<void>;
|
|
12
12
|
export declare namespace createWriter {
|
|
13
13
|
var isOptions: <T>(x: unknown) => x is Options<T>;
|
|
14
14
|
}
|
package/cjs/create-writer.js
CHANGED
|
@@ -52,43 +52,39 @@ function createWriter({ fs = nodeFs, cwd = 'public', name = defaultNamer, render
|
|
|
52
52
|
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(name)}' at 'name' property.`);
|
|
53
53
|
if (typeof onError !== 'function')
|
|
54
54
|
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(onError)}' at 'onError' property.`);
|
|
55
|
-
return async function (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
else {
|
|
75
|
-
resolve(undefined);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
55
|
+
return async function (data) {
|
|
56
|
+
try {
|
|
57
|
+
const filepath = cwd + '/' + await name(data);
|
|
58
|
+
const dirpath = (0, node_path_1.dirname)(filepath);
|
|
59
|
+
await new Promise((resolve, reject) => {
|
|
60
|
+
fs.stat(dirpath, (err, stats) => {
|
|
61
|
+
if (err) {
|
|
62
|
+
fs.mkdir(dirpath, { recursive: true }, (err) => {
|
|
63
|
+
if (err) {
|
|
64
|
+
reject(err);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
resolve(undefined);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
resolve(undefined);
|
|
73
|
+
}
|
|
78
74
|
});
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
75
|
+
});
|
|
76
|
+
const content = await render(data);
|
|
77
|
+
await new Promise((resolve, reject) => {
|
|
78
|
+
fs.writeFile(filepath, content, (err) => {
|
|
79
|
+
if (err)
|
|
80
|
+
reject(err);
|
|
81
|
+
else
|
|
82
|
+
resolve(undefined);
|
|
87
83
|
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
await onError(error);
|
|
92
88
|
}
|
|
93
89
|
};
|
|
94
90
|
}
|
package/cjs/static-pages.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare namespace staticPages {
|
|
|
5
5
|
interface Route<F = unknown, T = unknown> {
|
|
6
6
|
from?: Iterable<F> | AsyncIterable<F> | createReader.Options<F>;
|
|
7
7
|
to?: {
|
|
8
|
-
(data:
|
|
8
|
+
(data: T): MaybePromise<void>;
|
|
9
9
|
} | createWriter.Options<T>;
|
|
10
10
|
controller?(data: F): MaybePromise<undefined | T | Iterable<T> | AsyncIterable<T>>;
|
|
11
11
|
}
|
package/cjs/static-pages.js
CHANGED
|
@@ -19,21 +19,20 @@ async function staticPages(...routes) {
|
|
|
19
19
|
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(to)}' at 'to' property.`);
|
|
20
20
|
if (typeof controller !== 'undefined' && typeof controller !== 'function')
|
|
21
21
|
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(controller)}' at 'controller' property.`);
|
|
22
|
-
await
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
else if (typeof data !== 'undefined') {
|
|
33
|
-
yield data;
|
|
22
|
+
for await (const data of from) {
|
|
23
|
+
const finalData = controller ? await controller(data) : data;
|
|
24
|
+
if ((0, helpers_js_1.isIterable)(finalData) || (0, helpers_js_1.isAsyncIterable)(finalData)) {
|
|
25
|
+
for await (const finalDataItem of finalData) {
|
|
26
|
+
await to(finalDataItem);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
else if (typeof finalData !== 'undefined') {
|
|
30
|
+
await to(finalData);
|
|
31
|
+
}
|
|
34
32
|
}
|
|
35
33
|
}
|
|
36
34
|
}
|
|
35
|
+
exports.staticPages = staticPages;
|
|
37
36
|
staticPages.with = ({ from, to, controller }) => {
|
|
38
37
|
const withFunction = (newValue) => staticPages.with({
|
|
39
38
|
from: determineFrom(from, newValue.from),
|
package/esm/create-writer.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export declare namespace createWriter {
|
|
|
8
8
|
onError?(error: unknown): MaybePromise<void>;
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
-
export declare function createWriter<T>({ fs, cwd, name, render, onError, }?: createWriter.Options<T>): (
|
|
11
|
+
export declare function createWriter<T>({ fs, cwd, name, render, onError, }?: createWriter.Options<T>): (data: T) => Promise<void>;
|
|
12
12
|
export declare namespace createWriter {
|
|
13
13
|
var isOptions: <T>(x: unknown) => x is Options<T>;
|
|
14
14
|
}
|
package/esm/create-writer.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getType,
|
|
1
|
+
import { getType, isFilesystem } from './helpers.js';
|
|
2
2
|
import * as nodeFs from 'node:fs';
|
|
3
3
|
import { dirname } from 'node:path';
|
|
4
4
|
const defaultNamer = (data) => {
|
|
@@ -26,43 +26,39 @@ export function createWriter({ fs = nodeFs, cwd = 'public', name = defaultNamer,
|
|
|
26
26
|
throw new TypeError(`Expected 'function', recieved '${getType(name)}' at 'name' property.`);
|
|
27
27
|
if (typeof onError !== 'function')
|
|
28
28
|
throw new TypeError(`Expected 'function', recieved '${getType(onError)}' at 'onError' property.`);
|
|
29
|
-
return async function (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
else {
|
|
49
|
-
resolve(undefined);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
29
|
+
return async function (data) {
|
|
30
|
+
try {
|
|
31
|
+
const filepath = cwd + '/' + await name(data);
|
|
32
|
+
const dirpath = dirname(filepath);
|
|
33
|
+
await new Promise((resolve, reject) => {
|
|
34
|
+
fs.stat(dirpath, (err, stats) => {
|
|
35
|
+
if (err) {
|
|
36
|
+
fs.mkdir(dirpath, { recursive: true }, (err) => {
|
|
37
|
+
if (err) {
|
|
38
|
+
reject(err);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
resolve(undefined);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
resolve(undefined);
|
|
47
|
+
}
|
|
52
48
|
});
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
49
|
+
});
|
|
50
|
+
const content = await render(data);
|
|
51
|
+
await new Promise((resolve, reject) => {
|
|
52
|
+
fs.writeFile(filepath, content, (err) => {
|
|
53
|
+
if (err)
|
|
54
|
+
reject(err);
|
|
55
|
+
else
|
|
56
|
+
resolve(undefined);
|
|
61
57
|
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
await onError(error);
|
|
66
62
|
}
|
|
67
63
|
};
|
|
68
64
|
}
|
package/esm/static-pages.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare namespace staticPages {
|
|
|
5
5
|
interface Route<F = unknown, T = unknown> {
|
|
6
6
|
from?: Iterable<F> | AsyncIterable<F> | createReader.Options<F>;
|
|
7
7
|
to?: {
|
|
8
|
-
(data:
|
|
8
|
+
(data: T): MaybePromise<void>;
|
|
9
9
|
} | createWriter.Options<T>;
|
|
10
10
|
controller?(data: F): MaybePromise<undefined | T | Iterable<T> | AsyncIterable<T>>;
|
|
11
11
|
}
|
package/esm/static-pages.js
CHANGED
|
@@ -16,17 +16,16 @@ export async function staticPages(...routes) {
|
|
|
16
16
|
throw new TypeError(`Expected 'function', recieved '${getType(to)}' at 'to' property.`);
|
|
17
17
|
if (typeof controller !== 'undefined' && typeof controller !== 'function')
|
|
18
18
|
throw new TypeError(`Expected 'function', recieved '${getType(controller)}' at 'controller' property.`);
|
|
19
|
-
await
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
yield data;
|
|
19
|
+
for await (const data of from) {
|
|
20
|
+
const finalData = controller ? await controller(data) : data;
|
|
21
|
+
if (isIterable(finalData) || isAsyncIterable(finalData)) {
|
|
22
|
+
for await (const finalDataItem of finalData) {
|
|
23
|
+
await to(finalDataItem);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else if (typeof finalData !== 'undefined') {
|
|
27
|
+
await to(finalData);
|
|
28
|
+
}
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
31
|
}
|