@deckspec/cli 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 +190 -0
- package/package.json +38 -0
- package/src/cli.ts +123 -0
- package/src/commands/approve.ts +74 -0
- package/src/commands/dev.ts +363 -0
- package/src/commands/lock.ts +195 -0
- package/src/commands/patterns.ts +99 -0
- package/src/commands/render.ts +50 -0
- package/src/commands/validate.ts +41 -0
- package/src/index.ts +4 -0
- package/tsconfig.json +14 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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 the 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 the 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 any 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
|
+
Copyright 2026 hayaoo
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@deckspec/cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"deckspec": "./dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"chokidar": "^4.0.0",
|
|
12
|
+
"js-yaml": "^4.1.0",
|
|
13
|
+
"react": "^19.0.0",
|
|
14
|
+
"react-dom": "^19.0.0",
|
|
15
|
+
"zod": "^3.23.0",
|
|
16
|
+
"@deckspec/dsl": "0.1.0",
|
|
17
|
+
"@deckspec/schema": "0.1.0",
|
|
18
|
+
"@deckspec/renderer": "0.1.0"
|
|
19
|
+
},
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/hayaoo/deckspec",
|
|
24
|
+
"directory": "packages/cli"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/hayaoo/deckspec",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/hayaoo/deckspec/issues"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc",
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"test": "vitest run --passWithNoTests"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { validateCommand } from "./commands/validate.js";
|
|
4
|
+
import { renderCommand } from "./commands/render.js";
|
|
5
|
+
import { approveCommand } from "./commands/approve.js";
|
|
6
|
+
import { lockCommand } from "./commands/lock.js";
|
|
7
|
+
import { patternsCommand } from "./commands/patterns.js";
|
|
8
|
+
import { devCommand } from "./commands/dev.js";
|
|
9
|
+
|
|
10
|
+
const USAGE = `
|
|
11
|
+
Usage: deckspec <command> [options]
|
|
12
|
+
|
|
13
|
+
Commands:
|
|
14
|
+
validate <file> Validate a deck YAML file
|
|
15
|
+
render <file> -o <output> Render a deck YAML file to HTML
|
|
16
|
+
approve <file> [options] Approve/reject slides or archive/activate decks
|
|
17
|
+
lock <file> [options] Lock an approved slide as a reusable pattern
|
|
18
|
+
patterns [--theme <name>] List available patterns with schemas
|
|
19
|
+
dev [dir] Start dev server with dashboard (port 3002)
|
|
20
|
+
|
|
21
|
+
Approve options:
|
|
22
|
+
--slide <index[,...]> Slide indices to approve
|
|
23
|
+
--reject Reject (reset to generated) instead of approve
|
|
24
|
+
--archive Archive the deck
|
|
25
|
+
--activate Activate an archived deck
|
|
26
|
+
|
|
27
|
+
Lock options:
|
|
28
|
+
--slide <index> Slide index to lock
|
|
29
|
+
--name <pattern-name> Name for the generated pattern
|
|
30
|
+
|
|
31
|
+
Options:
|
|
32
|
+
--help Show this help message
|
|
33
|
+
`.trim();
|
|
34
|
+
|
|
35
|
+
async function main(): Promise<void> {
|
|
36
|
+
const args = process.argv.slice(2);
|
|
37
|
+
|
|
38
|
+
if (args.length === 0 || args.includes("--help") || args.includes("-h")) {
|
|
39
|
+
console.log(USAGE);
|
|
40
|
+
process.exit(0);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const command = args[0];
|
|
44
|
+
|
|
45
|
+
switch (command) {
|
|
46
|
+
case "validate": {
|
|
47
|
+
const filePath = args[1];
|
|
48
|
+
if (!filePath) {
|
|
49
|
+
console.error("Error: Missing file path.\n");
|
|
50
|
+
console.error("Usage: deckspec validate <file>");
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
await validateCommand(filePath);
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
case "render": {
|
|
58
|
+
const filePath = args[1];
|
|
59
|
+
if (!filePath) {
|
|
60
|
+
console.error("Error: Missing file path.\n");
|
|
61
|
+
console.error("Usage: deckspec render <file> -o <output>");
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const outputFlagIndex = args.indexOf("-o");
|
|
66
|
+
const outputPath = outputFlagIndex !== -1 ? args[outputFlagIndex + 1] : undefined;
|
|
67
|
+
|
|
68
|
+
if (!outputPath) {
|
|
69
|
+
console.error("Error: Missing output path. Use -o <output> to specify.\n");
|
|
70
|
+
console.error("Usage: deckspec render <file> -o <output>");
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
await renderCommand(filePath, outputPath);
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
case "approve": {
|
|
79
|
+
const filePath = args[1];
|
|
80
|
+
if (!filePath) {
|
|
81
|
+
console.error("Error: Missing file path.\n");
|
|
82
|
+
console.error("Usage: deckspec approve <file> --slide <index>");
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
await approveCommand(filePath, args.slice(2));
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
case "lock": {
|
|
90
|
+
const filePath = args[1];
|
|
91
|
+
if (!filePath) {
|
|
92
|
+
console.error("Error: Missing file path.\n");
|
|
93
|
+
console.error("Usage: deckspec lock <file> --slide <index> --name <pattern-name>");
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
await lockCommand(filePath, args.slice(2));
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
case "patterns": {
|
|
101
|
+
await patternsCommand(args.slice(1));
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
case "dev": {
|
|
106
|
+
const dir = args[1] || process.cwd();
|
|
107
|
+
await devCommand(dir);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
default: {
|
|
112
|
+
console.error(`Error: Unknown command "${command}".\n`);
|
|
113
|
+
console.log(USAGE);
|
|
114
|
+
process.exit(1);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
main().catch((error: unknown) => {
|
|
120
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
121
|
+
console.error(`Error: ${message}`);
|
|
122
|
+
process.exit(1);
|
|
123
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
approveSlide,
|
|
3
|
+
rejectSlide,
|
|
4
|
+
archiveDeck,
|
|
5
|
+
activateDeck,
|
|
6
|
+
} from "@deckspec/dsl";
|
|
7
|
+
|
|
8
|
+
interface ApproveOptions {
|
|
9
|
+
slideIndices?: number[];
|
|
10
|
+
reject?: boolean;
|
|
11
|
+
archive?: boolean;
|
|
12
|
+
activate?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseOptions(args: string[]): ApproveOptions {
|
|
16
|
+
const options: ApproveOptions = {};
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < args.length; i++) {
|
|
19
|
+
const arg = args[i];
|
|
20
|
+
if (arg === "--slide" && args[i + 1]) {
|
|
21
|
+
options.slideIndices = args[i + 1].split(",").map((s) => {
|
|
22
|
+
const n = parseInt(s.trim(), 10);
|
|
23
|
+
if (Number.isNaN(n) || n < 0) {
|
|
24
|
+
throw new Error(`Invalid slide index: "${s.trim()}"`);
|
|
25
|
+
}
|
|
26
|
+
return n;
|
|
27
|
+
});
|
|
28
|
+
i++;
|
|
29
|
+
} else if (arg === "--reject") {
|
|
30
|
+
options.reject = true;
|
|
31
|
+
} else if (arg === "--archive") {
|
|
32
|
+
options.archive = true;
|
|
33
|
+
} else if (arg === "--activate") {
|
|
34
|
+
options.activate = true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return options;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function approveCommand(
|
|
42
|
+
filePath: string,
|
|
43
|
+
extraArgs: string[],
|
|
44
|
+
): Promise<void> {
|
|
45
|
+
const options = parseOptions(extraArgs);
|
|
46
|
+
|
|
47
|
+
if (options.archive) {
|
|
48
|
+
await archiveDeck(filePath);
|
|
49
|
+
console.log(`Archived deck: ${filePath}`);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (options.activate) {
|
|
54
|
+
await activateDeck(filePath);
|
|
55
|
+
console.log(`Activated deck: ${filePath}`);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!options.slideIndices || options.slideIndices.length === 0) {
|
|
60
|
+
console.error("Error: Specify slides with --slide <index[,index,...]>");
|
|
61
|
+
console.error(" Or use --archive / --activate for deck lifecycle.");
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
for (const index of options.slideIndices) {
|
|
66
|
+
if (options.reject) {
|
|
67
|
+
await rejectSlide(filePath, index);
|
|
68
|
+
console.log(`Rejected slide ${index} in ${filePath}`);
|
|
69
|
+
} else {
|
|
70
|
+
await approveSlide(filePath, index);
|
|
71
|
+
console.log(`Approved slide ${index} in ${filePath}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
|
2
|
+
import { resolve, dirname, join } from "node:path";
|
|
3
|
+
import { loadDeckFile, validateDeck, scanDecks, approveSlide, rejectSlide, archiveDeck, activateDeck } from "@deckspec/dsl";
|
|
4
|
+
import type { Deck, Slide } from "@deckspec/schema";
|
|
5
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
6
|
+
import { renderDeck, renderDashboard, renderSlide, renderThemeDetail, loadThemeCSS, loadThemeTokens, extractThemeName, resolveThemePatternsDir, resolveThemePatternsSrcDir, compileTsxCached, clearCompileCache, type DeckWithPreviews, type ThemeSummary } from "@deckspec/renderer";
|
|
7
|
+
|
|
8
|
+
const PORT = 3002;
|
|
9
|
+
|
|
10
|
+
/** SSE clients for live reload */
|
|
11
|
+
const sseClients = new Set<ServerResponse>();
|
|
12
|
+
|
|
13
|
+
function sendSSE(event: string, data: string = ""): void {
|
|
14
|
+
for (const res of sseClients) {
|
|
15
|
+
res.write(`event: ${event}\ndata: ${data}\n\n`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function readBody(req: IncomingMessage): Promise<string> {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const chunks: Buffer[] = [];
|
|
22
|
+
req.on("data", (chunk: Buffer) => chunks.push(chunk));
|
|
23
|
+
req.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8")));
|
|
24
|
+
req.on("error", reject);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function json(res: ServerResponse, status: number, data: unknown): void {
|
|
29
|
+
res.writeHead(status, { "Content-Type": "application/json" });
|
|
30
|
+
res.end(JSON.stringify(data));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function html(res: ServerResponse, content: string): void {
|
|
34
|
+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
35
|
+
res.end(content);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function error(res: ServerResponse, status: number, message: string): void {
|
|
39
|
+
res.writeHead(status, { "Content-Type": "text/plain" });
|
|
40
|
+
res.end(message);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function renderDeckByPath(
|
|
44
|
+
baseDir: string,
|
|
45
|
+
relativePath: string,
|
|
46
|
+
): Promise<string> {
|
|
47
|
+
const filePath = resolve(baseDir, relativePath);
|
|
48
|
+
const raw = await loadDeckFile(filePath);
|
|
49
|
+
const basePath = dirname(filePath);
|
|
50
|
+
const themeName = extractThemeName(raw);
|
|
51
|
+
const patternsDir = resolveThemePatternsDir(themeName);
|
|
52
|
+
const result = await validateDeck(raw, { basePath, patternsDir, compileTsx: compileTsxCached });
|
|
53
|
+
|
|
54
|
+
if (!result.valid) {
|
|
55
|
+
const errors = result.results
|
|
56
|
+
.filter((r) => !r.valid)
|
|
57
|
+
.map((r) => `slides[${r.index}]: ${r.errors?.issues.map((i) => i.message).join(", ")}`)
|
|
58
|
+
.join("\n");
|
|
59
|
+
throw new Error(`Validation failed:\n${errors}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const deck = raw as Deck;
|
|
63
|
+
const themeCSS = await loadThemeCSS(deck.meta.theme);
|
|
64
|
+
const patternsSrcDir = resolveThemePatternsSrcDir(themeName);
|
|
65
|
+
return renderDeck(deck, themeCSS, { basePath, patternsDir, patternsSrcDir });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function handleRequest(
|
|
69
|
+
req: IncomingMessage,
|
|
70
|
+
res: ServerResponse,
|
|
71
|
+
baseDir: string,
|
|
72
|
+
): Promise<void> {
|
|
73
|
+
const url = new URL(req.url ?? "/", `http://localhost:${PORT}`);
|
|
74
|
+
const pathname = url.pathname;
|
|
75
|
+
const method = req.method ?? "GET";
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
// SSE endpoint
|
|
79
|
+
if (method === "GET" && pathname === "/events") {
|
|
80
|
+
res.writeHead(200, {
|
|
81
|
+
"Content-Type": "text/event-stream",
|
|
82
|
+
"Cache-Control": "no-cache",
|
|
83
|
+
Connection: "keep-alive",
|
|
84
|
+
});
|
|
85
|
+
res.write(":\n\n");
|
|
86
|
+
sseClients.add(res);
|
|
87
|
+
req.on("close", () => sseClients.delete(res));
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Dashboard
|
|
92
|
+
if (method === "GET" && pathname === "/") {
|
|
93
|
+
const summaries = await scanDecks(baseDir);
|
|
94
|
+
|
|
95
|
+
// Scan themes
|
|
96
|
+
const themes: ThemeSummary[] = [];
|
|
97
|
+
const themesDir = join(baseDir, "themes");
|
|
98
|
+
try {
|
|
99
|
+
const themeDirs = await readdir(themesDir);
|
|
100
|
+
for (const dir of themeDirs) {
|
|
101
|
+
try {
|
|
102
|
+
const dirStat = await stat(join(themesDir, dir));
|
|
103
|
+
if (!dirStat.isDirectory()) continue;
|
|
104
|
+
const tokens = await loadThemeTokens(dir);
|
|
105
|
+
const patternsSrcDir = resolveThemePatternsSrcDir(dir);
|
|
106
|
+
let patternCount = 0;
|
|
107
|
+
try {
|
|
108
|
+
const patternEntries = await readdir(patternsSrcDir);
|
|
109
|
+
patternCount = patternEntries.filter((e) => !e.startsWith("_") && !e.startsWith(".") && !e.includes(".")).length;
|
|
110
|
+
} catch { /* no patterns dir */ }
|
|
111
|
+
themes.push({
|
|
112
|
+
name: dir,
|
|
113
|
+
displayName: tokens.displayName,
|
|
114
|
+
patternCount,
|
|
115
|
+
colors: [
|
|
116
|
+
{ name: "foreground", hex: tokens.colors.foreground },
|
|
117
|
+
{ name: "primary", hex: tokens.colors.primary },
|
|
118
|
+
{ name: "background", hex: tokens.colors.background },
|
|
119
|
+
{ name: "card", hex: tokens.colors["card-background"] ?? tokens.colors.background },
|
|
120
|
+
],
|
|
121
|
+
});
|
|
122
|
+
} catch { /* skip invalid theme */ }
|
|
123
|
+
}
|
|
124
|
+
} catch { /* no themes dir */ }
|
|
125
|
+
|
|
126
|
+
// Build previews for each deck (first slide only for thumbnail)
|
|
127
|
+
const decksWithPreviews: DeckWithPreviews[] = [];
|
|
128
|
+
let themeCSS = "";
|
|
129
|
+
|
|
130
|
+
for (const summary of summaries) {
|
|
131
|
+
try {
|
|
132
|
+
const raw = await loadDeckFile(summary.filePath);
|
|
133
|
+
const deck = raw as Deck;
|
|
134
|
+
const basePath = dirname(summary.filePath);
|
|
135
|
+
const themeName = extractThemeName(raw);
|
|
136
|
+
const patternsDir = resolveThemePatternsDir(themeName);
|
|
137
|
+
const patternsSrcDir = resolveThemePatternsSrcDir(themeName);
|
|
138
|
+
|
|
139
|
+
if (!themeCSS) {
|
|
140
|
+
themeCSS = await loadThemeCSS(deck.meta.theme);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Only render first slide for Home thumbnail
|
|
144
|
+
const slidePreviews = [];
|
|
145
|
+
if (deck.slides.length > 0) {
|
|
146
|
+
const slide = deck.slides[0];
|
|
147
|
+
let previewHtml: string;
|
|
148
|
+
try {
|
|
149
|
+
previewHtml = await renderSlide(slide, { basePath, patternsDir, patternsSrcDir });
|
|
150
|
+
} catch {
|
|
151
|
+
previewHtml = `<div style="padding:1rem;color:#999;font-size:12px">Preview unavailable</div>`;
|
|
152
|
+
}
|
|
153
|
+
slidePreviews.push({
|
|
154
|
+
index: 0,
|
|
155
|
+
state: String(slide.state ?? "generated"),
|
|
156
|
+
html: previewHtml,
|
|
157
|
+
file: slide.file,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Get mtime of deck.yaml
|
|
162
|
+
let mtime: string | undefined;
|
|
163
|
+
try {
|
|
164
|
+
const fileStat = await stat(summary.filePath);
|
|
165
|
+
mtime = fileStat.mtime.toISOString();
|
|
166
|
+
} catch { /* ignore */ }
|
|
167
|
+
|
|
168
|
+
decksWithPreviews.push({ summary, slidePreviews, mtime });
|
|
169
|
+
} catch {
|
|
170
|
+
let mtime: string | undefined;
|
|
171
|
+
try {
|
|
172
|
+
const fileStat = await stat(summary.filePath);
|
|
173
|
+
mtime = fileStat.mtime.toISOString();
|
|
174
|
+
} catch { /* ignore */ }
|
|
175
|
+
decksWithPreviews.push({
|
|
176
|
+
summary,
|
|
177
|
+
slidePreviews: [],
|
|
178
|
+
mtime,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const dashboardHtml = renderDashboard(decksWithPreviews, {
|
|
184
|
+
mode: "interactive",
|
|
185
|
+
themeCSS,
|
|
186
|
+
}, themes);
|
|
187
|
+
html(res, dashboardHtml);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Theme detail: /theme/<name>
|
|
192
|
+
if (method === "GET" && pathname.startsWith("/theme/")) {
|
|
193
|
+
const themeName = decodeURIComponent(pathname.slice(7));
|
|
194
|
+
const tokens = await loadThemeTokens(themeName);
|
|
195
|
+
|
|
196
|
+
// Read pattern names and generate previews
|
|
197
|
+
const patternsSrcDir = resolveThemePatternsSrcDir(themeName);
|
|
198
|
+
const patternsDir = resolveThemePatternsDir(themeName);
|
|
199
|
+
let patternInfos: { name: string; previewHtml?: string }[] = [];
|
|
200
|
+
try {
|
|
201
|
+
const entries = await readdir(patternsSrcDir);
|
|
202
|
+
const patternNames = entries.filter((e) => !e.startsWith("_") && !e.startsWith(".") && !e.includes(".")).sort();
|
|
203
|
+
|
|
204
|
+
for (const name of patternNames) {
|
|
205
|
+
let previewHtml: string | undefined;
|
|
206
|
+
try {
|
|
207
|
+
// Check for examples.yaml
|
|
208
|
+
const examplesPath = join(patternsSrcDir, name, "examples.yaml");
|
|
209
|
+
const examplesRaw = await readFile(examplesPath, "utf-8");
|
|
210
|
+
const { load } = await import("js-yaml");
|
|
211
|
+
const examples = load(examplesRaw) as { name: string; vars: Record<string, unknown> }[];
|
|
212
|
+
if (examples && examples.length > 0) {
|
|
213
|
+
const firstExample = examples[0];
|
|
214
|
+
previewHtml = await renderSlide(
|
|
215
|
+
{ file: name, vars: firstExample.vars } as Slide,
|
|
216
|
+
{ basePath: patternsSrcDir, patternsDir, patternsSrcDir },
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
} catch { /* no examples or render failed */ }
|
|
220
|
+
patternInfos.push({ name, previewHtml });
|
|
221
|
+
}
|
|
222
|
+
} catch { /* no patterns */ }
|
|
223
|
+
|
|
224
|
+
// Load theme CSS for pattern previews
|
|
225
|
+
let themeCSS = "";
|
|
226
|
+
try {
|
|
227
|
+
themeCSS = await loadThemeCSS(themeName);
|
|
228
|
+
} catch { /* no theme CSS */ }
|
|
229
|
+
|
|
230
|
+
// Read design.md description (first paragraph)
|
|
231
|
+
let designDescription = "";
|
|
232
|
+
try {
|
|
233
|
+
const designMd = await readFile(join(baseDir, "themes", themeName, "design.md"), "utf-8");
|
|
234
|
+
const lines = designMd.split("\n");
|
|
235
|
+
const descLines: string[] = [];
|
|
236
|
+
let pastTitle = false;
|
|
237
|
+
for (const line of lines) {
|
|
238
|
+
if (!pastTitle) {
|
|
239
|
+
if (line.startsWith("# ")) { pastTitle = true; continue; }
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (line.startsWith("## ")) break;
|
|
243
|
+
if (line.trim() === "" && descLines.length > 0) break;
|
|
244
|
+
if (line.trim() !== "") descLines.push(line.trim());
|
|
245
|
+
}
|
|
246
|
+
designDescription = descLines.join("\n");
|
|
247
|
+
} catch { /* no design.md */ }
|
|
248
|
+
|
|
249
|
+
const detailHtml = renderThemeDetail({ tokens, patterns: patternInfos, designDescription, themeCSS });
|
|
250
|
+
html(res, detailHtml);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Deck viewer: /deck/<name> → decks/<name>/deck.yaml
|
|
255
|
+
if (method === "GET" && pathname.startsWith("/deck/")) {
|
|
256
|
+
const deckName = decodeURIComponent(pathname.slice(6));
|
|
257
|
+
const relativePath = `decks/${deckName}/deck.yaml`;
|
|
258
|
+
const deckHtml = await renderDeckByPath(baseDir, relativePath);
|
|
259
|
+
html(res, deckHtml);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// API: approve slide
|
|
264
|
+
if (method === "POST" && pathname === "/api/approve") {
|
|
265
|
+
const body = JSON.parse(await readBody(req));
|
|
266
|
+
const filePath = resolve(baseDir, body.file);
|
|
267
|
+
await approveSlide(filePath, body.slideIndex);
|
|
268
|
+
sendSSE("reload");
|
|
269
|
+
json(res, 200, { ok: true });
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// API: reject slide
|
|
274
|
+
if (method === "POST" && pathname === "/api/reject") {
|
|
275
|
+
const body = JSON.parse(await readBody(req));
|
|
276
|
+
const filePath = resolve(baseDir, body.file);
|
|
277
|
+
await rejectSlide(filePath, body.slideIndex);
|
|
278
|
+
sendSSE("reload");
|
|
279
|
+
json(res, 200, { ok: true });
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// API: archive deck
|
|
284
|
+
if (method === "POST" && pathname === "/api/archive") {
|
|
285
|
+
const body = JSON.parse(await readBody(req));
|
|
286
|
+
const filePath = resolve(baseDir, body.file);
|
|
287
|
+
await archiveDeck(filePath);
|
|
288
|
+
sendSSE("reload");
|
|
289
|
+
json(res, 200, { ok: true });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// API: activate deck
|
|
294
|
+
if (method === "POST" && pathname === "/api/activate") {
|
|
295
|
+
const body = JSON.parse(await readBody(req));
|
|
296
|
+
const filePath = resolve(baseDir, body.file);
|
|
297
|
+
await activateDeck(filePath);
|
|
298
|
+
sendSSE("reload");
|
|
299
|
+
json(res, 200, { ok: true });
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
error(res, 404, "Not found");
|
|
304
|
+
} catch (err) {
|
|
305
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
306
|
+
console.error(`[dev] Error: ${message}`);
|
|
307
|
+
error(res, 500, message);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export async function devCommand(dir: string): Promise<void> {
|
|
312
|
+
const baseDir = resolve(dir);
|
|
313
|
+
|
|
314
|
+
// Try dynamic import of chokidar for file watching
|
|
315
|
+
let watcherActive = false;
|
|
316
|
+
try {
|
|
317
|
+
const chokidar = await import("chokidar");
|
|
318
|
+
const fsWatcher = chokidar.watch(
|
|
319
|
+
[
|
|
320
|
+
join(baseDir, "decks/**/*.yaml"),
|
|
321
|
+
join(baseDir, "decks/**/*.html"),
|
|
322
|
+
join(baseDir, "decks/**/*.tsx"),
|
|
323
|
+
join(baseDir, "themes/**/*"),
|
|
324
|
+
],
|
|
325
|
+
{
|
|
326
|
+
ignoreInitial: true,
|
|
327
|
+
awaitWriteFinish: { stabilityThreshold: 200 },
|
|
328
|
+
},
|
|
329
|
+
);
|
|
330
|
+
fsWatcher.on("change", (filePath: string) => {
|
|
331
|
+
console.log("[dev] File changed, reloading...");
|
|
332
|
+
// Clear compile cache for .tsx files so they get recompiled
|
|
333
|
+
if (filePath.endsWith(".tsx")) {
|
|
334
|
+
clearCompileCache(filePath).catch(() => {});
|
|
335
|
+
}
|
|
336
|
+
sendSSE("reload");
|
|
337
|
+
});
|
|
338
|
+
fsWatcher.on("add", () => {
|
|
339
|
+
sendSSE("reload");
|
|
340
|
+
});
|
|
341
|
+
watcherActive = true;
|
|
342
|
+
} catch {
|
|
343
|
+
console.warn(
|
|
344
|
+
"[dev] chokidar not installed — file watching disabled. Run: pnpm add -D chokidar",
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const server = createServer((req, res) => {
|
|
349
|
+
handleRequest(req, res, baseDir).catch((err) => {
|
|
350
|
+
console.error("[dev] Unhandled error:", err);
|
|
351
|
+
if (!res.headersSent) {
|
|
352
|
+
error(res, 500, "Internal server error");
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
server.listen(PORT, () => {
|
|
358
|
+
console.log(`\n DeckSpec Dev Server`);
|
|
359
|
+
console.log(` Dashboard: http://localhost:${PORT}/`);
|
|
360
|
+
console.log(` Watching: ${baseDir}`);
|
|
361
|
+
console.log(` ${watcherActive ? "Live reload: enabled" : "Live reload: disabled (install chokidar)"}\n`);
|
|
362
|
+
});
|
|
363
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
|
2
|
+
import { resolve, dirname, join } from "node:path";
|
|
3
|
+
import { loadDeckFile, lockSlide } from "@deckspec/dsl";
|
|
4
|
+
import type { Deck } from "@deckspec/schema";
|
|
5
|
+
import { renderSlide, extractThemeName, resolveThemePatternsDir, resolveThemePatternsSrcDir } from "@deckspec/renderer";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Extracts text nodes from HTML as variable candidates.
|
|
9
|
+
* Returns a map of variable names to their values.
|
|
10
|
+
*/
|
|
11
|
+
function extractVarsFromHtml(html: string): Record<string, string> {
|
|
12
|
+
const vars: Record<string, string> = {};
|
|
13
|
+
|
|
14
|
+
// Extract <h1> → title
|
|
15
|
+
const h1Match = /<h1[^>]*>(.*?)<\/h1>/i.exec(html);
|
|
16
|
+
if (h1Match) {
|
|
17
|
+
vars.title = h1Match[1].replace(/<[^>]*>/g, "").trim();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Extract <h2> → headline
|
|
21
|
+
const h2Match = /<h2[^>]*>(.*?)<\/h2>/i.exec(html);
|
|
22
|
+
if (h2Match) {
|
|
23
|
+
vars.headline = h2Match[1].replace(/<[^>]*>/g, "").trim();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Extract first <p> → subtitle or body
|
|
27
|
+
const pMatch = /<p[^>]*>(.*?)<\/p>/i.exec(html);
|
|
28
|
+
if (pMatch) {
|
|
29
|
+
const text = pMatch[1].replace(/<[^>]*>/g, "").trim();
|
|
30
|
+
if (vars.title && !vars.headline) {
|
|
31
|
+
vars.subtitle = text;
|
|
32
|
+
} else {
|
|
33
|
+
vars.body = text;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return vars;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Generates a .tsx pattern file from HTML content and extracted vars.
|
|
42
|
+
*/
|
|
43
|
+
function generatePatternTsx(
|
|
44
|
+
name: string,
|
|
45
|
+
html: string,
|
|
46
|
+
vars: Record<string, string>,
|
|
47
|
+
): string {
|
|
48
|
+
const componentName = name
|
|
49
|
+
.split("-")
|
|
50
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
51
|
+
.join("");
|
|
52
|
+
|
|
53
|
+
const schemaFields = Object.entries(vars)
|
|
54
|
+
.map(([key, value]) => {
|
|
55
|
+
const maxLen = Math.max(value.length * 2, 60);
|
|
56
|
+
return ` ${key}: z.string().min(1).max(${maxLen}).describe("${key}"),`;
|
|
57
|
+
})
|
|
58
|
+
.join("\n");
|
|
59
|
+
|
|
60
|
+
// Replace literal values with JSX expressions in the HTML
|
|
61
|
+
let jsxBody = html.trim();
|
|
62
|
+
for (const [key, value] of Object.entries(vars)) {
|
|
63
|
+
// Escape special regex characters in value
|
|
64
|
+
const escaped = value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
65
|
+
jsxBody = jsxBody.replace(new RegExp(`>${escaped}<`, "g"), `>{${key}}<`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Convert HTML attributes to JSX
|
|
69
|
+
jsxBody = jsxBody.replace(/class="/g, 'className="');
|
|
70
|
+
jsxBody = jsxBody.replace(/style="([^"]*)"/g, (_match, styleStr: string) => {
|
|
71
|
+
const props = styleStr
|
|
72
|
+
.split(";")
|
|
73
|
+
.filter(Boolean)
|
|
74
|
+
.map((prop: string) => {
|
|
75
|
+
const [k, v] = prop.split(":").map((s: string) => s.trim());
|
|
76
|
+
const camelKey = k.replace(/-([a-z])/g, (_: string, c: string) => c.toUpperCase());
|
|
77
|
+
// Try to convert numeric values
|
|
78
|
+
const numVal = parseFloat(v);
|
|
79
|
+
if (!isNaN(numVal) && v.endsWith("px")) {
|
|
80
|
+
return `${camelKey}: ${numVal}`;
|
|
81
|
+
}
|
|
82
|
+
return `${camelKey}: "${v}"`;
|
|
83
|
+
})
|
|
84
|
+
.join(", ");
|
|
85
|
+
return `style={{ ${props} }}`;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const propsDestructure = Object.keys(vars).join(", ");
|
|
89
|
+
|
|
90
|
+
return `import { z } from "zod";
|
|
91
|
+
|
|
92
|
+
export const schema = z.object({
|
|
93
|
+
${schemaFields}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
type Props = z.infer<typeof schema>;
|
|
97
|
+
|
|
98
|
+
export default function ${componentName}({ ${propsDestructure} }: Props) {
|
|
99
|
+
return (
|
|
100
|
+
${jsxBody}
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
interface LockOptions {
|
|
107
|
+
slideIndex: number;
|
|
108
|
+
patternName: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function parseOptions(args: string[]): LockOptions {
|
|
112
|
+
let slideIndex: number | undefined;
|
|
113
|
+
let patternName: string | undefined;
|
|
114
|
+
|
|
115
|
+
for (let i = 0; i < args.length; i++) {
|
|
116
|
+
const arg = args[i];
|
|
117
|
+
if (arg === "--slide" && args[i + 1]) {
|
|
118
|
+
slideIndex = parseInt(args[i + 1], 10);
|
|
119
|
+
if (Number.isNaN(slideIndex) || slideIndex < 0) {
|
|
120
|
+
throw new Error(`Invalid slide index: "${args[i + 1]}"`);
|
|
121
|
+
}
|
|
122
|
+
i++;
|
|
123
|
+
} else if (arg === "--name" && args[i + 1]) {
|
|
124
|
+
patternName = args[i + 1];
|
|
125
|
+
i++;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (slideIndex === undefined) {
|
|
130
|
+
throw new Error("Missing --slide <index>");
|
|
131
|
+
}
|
|
132
|
+
if (!patternName) {
|
|
133
|
+
throw new Error("Missing --name <pattern-name>");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return { slideIndex, patternName };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function lockCommand(
|
|
140
|
+
filePath: string,
|
|
141
|
+
extraArgs: string[],
|
|
142
|
+
): Promise<void> {
|
|
143
|
+
const options = parseOptions(extraArgs);
|
|
144
|
+
const absPath = resolve(filePath);
|
|
145
|
+
const basePath = dirname(absPath);
|
|
146
|
+
const raw = await loadDeckFile(absPath);
|
|
147
|
+
const themeName = extractThemeName(raw);
|
|
148
|
+
const patternsDir = resolveThemePatternsDir(themeName);
|
|
149
|
+
const patternsSrcDir = resolveThemePatternsSrcDir(themeName);
|
|
150
|
+
|
|
151
|
+
const deck = raw as Deck;
|
|
152
|
+
|
|
153
|
+
if (options.slideIndex < 0 || options.slideIndex >= deck.slides.length) {
|
|
154
|
+
console.error(
|
|
155
|
+
`Error: Slide index ${options.slideIndex} out of range (0–${deck.slides.length - 1})`,
|
|
156
|
+
);
|
|
157
|
+
process.exit(1);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const slide = deck.slides[options.slideIndex];
|
|
161
|
+
const currentState = slide.state ?? "generated";
|
|
162
|
+
|
|
163
|
+
if (currentState !== "approved") {
|
|
164
|
+
console.error(
|
|
165
|
+
`Error: Slide ${options.slideIndex} is "${currentState}". Only "approved" slides can be locked.`,
|
|
166
|
+
);
|
|
167
|
+
process.exit(1);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Render the slide to get its HTML
|
|
171
|
+
const slideHtml = await renderSlide(slide, { basePath, patternsDir, patternsSrcDir });
|
|
172
|
+
|
|
173
|
+
// Extract variables from the HTML
|
|
174
|
+
const vars = extractVarsFromHtml(slideHtml);
|
|
175
|
+
|
|
176
|
+
if (Object.keys(vars).length === 0) {
|
|
177
|
+
console.error("Warning: No text variables could be extracted from the slide.");
|
|
178
|
+
console.error("The pattern will be generated without variables.");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Generate the .tsx pattern
|
|
182
|
+
const tsxContent = generatePatternTsx(options.patternName, slideHtml, vars);
|
|
183
|
+
|
|
184
|
+
// Write the pattern file to source directory
|
|
185
|
+
const patternDir = join(patternsSrcDir, options.patternName);
|
|
186
|
+
await mkdir(patternDir, { recursive: true });
|
|
187
|
+
const patternPath = join(patternDir, "index.tsx");
|
|
188
|
+
await writeFile(patternPath, tsxContent, "utf-8");
|
|
189
|
+
console.log(`\u2713 Generated pattern: ${patternPath}`);
|
|
190
|
+
|
|
191
|
+
// Update the deck YAML: rewrite the slide entry
|
|
192
|
+
await lockSlide(absPath, options.slideIndex, options.patternName, vars);
|
|
193
|
+
console.log(`\u2713 Locked slide ${options.slideIndex} → pattern "${options.patternName}"`);
|
|
194
|
+
console.log(`\nRebuild patterns to use: pnpm build`);
|
|
195
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { resolveThemePatternsDir, resolveThemePatternsSrcDir } from "@deckspec/renderer";
|
|
4
|
+
import yaml from "js-yaml";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Lists available patterns with their schema definitions.
|
|
8
|
+
* --examples flag shows example vars from examples.yaml.
|
|
9
|
+
*/
|
|
10
|
+
export async function patternsCommand(args: string[]): Promise<void> {
|
|
11
|
+
let themeName = "noir-display";
|
|
12
|
+
let showExamples = false;
|
|
13
|
+
for (let i = 0; i < args.length; i++) {
|
|
14
|
+
if (args[i] === "--theme" && args[i + 1]) {
|
|
15
|
+
themeName = args[i + 1];
|
|
16
|
+
i++;
|
|
17
|
+
}
|
|
18
|
+
if (args[i] === "--examples") {
|
|
19
|
+
showExamples = true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const patternsDir = resolveThemePatternsDir(themeName);
|
|
24
|
+
const patternsSrcDir = resolveThemePatternsSrcDir(themeName);
|
|
25
|
+
|
|
26
|
+
let entries: string[];
|
|
27
|
+
try {
|
|
28
|
+
entries = await readdir(patternsDir);
|
|
29
|
+
} catch {
|
|
30
|
+
console.log(`No patterns found for theme "${themeName}".`);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Filter to directories that look like pattern names
|
|
35
|
+
const patternNames: string[] = [];
|
|
36
|
+
for (const name of entries) {
|
|
37
|
+
if (name.startsWith(".") || name.startsWith("_") || name === "node_modules" || name === "dist") continue;
|
|
38
|
+
try {
|
|
39
|
+
const s = await stat(join(patternsDir, name));
|
|
40
|
+
if (s.isDirectory()) patternNames.push(name);
|
|
41
|
+
} catch {
|
|
42
|
+
// skip
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (patternNames.length === 0) {
|
|
47
|
+
console.log(`No patterns found for theme "${themeName}".`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
patternNames.sort();
|
|
52
|
+
console.log(`Available patterns (theme: ${themeName}):\n`);
|
|
53
|
+
|
|
54
|
+
for (const name of patternNames) {
|
|
55
|
+
const indexPath = join(patternsDir, name, "index.js");
|
|
56
|
+
try {
|
|
57
|
+
const mod = await import(indexPath);
|
|
58
|
+
|
|
59
|
+
// Check for examples.yaml
|
|
60
|
+
let hasExamples = false;
|
|
61
|
+
let examples: Array<{ name: string; description?: string; vars: unknown }> = [];
|
|
62
|
+
try {
|
|
63
|
+
const examplesPath = join(patternsSrcDir, name, "examples.yaml");
|
|
64
|
+
const examplesContent = await readFile(examplesPath, "utf-8");
|
|
65
|
+
examples = yaml.load(examplesContent) as typeof examples;
|
|
66
|
+
hasExamples = true;
|
|
67
|
+
} catch {
|
|
68
|
+
// no examples
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (mod.schema) {
|
|
72
|
+
const shape = mod.schema.shape;
|
|
73
|
+
const fields = Object.entries(shape)
|
|
74
|
+
.map(([key, val]: [string, any]) => {
|
|
75
|
+
const desc = val.description ?? "";
|
|
76
|
+
const optional = val.isOptional?.() ? "?" : "";
|
|
77
|
+
return ` ${key}${optional}: ${desc}`;
|
|
78
|
+
})
|
|
79
|
+
.join("\n");
|
|
80
|
+
const badge = hasExamples ? " [examples]" : "";
|
|
81
|
+
console.log(` ${name}:${badge}`);
|
|
82
|
+
console.log(fields);
|
|
83
|
+
} else {
|
|
84
|
+
console.log(` ${name}: (no schema)`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Show examples if requested
|
|
88
|
+
if (showExamples && examples.length > 0) {
|
|
89
|
+
console.log(` --- examples ---`);
|
|
90
|
+
for (const ex of examples) {
|
|
91
|
+
console.log(` "${ex.name}"${ex.description ? ` — ${ex.description}` : ""}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
} catch {
|
|
95
|
+
console.log(` ${name}: (not compiled — run pnpm build)`);
|
|
96
|
+
}
|
|
97
|
+
console.log();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import { resolve, dirname } from "node:path";
|
|
3
|
+
import { loadDeckFile, validateDeck } from "@deckspec/dsl";
|
|
4
|
+
import type { Deck } from "@deckspec/schema";
|
|
5
|
+
import { renderDeck, loadThemeCSS, extractThemeName, resolveThemePatternsDir, resolveThemePatternsSrcDir, compileTsxCached } from "@deckspec/renderer";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Renders a deck YAML file to standalone HTML.
|
|
9
|
+
* Validates the deck first, then renders via @deckspec/renderer.
|
|
10
|
+
*/
|
|
11
|
+
export async function renderCommand(
|
|
12
|
+
filePath: string,
|
|
13
|
+
outputPath: string,
|
|
14
|
+
): Promise<void> {
|
|
15
|
+
const raw = await loadDeckFile(filePath);
|
|
16
|
+
const basePath = dirname(resolve(filePath));
|
|
17
|
+
const themeName = extractThemeName(raw);
|
|
18
|
+
const patternsDir = resolveThemePatternsDir(themeName);
|
|
19
|
+
|
|
20
|
+
// Validate first
|
|
21
|
+
const result = await validateDeck(raw, { basePath, patternsDir, compileTsx: compileTsxCached });
|
|
22
|
+
|
|
23
|
+
if (result.deckError) {
|
|
24
|
+
console.error("\u2717 Deck structure is invalid:");
|
|
25
|
+
for (const issue of result.deckError.issues) {
|
|
26
|
+
console.error(` ${issue.path.join(".")}: ${issue.message}`);
|
|
27
|
+
}
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!result.valid) {
|
|
32
|
+
console.error("\u2717 Validation failed. Fix errors before rendering:");
|
|
33
|
+
for (const slideResult of result.results) {
|
|
34
|
+
if (!slideResult.valid) {
|
|
35
|
+
for (const issue of slideResult.errors!.issues) {
|
|
36
|
+
console.error(` slides[${slideResult.index}]: ${issue.message}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const deck = raw as Deck;
|
|
44
|
+
const themeCSS = await loadThemeCSS(deck.meta.theme);
|
|
45
|
+
const patternsSrcDir = resolveThemePatternsSrcDir(themeName);
|
|
46
|
+
const html = await renderDeck(deck, themeCSS, { basePath, patternsDir, patternsSrcDir });
|
|
47
|
+
|
|
48
|
+
await writeFile(outputPath, html, "utf-8");
|
|
49
|
+
console.log(`\u2713 Rendered ${result.results.length} slide(s) to ${outputPath}`);
|
|
50
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { resolve, dirname } from "node:path";
|
|
2
|
+
import { loadDeckFile, validateDeck } from "@deckspec/dsl";
|
|
3
|
+
import { extractThemeName, resolveThemePatternsDir, compileTsxCached } from "@deckspec/renderer";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validates a deck YAML file.
|
|
7
|
+
* Prints per-slide results to stdout and exits with code 1 if any errors.
|
|
8
|
+
*/
|
|
9
|
+
export async function validateCommand(filePath: string): Promise<void> {
|
|
10
|
+
const raw = await loadDeckFile(filePath);
|
|
11
|
+
const basePath = dirname(resolve(filePath));
|
|
12
|
+
const themeName = extractThemeName(raw);
|
|
13
|
+
const patternsDir = resolveThemePatternsDir(themeName);
|
|
14
|
+
|
|
15
|
+
const result = await validateDeck(raw, { basePath, patternsDir, compileTsx: compileTsxCached });
|
|
16
|
+
|
|
17
|
+
if (result.deckError) {
|
|
18
|
+
console.error("\u2717 Deck structure is invalid:");
|
|
19
|
+
for (const issue of result.deckError.issues) {
|
|
20
|
+
console.error(` ${issue.path.join(".")}: ${issue.message}`);
|
|
21
|
+
}
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
for (const slideResult of result.results) {
|
|
26
|
+
if (slideResult.valid) {
|
|
27
|
+
console.log(`\u2713 slides[${slideResult.index}]: valid`);
|
|
28
|
+
} else {
|
|
29
|
+
for (const issue of slideResult.errors!.issues) {
|
|
30
|
+
const detail = issue.message;
|
|
31
|
+
console.error(`\u2717 slides[${slideResult.index}]: ${detail}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!result.valid) {
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
console.log(`\nAll ${result.results.length} slide(s) passed validation.`);
|
|
41
|
+
}
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dist",
|
|
5
|
+
"rootDir": "src",
|
|
6
|
+
"jsx": "react-jsx"
|
|
7
|
+
},
|
|
8
|
+
"include": ["src/"],
|
|
9
|
+
"references": [
|
|
10
|
+
{ "path": "../schema" },
|
|
11
|
+
{ "path": "../dsl" },
|
|
12
|
+
{ "path": "../renderer" }
|
|
13
|
+
]
|
|
14
|
+
}
|