@static-pages/core 7.0.0-alpha.2 → 7.0.0-alpha.4
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 +18 -39
- package/cjs/create-reader.js +17 -8
- package/cjs/create-writer.js +1 -1
- package/cjs/helpers.d.ts +4 -17
- package/esm/create-reader.js +17 -8
- package/esm/create-writer.js +1 -1
- package/esm/helpers.d.ts +4 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -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-reader.js
CHANGED
|
@@ -7,7 +7,6 @@ exports.createReader = void 0;
|
|
|
7
7
|
const picomatch_1 = __importDefault(require("picomatch"));
|
|
8
8
|
const autoparse_js_1 = require("./autoparse.js");
|
|
9
9
|
const helpers_js_1 = require("./helpers.js");
|
|
10
|
-
const node_path_1 = require("node:path");
|
|
11
10
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
12
11
|
async function* createReader({ fs = node_fs_1.default, cwd = 'pages', pattern, ignore, parse = autoparse_js_1.autoparse, onError = (error) => { throw error; }, } = {}) {
|
|
13
12
|
if (!(0, helpers_js_1.isFilesystem)(fs))
|
|
@@ -25,13 +24,23 @@ async function* createReader({ fs = node_fs_1.default, cwd = 'pages', pattern, i
|
|
|
25
24
|
if (typeof onError !== 'function')
|
|
26
25
|
throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(onError)}' at 'onError' property.`);
|
|
27
26
|
let filenames = await new Promise((resolve, reject) => {
|
|
28
|
-
fs.readdir(cwd, {
|
|
27
|
+
fs.readdir(cwd, { recursive: true, withFileTypes: false, encoding: 'utf8' }, (err, entries) => {
|
|
29
28
|
if (err)
|
|
30
|
-
reject(err);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
return reject(err);
|
|
30
|
+
let filtered = [];
|
|
31
|
+
let processed = 0;
|
|
32
|
+
for (const entry of entries) {
|
|
33
|
+
fs.stat(cwd + '/' + entry, (err, stats) => {
|
|
34
|
+
if (err)
|
|
35
|
+
return reject(err);
|
|
36
|
+
if (stats.isFile()) {
|
|
37
|
+
filtered.push(entry);
|
|
38
|
+
}
|
|
39
|
+
if (++processed === entries.length) {
|
|
40
|
+
return resolve(filtered);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
35
44
|
});
|
|
36
45
|
});
|
|
37
46
|
if (typeof pattern !== 'undefined' || typeof ignore !== 'undefined') {
|
|
@@ -47,7 +56,7 @@ async function* createReader({ fs = node_fs_1.default, cwd = 'pages', pattern, i
|
|
|
47
56
|
for (const filename of filenames) {
|
|
48
57
|
try {
|
|
49
58
|
const content = await new Promise((resolve, reject) => {
|
|
50
|
-
fs.readFile(
|
|
59
|
+
fs.readFile(cwd + '/' + filename, (err, data) => {
|
|
51
60
|
if (err)
|
|
52
61
|
reject(err);
|
|
53
62
|
else
|
package/cjs/create-writer.js
CHANGED
|
@@ -25,8 +25,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.createWriter = void 0;
|
|
27
27
|
const helpers_js_1 = require("./helpers.js");
|
|
28
|
-
const node_path_1 = require("node:path");
|
|
29
28
|
const nodeFs = __importStar(require("node:fs"));
|
|
29
|
+
const node_path_1 = require("node:path");
|
|
30
30
|
const defaultNamer = (data) => {
|
|
31
31
|
if (!!data && typeof data === 'object' && 'url' in data && typeof data.url === 'string') {
|
|
32
32
|
return data.url.concat('.html');
|
package/cjs/helpers.d.ts
CHANGED
|
@@ -1,26 +1,14 @@
|
|
|
1
1
|
export type MaybePromise<T> = T | Promise<T>;
|
|
2
|
-
interface Stats {
|
|
3
|
-
isFile(): boolean;
|
|
4
|
-
isDirectory(): boolean;
|
|
5
|
-
}
|
|
6
|
-
interface Dirent {
|
|
7
|
-
name: string;
|
|
8
|
-
path: string;
|
|
9
|
-
isFile(): boolean;
|
|
10
|
-
isDirectory(): boolean;
|
|
11
|
-
}
|
|
12
2
|
export interface Filesystem {
|
|
3
|
+
stat(path: string | URL, callback: (err: Error | null, stats: {
|
|
4
|
+
isFile(): boolean;
|
|
5
|
+
isDirectory(): boolean;
|
|
6
|
+
}) => void): void;
|
|
13
7
|
readdir(path: string | URL, options: {
|
|
14
8
|
encoding: 'utf8';
|
|
15
9
|
withFileTypes: false;
|
|
16
10
|
recursive: boolean;
|
|
17
11
|
}, callback: (err: Error | null, files: string[]) => void): void;
|
|
18
|
-
readdir(path: string | URL, options: {
|
|
19
|
-
encoding: 'utf8';
|
|
20
|
-
withFileTypes: true;
|
|
21
|
-
recursive: boolean;
|
|
22
|
-
}, callback: (err: Error | null, files: Dirent[]) => void): void;
|
|
23
|
-
stat(path: string | URL, callback: (err: Error | null, stats: Stats) => void): void;
|
|
24
12
|
mkdir(path: string | URL, options: {
|
|
25
13
|
recursive: true;
|
|
26
14
|
}, callback: (err: Error | null, path?: string) => void): void;
|
|
@@ -31,4 +19,3 @@ export declare const isFilesystem: (x: unknown) => x is Filesystem;
|
|
|
31
19
|
export declare const isIterable: <T>(x: unknown) => x is Iterable<T>;
|
|
32
20
|
export declare const isAsyncIterable: <T>(x: unknown) => x is AsyncIterable<T>;
|
|
33
21
|
export declare const getType: (x: unknown) => string;
|
|
34
|
-
export {};
|
package/esm/create-reader.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import picomatch from 'picomatch';
|
|
2
2
|
import { autoparse } from './autoparse.js';
|
|
3
3
|
import { getType, isIterable, isAsyncIterable, isFilesystem } from './helpers.js';
|
|
4
|
-
import { join, relative } from 'node:path';
|
|
5
4
|
import nodeFs from 'node:fs';
|
|
6
5
|
export async function* createReader({ fs = nodeFs, cwd = 'pages', pattern, ignore, parse = autoparse, onError = (error) => { throw error; }, } = {}) {
|
|
7
6
|
if (!isFilesystem(fs))
|
|
@@ -19,13 +18,23 @@ export async function* createReader({ fs = nodeFs, cwd = 'pages', pattern, ignor
|
|
|
19
18
|
if (typeof onError !== 'function')
|
|
20
19
|
throw new TypeError(`Expected 'function', recieved '${getType(onError)}' at 'onError' property.`);
|
|
21
20
|
let filenames = await new Promise((resolve, reject) => {
|
|
22
|
-
fs.readdir(cwd, {
|
|
21
|
+
fs.readdir(cwd, { recursive: true, withFileTypes: false, encoding: 'utf8' }, (err, entries) => {
|
|
23
22
|
if (err)
|
|
24
|
-
reject(err);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
return reject(err);
|
|
24
|
+
let filtered = [];
|
|
25
|
+
let processed = 0;
|
|
26
|
+
for (const entry of entries) {
|
|
27
|
+
fs.stat(cwd + '/' + entry, (err, stats) => {
|
|
28
|
+
if (err)
|
|
29
|
+
return reject(err);
|
|
30
|
+
if (stats.isFile()) {
|
|
31
|
+
filtered.push(entry);
|
|
32
|
+
}
|
|
33
|
+
if (++processed === entries.length) {
|
|
34
|
+
return resolve(filtered);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
29
38
|
});
|
|
30
39
|
});
|
|
31
40
|
if (typeof pattern !== 'undefined' || typeof ignore !== 'undefined') {
|
|
@@ -41,7 +50,7 @@ export async function* createReader({ fs = nodeFs, cwd = 'pages', pattern, ignor
|
|
|
41
50
|
for (const filename of filenames) {
|
|
42
51
|
try {
|
|
43
52
|
const content = await new Promise((resolve, reject) => {
|
|
44
|
-
fs.readFile(
|
|
53
|
+
fs.readFile(cwd + '/' + filename, (err, data) => {
|
|
45
54
|
if (err)
|
|
46
55
|
reject(err);
|
|
47
56
|
else
|
package/esm/create-writer.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getType, isIterable, isAsyncIterable, isFilesystem } from './helpers.js';
|
|
2
|
-
import { dirname } from 'node:path';
|
|
3
2
|
import * as nodeFs from 'node:fs';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
4
|
const defaultNamer = (data) => {
|
|
5
5
|
if (!!data && typeof data === 'object' && 'url' in data && typeof data.url === 'string') {
|
|
6
6
|
return data.url.concat('.html');
|
package/esm/helpers.d.ts
CHANGED
|
@@ -1,26 +1,14 @@
|
|
|
1
1
|
export type MaybePromise<T> = T | Promise<T>;
|
|
2
|
-
interface Stats {
|
|
3
|
-
isFile(): boolean;
|
|
4
|
-
isDirectory(): boolean;
|
|
5
|
-
}
|
|
6
|
-
interface Dirent {
|
|
7
|
-
name: string;
|
|
8
|
-
path: string;
|
|
9
|
-
isFile(): boolean;
|
|
10
|
-
isDirectory(): boolean;
|
|
11
|
-
}
|
|
12
2
|
export interface Filesystem {
|
|
3
|
+
stat(path: string | URL, callback: (err: Error | null, stats: {
|
|
4
|
+
isFile(): boolean;
|
|
5
|
+
isDirectory(): boolean;
|
|
6
|
+
}) => void): void;
|
|
13
7
|
readdir(path: string | URL, options: {
|
|
14
8
|
encoding: 'utf8';
|
|
15
9
|
withFileTypes: false;
|
|
16
10
|
recursive: boolean;
|
|
17
11
|
}, callback: (err: Error | null, files: string[]) => void): void;
|
|
18
|
-
readdir(path: string | URL, options: {
|
|
19
|
-
encoding: 'utf8';
|
|
20
|
-
withFileTypes: true;
|
|
21
|
-
recursive: boolean;
|
|
22
|
-
}, callback: (err: Error | null, files: Dirent[]) => void): void;
|
|
23
|
-
stat(path: string | URL, callback: (err: Error | null, stats: Stats) => void): void;
|
|
24
12
|
mkdir(path: string | URL, options: {
|
|
25
13
|
recursive: true;
|
|
26
14
|
}, callback: (err: Error | null, path?: string) => void): void;
|
|
@@ -31,4 +19,3 @@ export declare const isFilesystem: (x: unknown) => x is Filesystem;
|
|
|
31
19
|
export declare const isIterable: <T>(x: unknown) => x is Iterable<T>;
|
|
32
20
|
export declare const isAsyncIterable: <T>(x: unknown) => x is AsyncIterable<T>;
|
|
33
21
|
export declare const getType: (x: unknown) => string;
|
|
34
|
-
export {};
|