zero-swift 0.7.10 → 0.7.12

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/dist/bin.js CHANGED
@@ -1,67 +1,27 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // src/cli.ts
3
+ // ../zerogen/src/cli.ts
4
4
  import { existsSync, readFileSync, writeFileSync } from "node:fs";
5
5
  import { relative, resolve as resolve2 } from "node:path";
6
6
  import { pathToFileURL } from "node:url";
7
- import { Cli, z } from "incur";
7
+ import { Cli, z as z2 } from "incur";
8
8
 
9
- // package.json
10
- var package_default = {
11
- name: "zero-swift",
12
- version: "0.7.10",
13
- description: "Generates the Swift row types and schema of a Zero app for the Zero Swift client.",
14
- license: "Apache-2.0",
15
- repository: {
16
- type: "git",
17
- url: "git+https://github.com/wevm/zero.git",
18
- directory: "packages/zero-swift-cli"
19
- },
20
- bin: {
21
- "zero-swift": "./src/bin.ts"
22
- },
23
- files: [
24
- "dist"
25
- ],
26
- type: "module",
27
- publishConfig: {
28
- bin: {
29
- "zero-swift": "./dist/bin.js"
30
- }
31
- },
32
- scripts: {
33
- build: "esbuild src/bin.ts --bundle --platform=node --format=esm --target=node22 --splitting --outdir=dist --external:@rocicorp/zero --external:typescript --external:incur",
34
- test: "vitest run",
35
- generate: "UPDATE_GENERATED=1 vitest run",
36
- "check-types": "tsc",
37
- lint: "oxlint --quiet --config ../../oxlint.config.ts src/",
38
- format: "oxfmt .",
39
- "check-format": "oxfmt --check .",
40
- "version-packages": "cd ../.. && pnpm dlx @changesets/cli@3.0.3 version && jq -r .version packages/zero-swift-cli/package.json > swift/VERSION"
41
- },
42
- dependencies: {
43
- incur: "0.7.1",
44
- typescript: "6.0.3"
45
- },
46
- devDependencies: {
47
- "@rocicorp/zero": "workspace:*",
48
- "@types/node": "^22.19.19",
49
- esbuild: "0.28.2",
50
- oxfmt: "^0.74.0",
51
- vitest: "^5.0.5",
52
- "zero-schema": "workspace:*",
53
- "zero-types": "workspace:*",
54
- zql: "workspace:*"
55
- },
56
- peerDependencies: {
57
- "@rocicorp/zero": ">=1.11.0"
58
- },
59
- engines: {
60
- node: ">=22.18"
61
- }
9
+ // ../zerogen/src/targets/swift.ts
10
+ import { z } from "incur";
11
+ var options = {
12
+ "schema-name": z.string().optional().describe("Name of the generated ZeroSchema static (default app)"),
13
+ "zql": z.string().optional().describe("Name of the generated query builder global (default zql)"),
14
+ "type-prefix": z.string().optional().describe("Prefix for every generated type name (default none)"),
15
+ "access": z.enum(["public", "internal"]).optional().describe("Access level of the generated code (default public)")
62
16
  };
63
-
64
- // src/generate.ts
17
+ function overrides(input) {
18
+ return {
19
+ ...input["schema-name"] !== void 0 ? { schemaName: input["schema-name"] } : {},
20
+ ...input.zql !== void 0 ? { zql: input.zql } : {},
21
+ ...input["type-prefix"] !== void 0 ? { typePrefix: input["type-prefix"] } : {},
22
+ ...input.access !== void 0 ? { access: input.access } : {}
23
+ };
24
+ }
65
25
  var reservedTypeNames = /* @__PURE__ */ new Set([
66
26
  "AnyQuery",
67
27
  "Cardinality",
@@ -799,7 +759,7 @@ function schemaDecl(pub, schema, name) {
799
759
  return lines.join("\n");
800
760
  }
801
761
 
802
- // src/types.ts
762
+ // ../zerogen/src/types.ts
803
763
  import { dirname, resolve } from "node:path";
804
764
  import ts from "typescript";
805
765
  var defaultOptions = {
@@ -948,7 +908,7 @@ function aliasName(type) {
948
908
  return void 0;
949
909
  }
950
910
 
951
- // src/cli.ts
911
+ // ../zerogen/src/cli.ts
952
912
  var UsageError = class extends Error {
953
913
  code;
954
914
  constructor(code, message) {
@@ -962,11 +922,19 @@ function mustExist(path, code, message) {
962
922
  }
963
923
  }
964
924
  async function codegenFile(config, cwd) {
925
+ const { schema, customTypes, source } = await loadSchema(
926
+ config,
927
+ cwd,
928
+ "zero-swift.json"
929
+ );
930
+ return generate(schema, customTypes, config, source);
931
+ }
932
+ async function loadSchema(config, cwd, configFile) {
965
933
  const schemaPath = resolve2(cwd, config.schema ?? "schema.ts");
966
934
  mustExist(
967
935
  schemaPath,
968
936
  "SCHEMA_NOT_FOUND",
969
- `Schema ${relative(cwd, schemaPath)} doesn't exist. Pass --schema-file, or set "schema" in zero-swift.json.`
937
+ `Schema ${relative(cwd, schemaPath)} doesn't exist. Pass --schema-file, or set "schema" in ${configFile}.`
970
938
  );
971
939
  const exportName = config.export ?? "schema";
972
940
  const module = await import(pathToFileURL(schemaPath).href);
@@ -974,16 +942,14 @@ async function codegenFile(config, cwd) {
974
942
  if (!schema || typeof schema !== "object" || !("tables" in schema)) {
975
943
  throw new UsageError(
976
944
  "SCHEMA_NOT_EXPORTED",
977
- `${relative(cwd, schemaPath)} does not export a schema as "${exportName}". Pass --export, or set "export" in zero-swift.json.`
945
+ `${relative(cwd, schemaPath)} does not export a schema as "${exportName}". Pass --export, or set "export" in ${configFile}.`
978
946
  );
979
947
  }
980
- const customTypes = readCustomTypes(schemaPath, exportName);
981
- return generate(
948
+ return {
982
949
  schema,
983
- customTypes,
984
- config,
985
- relative(cwd, schemaPath).replaceAll("\\", "/")
986
- );
950
+ customTypes: readCustomTypes(schemaPath, exportName),
951
+ source: relative(cwd, schemaPath).replaceAll("\\", "/")
952
+ };
987
953
  }
988
954
  async function reporting(fail, body) {
989
955
  try {
@@ -995,15 +961,15 @@ async function reporting(fail, body) {
995
961
  throw e;
996
962
  }
997
963
  }
998
- function writeOutput(fail, output, contents, check2, cwd) {
964
+ function writeOutput(output, contents, check2, cwd) {
999
965
  const path = relative(cwd, output);
1000
966
  const current = existsSync(output) ? readFileSync(output, "utf8") : void 0;
1001
967
  if (check2) {
1002
968
  if (current !== contents) {
1003
- return fail({
1004
- code: "OUT_OF_DATE",
1005
- message: `${path} is out of date. Run the command again without --check to update it.`
1006
- });
969
+ throw new UsageError(
970
+ "OUT_OF_DATE",
971
+ `${path} is out of date. Run the command again without --check to update it.`
972
+ );
1007
973
  }
1008
974
  return { path, upToDate: true };
1009
975
  }
@@ -1035,115 +1001,168 @@ function legacyArgv(argv) {
1035
1001
  }
1036
1002
  return out;
1037
1003
  }
1038
- var check = z.boolean().optional().describe("Fail when the output is out of date instead of writing it");
1039
- var cli = Cli.create("zero-swift", {
1040
- version: package_default.version,
1041
- description: "Generates what the Zero Swift client needs from a Zero app's TypeScript schema, queries, and mutators."
1042
- }).command("codegen", {
1043
- description: "Generate the Swift row types, schema, and query builder from the app's TypeScript schema. Options override zero-swift.json, which is read when present. See swift/Sources/Zero/Zero.docc/GettingStarted.md in github.com/wevm/zero.",
1044
- options: z.object({
1045
- "config": z.string().optional().describe("Config file (default zero-swift.json, when present)"),
1046
- "schema-file": z.string().optional().describe("TS schema module (default schema.ts)"),
1047
- "export": z.string().optional().describe("Export of the module that holds the schema (default schema)"),
1048
- "out": z.string().optional().describe("Swift file to write (default Schema.swift)"),
1049
- "schema-name": z.string().optional().describe("Name of the generated ZeroSchema static (default app)"),
1050
- "zql": z.string().optional().describe("Name of the generated query builder global (default zql)"),
1051
- "type-prefix": z.string().optional().describe("Prefix for every generated type name (default none)"),
1052
- "access": z.enum(["public", "internal"]).optional().describe("Access level of the generated code (default public)"),
1053
- check
1054
- }),
1055
- // incur 0.7.1 prints a boolean example option as `--check true`, so the
1056
- // hint shows a bare `--check` instead.
1057
- examples: [
1058
- { description: "Generate from zero-swift.json" },
1059
- {
1060
- options: {
1061
- "schema-file": "shared/schema.ts",
1062
- "out": "Sources/App/Schema.swift"
1063
- },
1064
- description: "Generate without a config file"
1065
- }
1066
- ],
1067
- hint: "In CI, zero-swift codegen --check fails when the output is stale.",
1068
- run: (c) => reporting(c.error, async () => {
1069
- const cwd = process.cwd();
1070
- const {
1071
- "config": configOption,
1072
- "schema-file": schemaFile,
1073
- "schema-name": schemaName,
1074
- "type-prefix": typePrefix,
1075
- out,
1076
- check: check2,
1077
- ...overrides
1078
- } = c.options;
1079
- const configPath = configOption ?? (existsSync(resolve2(cwd, "zero-swift.json")) ? "zero-swift.json" : void 0);
1080
- if (configPath) {
1081
- mustExist(
1082
- resolve2(cwd, configPath),
1083
- "CONFIG_NOT_FOUND",
1084
- `Config ${configPath} doesn't exist.`
1085
- );
1086
- }
1087
- const fileConfig = configPath ? JSON.parse(readFileSync(resolve2(cwd, configPath), "utf8")) : {};
1088
- const config = {
1089
- ...fileConfig,
1090
- ...Object.fromEntries(
1091
- Object.entries({ ...overrides, schemaName, typePrefix }).filter(
1092
- ([, v]) => v !== void 0
1093
- )
1004
+ var check = z2.boolean().optional().describe("Fail when the output is out of date instead of writing it");
1005
+ function createSwiftCli(version) {
1006
+ const cli2 = Cli.create("zero-swift", {
1007
+ version,
1008
+ description: "Generates what the Zero Swift client needs from a Zero app's TypeScript schema, queries, and mutators."
1009
+ }).command("codegen", {
1010
+ description: "Generate the Swift row types, schema, and query builder from the app's TypeScript schema. Options override zero-swift.json, which is read when present. See swift/Sources/Zero/Zero.docc/GettingStarted.md in github.com/wevm/zero.",
1011
+ options: z2.object({
1012
+ "config": z2.string().optional().describe("Config file (default zero-swift.json, when present)"),
1013
+ "schema-file": z2.string().optional().describe("TS schema module (default schema.ts)"),
1014
+ "export": z2.string().optional().describe(
1015
+ "Export of the module that holds the schema (default schema)"
1094
1016
  ),
1095
- ...schemaFile ? { schema: schemaFile } : {},
1096
- ...out ? { output: out } : {}
1097
- };
1098
- const swift = await codegenFile(config, cwd);
1099
- return writeOutput(
1100
- c.error,
1101
- resolve2(cwd, config.output ?? "Schema.swift"),
1102
- swift,
1103
- check2 ?? false,
1104
- cwd
1105
- );
1106
- })
1107
- });
1108
- cli.command(
1109
- Cli.create("drift", {
1110
- description: "Record what the TS queries and mutators do, for the Swift drift test (Drift.failures in ZeroTesting)."
1111
- }).command("record", {
1112
- description: "Run the app's TS queries and mutators on the samples a module exports, and write what they did.",
1113
- options: z.object({
1114
- samples: z.string().optional().describe("Samples module (default drift-samples.ts)"),
1115
- out: z.string().optional().describe("Vectors file to write (default drift-vectors.json)"),
1017
+ "out": z2.string().optional().describe("Swift file to write (default Schema.swift)"),
1018
+ ...options,
1116
1019
  check
1117
1020
  }),
1021
+ // incur 0.7.1 prints a boolean example option as `--check true`, so the
1022
+ // hint shows a bare `--check` instead.
1118
1023
  examples: [
1024
+ { description: "Generate from zero-swift.json" },
1119
1025
  {
1120
1026
  options: {
1121
- samples: "drift-samples.ts",
1122
- out: "Tests/AppTests/drift-vectors.json"
1027
+ "schema-file": "shared/schema.ts",
1028
+ "out": "Sources/App/Schema.swift"
1123
1029
  },
1124
- description: "Record the drift vectors"
1030
+ description: "Generate without a config file"
1125
1031
  }
1126
1032
  ],
1033
+ hint: "In CI, zero-swift codegen --check fails when the output is stale.",
1127
1034
  run: (c) => reporting(c.error, async () => {
1128
1035
  const cwd = process.cwd();
1129
- const samples = resolve2(cwd, c.options.samples ?? "drift-samples.ts");
1130
- mustExist(
1131
- samples,
1132
- "SAMPLES_NOT_FOUND",
1133
- `Samples file ${relative(cwd, samples)} doesn't exist. Pass --samples.`
1134
- );
1135
- const { recordDriftFile } = await import("./drift-WXDTDUO2.js");
1136
- const vectors = await recordDriftFile(samples);
1036
+ const {
1037
+ "config": configOption,
1038
+ "schema-file": schemaFile,
1039
+ out
1040
+ } = c.options;
1041
+ const configPath = configOption ?? (existsSync(resolve2(cwd, "zero-swift.json")) ? "zero-swift.json" : void 0);
1042
+ if (configPath) {
1043
+ mustExist(
1044
+ resolve2(cwd, configPath),
1045
+ "CONFIG_NOT_FOUND",
1046
+ `Config ${configPath} doesn't exist.`
1047
+ );
1048
+ }
1049
+ const fileConfig = configPath ? JSON.parse(
1050
+ readFileSync(resolve2(cwd, configPath), "utf8")
1051
+ ) : {};
1052
+ const config = {
1053
+ ...fileConfig,
1054
+ ...overrides(c.options),
1055
+ ...c.options.export !== void 0 ? { export: c.options.export } : {},
1056
+ ...schemaFile ? { schema: schemaFile } : {},
1057
+ ...out ? { output: out } : {}
1058
+ };
1137
1059
  return writeOutput(
1138
- c.error,
1139
- resolve2(cwd, c.options.out ?? "drift-vectors.json"),
1140
- vectors,
1060
+ resolve2(cwd, config.output ?? "Schema.swift"),
1061
+ await codegenFile(config, cwd),
1141
1062
  c.options.check ?? false,
1142
1063
  cwd
1143
1064
  );
1144
1065
  })
1145
- })
1146
- );
1066
+ });
1067
+ cli2.command(
1068
+ Cli.create("drift", {
1069
+ description: "Record what the TS queries and mutators do, for the Swift drift test (Drift.failures in ZeroTesting)."
1070
+ }).command("record", {
1071
+ description: "Run the app's TS queries and mutators on the samples a module exports, and write what they did.",
1072
+ options: z2.object({
1073
+ samples: z2.string().optional().describe("Samples module (default drift-samples.ts)"),
1074
+ out: z2.string().optional().describe("Vectors file to write (default drift-vectors.json)"),
1075
+ check
1076
+ }),
1077
+ examples: [
1078
+ {
1079
+ options: {
1080
+ samples: "drift-samples.ts",
1081
+ out: "Tests/AppTests/drift-vectors.json"
1082
+ },
1083
+ description: "Record the drift vectors"
1084
+ }
1085
+ ],
1086
+ run: (c) => reporting(c.error, async () => {
1087
+ const cwd = process.cwd();
1088
+ const samples = resolve2(cwd, c.options.samples ?? "drift-samples.ts");
1089
+ mustExist(
1090
+ samples,
1091
+ "SAMPLES_NOT_FOUND",
1092
+ `Samples file ${relative(cwd, samples)} doesn't exist. Pass --samples.`
1093
+ );
1094
+ const { recordDriftFile } = await import("./drift-EQMCX3ZK.js");
1095
+ const vectors = await recordDriftFile(samples);
1096
+ return writeOutput(
1097
+ resolve2(cwd, c.options.out ?? "drift-vectors.json"),
1098
+ vectors,
1099
+ c.options.check ?? false,
1100
+ cwd
1101
+ );
1102
+ })
1103
+ })
1104
+ );
1105
+ return cli2;
1106
+ }
1107
+
1108
+ // package.json
1109
+ var package_default = {
1110
+ name: "zero-swift",
1111
+ version: "0.7.12",
1112
+ description: "Generates the Swift row types and schema of a Zero app for the Zero Swift client.",
1113
+ license: "Apache-2.0",
1114
+ repository: {
1115
+ type: "git",
1116
+ url: "git+https://github.com/wevm/zero.git",
1117
+ directory: "packages/zero-swift-cli"
1118
+ },
1119
+ bin: {
1120
+ "zero-swift": "./src/bin.ts"
1121
+ },
1122
+ files: [
1123
+ "dist"
1124
+ ],
1125
+ type: "module",
1126
+ publishConfig: {
1127
+ bin: {
1128
+ "zero-swift": "./dist/bin.js"
1129
+ }
1130
+ },
1131
+ scripts: {
1132
+ build: "esbuild src/bin.ts --bundle --platform=node --format=esm --target=node22 --splitting --outdir=dist --external:@rocicorp/zero --external:typescript --external:incur",
1133
+ test: "vitest run",
1134
+ smoke: "node test/packed.ts",
1135
+ generate: "UPDATE_GENERATED=1 vitest run",
1136
+ "check-types": "tsc",
1137
+ lint: "oxlint --quiet --config ../../oxlint.config.ts src/",
1138
+ format: "oxfmt .",
1139
+ "check-format": "oxfmt --check .",
1140
+ "version-packages": "cd ../.. && pnpm dlx @changesets/cli@3.0.3 version && jq -r .version packages/zero-swift-cli/package.json > swift/VERSION"
1141
+ },
1142
+ dependencies: {
1143
+ incur: "0.7.1",
1144
+ typescript: "6.0.3"
1145
+ },
1146
+ devDependencies: {
1147
+ "@rocicorp/zero": "workspace:*",
1148
+ "@types/node": "^22.19.19",
1149
+ esbuild: "0.28.2",
1150
+ oxfmt: "^0.74.0",
1151
+ vitest: "^5.0.5",
1152
+ "zero-schema": "workspace:*",
1153
+ zerogen: "workspace:*",
1154
+ zql: "workspace:*"
1155
+ },
1156
+ peerDependencies: {
1157
+ "@rocicorp/zero": ">=1.11.0"
1158
+ },
1159
+ engines: {
1160
+ node: ">=22.18"
1161
+ }
1162
+ };
1163
+
1164
+ // src/cli.ts
1165
+ var cli = createSwiftCli(package_default.version);
1147
1166
 
1148
1167
  // src/bin.ts
1149
1168
  await cli.serve(legacyArgv(process.argv.slice(2)));
@@ -1,4 +1,4 @@
1
- // src/drift.ts
1
+ // ../zerogen/src/drift.ts
2
2
  import { pathToFileURL } from "node:url";
3
3
  import {
4
4
  createBuilder,
@@ -2185,7 +2185,7 @@ function hashOfNormalizedAST(ast) {
2185
2185
  return getOrInsertComputed(hashCache, ast, hashAST);
2186
2186
  }
2187
2187
 
2188
- // src/drift.ts
2188
+ // ../zerogen/src/drift.ts
2189
2189
  var seedName = "__drift.seed";
2190
2190
  var dayMs = 24 * 60 * 60 * 1e3;
2191
2191
  var offlineWarning = "Zero starting up with no server URL. No data will be synced.";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zero-swift",
3
- "version": "0.7.10",
3
+ "version": "0.7.12",
4
4
  "description": "Generates the Swift row types and schema of a Zero app for the Zero Swift client.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -26,8 +26,8 @@
26
26
  "vitest": "^5.0.5",
27
27
  "@rocicorp/zero": "1.12.0",
28
28
  "zero-schema": "0.0.0",
29
- "zero-types": "0.0.0",
30
- "zql": "0.0.0"
29
+ "zql": "0.0.0",
30
+ "zerogen": "0.0.0"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@rocicorp/zero": ">=1.11.0"
@@ -38,6 +38,7 @@
38
38
  "scripts": {
39
39
  "build": "esbuild src/bin.ts --bundle --platform=node --format=esm --target=node22 --splitting --outdir=dist --external:@rocicorp/zero --external:typescript --external:incur",
40
40
  "test": "vitest run",
41
+ "smoke": "node test/packed.ts",
41
42
  "generate": "UPDATE_GENERATED=1 vitest run",
42
43
  "check-types": "tsc",
43
44
  "lint": "oxlint --quiet --config ../../oxlint.config.ts src/",