@static-pages/core 6.0.0 → 7.0.0-alpha.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.
@@ -0,0 +1,54 @@
1
+ import type { MaybePromise } from './helpers.js';
2
+ import { createReader } from './create-reader.js';
3
+ import { createWriter } from './create-writer.js';
4
+ export declare namespace staticPages {
5
+ interface Route<F = unknown, T = unknown> {
6
+ from?: Iterable<F> | AsyncIterable<F> | createReader.Options<F>;
7
+ to?: {
8
+ (data: AsyncIterable<T>): MaybePromise<void>;
9
+ } | createWriter.Options<T>;
10
+ controller?(data: F): MaybePromise<undefined | T | Iterable<T> | AsyncIterable<T>>;
11
+ }
12
+ }
13
+ export declare function staticPages<F1, T1>(...route: [
14
+ staticPages.Route<F1, T1>
15
+ ]): Promise<void>;
16
+ export declare function staticPages<F1, T1, F2, T2>(...route: [
17
+ staticPages.Route<F1, T1>,
18
+ staticPages.Route<F2, T2>
19
+ ]): Promise<void>;
20
+ export declare function staticPages<F1, T1, F2, T2, F3, T3>(...route: [
21
+ staticPages.Route<F1, T1>,
22
+ staticPages.Route<F2, T2>,
23
+ staticPages.Route<F3, T3>
24
+ ]): Promise<void>;
25
+ export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4>(...route: [
26
+ staticPages.Route<F1, T1>,
27
+ staticPages.Route<F2, T2>,
28
+ staticPages.Route<F3, T3>,
29
+ staticPages.Route<F4, T4>
30
+ ]): Promise<void>;
31
+ export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5>(...route: [
32
+ staticPages.Route<F1, T1>,
33
+ staticPages.Route<F2, T2>,
34
+ staticPages.Route<F3, T3>,
35
+ staticPages.Route<F4, T4>,
36
+ staticPages.Route<F5, T5>
37
+ ]): Promise<void>;
38
+ export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5, F6, T6>(...route: [
39
+ staticPages.Route<F1, T1>,
40
+ staticPages.Route<F2, T2>,
41
+ staticPages.Route<F3, T3>,
42
+ staticPages.Route<F4, T4>,
43
+ staticPages.Route<F5, T5>,
44
+ staticPages.Route<F6, T6>
45
+ ]): Promise<void>;
46
+ export declare function staticPages(...routes: staticPages.Route[]): Promise<void>;
47
+ export declare namespace staticPages {
48
+ var _a: ({ from, to, controller }: NullablePartialRoute) => typeof staticPages;
49
+ export { _a as with };
50
+ }
51
+ type NullablePartialRoute = {
52
+ [P in keyof staticPages.Route]?: null | staticPages.Route[P];
53
+ };
54
+ export {};
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.staticPages = void 0;
4
+ const helpers_js_1 = require("./helpers.js");
5
+ const create_reader_js_1 = require("./create-reader.js");
6
+ const create_writer_js_1 = require("./create-writer.js");
7
+ async function staticPages(...routes) {
8
+ for (const route of routes) {
9
+ if (typeof route !== 'object' || !route)
10
+ throw new TypeError(`Expected 'object', recieved '${(0, helpers_js_1.getType)(route)}'.`);
11
+ let { from, to, controller } = route;
12
+ if (create_reader_js_1.createReader.isOptions(from))
13
+ from = (0, create_reader_js_1.createReader)(from);
14
+ if (create_writer_js_1.createWriter.isOptions(to))
15
+ to = (0, create_writer_js_1.createWriter)(to);
16
+ if (!(0, helpers_js_1.isIterable)(from) && !(0, helpers_js_1.isAsyncIterable)(from))
17
+ throw new TypeError(`Expected 'Iterable' or 'AsyncIterable' at 'from' property.`);
18
+ if (typeof to !== 'function')
19
+ throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(to)}' at 'to' property.`);
20
+ if (typeof controller !== 'undefined' && typeof controller !== 'function')
21
+ throw new TypeError(`Expected 'function', recieved '${(0, helpers_js_1.getType)(controller)}' at 'controller' property.`);
22
+ await to(asyncGenerator(from, controller));
23
+ }
24
+ }
25
+ exports.staticPages = staticPages;
26
+ async function* asyncGenerator(items, controller) {
27
+ for await (const item of items) {
28
+ const data = controller ? await controller(item) : item;
29
+ if ((0, helpers_js_1.isIterable)(data) || (0, helpers_js_1.isAsyncIterable)(data)) {
30
+ yield* data;
31
+ }
32
+ else if (typeof data !== 'undefined') {
33
+ yield data;
34
+ }
35
+ }
36
+ }
37
+ staticPages.with = ({ from, to, controller }) => {
38
+ const withFunction = (newValue) => staticPages.with({
39
+ from: determineFrom(from, newValue.from),
40
+ to: determineTo(to, newValue.to),
41
+ controller: typeof newValue.controller !== 'undefined' ? newValue.controller : controller,
42
+ });
43
+ function modifiedStaticPages(...routes) {
44
+ return staticPages(...routes.map(route => {
45
+ var _a;
46
+ return ({
47
+ from: determineFrom(from, route.from),
48
+ to: determineTo(to, route.to),
49
+ controller: (_a = (typeof route.controller !== 'undefined' ? route.controller : controller)) !== null && _a !== void 0 ? _a : undefined,
50
+ });
51
+ }));
52
+ }
53
+ modifiedStaticPages.with = withFunction;
54
+ return modifiedStaticPages;
55
+ };
56
+ function determineFrom(oldValue, newValue) {
57
+ if (newValue && typeof newValue === 'object' && !(0, helpers_js_1.isIterable)(newValue) && !(0, helpers_js_1.isAsyncIterable)(newValue) &&
58
+ oldValue && typeof oldValue === 'object' && !(0, helpers_js_1.isIterable)(oldValue) && !(0, helpers_js_1.isAsyncIterable)(oldValue))
59
+ return { ...oldValue, ...newValue };
60
+ return typeof newValue !== 'undefined' ? newValue : oldValue;
61
+ }
62
+ function determineTo(oldValue, newValue) {
63
+ if (newValue && typeof newValue === 'object' &&
64
+ oldValue && typeof oldValue === 'object')
65
+ return { ...oldValue, ...newValue };
66
+ return typeof newValue !== 'undefined' ? newValue : oldValue;
67
+ }
@@ -0,0 +1 @@
1
+ export declare function autoparse(content: string | Uint8Array, filename: string): any;
@@ -0,0 +1,17 @@
1
+ import { parse as parseYAML } from 'yaml';
2
+ import parseMarkdown from 'gray-matter';
3
+ export function autoparse(content, filename) {
4
+ const extension = filename.split('.').pop().toLowerCase();
5
+ switch (extension) {
6
+ case 'json':
7
+ return JSON.parse(content.toString());
8
+ case 'yaml':
9
+ case 'yml':
10
+ return parseYAML(content.toString());
11
+ case 'md':
12
+ case 'markdown':
13
+ const { data, content: markdownContent } = parseMarkdown(content.toString());
14
+ return { ...data, content: markdownContent };
15
+ }
16
+ throw new Error(`Could not parse document with '${extension}' extension.`);
17
+ }
@@ -0,0 +1,15 @@
1
+ import type { MaybePromise, Filesystem } from './helpers.js';
2
+ export declare namespace createReader {
3
+ interface Options<T> {
4
+ fs?: Filesystem;
5
+ cwd?: string;
6
+ pattern?: string | string[];
7
+ ignore?: string | string[];
8
+ parse?(content: Uint8Array | string, filename: string): MaybePromise<T>;
9
+ onError?(error: unknown): MaybePromise<void>;
10
+ }
11
+ }
12
+ export declare function createReader<T>({ fs, cwd, pattern, ignore, parse, onError, }?: createReader.Options<T>): AsyncGenerator<Awaited<T>, void, unknown>;
13
+ export declare namespace createReader {
14
+ var isOptions: <T>(x: unknown) => x is Options<T>;
15
+ }
@@ -0,0 +1,60 @@
1
+ import picomatch from 'picomatch';
2
+ import { autoparse } from './autoparse.js';
3
+ import { getType, isIterable, isAsyncIterable, isFilesystem } from './helpers.js';
4
+ import { join, relative } from 'node:path';
5
+ import nodeFs from 'node:fs';
6
+ export async function* createReader({ fs = nodeFs, cwd = 'pages', pattern, ignore, parse = autoparse, onError = (error) => { throw error; }, } = {}) {
7
+ if (!isFilesystem(fs))
8
+ throw new TypeError(`Expected Node FS compatible implementation at 'fs' property.`);
9
+ if (typeof cwd !== 'string')
10
+ throw new TypeError(`Expected 'string', recieved '${getType(cwd)}' at 'cwd' property.`);
11
+ if (!cwd)
12
+ throw new TypeError(`Expected non-empty string at 'cwd'.`);
13
+ if (typeof pattern !== 'undefined' && typeof pattern !== 'string' && !Array.isArray(pattern))
14
+ throw new TypeError(`Expected 'string' or 'string[]', recieved '${getType(pattern)}' at 'pattern' property.`);
15
+ if (typeof ignore !== 'undefined' && typeof ignore !== 'string' && !Array.isArray(ignore))
16
+ throw new TypeError(`Expected 'string' or 'string[]', recieved '${getType(ignore)}' at 'ignore' property.`);
17
+ if (typeof parse !== 'function')
18
+ throw new TypeError(`Expected 'function', recieved '${getType(parse)}' at 'parse' property.`);
19
+ if (typeof onError !== 'function')
20
+ throw new TypeError(`Expected 'function', recieved '${getType(onError)}' at 'onError' property.`);
21
+ let filenames = await new Promise((resolve, reject) => {
22
+ fs.readdir(cwd, { encoding: 'utf8', recursive: true, withFileTypes: true }, (err, files) => {
23
+ if (err)
24
+ reject(err);
25
+ else
26
+ resolve(files
27
+ .filter(x => x.isFile())
28
+ .map(x => join(relative(cwd, x.path), x.name)));
29
+ });
30
+ });
31
+ if (typeof pattern !== 'undefined' || typeof ignore !== 'undefined') {
32
+ const filteredFilenames = [];
33
+ const isMatch = picomatch(pattern !== null && pattern !== void 0 ? pattern : '**/*', { ignore });
34
+ for (const filename of filenames) {
35
+ if (isMatch(filename)) {
36
+ filteredFilenames.push(filename);
37
+ }
38
+ }
39
+ filenames = filteredFilenames;
40
+ }
41
+ for (const filename of filenames) {
42
+ try {
43
+ const content = await new Promise((resolve, reject) => {
44
+ fs.readFile(join(cwd, filename), (err, data) => {
45
+ if (err)
46
+ reject(err);
47
+ else
48
+ resolve(data);
49
+ });
50
+ });
51
+ yield await parse(content, filename);
52
+ }
53
+ catch (error) {
54
+ await onError(error);
55
+ }
56
+ }
57
+ }
58
+ createReader.isOptions = (x) => {
59
+ return x == undefined || (!!x && typeof x === 'object' && !isIterable(x) && !isAsyncIterable(x));
60
+ };
@@ -0,0 +1,14 @@
1
+ import type { MaybePromise, Filesystem } from './helpers.js';
2
+ export declare namespace createWriter {
3
+ interface Options<T> {
4
+ fs?: Filesystem;
5
+ cwd?: string;
6
+ name?(data: T): MaybePromise<string>;
7
+ render?(data: T): MaybePromise<Uint8Array | string>;
8
+ onError?(error: unknown): MaybePromise<void>;
9
+ }
10
+ }
11
+ export declare function createWriter<T>({ fs, cwd, name, render, onError, }?: createWriter.Options<T>): (iterable: Iterable<T> | AsyncIterable<T>) => Promise<void>;
12
+ export declare namespace createWriter {
13
+ var isOptions: <T>(x: unknown) => x is Options<T>;
14
+ }
@@ -0,0 +1,71 @@
1
+ import { getType, isIterable, isAsyncIterable, isFilesystem } from './helpers.js';
2
+ import { dirname } from 'node:path';
3
+ import * as nodeFs from 'node:fs';
4
+ const defaultNamer = (data) => {
5
+ if (!!data && typeof data === 'object' && 'url' in data && typeof data.url === 'string') {
6
+ return data.url.concat('.html');
7
+ }
8
+ throw new Error(`Missing 'url' field in the document.`);
9
+ };
10
+ const defaultRenderer = (data) => {
11
+ if (!!data && typeof data === 'object' && 'content' in data) {
12
+ return '' + data.content;
13
+ }
14
+ throw new Error(`Missing 'content' field in the document.`);
15
+ };
16
+ export function createWriter({ fs = nodeFs, cwd = 'public', name = defaultNamer, render = defaultRenderer, onError = (error) => { throw error; }, } = {}) {
17
+ if (!isFilesystem(fs))
18
+ throw new TypeError(`Expected Node FS compatible implementation at 'fs' property.`);
19
+ if (typeof cwd !== 'string')
20
+ throw new TypeError(`Expected 'string', recieved '${getType(cwd)}' at 'cwd' property.`);
21
+ if (!cwd)
22
+ throw new TypeError(`Expected non-empty string at 'cwd'.`);
23
+ if (typeof render !== 'function')
24
+ throw new TypeError(`Expected 'function', recieved '${getType(render)}' at 'render' property.`);
25
+ if (typeof name !== 'function')
26
+ throw new TypeError(`Expected 'function', recieved '${getType(name)}' at 'name' property.`);
27
+ if (typeof onError !== 'function')
28
+ throw new TypeError(`Expected 'function', recieved '${getType(onError)}' at 'onError' property.`);
29
+ return async function (iterable) {
30
+ if (!isIterable(iterable) && !isAsyncIterable(iterable))
31
+ throw new TypeError(`Expected 'Iterable' or 'AsyncIterable' at callback.`);
32
+ for await (const data of iterable) {
33
+ try {
34
+ const filepath = cwd + '/' + await name(data);
35
+ const dirpath = dirname(filepath);
36
+ await new Promise((resolve, reject) => {
37
+ fs.stat(dirpath, (err, stats) => {
38
+ if (err) {
39
+ fs.mkdir(dirpath, { recursive: true }, (err) => {
40
+ if (err) {
41
+ reject(err);
42
+ }
43
+ else {
44
+ resolve(undefined);
45
+ }
46
+ });
47
+ }
48
+ else {
49
+ resolve(undefined);
50
+ }
51
+ });
52
+ });
53
+ const content = await render(data);
54
+ await new Promise((resolve, reject) => {
55
+ fs.writeFile(filepath, content, (err) => {
56
+ if (err)
57
+ reject(err);
58
+ else
59
+ resolve(undefined);
60
+ });
61
+ });
62
+ }
63
+ catch (error) {
64
+ await onError(error);
65
+ }
66
+ }
67
+ };
68
+ }
69
+ createWriter.isOptions = (x) => {
70
+ return x == undefined || (!!x && typeof x === 'object');
71
+ };
@@ -0,0 +1,34 @@
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
+ export interface Filesystem {
13
+ readdir(path: string | URL, options: {
14
+ encoding: 'utf8';
15
+ withFileTypes: false;
16
+ recursive: boolean;
17
+ }, 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
+ mkdir(path: string | URL, options: {
25
+ recursive: true;
26
+ }, callback: (err: Error | null, path?: string) => void): void;
27
+ readFile(path: string | URL, callback: (err: Error | null, data: Uint8Array) => void): void;
28
+ writeFile(path: string | URL, data: string | Uint8Array, callback: (err: Error | null) => void): void;
29
+ }
30
+ export declare const isFilesystem: (x: unknown) => x is Filesystem;
31
+ export declare const isIterable: <T>(x: unknown) => x is Iterable<T>;
32
+ export declare const isAsyncIterable: <T>(x: unknown) => x is AsyncIterable<T>;
33
+ export declare const getType: (x: unknown) => string;
34
+ export {};
package/esm/helpers.js ADDED
@@ -0,0 +1,4 @@
1
+ export const isFilesystem = (x) => !!x && typeof x === 'object' && 'stat' in x && 'mkdir' in x && 'readFile' in x && 'writeFile' in x;
2
+ export const isIterable = (x) => !!x && typeof x === 'object' && Symbol.iterator in x && typeof x[Symbol.iterator] === 'function';
3
+ export const isAsyncIterable = (x) => !!x && typeof x === 'object' && Symbol.asyncIterator in x && typeof x[Symbol.asyncIterator] === 'function';
4
+ export const getType = (x) => typeof x === 'object' ? (x ? (Array.isArray(x) ? 'array' : 'object') : 'null') : typeof x;
package/esm/index.d.ts CHANGED
@@ -1,15 +1,4 @@
1
- export declare namespace staticPages {
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(data: IteratorResult<W>): void | Promise<void>;
5
- controller?(data: R): void | W | W[] | Promise<void | W | W[]>;
6
- };
7
- }
8
- 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>;
9
- 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>;
10
- 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>;
11
- 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>;
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>, 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>;
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>, 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>;
14
- export declare function staticPages(...routes: staticPages.Route[]): Promise<void>;
15
- export default staticPages;
1
+ export { staticPages, staticPages as default } from './static-pages.js';
2
+ export { createReader } from './create-reader.js';
3
+ export { createWriter } from './create-writer.js';
4
+ export type { Filesystem } from './helpers.js';
package/esm/index.js CHANGED
@@ -1,32 +1,3 @@
1
- const getType = (x) => typeof x === 'object' ? (x ? 'object' : 'null') : typeof x;
2
- const isIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.iterator]) === 'function';
3
- const isAsyncIterable = (x) => typeof (x === null || x === void 0 ? void 0 : x[Symbol.asyncIterator]) === 'function';
4
- export async function staticPages(...routes) {
5
- for (const route of routes) {
6
- if (typeof route !== 'object' || !route)
7
- throw new Error(`Argument type mismatch, expected 'object', got '${getType(route)}'.`);
8
- const { from, to, controller } = route;
9
- if (!isIterable(from) && !isAsyncIterable(from))
10
- throw new Error('Argument type mismatch, \'from\' exptects \'iterable\' or \'asyncIterable\'.');
11
- if (typeof to !== 'function')
12
- throw new Error(`Argument type mismatch, 'to' expects 'function', got '${getType(to)}'.`);
13
- if (typeof controller !== 'undefined' && typeof controller !== 'function')
14
- throw new Error(`Argument type mismatch, 'controller' expects 'function', got '${getType(controller)}'.`);
15
- const isController = typeof controller === 'function';
16
- for await (const data of from) {
17
- const results = isController ? await controller(data) : data;
18
- if (typeof results === 'object' && results) {
19
- if (Array.isArray(results)) {
20
- for (const result of results) {
21
- await to({ value: result });
22
- }
23
- }
24
- else {
25
- await to({ value: results });
26
- }
27
- }
28
- }
29
- await to({ done: true, value: undefined });
30
- }
31
- }
32
- export default staticPages;
1
+ export { staticPages, staticPages as default } from './static-pages.js';
2
+ export { createReader } from './create-reader.js';
3
+ export { createWriter } from './create-writer.js';
@@ -0,0 +1,54 @@
1
+ import type { MaybePromise } from './helpers.js';
2
+ import { createReader } from './create-reader.js';
3
+ import { createWriter } from './create-writer.js';
4
+ export declare namespace staticPages {
5
+ interface Route<F = unknown, T = unknown> {
6
+ from?: Iterable<F> | AsyncIterable<F> | createReader.Options<F>;
7
+ to?: {
8
+ (data: AsyncIterable<T>): MaybePromise<void>;
9
+ } | createWriter.Options<T>;
10
+ controller?(data: F): MaybePromise<undefined | T | Iterable<T> | AsyncIterable<T>>;
11
+ }
12
+ }
13
+ export declare function staticPages<F1, T1>(...route: [
14
+ staticPages.Route<F1, T1>
15
+ ]): Promise<void>;
16
+ export declare function staticPages<F1, T1, F2, T2>(...route: [
17
+ staticPages.Route<F1, T1>,
18
+ staticPages.Route<F2, T2>
19
+ ]): Promise<void>;
20
+ export declare function staticPages<F1, T1, F2, T2, F3, T3>(...route: [
21
+ staticPages.Route<F1, T1>,
22
+ staticPages.Route<F2, T2>,
23
+ staticPages.Route<F3, T3>
24
+ ]): Promise<void>;
25
+ export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4>(...route: [
26
+ staticPages.Route<F1, T1>,
27
+ staticPages.Route<F2, T2>,
28
+ staticPages.Route<F3, T3>,
29
+ staticPages.Route<F4, T4>
30
+ ]): Promise<void>;
31
+ export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5>(...route: [
32
+ staticPages.Route<F1, T1>,
33
+ staticPages.Route<F2, T2>,
34
+ staticPages.Route<F3, T3>,
35
+ staticPages.Route<F4, T4>,
36
+ staticPages.Route<F5, T5>
37
+ ]): Promise<void>;
38
+ export declare function staticPages<F1, T1, F2, T2, F3, T3, F4, T4, F5, T5, F6, T6>(...route: [
39
+ staticPages.Route<F1, T1>,
40
+ staticPages.Route<F2, T2>,
41
+ staticPages.Route<F3, T3>,
42
+ staticPages.Route<F4, T4>,
43
+ staticPages.Route<F5, T5>,
44
+ staticPages.Route<F6, T6>
45
+ ]): Promise<void>;
46
+ export declare function staticPages(...routes: staticPages.Route[]): Promise<void>;
47
+ export declare namespace staticPages {
48
+ var _a: ({ from, to, controller }: NullablePartialRoute) => typeof staticPages;
49
+ export { _a as with };
50
+ }
51
+ type NullablePartialRoute = {
52
+ [P in keyof staticPages.Route]?: null | staticPages.Route[P];
53
+ };
54
+ export {};
@@ -0,0 +1,63 @@
1
+ import { getType, isIterable, isAsyncIterable } from './helpers.js';
2
+ import { createReader } from './create-reader.js';
3
+ import { createWriter } from './create-writer.js';
4
+ export async function staticPages(...routes) {
5
+ for (const route of routes) {
6
+ if (typeof route !== 'object' || !route)
7
+ throw new TypeError(`Expected 'object', recieved '${getType(route)}'.`);
8
+ let { from, to, controller } = route;
9
+ if (createReader.isOptions(from))
10
+ from = createReader(from);
11
+ if (createWriter.isOptions(to))
12
+ to = createWriter(to);
13
+ if (!isIterable(from) && !isAsyncIterable(from))
14
+ throw new TypeError(`Expected 'Iterable' or 'AsyncIterable' at 'from' property.`);
15
+ if (typeof to !== 'function')
16
+ throw new TypeError(`Expected 'function', recieved '${getType(to)}' at 'to' property.`);
17
+ if (typeof controller !== 'undefined' && typeof controller !== 'function')
18
+ throw new TypeError(`Expected 'function', recieved '${getType(controller)}' at 'controller' property.`);
19
+ await to(asyncGenerator(from, controller));
20
+ }
21
+ }
22
+ async function* asyncGenerator(items, controller) {
23
+ for await (const item of items) {
24
+ const data = controller ? await controller(item) : item;
25
+ if (isIterable(data) || isAsyncIterable(data)) {
26
+ yield* data;
27
+ }
28
+ else if (typeof data !== 'undefined') {
29
+ yield data;
30
+ }
31
+ }
32
+ }
33
+ staticPages.with = ({ from, to, controller }) => {
34
+ const withFunction = (newValue) => staticPages.with({
35
+ from: determineFrom(from, newValue.from),
36
+ to: determineTo(to, newValue.to),
37
+ controller: typeof newValue.controller !== 'undefined' ? newValue.controller : controller,
38
+ });
39
+ function modifiedStaticPages(...routes) {
40
+ return staticPages(...routes.map(route => {
41
+ var _a;
42
+ return ({
43
+ from: determineFrom(from, route.from),
44
+ to: determineTo(to, route.to),
45
+ controller: (_a = (typeof route.controller !== 'undefined' ? route.controller : controller)) !== null && _a !== void 0 ? _a : undefined,
46
+ });
47
+ }));
48
+ }
49
+ modifiedStaticPages.with = withFunction;
50
+ return modifiedStaticPages;
51
+ };
52
+ function determineFrom(oldValue, newValue) {
53
+ if (newValue && typeof newValue === 'object' && !isIterable(newValue) && !isAsyncIterable(newValue) &&
54
+ oldValue && typeof oldValue === 'object' && !isIterable(oldValue) && !isAsyncIterable(oldValue))
55
+ return { ...oldValue, ...newValue };
56
+ return typeof newValue !== 'undefined' ? newValue : oldValue;
57
+ }
58
+ function determineTo(oldValue, newValue) {
59
+ if (newValue && typeof newValue === 'object' &&
60
+ oldValue && typeof oldValue === 'object')
61
+ return { ...oldValue, ...newValue };
62
+ return typeof newValue !== 'undefined' ? newValue : oldValue;
63
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@static-pages/core",
3
- "version": "6.0.0",
3
+ "version": "7.0.0-alpha.1",
4
4
  "description": "General purpose static pages renderer.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.js",
@@ -12,20 +12,50 @@
12
12
  "default": "./esm/index.js"
13
13
  }
14
14
  },
15
+ "engines": {
16
+ "node": ">=16.0.0",
17
+ "deno": ">=1.0.0"
18
+ },
15
19
  "scripts": {
16
- "prepack": "npm run test",
17
- "clean": "rimraf esm cjs .nyc_output",
20
+ "prepack": "npm run build && npm run test",
21
+ "postversion": "git push && git push --tags",
22
+ "clean": "rimraf esm cjs coverage",
18
23
  "build": "npm run build:esm && npm run build:cjs",
19
24
  "build:esm": "tsc",
20
25
  "watch:esm": "tsc --watch",
21
- "watch:cjs": "npm run build:cjs && tsc --project tsconfig.cjs.json --watch",
22
- "build:cjs": "tsc --project tsconfig.cjs.json && echo { \"type\": \"commonjs\" }>cjs/package.json",
23
- "test": "tap --nyc-arg=\"--exclude=cjs/index.js\" --node-arg=\"--no-warnings\" --node-arg=\"--experimental-loader\" --node-arg=\"@istanbuljs/esm-loader-hook\""
26
+ "build:cjs": "tsc --outDir cjs --module commonjs && echo { \"type\": \"commonjs\" }>cjs/package.json",
27
+ "watch:cjs": "npm run build:cjs && tsc --outDir cjs --module commonjs --watch",
28
+ "lint": "eslint",
29
+ "test": "mocha",
30
+ "coverage": "c8 -r text -r text-summary -r lcov --include \"esm/*\" npm test"
31
+ },
32
+ "dependencies": {
33
+ "gray-matter": "^4.0.3",
34
+ "yaml": "^2.3.4"
35
+ },
36
+ "devDependencies": {
37
+ "@types/micromatch": "^4.0.4",
38
+ "@types/node": "^20.10.5",
39
+ "@types/picomatch": "^2.3.2",
40
+ "@typescript-eslint/eslint-plugin": "^6.15.0",
41
+ "@typescript-eslint/parser": "^6.15.0",
42
+ "c8": "^8.0.0",
43
+ "eslint": "^8.1.0",
44
+ "mocha": "^10.2.0",
45
+ "picomatch": "^3.0.1",
46
+ "rimraf": "^5.0.1",
47
+ "typescript": "^5.1.6"
24
48
  },
49
+ "author": "László BULIK",
50
+ "license": "MPL-2.0",
51
+ "homepage": "https://staticpagesjs.github.io/",
25
52
  "repository": {
26
53
  "type": "git",
27
54
  "url": "git+https://github.com/staticpagesjs/core.git"
28
55
  },
56
+ "bugs": {
57
+ "url": "https://github.com/staticpagesjs/core/issues"
58
+ },
29
59
  "keywords": [
30
60
  "static",
31
61
  "pages",
@@ -36,22 +66,5 @@
36
66
  "view",
37
67
  "template",
38
68
  "controller"
39
- ],
40
- "author": "László BULIK",
41
- "license": "MPL-2.0",
42
- "bugs": {
43
- "url": "https://github.com/staticpagesjs/core/issues"
44
- },
45
- "homepage": "https://staticpagesjs.github.io/",
46
- "devDependencies": {
47
- "@istanbuljs/esm-loader-hook": "^0.2.0",
48
- "@types/node": "^14.18.9",
49
- "@typescript-eslint/eslint-plugin": "^5.3.0",
50
- "@typescript-eslint/parser": "^5.3.0",
51
- "coveralls": "^3.1.1",
52
- "eslint": "^8.1.0",
53
- "rimraf": "^3.0.2",
54
- "tap": "^16.3.4",
55
- "typescript": "^4.4.4"
56
- }
69
+ ]
57
70
  }