@moltcms/static 0.1.0
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/LICENSE +201 -0
- package/README.md +23 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +222 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +142 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @moltcms/static
|
|
2
|
+
|
|
3
|
+
Incremental static site framework for moltcms.
|
|
4
|
+
|
|
5
|
+
## Runtime
|
|
6
|
+
|
|
7
|
+
This facade and its CLI require **Bun 1.3 or later**. Node.js alone cannot run the CLI.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
bun add @moltcms/static @moltcms/client
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## CLI
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
bunx moltcms-static build --site main
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
This package is part of [moltcms-static](https://github.com/smartcrabai/moltcms-static). See the repository README for adapters, configuration, and examples.
|
|
22
|
+
|
|
23
|
+
Licensed under Apache-2.0.
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { exists, rm } from "node:fs/promises";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { buildSite, codegenSite, planSite, synchronizeSite, } from "./index.js";
|
|
6
|
+
async function main() {
|
|
7
|
+
const args = parse(process.argv.slice(2));
|
|
8
|
+
if (args.command === "init") {
|
|
9
|
+
await initialize(args.config);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const config = await loadConfig(args.config);
|
|
13
|
+
const sites = args.site === undefined
|
|
14
|
+
? config.sites
|
|
15
|
+
: config.sites.filter((site) => site.id === args.site);
|
|
16
|
+
if (sites.length === 0)
|
|
17
|
+
throw new Error(`No configured site matches ${args.site ?? "selection"}`);
|
|
18
|
+
if (args.command === "sync") {
|
|
19
|
+
for (const site of sites) {
|
|
20
|
+
await synchronizeSite(site);
|
|
21
|
+
if (args.gitCommit)
|
|
22
|
+
await commitProjection(site.source.projectionDir, site.id, site.source.kind, args.allowDirty);
|
|
23
|
+
print(args, { site: site.id, status: "synced" });
|
|
24
|
+
}
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (args.command === "codegen") {
|
|
28
|
+
for (const site of sites) {
|
|
29
|
+
const output = args.output ?? "src/moltcms.generated.ts";
|
|
30
|
+
await codegenSite(site, output);
|
|
31
|
+
print(args, { site: site.id, output });
|
|
32
|
+
}
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (args.command === "plan") {
|
|
36
|
+
for (const site of sites)
|
|
37
|
+
print(args, await planSite(site));
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (args.command === "build") {
|
|
41
|
+
for (const site of sites) {
|
|
42
|
+
if (args.dryRun) {
|
|
43
|
+
print(args, {
|
|
44
|
+
site: site.id,
|
|
45
|
+
plan: await planSite(site),
|
|
46
|
+
dryRun: true,
|
|
47
|
+
});
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const report = await buildSite(site);
|
|
51
|
+
print(args, args.explain
|
|
52
|
+
? report
|
|
53
|
+
: {
|
|
54
|
+
site: site.id,
|
|
55
|
+
rendered: report.rendered.length,
|
|
56
|
+
reused: report.reused.length,
|
|
57
|
+
removed: report.removed.length,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (args.command === "inspect") {
|
|
63
|
+
for (const site of sites)
|
|
64
|
+
print(args, await Bun.file(`${site.cacheDir ?? ".cache/moltcms-static"}/route-state/routes.json`).json());
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (args.command === "doctor") {
|
|
68
|
+
for (const site of sites)
|
|
69
|
+
print(args, {
|
|
70
|
+
site: site.id,
|
|
71
|
+
bun: Bun.version,
|
|
72
|
+
config: true,
|
|
73
|
+
projection: await exists(`${site.source.projectionDir}/${site.source.kind}/state.json`),
|
|
74
|
+
cache: await exists(site.cacheDir ?? ".cache/moltcms-static"),
|
|
75
|
+
outDir: site.outDir,
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (args.command === "clean") {
|
|
80
|
+
for (const site of sites) {
|
|
81
|
+
await rm(site.cacheDir ?? ".cache/moltcms-static", {
|
|
82
|
+
recursive: true,
|
|
83
|
+
force: true,
|
|
84
|
+
});
|
|
85
|
+
await rm(site.outDir, { recursive: true, force: true });
|
|
86
|
+
print(args, { site: site.id, cleaned: true });
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (args.command === "preview") {
|
|
91
|
+
const site = sites[0];
|
|
92
|
+
if (site === undefined)
|
|
93
|
+
throw new Error("No site");
|
|
94
|
+
const server = Bun.serve({
|
|
95
|
+
port: 4173,
|
|
96
|
+
fetch(request) {
|
|
97
|
+
const path = new URL(request.url).pathname;
|
|
98
|
+
const file = path === "/"
|
|
99
|
+
? "index.html"
|
|
100
|
+
: `${path.replace(/^\//, "")}${path.endsWith("/") ? "index.html" : ""}`;
|
|
101
|
+
return new Response(Bun.file(resolve(site.outDir, file)));
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
console.log(`Previewing ${site.id} on http://localhost:${server.port}`);
|
|
105
|
+
const { promise } = Promise.withResolvers();
|
|
106
|
+
await promise;
|
|
107
|
+
}
|
|
108
|
+
if (args.command === "finalize") {
|
|
109
|
+
for (const site of sites) {
|
|
110
|
+
const report = await buildSite(site);
|
|
111
|
+
print(args, { site: site.id, finalized: true, report });
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (args.command === "dev") {
|
|
116
|
+
const controller = new AbortController();
|
|
117
|
+
const stop = () => controller.abort();
|
|
118
|
+
process.once("SIGINT", stop);
|
|
119
|
+
process.once("SIGTERM", stop);
|
|
120
|
+
const servers = await Promise.all(sites.map(async (site) => {
|
|
121
|
+
if (site.development === undefined)
|
|
122
|
+
throw new Error(`Site ${site.id} does not configure a development adapter`);
|
|
123
|
+
return site.development.start(controller.signal);
|
|
124
|
+
}));
|
|
125
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
126
|
+
controller.signal.addEventListener("abort", () => resolve(), {
|
|
127
|
+
once: true,
|
|
128
|
+
});
|
|
129
|
+
try {
|
|
130
|
+
await promise;
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
await Promise.all(servers.map((server) => server.close()));
|
|
134
|
+
}
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
throw new Error(`Unknown command ${args.command}`);
|
|
138
|
+
}
|
|
139
|
+
function parse(values) {
|
|
140
|
+
const command = values[0] ?? "build";
|
|
141
|
+
const options = {
|
|
142
|
+
command,
|
|
143
|
+
config: "moltcms.config.ts",
|
|
144
|
+
json: false,
|
|
145
|
+
dryRun: false,
|
|
146
|
+
explain: false,
|
|
147
|
+
gitCommit: false,
|
|
148
|
+
allowDirty: false,
|
|
149
|
+
};
|
|
150
|
+
for (let index = 1; index < values.length; index += 1) {
|
|
151
|
+
const value = values[index];
|
|
152
|
+
if (value === "--config")
|
|
153
|
+
options.config = values[++index] ?? options.config;
|
|
154
|
+
else if (value === "--site")
|
|
155
|
+
options.site = values[++index];
|
|
156
|
+
else if (value === "--output" || value === "--plan")
|
|
157
|
+
options.output = values[++index];
|
|
158
|
+
else if (value === "--json")
|
|
159
|
+
options.json = true;
|
|
160
|
+
else if (value === "--dry-run")
|
|
161
|
+
options.dryRun = true;
|
|
162
|
+
else if (value === "--explain")
|
|
163
|
+
options.explain = true;
|
|
164
|
+
else if (value === "--git-commit")
|
|
165
|
+
options.gitCommit = true;
|
|
166
|
+
else if (value === "--allow-dirty")
|
|
167
|
+
options.allowDirty = true;
|
|
168
|
+
}
|
|
169
|
+
return options;
|
|
170
|
+
}
|
|
171
|
+
async function loadConfig(path) {
|
|
172
|
+
// Runtime-selected user configuration cannot be statically imported.
|
|
173
|
+
const module = await import(pathToFileURL(resolve(path)).href);
|
|
174
|
+
if (!("default" in module))
|
|
175
|
+
throw new Error(`Config ${path} must have a default export`);
|
|
176
|
+
return module.default;
|
|
177
|
+
}
|
|
178
|
+
async function initialize(path) {
|
|
179
|
+
if (await exists(path))
|
|
180
|
+
throw new Error(`${path} already exists`);
|
|
181
|
+
await Bun.write(path, "import { defineConfig } from '@moltcms/static';\n\nexport default defineConfig({ sites: [] });\n");
|
|
182
|
+
}
|
|
183
|
+
async function git(args) {
|
|
184
|
+
const process = Bun.spawn(["git", ...args], {
|
|
185
|
+
stdout: "pipe",
|
|
186
|
+
stderr: "pipe",
|
|
187
|
+
});
|
|
188
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
189
|
+
new Response(process.stdout).text(),
|
|
190
|
+
new Response(process.stderr).text(),
|
|
191
|
+
process.exited,
|
|
192
|
+
]);
|
|
193
|
+
if (exitCode !== 0)
|
|
194
|
+
throw new Error(`git ${args[0] ?? ""} failed: ${stderr}`);
|
|
195
|
+
return stdout;
|
|
196
|
+
}
|
|
197
|
+
async function commitProjection(directory, site, kind, allowDirty) {
|
|
198
|
+
const dirty = await git(["status", "--porcelain"]);
|
|
199
|
+
if (!allowDirty &&
|
|
200
|
+
dirty.trim().length > 0 &&
|
|
201
|
+
dirty.split("\n").some((line) => !line.endsWith(directory)))
|
|
202
|
+
throw new Error("Repository has unrelated dirty files; use --allow-dirty to continue");
|
|
203
|
+
await git(["add", "--", directory]);
|
|
204
|
+
const names = await git(["diff", "--cached", "--name-only"]);
|
|
205
|
+
if (names.trim().length === 0)
|
|
206
|
+
return;
|
|
207
|
+
await git([
|
|
208
|
+
"commit",
|
|
209
|
+
"-m",
|
|
210
|
+
`moltcms sync ${site}/${kind} (${names.trim().split("\n").length} changes)`,
|
|
211
|
+
]);
|
|
212
|
+
}
|
|
213
|
+
function print(args, value) {
|
|
214
|
+
if (args.json)
|
|
215
|
+
console.log(JSON.stringify(value));
|
|
216
|
+
else
|
|
217
|
+
console.log(typeof value === "string" ? value : JSON.stringify(value, null, 2));
|
|
218
|
+
}
|
|
219
|
+
main().catch((error) => {
|
|
220
|
+
console.error(error instanceof Error ? (error.stack ?? error.message) : String(error));
|
|
221
|
+
process.exitCode = 1;
|
|
222
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { SchemaTypeMap } from "@moltcms/client";
|
|
2
|
+
import { feedRoute, itemRoute, queryRoute, singletonRoute, sitemapRoute, type ArtifactCache, type BuildContext, type BuildReport, type DeployAdapter, type FeedRoute, type ItemRoute, type PlannedRoute, type QueryRoute, type RouteDefinition, type SingletonRoute, type SitemapRoute } from "@moltcms/static-core";
|
|
3
|
+
import { type ProjectionKind } from "@moltcms/static-json";
|
|
4
|
+
export { feedRoute, itemRoute, queryRoute, singletonRoute, sitemapRoute };
|
|
5
|
+
export type { BuildContext, FeedRoute, ItemRoute, PlannedRoute, QueryRoute, SingletonRoute, SitemapRoute, };
|
|
6
|
+
export interface MoltcmsSource {
|
|
7
|
+
syncUrl?: string;
|
|
8
|
+
apiKey?: string;
|
|
9
|
+
kind: ProjectionKind;
|
|
10
|
+
projectionDir: string;
|
|
11
|
+
}
|
|
12
|
+
export interface GeneratedSchemas {
|
|
13
|
+
schemaVersions: unknown;
|
|
14
|
+
schemaFingerprint: string;
|
|
15
|
+
}
|
|
16
|
+
export interface DevelopmentAdapter {
|
|
17
|
+
start(signal?: AbortSignal): Promise<{
|
|
18
|
+
close(): Promise<void>;
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
21
|
+
export interface SiteConfig<_S extends SchemaTypeMap = SchemaTypeMap> {
|
|
22
|
+
id: string;
|
|
23
|
+
source: MoltcmsSource;
|
|
24
|
+
generatedSchemas: GeneratedSchemas;
|
|
25
|
+
locales: readonly string[];
|
|
26
|
+
outDir: string;
|
|
27
|
+
cacheDir?: string;
|
|
28
|
+
routes: readonly RouteDefinition[];
|
|
29
|
+
assets?: {
|
|
30
|
+
build(outDir: string): Promise<{
|
|
31
|
+
manifest: Record<string, string>;
|
|
32
|
+
}>;
|
|
33
|
+
};
|
|
34
|
+
renderer?: unknown;
|
|
35
|
+
development?: DevelopmentAdapter;
|
|
36
|
+
deploy?: DeployAdapter;
|
|
37
|
+
cache?: ArtifactCache;
|
|
38
|
+
}
|
|
39
|
+
export interface StaticConfig<S extends SchemaTypeMap = SchemaTypeMap> {
|
|
40
|
+
sites: readonly SiteConfig<S>[];
|
|
41
|
+
}
|
|
42
|
+
/** Declares a type-safe, ESM static site configuration. */
|
|
43
|
+
export declare function defineConfig<S extends SchemaTypeMap>(config: StaticConfig<S>): StaticConfig<S>;
|
|
44
|
+
/** Declares a moltcms projection source. Secrets remain in this server-only object. */
|
|
45
|
+
export declare function moltcmsSource(source: MoltcmsSource): MoltcmsSource;
|
|
46
|
+
export declare function synchronizeSite(site: SiteConfig, signal?: AbortSignal): Promise<void>;
|
|
47
|
+
export declare function codegenSite(site: SiteConfig, output: string): Promise<string>;
|
|
48
|
+
/** Builds one immutable site generation from a complete local projection. */
|
|
49
|
+
export declare function buildSite(site: SiteConfig, signal?: AbortSignal): Promise<BuildReport>;
|
|
50
|
+
export declare function fingerprintSite(site: SiteConfig): string;
|
|
51
|
+
export declare function planSite(site: SiteConfig): Promise<unknown>;
|
|
52
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { dirname, basename } from "node:path";
|
|
2
|
+
import { buildGeneration, canonicalJson, feedRoute, itemRoute, planRoutes, queryRoute, sha256, singletonRoute, sitemapRoute, } from "@moltcms/static-core";
|
|
3
|
+
import { loadProjection, syncProjection, } from "@moltcms/static-json";
|
|
4
|
+
export { feedRoute, itemRoute, queryRoute, singletonRoute, sitemapRoute };
|
|
5
|
+
/** Declares a type-safe, ESM static site configuration. */
|
|
6
|
+
export function defineConfig(config) {
|
|
7
|
+
validateConfig(config);
|
|
8
|
+
return config;
|
|
9
|
+
}
|
|
10
|
+
/** Declares a moltcms projection source. Secrets remain in this server-only object. */
|
|
11
|
+
export function moltcmsSource(source) {
|
|
12
|
+
return source;
|
|
13
|
+
}
|
|
14
|
+
function validateConfig(config) {
|
|
15
|
+
const ids = new Set();
|
|
16
|
+
for (const site of config.sites) {
|
|
17
|
+
if (site.id.length === 0 || ids.has(site.id))
|
|
18
|
+
throw new TypeError(`Site id must be unique: ${site.id}`);
|
|
19
|
+
ids.add(site.id);
|
|
20
|
+
if (site.locales.length === 0)
|
|
21
|
+
throw new TypeError(`Site ${site.id} must define at least one locale`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export async function synchronizeSite(site, signal) {
|
|
25
|
+
if (site.source.syncUrl === undefined || site.source.apiKey === undefined)
|
|
26
|
+
throw new Error(`Site ${site.id} requires syncUrl and apiKey for sync`);
|
|
27
|
+
const stream = await syncProjection({
|
|
28
|
+
root: dirname(site.source.projectionDir),
|
|
29
|
+
siteId: basename(site.source.projectionDir),
|
|
30
|
+
kind: site.source.kind,
|
|
31
|
+
syncUrl: site.source.syncUrl,
|
|
32
|
+
apiKey: site.source.apiKey,
|
|
33
|
+
signal,
|
|
34
|
+
});
|
|
35
|
+
await stream.done;
|
|
36
|
+
}
|
|
37
|
+
export async function codegenSite(site, output) {
|
|
38
|
+
const projection = await loadProjection({
|
|
39
|
+
root: dirname(site.source.projectionDir),
|
|
40
|
+
siteId: basename(site.source.projectionDir),
|
|
41
|
+
kind: site.source.kind,
|
|
42
|
+
});
|
|
43
|
+
const schemaFingerprint = projection.state.schema_fingerprint ??
|
|
44
|
+
sha256(canonicalJson(projection.schemas));
|
|
45
|
+
const content = `${renderSchemaTypes(projection.schemas)}\nexport const schemaFingerprint = ${JSON.stringify(schemaFingerprint)};\n`;
|
|
46
|
+
await Bun.write(output, content);
|
|
47
|
+
return content;
|
|
48
|
+
}
|
|
49
|
+
/** Builds one immutable site generation from a complete local projection. */
|
|
50
|
+
export async function buildSite(site, signal) {
|
|
51
|
+
const projection = await loadProjection({
|
|
52
|
+
root: dirname(site.source.projectionDir),
|
|
53
|
+
siteId: basename(site.source.projectionDir),
|
|
54
|
+
kind: site.source.kind,
|
|
55
|
+
});
|
|
56
|
+
if (projection.state.schema_fingerprint !==
|
|
57
|
+
site.generatedSchemas.schemaFingerprint)
|
|
58
|
+
throw new Error(`Schema fingerprint mismatch for ${site.id}; run moltcms-static codegen`);
|
|
59
|
+
const cacheDirectory = site.cacheDir ?? ".cache/moltcms-static";
|
|
60
|
+
const assetDirectory = `${cacheDirectory}/assets`;
|
|
61
|
+
const assets = site.assets === undefined
|
|
62
|
+
? {}
|
|
63
|
+
: (await site.assets.build(assetDirectory)).manifest;
|
|
64
|
+
const routes = planRoutes(site.id, site.source.kind, site.locales, site.routes, projection.snapshot);
|
|
65
|
+
const report = await buildGeneration({
|
|
66
|
+
outDir: site.outDir,
|
|
67
|
+
stateDir: `${cacheDirectory}/route-state`,
|
|
68
|
+
configFingerprint: fingerprintSite(site),
|
|
69
|
+
snapshot: projection.snapshot,
|
|
70
|
+
routes,
|
|
71
|
+
assets,
|
|
72
|
+
assetDirectory: site.assets === undefined ? undefined : assetDirectory,
|
|
73
|
+
signal,
|
|
74
|
+
});
|
|
75
|
+
if (site.deploy !== undefined) {
|
|
76
|
+
await site.deploy.publish({ id: report.buildId, directory: site.outDir, manifest: report }, signal);
|
|
77
|
+
}
|
|
78
|
+
return report;
|
|
79
|
+
}
|
|
80
|
+
export function fingerprintSite(site) {
|
|
81
|
+
return sha256(canonicalJson({
|
|
82
|
+
id: site.id,
|
|
83
|
+
kind: site.source.kind,
|
|
84
|
+
projectionDir: site.source.projectionDir,
|
|
85
|
+
locales: site.locales,
|
|
86
|
+
outDir: site.outDir,
|
|
87
|
+
routes: site.routes.map((route) => ({ id: route.id, kind: route.kind })),
|
|
88
|
+
schemaFingerprint: site.generatedSchemas.schemaFingerprint,
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
export async function planSite(site) {
|
|
92
|
+
const projection = await loadProjection({
|
|
93
|
+
root: dirname(site.source.projectionDir),
|
|
94
|
+
siteId: basename(site.source.projectionDir),
|
|
95
|
+
kind: site.source.kind,
|
|
96
|
+
});
|
|
97
|
+
return planRoutes(site.id, site.source.kind, site.locales, site.routes, projection.snapshot);
|
|
98
|
+
}
|
|
99
|
+
function renderSchemaTypes(schemas) {
|
|
100
|
+
const groups = Map.groupBy([...schemas].sort((left, right) => left.content_type.localeCompare(right.content_type) ||
|
|
101
|
+
left.version - right.version), (schema) => schema.content_type);
|
|
102
|
+
const blocks = [...groups]
|
|
103
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
104
|
+
.map(([contentType, versions]) => `\t${JSON.stringify(contentType)}: {\n${versions
|
|
105
|
+
.map((schema) => `\t\t${schema.version}: {\n${schema.fields
|
|
106
|
+
.map(renderField)
|
|
107
|
+
.join("\n")}\n\t\t};`)
|
|
108
|
+
.join("\n")}\n\t};`)
|
|
109
|
+
.join("\n");
|
|
110
|
+
const versions = Object.fromEntries([...groups].map(([contentType, values]) => [
|
|
111
|
+
contentType,
|
|
112
|
+
values.map((schema) => schema.version),
|
|
113
|
+
]));
|
|
114
|
+
return `// Generated by @moltcms/static. Do not edit.\nimport type { SchemaVersionIndex } from "@moltcms/client";\n\nexport interface MoltcmsSchemas {\n${blocks}\n}\n\nexport const schemaVersions = ${JSON.stringify(versions, null, "\t")} as const satisfies SchemaVersionIndex<MoltcmsSchemas>;\n`;
|
|
115
|
+
}
|
|
116
|
+
function renderField(field) {
|
|
117
|
+
const optional = field.required ? "" : "?";
|
|
118
|
+
return `\t\t\t${JSON.stringify(field.name)}${optional}: ${renderFieldType(field)}${field.required ? "" : " | null"};`;
|
|
119
|
+
}
|
|
120
|
+
function renderFieldType(field) {
|
|
121
|
+
switch (field.kind) {
|
|
122
|
+
case "string":
|
|
123
|
+
case "text":
|
|
124
|
+
case "datetime":
|
|
125
|
+
return "string";
|
|
126
|
+
case "number":
|
|
127
|
+
return "number";
|
|
128
|
+
case "boolean":
|
|
129
|
+
return "boolean";
|
|
130
|
+
case "image":
|
|
131
|
+
case "relation":
|
|
132
|
+
return field.multiple ? "string[]" : "string";
|
|
133
|
+
case "select": {
|
|
134
|
+
const values = field.options.map((option) => JSON.stringify(option)).join(" | ") ||
|
|
135
|
+
"never";
|
|
136
|
+
return field.multiple ? `(${values})[]` : values;
|
|
137
|
+
}
|
|
138
|
+
case "richtext":
|
|
139
|
+
case "json":
|
|
140
|
+
return "unknown";
|
|
141
|
+
}
|
|
142
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@moltcms/static",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Incremental static site framework for moltcms.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"bin": {
|
|
19
|
+
"moltcms-static": "./dist/cli.js"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc -p tsconfig.build.json",
|
|
23
|
+
"check": "tsc -p ../../tsconfig.json --noEmit",
|
|
24
|
+
"test": "bun test src",
|
|
25
|
+
"prepublishOnly": "bun run build"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"registry": "https://registry.npmjs.org"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/smartcrabai/moltcms-static.git"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"moltcms",
|
|
37
|
+
"static-site-generator",
|
|
38
|
+
"incremental-build",
|
|
39
|
+
"cms"
|
|
40
|
+
],
|
|
41
|
+
"engines": {
|
|
42
|
+
"bun": ">=1.3.0"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@moltcms/client": "^0.4.0",
|
|
46
|
+
"@moltcms/static-core": "^0.1.0",
|
|
47
|
+
"@moltcms/static-json": "^0.1.0"
|
|
48
|
+
}
|
|
49
|
+
}
|