@typecad/ui 1.0.0-alpha.13 → 1.0.0-alpha.14

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,98 @@
1
+ // ---------------------------------------------------------------------------
2
+ // package.json script writer for the @typecad/ui integration wizard.
3
+ //
4
+ // package.json is plain JSON (no comments, no expressions), so unlike
5
+ // cuttlefish.config.ts (see config-writer.ts) this needs no AST surgery —
6
+ // parse, compare, set, and re-serialize with the 2-space formatting npm and
7
+ // `cuttlefish create` both write. An unchanged script is a no-op: the
8
+ // original text is returned verbatim so a re-run never reformats the file.
9
+ // ---------------------------------------------------------------------------
10
+
11
+ import fs from "node:fs";
12
+ import path from "node:path";
13
+
14
+ const PACKAGE_FILENAME = "package.json";
15
+
16
+ /** Walk up from `startDir` looking for package.json. */
17
+ export function findPackageJson(startDir: string): string | undefined {
18
+ let dir = path.resolve(startDir);
19
+ for (;;) {
20
+ const candidate = path.join(dir, PACKAGE_FILENAME);
21
+ if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
22
+ return candidate;
23
+ }
24
+ const parent = path.dirname(dir);
25
+ if (parent === dir) return undefined;
26
+ dir = parent;
27
+ }
28
+ }
29
+
30
+ function parsePackageJson(sourceText: string): Record<string, unknown> {
31
+ try {
32
+ return JSON.parse(sourceText) as Record<string, unknown>;
33
+ } catch (err) {
34
+ throw new Error(
35
+ `package.json is not valid JSON: ${err instanceof Error ? err.message : String(err)}`,
36
+ );
37
+ }
38
+ }
39
+
40
+ function scriptsOf(pkg: Record<string, unknown>): Record<string, string> | undefined {
41
+ const scripts = pkg.scripts;
42
+ return typeof scripts === "object" && scripts !== null && !Array.isArray(scripts)
43
+ ? (scripts as Record<string, string>)
44
+ : undefined;
45
+ }
46
+
47
+ /** Read a single npm script; undefined when missing. Throws on invalid JSON. */
48
+ export function readPackageScript(sourceText: string, name: string): string | undefined {
49
+ const scripts = scriptsOf(parsePackageJson(sourceText));
50
+ const value = scripts?.[name];
51
+ return typeof value === "string" ? value : undefined;
52
+ }
53
+
54
+ export interface UpsertScriptResult {
55
+ text: string;
56
+ /** false — the script already had this exact command; text is unchanged. */
57
+ changed: boolean;
58
+ /** The command that was replaced, when a different script existed. */
59
+ previous?: string;
60
+ }
61
+
62
+ /** Insert or update one npm script, preserving every other key and their order. */
63
+ export function upsertPackageScript(
64
+ sourceText: string,
65
+ name: string,
66
+ command: string,
67
+ ): UpsertScriptResult {
68
+ const pkg = parsePackageJson(sourceText);
69
+ const scripts = scriptsOf(pkg);
70
+ const previous = scripts?.[name];
71
+ if (previous === command) {
72
+ return { text: sourceText, changed: false };
73
+ }
74
+ if (!scripts) {
75
+ pkg.scripts = {};
76
+ }
77
+ (pkg.scripts as Record<string, string>)[name] = command;
78
+ // Keep the file's trailing-newline convention (npm writes LF; a missing
79
+ // final newline stays missing so the diff is one line, not two).
80
+ const eol = sourceText.endsWith("\r\n") ? "\r\n" : sourceText.endsWith("\n") ? "\n" : "";
81
+ return {
82
+ text: `${JSON.stringify(pkg, null, 2)}${eol}`,
83
+ changed: true,
84
+ ...(previous !== undefined ? { previous } : {}),
85
+ };
86
+ }
87
+
88
+ /**
89
+ * The `cuttlefish preview` command the wizard writes as the `preview` npm
90
+ * script. The --config path is made relative to the package.json directory
91
+ * (npm scripts run there), posix-separated, and explicitly ./-prefixed so
92
+ * sibling configs render as `./cuttlefish.config.ts`.
93
+ */
94
+ export function previewScriptCommand(packageJsonPath: string, configPath: string): string {
95
+ let rel = path.relative(path.dirname(packageJsonPath), configPath).split(path.sep).join("/");
96
+ if (!rel.startsWith(".")) rel = `./${rel}`;
97
+ return `cuttlefish preview --config ${rel}`;
98
+ }