apify-schema-tools 3.1.0 → 3.2.1
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/.node-version +1 -1
- package/CHANGELOG.md +7 -1
- package/biome.json +8 -2
- package/dist/apify-schema-tools.js +12 -9
- package/dist/apify-schema-tools.js.map +1 -1
- package/dist/apify.d.ts +1 -1
- package/dist/apify.d.ts.map +1 -1
- package/dist/apify.js +19 -5
- package/dist/apify.js.map +1 -1
- package/dist/cli/check.d.ts +5 -0
- package/dist/cli/check.d.ts.map +1 -0
- package/dist/cli/check.js +86 -0
- package/dist/cli/check.js.map +1 -0
- package/dist/cli/init.d.ts +5 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +92 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/sync.d.ts +5 -0
- package/dist/cli/sync.d.ts.map +1 -0
- package/dist/cli/sync.js +112 -0
- package/dist/cli/sync.js.map +1 -0
- package/dist/configuration.d.ts +14 -5
- package/dist/configuration.d.ts.map +1 -1
- package/dist/configuration.js +9 -5
- package/dist/configuration.js.map +1 -1
- package/dist/main.d.ts +4 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +19 -0
- package/dist/main.js.map +1 -0
- package/dist/middle-schema/compare-schemas.d.ts +3 -0
- package/dist/middle-schema/compare-schemas.d.ts.map +1 -0
- package/dist/middle-schema/compare-schemas.js +90 -0
- package/dist/middle-schema/compare-schemas.js.map +1 -0
- package/dist/middle-schema/generate-typescript.d.ts +7 -0
- package/dist/middle-schema/generate-typescript.d.ts.map +1 -0
- package/dist/middle-schema/generate-typescript.js +70 -0
- package/dist/middle-schema/generate-typescript.js.map +1 -0
- package/dist/middle-schema/parse-json-schema.d.ts +4 -0
- package/dist/middle-schema/parse-json-schema.d.ts.map +1 -0
- package/dist/middle-schema/parse-json-schema.js +65 -0
- package/dist/middle-schema/parse-json-schema.js.map +1 -0
- package/dist/middle-schema/parse-typescript.d.ts +4 -0
- package/dist/middle-schema/parse-typescript.d.ts.map +1 -0
- package/dist/middle-schema/parse-typescript.js +199 -0
- package/dist/middle-schema/parse-typescript.js.map +1 -0
- package/dist/middle-schema/schema-types.d.ts +24 -0
- package/dist/middle-schema/schema-types.d.ts.map +1 -0
- package/dist/middle-schema/schema-types.js +14 -0
- package/dist/middle-schema/schema-types.js.map +1 -0
- package/dist/middle-schema/schema.d.ts +24 -0
- package/dist/middle-schema/schema.d.ts.map +1 -0
- package/dist/middle-schema/schema.js +14 -0
- package/dist/middle-schema/schema.js.map +1 -0
- package/dist/schema/entities/abstract-entity.d.ts +5 -0
- package/dist/schema/entities/abstract-entity.d.ts.map +1 -0
- package/dist/schema/entities/abstract-entity.js +3 -0
- package/dist/schema/entities/abstract-entity.js.map +1 -0
- package/dist/schema/entities/primitive-union.d.ts +12 -0
- package/dist/schema/entities/primitive-union.d.ts.map +1 -0
- package/dist/schema/entities/primitive-union.js +74 -0
- package/dist/schema/entities/primitive-union.js.map +1 -0
- package/dist/schema/entities/primitive.d.ts +15 -0
- package/dist/schema/entities/primitive.d.ts.map +1 -0
- package/dist/schema/entities/primitive.js +54 -0
- package/dist/schema/entities/primitive.js.map +1 -0
- package/dist/schema/parsers/json-schema.d.ts +4 -0
- package/dist/schema/parsers/json-schema.d.ts.map +1 -0
- package/dist/schema/parsers/json-schema.js +12 -0
- package/dist/schema/parsers/json-schema.js.map +1 -0
- package/dist/schema/parsers/typescript.d.ts +3 -0
- package/dist/schema/parsers/typescript.d.ts.map +1 -0
- package/dist/schema/parsers/typescript.js +24 -0
- package/dist/schema/parsers/typescript.js.map +1 -0
- package/dist/schemas/input.d.ts +840 -0
- package/dist/schemas/input.d.ts.map +1 -0
- package/dist/schemas/input.js +349 -0
- package/dist/schemas/input.js.map +1 -0
- package/dist/utils/filesystem.d.ts +8 -0
- package/dist/utils/filesystem.d.ts.map +1 -0
- package/dist/utils/filesystem.js +16 -0
- package/dist/utils/filesystem.js.map +1 -0
- package/dist/utils/json-schemas-interactive-conflict.d.ts +16 -0
- package/dist/utils/json-schemas-interactive-conflict.d.ts.map +1 -0
- package/dist/utils/json-schemas-interactive-conflict.js +165 -0
- package/dist/utils/json-schemas-interactive-conflict.js.map +1 -0
- package/dist/utils/json-schemas.d.ts +42 -0
- package/dist/utils/json-schemas.d.ts.map +1 -0
- package/dist/utils/json-schemas.js +162 -0
- package/dist/utils/json-schemas.js.map +1 -0
- package/dist/zod/schemas/input.d.ts +840 -0
- package/dist/zod/schemas/input.d.ts.map +1 -0
- package/dist/zod/schemas/input.js +393 -0
- package/dist/zod/schemas/input.js.map +1 -0
- package/package.json +12 -12
- package/samples/all-defaults/.actor/input_schema.json +32 -3
- package/samples/all-defaults/src-schemas/input.json +2 -1
- package/samples/deep-merged-schemas/.actor/input_schema.json +36 -3
- package/samples/merged-schemas/.actor/input_schema.json +27 -3
- package/samples/package-json-config/.actor/input_schema.json +32 -3
- package/samples/package-json-config-merged/.actor/input_schema.json +36 -3
- package/src/apify.ts +21 -6
- package/src/cli/check.ts +114 -0
- package/src/cli/init.ts +125 -0
- package/src/cli/sync.ts +164 -0
- package/src/configuration.ts +17 -7
- package/src/main.ts +25 -0
- package/src/middle-schema/compare-schemas.ts +113 -0
- package/src/middle-schema/generate-typescript.ts +88 -0
- package/src/middle-schema/parse-json-schema.ts +104 -0
- package/src/middle-schema/parse-typescript.ts +239 -0
- package/src/middle-schema/schema-types.ts +40 -0
- package/test/apify.test.ts +410 -2
- package/test/cli/check.test.ts +1571 -0
- package/test/cli/init.test.ts +459 -0
- package/test/cli/sync.test.ts +341 -0
- package/test/common.ts +68 -0
- package/test/configuration.test.ts +8 -8
- package/test/middle-schema/compare-schemas.test.ts +585 -0
- package/test/middle-schema/generate-typescript.test.ts +191 -0
- package/test/middle-schema/parse-json-schema.test.ts +178 -0
- package/test/middle-schema/parse-typescript.test.ts +143 -0
- package/test/{json-schema-conflicts.test.ts → utils/json-schemas-interactive-conflict.test.ts} +2 -2
- package/test/{json-schemas.test.ts → utils/json-schemas.test.ts} +3 -3
- package/src/apify-schema-tools.ts +0 -420
- package/src/typescript.ts +0 -563
- package/test/apify-schema-tools.test.ts +0 -2216
- package/test/typescript.test.ts +0 -1079
- /package/src/{filesystem.ts → utils/filesystem.ts} +0 -0
- /package/src/{json-schema-conflicts.ts → utils/json-schemas-interactive-conflict.ts} +0 -0
- /package/src/{json-schemas.ts → utils/json-schemas.ts} +0 -0
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/** biome-ignore-all lint/style/useNamingConvention: the package `argparse` uses snake_case names */
|
|
4
|
-
|
|
5
|
-
import { existsSync } from "node:fs";
|
|
6
|
-
import { join } from "node:path";
|
|
7
|
-
import { ArgumentDefaultsHelpFormatter, ArgumentParser } from "argparse";
|
|
8
|
-
import {
|
|
9
|
-
ACTOR_CONFIG_PATH,
|
|
10
|
-
DATASET_SCHEMA_FIELD,
|
|
11
|
-
DESCRIPTION_FIELDS,
|
|
12
|
-
filterValidInputSchemaProperties,
|
|
13
|
-
generateInputDefaultsTsFileContent,
|
|
14
|
-
getPathRelativeToActorConfig,
|
|
15
|
-
} from "./apify.js";
|
|
16
|
-
import {
|
|
17
|
-
addCommonCliArgs,
|
|
18
|
-
type CommonCliArgs,
|
|
19
|
-
parseConfigurationFromFileOrDefault,
|
|
20
|
-
writeConfigurationToPackageJson,
|
|
21
|
-
} from "./configuration.js";
|
|
22
|
-
import { readFile, writeFile } from "./filesystem.js";
|
|
23
|
-
import { type ConflictResolutionStrategy, checkConflicts } from "./json-schema-conflicts.js";
|
|
24
|
-
import {
|
|
25
|
-
compareSchemas,
|
|
26
|
-
mergeObjectSchemas,
|
|
27
|
-
type ObjectSchema,
|
|
28
|
-
readJsonSchema,
|
|
29
|
-
readJsonSchemaField,
|
|
30
|
-
writeJsonSchema,
|
|
31
|
-
writeSchemaToField,
|
|
32
|
-
} from "./json-schemas.js";
|
|
33
|
-
import {
|
|
34
|
-
compareTypescriptInterfaces,
|
|
35
|
-
jsonSchemaToTypeScriptInterface,
|
|
36
|
-
parseTypeScriptInterface,
|
|
37
|
-
removeTypeScriptHeader,
|
|
38
|
-
serializeTypeScriptInterface,
|
|
39
|
-
writeTypeScriptFile,
|
|
40
|
-
} from "./typescript.js";
|
|
41
|
-
|
|
42
|
-
const configuration = parseConfigurationFromFileOrDefault();
|
|
43
|
-
|
|
44
|
-
const rootParser = new ArgumentParser({
|
|
45
|
-
description: "Apify Schema Tools - Generate JSON schemas and TypeScript files for Actor input and output dataset.",
|
|
46
|
-
formatter_class: ArgumentDefaultsHelpFormatter,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Create subparsers for commands, and define arguments shared by multiple commands
|
|
50
|
-
|
|
51
|
-
const subparsers = rootParser.add_subparsers();
|
|
52
|
-
|
|
53
|
-
// init command
|
|
54
|
-
|
|
55
|
-
interface InitArgs extends CommonCliArgs {
|
|
56
|
-
no_config_file: boolean;
|
|
57
|
-
only_config_file: boolean;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const initParser = subparsers.add_parser("init", {
|
|
61
|
-
help: "Initialize the Apify Schema Tools project with default settings.",
|
|
62
|
-
formatter_class: ArgumentDefaultsHelpFormatter,
|
|
63
|
-
});
|
|
64
|
-
addCommonCliArgs(initParser, configuration);
|
|
65
|
-
initParser.add_argument("--no-config-file", {
|
|
66
|
-
help: "do not create a configuration file in package.json",
|
|
67
|
-
action: "store_true",
|
|
68
|
-
default: false,
|
|
69
|
-
});
|
|
70
|
-
initParser.add_argument("--only-config-file", {
|
|
71
|
-
help: "create only the configuration file in package.json, without initializing schemas",
|
|
72
|
-
action: "store_true",
|
|
73
|
-
default: false,
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
function initializeInputSchema(args: InitArgs): void {
|
|
77
|
-
if (existsSync(args.src_input)) {
|
|
78
|
-
console.log(`Input schema already exists at ${args.src_input}, skipping initialization.`);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (!existsSync(args.input_schema)) {
|
|
82
|
-
throw new Error("The current Actor does not have an input schema.");
|
|
83
|
-
}
|
|
84
|
-
const inputSchema = readJsonSchema(args.input_schema);
|
|
85
|
-
writeJsonSchema(args.src_input, inputSchema);
|
|
86
|
-
console.log(`Input schema initialized at ${args.src_input}`);
|
|
87
|
-
if (args.add_input) {
|
|
88
|
-
writeJsonSchema(args.add_input, { type: "object", properties: {} });
|
|
89
|
-
console.log(`Additional input schema initialized at ${args.add_input}`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function initializeDatasetSchema(args: InitArgs): void {
|
|
94
|
-
if (!existsSync(ACTOR_CONFIG_PATH)) {
|
|
95
|
-
throw new Error(`The current Actor does not have an ${ACTOR_CONFIG_PATH} configuration file.`);
|
|
96
|
-
}
|
|
97
|
-
const actorConfig = JSON.parse(readFile(ACTOR_CONFIG_PATH));
|
|
98
|
-
const relativeDatasetPath = getPathRelativeToActorConfig(args.dataset_schema);
|
|
99
|
-
if (actorConfig.storages?.dataset !== relativeDatasetPath) {
|
|
100
|
-
writeFile(
|
|
101
|
-
ACTOR_CONFIG_PATH,
|
|
102
|
-
JSON.stringify(
|
|
103
|
-
{
|
|
104
|
-
...actorConfig,
|
|
105
|
-
storages: {
|
|
106
|
-
...actorConfig.storages,
|
|
107
|
-
dataset: relativeDatasetPath,
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
null,
|
|
111
|
-
4,
|
|
112
|
-
),
|
|
113
|
-
);
|
|
114
|
-
console.log(`Updated ${ACTOR_CONFIG_PATH} to use the dataset schema at ${args.dataset_schema}`);
|
|
115
|
-
}
|
|
116
|
-
if (!existsSync(args.dataset_schema)) {
|
|
117
|
-
writeFile(
|
|
118
|
-
args.dataset_schema,
|
|
119
|
-
JSON.stringify(
|
|
120
|
-
{
|
|
121
|
-
actorSpecification: 1,
|
|
122
|
-
[DATASET_SCHEMA_FIELD]: { type: "object", properties: {} },
|
|
123
|
-
},
|
|
124
|
-
null,
|
|
125
|
-
4,
|
|
126
|
-
),
|
|
127
|
-
);
|
|
128
|
-
console.log(`Dataset schema initialized at ${args.dataset_schema}`);
|
|
129
|
-
}
|
|
130
|
-
if (existsSync(args.src_dataset)) {
|
|
131
|
-
console.log(`Dataset schema already exists at ${args.src_dataset}, skipping initialization.`);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
const datasetItemSchema = readJsonSchemaField(args.dataset_schema, DATASET_SCHEMA_FIELD);
|
|
135
|
-
writeJsonSchema(args.src_dataset, datasetItemSchema);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function init(args: InitArgs): void {
|
|
139
|
-
if (args.only_config_file && args.no_config_file) {
|
|
140
|
-
throw new Error("The options --only-config-file and --no-config-file were defined together: doing nothing.");
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
console.log("Initializing Apify Schema Tools in the current project...");
|
|
144
|
-
|
|
145
|
-
if (!args.no_config_file) {
|
|
146
|
-
writeConfigurationToPackageJson(args);
|
|
147
|
-
console.log("Configuration written to package.json");
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (args.only_config_file) {
|
|
151
|
-
console.log("Only configuration file created, skipping schema initialization.");
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (args.input.includes("input")) {
|
|
156
|
-
initializeInputSchema(args);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (args.input.includes("dataset")) {
|
|
160
|
-
initializeDatasetSchema(args);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
initParser.set_defaults({ func: init });
|
|
165
|
-
|
|
166
|
-
// sync command
|
|
167
|
-
|
|
168
|
-
interface SyncArgs extends CommonCliArgs {
|
|
169
|
-
include_input_utils: string;
|
|
170
|
-
force: boolean;
|
|
171
|
-
fail_on_conflict: boolean;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const syncParser = subparsers.add_parser("sync", {
|
|
175
|
-
help: `Generate JSON schemas and TypeScript files from the source schemas. \
|
|
176
|
-
By default, if conflicts are detected, the user will be prompted to resolve them.`,
|
|
177
|
-
formatter_class: ArgumentDefaultsHelpFormatter,
|
|
178
|
-
});
|
|
179
|
-
addCommonCliArgs(syncParser, configuration);
|
|
180
|
-
syncParser.add_argument("--include-input-utils", {
|
|
181
|
-
help: "include input utilities in the generated TypeScript files: 'input' input and 'ts-types' output are required",
|
|
182
|
-
choices: ["true", "false"],
|
|
183
|
-
default: "true",
|
|
184
|
-
});
|
|
185
|
-
syncParser.add_argument("--force", {
|
|
186
|
-
help: "force the sync operation, even if conflicts are detected",
|
|
187
|
-
action: "store_true",
|
|
188
|
-
default: false,
|
|
189
|
-
});
|
|
190
|
-
syncParser.add_argument("--fail-on-conflict", {
|
|
191
|
-
help: "fail the sync operation if conflicts are detected",
|
|
192
|
-
action: "store_true",
|
|
193
|
-
default: false,
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
function getConflictResolutionStrategy(args: SyncArgs): ConflictResolutionStrategy {
|
|
197
|
-
if (args.fail_on_conflict && args.force) {
|
|
198
|
-
throw new Error("The options --force and --fail-on-conflict cannot be defined together.");
|
|
199
|
-
}
|
|
200
|
-
if (args.force) {
|
|
201
|
-
return "log";
|
|
202
|
-
}
|
|
203
|
-
if (args.fail_on_conflict) {
|
|
204
|
-
return "error";
|
|
205
|
-
}
|
|
206
|
-
return "interactive";
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
async function handleInputSync(args: SyncArgs): Promise<void> {
|
|
210
|
-
let inputSchemas = {
|
|
211
|
-
sourceSchema: readJsonSchema(args.src_input),
|
|
212
|
-
additionalSchema: args.add_input ? readJsonSchema(args.add_input) : undefined,
|
|
213
|
-
};
|
|
214
|
-
let resultingInputSchema: ObjectSchema | undefined;
|
|
215
|
-
if (args.output.includes("json-schemas")) {
|
|
216
|
-
const conflictResolutionStrategy = getConflictResolutionStrategy(args);
|
|
217
|
-
const existingInputSchema = readJsonSchema(args.input_schema);
|
|
218
|
-
inputSchemas = await checkConflicts(
|
|
219
|
-
inputSchemas.sourceSchema,
|
|
220
|
-
args.src_input,
|
|
221
|
-
inputSchemas.additionalSchema,
|
|
222
|
-
args.add_input,
|
|
223
|
-
existingInputSchema,
|
|
224
|
-
args.deep_merge,
|
|
225
|
-
DESCRIPTION_FIELDS,
|
|
226
|
-
conflictResolutionStrategy,
|
|
227
|
-
);
|
|
228
|
-
resultingInputSchema = inputSchemas.additionalSchema
|
|
229
|
-
? mergeObjectSchemas(inputSchemas.sourceSchema, inputSchemas.additionalSchema, args.deep_merge)
|
|
230
|
-
: inputSchemas.sourceSchema;
|
|
231
|
-
writeJsonSchema(args.input_schema, filterValidInputSchemaProperties(resultingInputSchema));
|
|
232
|
-
}
|
|
233
|
-
if (args.output.includes("ts-types")) {
|
|
234
|
-
if (!resultingInputSchema) {
|
|
235
|
-
resultingInputSchema = inputSchemas.additionalSchema
|
|
236
|
-
? mergeObjectSchemas(inputSchemas.sourceSchema, inputSchemas.additionalSchema, args.deep_merge)
|
|
237
|
-
: inputSchemas.sourceSchema;
|
|
238
|
-
}
|
|
239
|
-
writeTypeScriptFile(
|
|
240
|
-
join(args.output_ts_dir, "input.ts"),
|
|
241
|
-
serializeTypeScriptInterface("Input", jsonSchemaToTypeScriptInterface(resultingInputSchema)),
|
|
242
|
-
);
|
|
243
|
-
if (args.include_input_utils === "true") {
|
|
244
|
-
writeTypeScriptFile(
|
|
245
|
-
join(args.output_ts_dir, "input-utils.ts"),
|
|
246
|
-
generateInputDefaultsTsFileContent(resultingInputSchema),
|
|
247
|
-
);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
// Update the source schemas, which may have been modified by the conflict resolution
|
|
251
|
-
writeJsonSchema(args.src_input, inputSchemas.sourceSchema);
|
|
252
|
-
if (args.add_input && inputSchemas.additionalSchema) {
|
|
253
|
-
writeJsonSchema(args.add_input, inputSchemas.additionalSchema);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async function handleDatasetSync(args: SyncArgs): Promise<void> {
|
|
258
|
-
let datasetSchemas = {
|
|
259
|
-
sourceSchema: readJsonSchema(args.src_dataset),
|
|
260
|
-
additionalSchema: args.add_dataset ? readJsonSchema(args.add_dataset) : undefined,
|
|
261
|
-
};
|
|
262
|
-
let resultingDatasetSchema: ObjectSchema | undefined;
|
|
263
|
-
if (args.output.includes("json-schemas")) {
|
|
264
|
-
const conflictResolutionStrategy = getConflictResolutionStrategy(args);
|
|
265
|
-
const existingDatasetSchema = readJsonSchemaField(args.dataset_schema, DATASET_SCHEMA_FIELD);
|
|
266
|
-
datasetSchemas = await checkConflicts(
|
|
267
|
-
datasetSchemas.sourceSchema,
|
|
268
|
-
args.src_dataset,
|
|
269
|
-
datasetSchemas.additionalSchema,
|
|
270
|
-
args.add_dataset,
|
|
271
|
-
existingDatasetSchema,
|
|
272
|
-
args.deep_merge,
|
|
273
|
-
DESCRIPTION_FIELDS,
|
|
274
|
-
conflictResolutionStrategy,
|
|
275
|
-
);
|
|
276
|
-
resultingDatasetSchema = datasetSchemas.additionalSchema
|
|
277
|
-
? mergeObjectSchemas(datasetSchemas.sourceSchema, datasetSchemas.additionalSchema, args.deep_merge)
|
|
278
|
-
: datasetSchemas.sourceSchema;
|
|
279
|
-
writeSchemaToField(args.dataset_schema, resultingDatasetSchema, DATASET_SCHEMA_FIELD);
|
|
280
|
-
}
|
|
281
|
-
if (args.output.includes("ts-types")) {
|
|
282
|
-
if (!resultingDatasetSchema) {
|
|
283
|
-
resultingDatasetSchema = datasetSchemas.additionalSchema
|
|
284
|
-
? mergeObjectSchemas(datasetSchemas.sourceSchema, datasetSchemas.additionalSchema, args.deep_merge)
|
|
285
|
-
: datasetSchemas.sourceSchema;
|
|
286
|
-
}
|
|
287
|
-
writeTypeScriptFile(
|
|
288
|
-
join(args.output_ts_dir, "dataset.ts"),
|
|
289
|
-
serializeTypeScriptInterface("DatasetItem", jsonSchemaToTypeScriptInterface(resultingDatasetSchema)),
|
|
290
|
-
);
|
|
291
|
-
}
|
|
292
|
-
// Update the source schemas, which may have been modified by the conflict resolution
|
|
293
|
-
writeJsonSchema(args.src_dataset, datasetSchemas.sourceSchema);
|
|
294
|
-
if (args.add_dataset && datasetSchemas.additionalSchema) {
|
|
295
|
-
writeJsonSchema(args.add_dataset, datasetSchemas.additionalSchema);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
function sync(args: SyncArgs): void {
|
|
300
|
-
console.log("Syncing schemas...");
|
|
301
|
-
|
|
302
|
-
if (args.input.includes("input")) {
|
|
303
|
-
handleInputSync(args);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (args.input.includes("dataset")) {
|
|
307
|
-
handleDatasetSync(args);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
syncParser.set_defaults({ func: sync });
|
|
312
|
-
|
|
313
|
-
// check command
|
|
314
|
-
|
|
315
|
-
interface CheckArgs extends CommonCliArgs {
|
|
316
|
-
ignore_descriptions: boolean;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
const checkParser = subparsers.add_parser("check", {
|
|
320
|
-
help: "Check the schemas for consistency and correctness. (WARNING: input utils will not be checked!)",
|
|
321
|
-
formatter_class: ArgumentDefaultsHelpFormatter,
|
|
322
|
-
});
|
|
323
|
-
addCommonCliArgs(checkParser, configuration);
|
|
324
|
-
checkParser.add_argument("--ignore-descriptions", {
|
|
325
|
-
help: "ignore the 'title' and 'description' fields during the comparison",
|
|
326
|
-
action: "store_true",
|
|
327
|
-
default: false,
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
function checkInputSchemas(args: CheckArgs): void {
|
|
331
|
-
let sourceInputSchema = readJsonSchema(args.src_input);
|
|
332
|
-
if (args.add_input) {
|
|
333
|
-
sourceInputSchema = mergeObjectSchemas(sourceInputSchema, readJsonSchema(args.add_input), args.deep_merge);
|
|
334
|
-
}
|
|
335
|
-
const generatedInputSchema = filterValidInputSchemaProperties(sourceInputSchema);
|
|
336
|
-
if (args.output.includes("json-schemas")) {
|
|
337
|
-
const outputInputSchema = readJsonSchema(args.input_schema);
|
|
338
|
-
const isInputSchemaCoherent = compareSchemas(
|
|
339
|
-
generatedInputSchema,
|
|
340
|
-
outputInputSchema,
|
|
341
|
-
args.ignore_descriptions ? DESCRIPTION_FIELDS : undefined,
|
|
342
|
-
);
|
|
343
|
-
if (!isInputSchemaCoherent) {
|
|
344
|
-
console.error("Input schema validation failed: the input schema does not match the source schema.");
|
|
345
|
-
process.exit(1);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
if (args.output.includes("ts-types")) {
|
|
349
|
-
const inputTsContent = readFile(join(args.output_ts_dir, "input.ts"));
|
|
350
|
-
const inputTsInterface = parseTypeScriptInterface(removeTypeScriptHeader(inputTsContent));
|
|
351
|
-
const sourceInputTsInterface = jsonSchemaToTypeScriptInterface(sourceInputSchema);
|
|
352
|
-
const isTsInputCoherent = compareTypescriptInterfaces(
|
|
353
|
-
sourceInputTsInterface,
|
|
354
|
-
inputTsInterface,
|
|
355
|
-
args.ignore_descriptions,
|
|
356
|
-
);
|
|
357
|
-
if (!isTsInputCoherent) {
|
|
358
|
-
console.error(
|
|
359
|
-
"Input TypeScript interface validation failed: the TypeScript interface does not match the source schema.",
|
|
360
|
-
);
|
|
361
|
-
process.exit(1);
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
function checkDatasetSchemas(args: CheckArgs): void {
|
|
367
|
-
let sourceDatasetSchema = readJsonSchema(args.src_dataset);
|
|
368
|
-
if (args.add_dataset) {
|
|
369
|
-
sourceDatasetSchema = mergeObjectSchemas(sourceDatasetSchema, readJsonSchema(args.add_dataset), args.deep_merge);
|
|
370
|
-
}
|
|
371
|
-
if (args.output.includes("json-schemas")) {
|
|
372
|
-
const outputDatasetSchema = readJsonSchemaField(args.dataset_schema, DATASET_SCHEMA_FIELD);
|
|
373
|
-
if (!outputDatasetSchema) {
|
|
374
|
-
console.error(`Dataset schema field "${DATASET_SCHEMA_FIELD}" not found in ${args.dataset_schema}.`);
|
|
375
|
-
process.exit(1);
|
|
376
|
-
}
|
|
377
|
-
const isDatasetSchemaCoherent = compareSchemas(
|
|
378
|
-
sourceDatasetSchema,
|
|
379
|
-
outputDatasetSchema,
|
|
380
|
-
args.ignore_descriptions ? DESCRIPTION_FIELDS : undefined,
|
|
381
|
-
);
|
|
382
|
-
if (!isDatasetSchemaCoherent) {
|
|
383
|
-
console.error("Dataset schema validation failed: the dataset schema does not match the source schema.");
|
|
384
|
-
process.exit(1);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
if (args.output.includes("ts-types")) {
|
|
388
|
-
const datasetTsContent = readFile(join(args.output_ts_dir, "dataset.ts"));
|
|
389
|
-
const datasetTsInterface = parseTypeScriptInterface(removeTypeScriptHeader(datasetTsContent));
|
|
390
|
-
const sourceDatasetTsInterface = jsonSchemaToTypeScriptInterface(sourceDatasetSchema);
|
|
391
|
-
const isTsDatasetCoherent = compareTypescriptInterfaces(
|
|
392
|
-
sourceDatasetTsInterface,
|
|
393
|
-
datasetTsInterface,
|
|
394
|
-
args.ignore_descriptions,
|
|
395
|
-
);
|
|
396
|
-
if (!isTsDatasetCoherent) {
|
|
397
|
-
console.error(
|
|
398
|
-
"Dataset TypeScript interface validation failed: the TypeScript interface does not match the source schema.",
|
|
399
|
-
);
|
|
400
|
-
process.exit(1);
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
function check(args: CheckArgs): void {
|
|
406
|
-
if (args.input.includes("input")) {
|
|
407
|
-
checkInputSchemas(args);
|
|
408
|
-
}
|
|
409
|
-
if (args.input.includes("dataset")) {
|
|
410
|
-
checkDatasetSchemas(args);
|
|
411
|
-
}
|
|
412
|
-
console.log("Check passed.");
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
checkParser.set_defaults({ func: check });
|
|
416
|
-
|
|
417
|
-
// Parse and execute
|
|
418
|
-
|
|
419
|
-
const parsedArgs = rootParser.parse_args();
|
|
420
|
-
parsedArgs.func(parsedArgs);
|