@cyphra/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/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # @cyphra/cli
2
+
3
+ Command-line interface for Cyphra.
4
+
5
+ Installing the **`cyphra`** meta-package pulls in **`@cyphra/cli`** as a dependency, so you get the `cyphra` binary without a second install step. Use this package on its own when you only need the CLI (for example `pnpm add -D @cyphra/cli`).
6
+
7
+ ## Commands
8
+
9
+ - `cyphra init` — scaffold `cyphra.json`, `schema.cyphra`, `migrations/`, `.env.example`
10
+ - `cyphra migrate deploy` / `cyphra migrate dev` — run pending `.mjs` migrations
11
+ - `cyphra migration:create <name>` — add a migration stub
12
+ - `cyphra push` — apply `@id` / `@unique` constraints from the schema
13
+ - `cyphra generate` — reserved (not implemented)
14
+
15
+ ## Migrations
16
+
17
+ Migration files are ESM (`.mjs`) and should import `defineMigration` from **`cyphra`** or `@cyphra/migrator` (library dependency).
18
+
19
+ ## Environment
20
+
21
+ - `NEO4J_URI`, `NEO4J_USER`, `NEO4J_PASSWORD`
22
+ - Optional: `NEO4J_DATABASE`
package/dist/index.js ADDED
@@ -0,0 +1,289 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/index.ts
4
+ import { Command } from "commander";
5
+
6
+ // src/commands/init.ts
7
+ import { mkdir, writeFile } from "fs/promises";
8
+ import path from "path";
9
+ var defaultSchema = `node Example {
10
+ id String @id @default(cuid())
11
+ }
12
+ `;
13
+ var defaultEnvExample = `# NEO4J_URI=neo4j+s://xxxx.databases.neo4j.io
14
+ # NEO4J_USER=neo4j
15
+ # NEO4J_PASSWORD=secret
16
+ # NEO4J_DATABASE=neo4j
17
+ `;
18
+ async function runInit(cwd) {
19
+ const configPath = path.join(cwd, "cyphra.json");
20
+ await writeFile(
21
+ configPath,
22
+ JSON.stringify(
23
+ {
24
+ schema: "./schema.cyphra",
25
+ migrations: "./migrations",
26
+ generate: "./cyphra.gen.ts"
27
+ },
28
+ null,
29
+ 2
30
+ ) + "\n",
31
+ "utf8"
32
+ );
33
+ await writeFile(path.join(cwd, "schema.cyphra"), defaultSchema, "utf8");
34
+ await mkdir(path.join(cwd, "migrations"), { recursive: true });
35
+ await writeFile(path.join(cwd, "migrations", ".gitkeep"), "", "utf8");
36
+ await writeFile(path.join(cwd, ".env.example"), defaultEnvExample, "utf8");
37
+ console.log("Created cyphra.json, schema.cyphra, migrations/, .env.example");
38
+ }
39
+
40
+ // src/config.ts
41
+ import { readFile } from "fs/promises";
42
+ import path3 from "path";
43
+
44
+ // src/paths.ts
45
+ import path2 from "path";
46
+ function resolveUnderRoot(root, p) {
47
+ const absRoot = path2.resolve(root);
48
+ const resolved = path2.resolve(absRoot, p);
49
+ const rel = path2.relative(absRoot, resolved);
50
+ if (rel.startsWith("..") || path2.isAbsolute(rel)) {
51
+ throw new Error(`Path escapes project root: ${p}`);
52
+ }
53
+ return resolved;
54
+ }
55
+
56
+ // src/config.ts
57
+ async function loadConfig(cwd) {
58
+ const configPath = path3.join(cwd, "cyphra.json");
59
+ const raw = await readFile(configPath, "utf8");
60
+ const data = JSON.parse(raw);
61
+ if (!data || typeof data !== "object") {
62
+ throw new Error("cyphra.json must be a JSON object");
63
+ }
64
+ const schema = data.schema;
65
+ const migrations = data.migrations;
66
+ const generateRaw = data.generate;
67
+ if (typeof schema !== "string" || typeof migrations !== "string") {
68
+ throw new Error('cyphra.json requires string fields "schema" and "migrations"');
69
+ }
70
+ const generatePath = typeof generateRaw === "string" ? generateRaw : "./cyphra.gen.ts";
71
+ return {
72
+ schema: resolveUnderRoot(cwd, schema),
73
+ migrations: resolveUnderRoot(cwd, migrations),
74
+ generate: resolveUnderRoot(cwd, generatePath)
75
+ };
76
+ }
77
+
78
+ // src/clientFromEnv.ts
79
+ import { CyphraClient } from "@cyphra/runtime";
80
+ function clientFromEnv(debug = false) {
81
+ const uri = process.env.NEO4J_URI;
82
+ const user = process.env.NEO4J_USER;
83
+ const password = process.env.NEO4J_PASSWORD;
84
+ if (!uri || !user || !password) {
85
+ throw new Error("Set NEO4J_URI, NEO4J_USER, and NEO4J_PASSWORD in the environment");
86
+ }
87
+ return new CyphraClient({
88
+ uri,
89
+ user,
90
+ password,
91
+ database: process.env.NEO4J_DATABASE,
92
+ debug
93
+ });
94
+ }
95
+
96
+ // src/loadMigrations.ts
97
+ import { createHash } from "crypto";
98
+ import { readdir, readFile as readFile2 } from "fs/promises";
99
+ import path4 from "path";
100
+ import { pathToFileURL } from "url";
101
+ function assertDirUnderRoot(dir, root) {
102
+ const absRoot = path4.resolve(root);
103
+ const absDir = path4.resolve(dir);
104
+ const rel = path4.relative(absRoot, absDir);
105
+ if (rel.startsWith("..") || path4.isAbsolute(rel)) {
106
+ throw new Error("Migrations directory must be inside the project root");
107
+ }
108
+ }
109
+ async function loadMigrationsFromDir(migrationsDir, root) {
110
+ assertDirUnderRoot(migrationsDir, root);
111
+ const entries = await readdir(migrationsDir);
112
+ const files = entries.filter((f) => f.endsWith(".js") || f.endsWith(".mjs")).sort();
113
+ const out = [];
114
+ for (const f of files) {
115
+ const full = path4.join(migrationsDir, f);
116
+ const src = await readFile2(full, "utf8");
117
+ const checksum = createHash("sha256").update(src).digest("hex");
118
+ const href = pathToFileURL(full).href;
119
+ const mod = await import(href);
120
+ const def = mod.default;
121
+ if (!def?.name || typeof def.up !== "function") {
122
+ throw new Error(`Migration ${f} must export default defineMigration({ name, up })`);
123
+ }
124
+ out.push({ name: def.name, checksum, definition: def });
125
+ }
126
+ return out;
127
+ }
128
+
129
+ // src/commands/migrate.ts
130
+ import { runPendingMigrations } from "@cyphra/migrator";
131
+ async function runMigrate(cwd, verbose) {
132
+ const config = await loadConfig(cwd);
133
+ const client = clientFromEnv(verbose);
134
+ try {
135
+ const loaded = await loadMigrationsFromDir(config.migrations, cwd);
136
+ const applied = await runPendingMigrations(client, loaded);
137
+ if (verbose || applied.length > 0) {
138
+ console.log(applied.length ? `Applied: ${applied.join(", ")}` : "No pending migrations");
139
+ }
140
+ } finally {
141
+ await client.close();
142
+ }
143
+ }
144
+
145
+ // src/commands/migrationCreate.ts
146
+ import { writeFile as writeFile2 } from "fs/promises";
147
+ import path5 from "path";
148
+ function migrationFileBody(name) {
149
+ return `import { defineMigration } from "@cyphra/migrator";
150
+
151
+ export default defineMigration({
152
+ name: ${JSON.stringify(name)},
153
+ async up({ db }) {
154
+ await db.run\`RETURN 1 AS ok\`;
155
+ },
156
+ });
157
+ `;
158
+ }
159
+ async function runMigrationCreate(cwd, migrationsDir, stem) {
160
+ const safe = stem.replace(/[^a-zA-Z0-9_-]/g, "_");
161
+ const fileName = `${safe}.mjs`;
162
+ const full = path5.join(migrationsDir, fileName);
163
+ await writeFile2(full, migrationFileBody(safe), "utf8");
164
+ console.log(`Created ${path5.relative(cwd, full)}`);
165
+ }
166
+
167
+ // src/commands/generate.ts
168
+ import { readFile as readFile3, writeFile as writeFile3 } from "fs/promises";
169
+ import path6 from "path";
170
+ import { parseSchema, renderCyphraGenSource, validateSchema } from "@cyphra/schema";
171
+ async function runGenerate(cwd, config) {
172
+ const source = await readFile3(config.schema, "utf8");
173
+ const doc = parseSchema(source);
174
+ validateSchema(doc);
175
+ const out = renderCyphraGenSource(doc);
176
+ await writeFile3(config.generate, out, "utf8");
177
+ console.log(`Wrote ${path6.relative(cwd, config.generate)}`);
178
+ }
179
+
180
+ // src/commands/push.ts
181
+ import { readFile as readFile4 } from "fs/promises";
182
+ import {
183
+ applyConstraintStatements,
184
+ constraintStatementsFromSchema,
185
+ indexStatementsFromSchema
186
+ } from "@cyphra/migrator";
187
+ import { parseSchema as parseSchema2 } from "@cyphra/schema";
188
+ async function runPush(cwd) {
189
+ const config = await loadConfig(cwd);
190
+ const source = await readFile4(config.schema, "utf8");
191
+ const doc = parseSchema2(source);
192
+ const constraints = constraintStatementsFromSchema(doc);
193
+ const indexes = indexStatementsFromSchema(doc);
194
+ const statements = [...constraints, ...indexes];
195
+ const client = clientFromEnv(false);
196
+ try {
197
+ await applyConstraintStatements(client, statements);
198
+ console.log(
199
+ `Applied ${constraints.length} constraint statement(s) and ${indexes.length} range index statement(s)`
200
+ );
201
+ } finally {
202
+ await client.close();
203
+ }
204
+ }
205
+
206
+ // src/commands/schemaDdl.ts
207
+ import { readFile as readFile5 } from "fs/promises";
208
+ import { constraintStatementsFromSchema as constraintStatementsFromSchema2, indexStatementsFromSchema as indexStatementsFromSchema2 } from "@cyphra/migrator";
209
+ import { parseSchema as parseSchema3 } from "@cyphra/schema";
210
+ async function runSchemaDdl(config) {
211
+ const source = await readFile5(config.schema, "utf8");
212
+ const doc = parseSchema3(source);
213
+ for (const stmt of constraintStatementsFromSchema2(doc)) {
214
+ console.log(stmt);
215
+ }
216
+ for (const stmt of indexStatementsFromSchema2(doc)) {
217
+ console.log(stmt);
218
+ }
219
+ }
220
+
221
+ // src/commands/schemaPrint.ts
222
+ import { readFile as readFile6 } from "fs/promises";
223
+ import { parseSchema as parseSchema4, printSchemaDocument, validateSchema as validateSchema2 } from "@cyphra/schema";
224
+ async function runSchemaPrint(config) {
225
+ const raw = await readFile6(config.schema, "utf8");
226
+ const doc = parseSchema4(raw);
227
+ validateSchema2(doc);
228
+ process.stdout.write(printSchemaDocument(doc));
229
+ }
230
+
231
+ // src/commands/validateSchema.ts
232
+ import { readFile as readFile7 } from "fs/promises";
233
+ import path7 from "path";
234
+ import { parseSchema as parseSchema5, validateSchema as validateSchema3 } from "@cyphra/schema";
235
+ async function runValidateSchema(cwd, config) {
236
+ const raw = await readFile7(config.schema, "utf8");
237
+ const doc = parseSchema5(raw);
238
+ validateSchema3(doc);
239
+ const rel = path7.relative(cwd, config.schema);
240
+ console.log(`OK \u2014 ${rel} (${doc.declarations.length} declaration(s))`);
241
+ }
242
+
243
+ // src/index.ts
244
+ var program = new Command();
245
+ program.name("cyphra").description("Cyphra toolkit for Neo4j").version("0.0.0");
246
+ program.command("init").description("Create cyphra.json, schema.cyphra, migrations/, .env.example").action(async () => {
247
+ await runInit(process.cwd());
248
+ });
249
+ var migrate = program.command("migrate").description("Migration commands");
250
+ migrate.command("deploy").description("Apply pending migrations").action(async () => {
251
+ await runMigrate(process.cwd(), false);
252
+ });
253
+ migrate.command("dev").description("Apply pending migrations (verbose)").action(async () => {
254
+ await runMigrate(process.cwd(), true);
255
+ });
256
+ program.command("migration:create").argument("<name>", "Migration file stem (e.g. add-index)").description("Create a new .mjs migration").action(async (name) => {
257
+ const cwd = process.cwd();
258
+ const config = await loadConfig(cwd);
259
+ await runMigrationCreate(cwd, config.migrations, name);
260
+ });
261
+ program.command("push").description("Push schema constraints to Neo4j").action(async () => {
262
+ await runPush(process.cwd());
263
+ });
264
+ program.command("validate").description("Parse and validate schema.cyphra").action(async () => {
265
+ const cwd = process.cwd();
266
+ const config = await loadConfig(cwd);
267
+ await runValidateSchema(cwd, config);
268
+ });
269
+ program.command("generate").description("Emit cyphra.gen.ts (labels, relationship types) from the schema").action(async () => {
270
+ const cwd = process.cwd();
271
+ const config = await loadConfig(cwd);
272
+ await runGenerate(cwd, config);
273
+ });
274
+ var schemaCmd = program.command("schema").description("Schema utilities");
275
+ schemaCmd.command("print").description("Pretty-print schema.cyphra to stdout (canonical formatting)").action(async () => {
276
+ const cwd = process.cwd();
277
+ const config = await loadConfig(cwd);
278
+ await runSchemaPrint(config);
279
+ });
280
+ schemaCmd.command("ddl").description("Print constraint + range-index DDL for push (no database connection)").action(async () => {
281
+ const cwd = process.cwd();
282
+ const config = await loadConfig(cwd);
283
+ await runSchemaDdl(config);
284
+ });
285
+ program.parseAsync(process.argv).catch((err) => {
286
+ console.error(err instanceof Error ? err.message : err);
287
+ process.exitCode = 1;
288
+ });
289
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/commands/init.ts","../src/config.ts","../src/paths.ts","../src/clientFromEnv.ts","../src/loadMigrations.ts","../src/commands/migrate.ts","../src/commands/migrationCreate.ts","../src/commands/generate.ts","../src/commands/push.ts","../src/commands/schemaDdl.ts","../src/commands/schemaPrint.ts","../src/commands/validateSchema.ts"],"sourcesContent":["import { Command } from \"commander\";\nimport { runInit } from \"./commands/init.js\";\nimport { runMigrate } from \"./commands/migrate.js\";\nimport { runMigrationCreate } from \"./commands/migrationCreate.js\";\nimport { runGenerate } from \"./commands/generate.js\";\nimport { runPush } from \"./commands/push.js\";\nimport { runSchemaDdl } from \"./commands/schemaDdl.js\";\nimport { runSchemaPrint } from \"./commands/schemaPrint.js\";\nimport { runValidateSchema } from \"./commands/validateSchema.js\";\nimport { loadConfig } from \"./config.js\";\n\nconst program = new Command();\nprogram.name(\"cyphra\").description(\"Cyphra toolkit for Neo4j\").version(\"0.0.0\");\n\nprogram\n .command(\"init\")\n .description(\"Create cyphra.json, schema.cyphra, migrations/, .env.example\")\n .action(async () => {\n await runInit(process.cwd());\n });\n\nconst migrate = program.command(\"migrate\").description(\"Migration commands\");\n\nmigrate\n .command(\"deploy\")\n .description(\"Apply pending migrations\")\n .action(async () => {\n await runMigrate(process.cwd(), false);\n });\n\nmigrate\n .command(\"dev\")\n .description(\"Apply pending migrations (verbose)\")\n .action(async () => {\n await runMigrate(process.cwd(), true);\n });\n\nprogram\n .command(\"migration:create\")\n .argument(\"<name>\", \"Migration file stem (e.g. add-index)\")\n .description(\"Create a new .mjs migration\")\n .action(async (name: string) => {\n const cwd = process.cwd();\n const config = await loadConfig(cwd);\n await runMigrationCreate(cwd, config.migrations, name);\n });\n\nprogram\n .command(\"push\")\n .description(\"Push schema constraints to Neo4j\")\n .action(async () => {\n await runPush(process.cwd());\n });\n\nprogram\n .command(\"validate\")\n .description(\"Parse and validate schema.cyphra\")\n .action(async () => {\n const cwd = process.cwd();\n const config = await loadConfig(cwd);\n await runValidateSchema(cwd, config);\n });\n\nprogram\n .command(\"generate\")\n .description(\"Emit cyphra.gen.ts (labels, relationship types) from the schema\")\n .action(async () => {\n const cwd = process.cwd();\n const config = await loadConfig(cwd);\n await runGenerate(cwd, config);\n });\n\nconst schemaCmd = program.command(\"schema\").description(\"Schema utilities\");\n\nschemaCmd\n .command(\"print\")\n .description(\"Pretty-print schema.cyphra to stdout (canonical formatting)\")\n .action(async () => {\n const cwd = process.cwd();\n const config = await loadConfig(cwd);\n await runSchemaPrint(config);\n });\n\nschemaCmd\n .command(\"ddl\")\n .description(\"Print constraint + range-index DDL for push (no database connection)\")\n .action(async () => {\n const cwd = process.cwd();\n const config = await loadConfig(cwd);\n await runSchemaDdl(config);\n });\n\nprogram.parseAsync(process.argv).catch((err: unknown) => {\n console.error(err instanceof Error ? err.message : err);\n process.exitCode = 1;\n});\n","import { mkdir, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\n\nconst defaultSchema = `node Example {\n id String @id @default(cuid())\n}\n`;\n\nconst defaultEnvExample = `# NEO4J_URI=neo4j+s://xxxx.databases.neo4j.io\n# NEO4J_USER=neo4j\n# NEO4J_PASSWORD=secret\n# NEO4J_DATABASE=neo4j\n`;\n\n/**\n * Scaffold `cyphra.json`, `schema.cyphra`, `migrations/`, and `.env.example`.\n *\n * @param cwd - Project directory.\n */\nexport async function runInit(cwd: string): Promise<void> {\n const configPath = path.join(cwd, \"cyphra.json\");\n await writeFile(\n configPath,\n JSON.stringify(\n {\n schema: \"./schema.cyphra\",\n migrations: \"./migrations\",\n generate: \"./cyphra.gen.ts\",\n },\n null,\n 2,\n ) + \"\\n\",\n \"utf8\",\n );\n await writeFile(path.join(cwd, \"schema.cyphra\"), defaultSchema, \"utf8\");\n await mkdir(path.join(cwd, \"migrations\"), { recursive: true });\n await writeFile(path.join(cwd, \"migrations\", \".gitkeep\"), \"\", \"utf8\");\n await writeFile(path.join(cwd, \".env.example\"), defaultEnvExample, \"utf8\");\n console.log(\"Created cyphra.json, schema.cyphra, migrations/, .env.example\");\n}\n","import { readFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { resolveUnderRoot } from \"./paths.js\";\n\nexport type CyphraConfig = {\n readonly schema: string;\n readonly migrations: string;\n /** Absolute path for `cyphra generate` output (default `./cyphra.gen.ts`). */\n readonly generate: string;\n};\n\n/**\n * Load `cyphra.json` from the working directory.\n *\n * @param cwd - Current working directory (absolute).\n */\nexport async function loadConfig(cwd: string): Promise<CyphraConfig> {\n const configPath = path.join(cwd, \"cyphra.json\");\n const raw = await readFile(configPath, \"utf8\");\n const data = JSON.parse(raw) as unknown;\n if (!data || typeof data !== \"object\") {\n throw new Error(\"cyphra.json must be a JSON object\");\n }\n const schema = (data as Record<string, unknown>).schema;\n const migrations = (data as Record<string, unknown>).migrations;\n const generateRaw = (data as Record<string, unknown>).generate;\n if (typeof schema !== \"string\" || typeof migrations !== \"string\") {\n throw new Error('cyphra.json requires string fields \"schema\" and \"migrations\"');\n }\n const generatePath = typeof generateRaw === \"string\" ? generateRaw : \"./cyphra.gen.ts\";\n return {\n schema: resolveUnderRoot(cwd, schema),\n migrations: resolveUnderRoot(cwd, migrations),\n generate: resolveUnderRoot(cwd, generatePath),\n };\n}\n","import path from \"node:path\";\n\n/**\n * Resolve `p` under `root` and throw if the result escapes `root` (path traversal guard).\n *\n * @param root - Project root directory (absolute).\n * @param p - Path segment or relative path from config.\n * @returns Absolute normalized path under `root`.\n */\nexport function resolveUnderRoot(root: string, p: string): string {\n const absRoot = path.resolve(root);\n const resolved = path.resolve(absRoot, p);\n const rel = path.relative(absRoot, resolved);\n if (rel.startsWith(\"..\") || path.isAbsolute(rel)) {\n throw new Error(`Path escapes project root: ${p}`);\n }\n return resolved;\n}\n","import { CyphraClient } from \"@cyphra/runtime\";\n\n/**\n * Build a {@link CyphraClient} from `NEO4J_URI`, `NEO4J_USER`, `NEO4J_PASSWORD`, optional `NEO4J_DATABASE`.\n *\n * @param debug - Forwarded to client options.\n */\nexport function clientFromEnv(debug = false): CyphraClient {\n const uri = process.env.NEO4J_URI;\n const user = process.env.NEO4J_USER;\n const password = process.env.NEO4J_PASSWORD;\n if (!uri || !user || !password) {\n throw new Error(\"Set NEO4J_URI, NEO4J_USER, and NEO4J_PASSWORD in the environment\");\n }\n return new CyphraClient({\n uri,\n user,\n password,\n database: process.env.NEO4J_DATABASE,\n debug,\n });\n}\n","import { createHash } from \"node:crypto\";\nimport { readdir, readFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\nimport type { LoadedMigration, MigrationDefinition } from \"@cyphra/migrator\";\n\nfunction assertDirUnderRoot(dir: string, root: string): void {\n const absRoot = path.resolve(root);\n const absDir = path.resolve(dir);\n const rel = path.relative(absRoot, absDir);\n if (rel.startsWith(\"..\") || path.isAbsolute(rel)) {\n throw new Error(\"Migrations directory must be inside the project root\");\n }\n}\n\n/**\n * Load sorted `.js` / `.mjs` migration modules from a directory.\n *\n * @param migrationsDir - Absolute migrations directory (from config).\n * @param root - Project root for validation.\n */\nexport async function loadMigrationsFromDir(\n migrationsDir: string,\n root: string,\n): Promise<LoadedMigration[]> {\n assertDirUnderRoot(migrationsDir, root);\n const entries = await readdir(migrationsDir);\n const files = entries.filter((f) => f.endsWith(\".js\") || f.endsWith(\".mjs\")).sort();\n const out: LoadedMigration[] = [];\n for (const f of files) {\n const full = path.join(migrationsDir, f);\n const src = await readFile(full, \"utf8\");\n const checksum = createHash(\"sha256\").update(src).digest(\"hex\");\n const href = pathToFileURL(full).href;\n const mod = (await import(href)) as { default?: MigrationDefinition };\n const def = mod.default;\n if (!def?.name || typeof def.up !== \"function\") {\n throw new Error(`Migration ${f} must export default defineMigration({ name, up })`);\n }\n out.push({ name: def.name, checksum, definition: def });\n }\n return out;\n}\n","import { loadConfig } from \"../config.js\";\nimport { clientFromEnv } from \"../clientFromEnv.js\";\nimport { loadMigrationsFromDir } from \"../loadMigrations.js\";\nimport { runPendingMigrations } from \"@cyphra/migrator\";\n\n/**\n * Apply pending migrations (deploy / dev — same behavior; dev may pass verbose later).\n *\n * @param cwd - Project root.\n * @param verbose - Log applied migration names.\n */\nexport async function runMigrate(cwd: string, verbose: boolean): Promise<void> {\n const config = await loadConfig(cwd);\n const client = clientFromEnv(verbose);\n try {\n const loaded = await loadMigrationsFromDir(config.migrations, cwd);\n const applied = await runPendingMigrations(client, loaded);\n if (verbose || applied.length > 0) {\n console.log(applied.length ? `Applied: ${applied.join(\", \")}` : \"No pending migrations\");\n }\n } finally {\n await client.close();\n }\n}\n","import { writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\n\nfunction migrationFileBody(name: string): string {\n return `import { defineMigration } from \"@cyphra/migrator\";\n\nexport default defineMigration({\n name: ${JSON.stringify(name)},\n async up({ db }) {\n await db.run\\`RETURN 1 AS ok\\`;\n },\n});\n`;\n}\n\n/**\n * Create a new `.mjs` migration stub under `migrations/`.\n *\n * @param cwd - Project root.\n * @param migrationsDir - Absolute migrations directory.\n * @param stem - File stem used for file name and migration `name`.\n */\nexport async function runMigrationCreate(\n cwd: string,\n migrationsDir: string,\n stem: string,\n): Promise<void> {\n const safe = stem.replace(/[^a-zA-Z0-9_-]/g, \"_\");\n const fileName = `${safe}.mjs`;\n const full = path.join(migrationsDir, fileName);\n await writeFile(full, migrationFileBody(safe), \"utf8\");\n console.log(`Created ${path.relative(cwd, full)}`);\n}\n","import { readFile, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { parseSchema, renderCyphraGenSource, validateSchema } from \"@cyphra/schema\";\nimport type { CyphraConfig } from \"../config.js\";\n\n/**\n * Emit `cyphra.gen.ts` (or configured path) from the schema file.\n *\n * @param cwd - Project root.\n * @param config - Loaded config with absolute schema and output paths.\n */\nexport async function runGenerate(cwd: string, config: CyphraConfig): Promise<void> {\n const source = await readFile(config.schema, \"utf8\");\n const doc = parseSchema(source);\n validateSchema(doc);\n const out = renderCyphraGenSource(doc);\n await writeFile(config.generate, out, \"utf8\");\n console.log(`Wrote ${path.relative(cwd, config.generate)}`);\n}\n","import { readFile } from \"node:fs/promises\";\nimport {\n applyConstraintStatements,\n constraintStatementsFromSchema,\n indexStatementsFromSchema,\n} from \"@cyphra/migrator\";\nimport { parseSchema } from \"@cyphra/schema\";\nimport { loadConfig } from \"../config.js\";\nimport { clientFromEnv } from \"../clientFromEnv.js\";\n\n/**\n * Parse `schema.cyphra` and apply generated constraints.\n *\n * @param cwd - Project root.\n */\nexport async function runPush(cwd: string): Promise<void> {\n const config = await loadConfig(cwd);\n const source = await readFile(config.schema, \"utf8\");\n const doc = parseSchema(source);\n const constraints = constraintStatementsFromSchema(doc);\n const indexes = indexStatementsFromSchema(doc);\n const statements = [...constraints, ...indexes];\n const client = clientFromEnv(false);\n try {\n await applyConstraintStatements(client, statements);\n console.log(\n `Applied ${constraints.length} constraint statement(s) and ${indexes.length} range index statement(s)`,\n );\n } finally {\n await client.close();\n }\n}\n","import { readFile } from \"node:fs/promises\";\nimport { constraintStatementsFromSchema, indexStatementsFromSchema } from \"@cyphra/migrator\";\nimport { parseSchema } from \"@cyphra/schema\";\nimport type { CyphraConfig } from \"../config.js\";\n\n/**\n * Print `CREATE CONSTRAINT` / `CREATE RANGE INDEX` lines for the schema (same order as `cyphra push`).\n * Does not connect to Neo4j.\n *\n * @param config - Config with absolute schema path.\n */\nexport async function runSchemaDdl(config: CyphraConfig): Promise<void> {\n const source = await readFile(config.schema, \"utf8\");\n const doc = parseSchema(source);\n for (const stmt of constraintStatementsFromSchema(doc)) {\n console.log(stmt);\n }\n for (const stmt of indexStatementsFromSchema(doc)) {\n console.log(stmt);\n }\n}\n","import { readFile } from \"node:fs/promises\";\nimport { parseSchema, printSchemaDocument, validateSchema } from \"@cyphra/schema\";\nimport type { CyphraConfig } from \"../config.js\";\n\n/**\n * Print a canonical `.cyphra` rendering of the schema to stdout.\n *\n * @param config - Config with absolute schema path.\n */\nexport async function runSchemaPrint(config: CyphraConfig): Promise<void> {\n const raw = await readFile(config.schema, \"utf8\");\n const doc = parseSchema(raw);\n validateSchema(doc);\n process.stdout.write(printSchemaDocument(doc));\n}\n","import { readFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { parseSchema, validateSchema } from \"@cyphra/schema\";\nimport type { CyphraConfig } from \"../config.js\";\n\n/**\n * Parse and semantically validate `schema.cyphra`.\n *\n * @param cwd - Project root (for relative log paths).\n * @param config - Config with absolute schema path.\n */\nexport async function runValidateSchema(cwd: string, config: CyphraConfig): Promise<void> {\n const raw = await readFile(config.schema, \"utf8\");\n const doc = parseSchema(raw);\n validateSchema(doc);\n const rel = path.relative(cwd, config.schema);\n console.log(`OK — ${rel} (${doc.declarations.length} declaration(s))`);\n}\n"],"mappings":";;;AAAA,SAAS,eAAe;;;ACAxB,SAAS,OAAO,iBAAiB;AACjC,OAAO,UAAU;AAEjB,IAAM,gBAAgB;AAAA;AAAA;AAAA;AAKtB,IAAM,oBAAoB;AAAA;AAAA;AAAA;AAAA;AAW1B,eAAsB,QAAQ,KAA4B;AACxD,QAAM,aAAa,KAAK,KAAK,KAAK,aAAa;AAC/C,QAAM;AAAA,IACJ;AAAA,IACA,KAAK;AAAA,MACH;AAAA,QACE,QAAQ;AAAA,QACR,YAAY;AAAA,QACZ,UAAU;AAAA,MACZ;AAAA,MACA;AAAA,MACA;AAAA,IACF,IAAI;AAAA,IACJ;AAAA,EACF;AACA,QAAM,UAAU,KAAK,KAAK,KAAK,eAAe,GAAG,eAAe,MAAM;AACtE,QAAM,MAAM,KAAK,KAAK,KAAK,YAAY,GAAG,EAAE,WAAW,KAAK,CAAC;AAC7D,QAAM,UAAU,KAAK,KAAK,KAAK,cAAc,UAAU,GAAG,IAAI,MAAM;AACpE,QAAM,UAAU,KAAK,KAAK,KAAK,cAAc,GAAG,mBAAmB,MAAM;AACzE,UAAQ,IAAI,+DAA+D;AAC7E;;;ACvCA,SAAS,gBAAgB;AACzB,OAAOA,WAAU;;;ACDjB,OAAOC,WAAU;AASV,SAAS,iBAAiB,MAAc,GAAmB;AAChE,QAAM,UAAUA,MAAK,QAAQ,IAAI;AACjC,QAAM,WAAWA,MAAK,QAAQ,SAAS,CAAC;AACxC,QAAM,MAAMA,MAAK,SAAS,SAAS,QAAQ;AAC3C,MAAI,IAAI,WAAW,IA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K,MAAM;AAC/B,CAAC;AAEH,IAAM,YAAY,QAAQ,QAAQ,QAAQ,EAAE,YAAY,kBAAkB;AAE1E,UACG,QAAQ,OAAO,EACf,YAAY,6DAA6D,EACzE,OAAO,YAAY;AAClB,QAAM,MAAM,QAAQ,IAAI;AACxB,QAAM,SAAS,MAAM,WAAW,GAAG;AACnC,QAAM,eAAe,MAAM;AAC7B,CAAC;AAEH,UACG,QAAQ,KAAK,EACb,YAAY,sEAAsE,EAClF,OAAO,YAAY;AAClB,QAAM,MAAM,QAAQ,IAAI;AACxB,QAAM,SAAS,MAAM,WAAW,GAAG;AACnC,QAAM,aAAa,MAAM;AAC3B,CAAC;AAEH,QAAQ,WAAW,QAAQ,IAAI,EAAE,MAAM,CAAC,QAAiB;AACvD,UAAQ,MAAM,eAAe,QAAQ,IAAI,UAAU,GAAG;AACtD,UAAQ,WAAW;AACrB,CAAC;","names":["path","path","path","readFile","path","writeFile","path","readFile","writeFile","path","readFile","parseSchema","readFile","parseSchema","readFile","constraintStatementsFromSchema","indexStatementsFromSchema","parseSchema","readFile","parseSchema","validateSchema","readFile","path","parseSchema","validateSchema"]}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@cyphra/cli",
3
+ "version": "0.1.0",
4
+ "description": "Cyphra CLI: init, migrate, push",
5
+ "type": "module",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "files": [
10
+ "dist",
11
+ "README.md"
12
+ ],
13
+ "bin": {
14
+ "cyphra": "./dist/index.js"
15
+ },
16
+ "dependencies": {
17
+ "commander": "^12.1.0",
18
+ "@cyphra/runtime": "0.1.0",
19
+ "@cyphra/schema": "0.1.0",
20
+ "@cyphra/migrator": "0.1.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "^22.10.2",
24
+ "tsup": "^8.3.5",
25
+ "typescript": "^5.7.2",
26
+ "vitest": "^3.0.2"
27
+ },
28
+ "scripts": {
29
+ "build": "tsup",
30
+ "typecheck": "tsc --noEmit",
31
+ "test": "vitest run"
32
+ }
33
+ }