@postxl/cli 1.8.2 → 1.8.3
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.
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { Command } from 'commander';
|
|
2
2
|
import { ProjectSchema } from '@postxl/schema';
|
|
3
|
+
/**
|
|
4
|
+
* Loads `postxl-schema.json` from the given project path and merges in any
|
|
5
|
+
* per-model / per-enum files under `schema/`. Returns the unvalidated merged
|
|
6
|
+
* JSON object — callers run their own `safeParse` so they can format errors
|
|
7
|
+
* however they like.
|
|
8
|
+
*
|
|
9
|
+
* Errors during file IO / JSON parsing call `program.error`, which exits.
|
|
10
|
+
*/
|
|
11
|
+
export declare function loadAndMergeRawSchema(params: {
|
|
12
|
+
projectPath: string;
|
|
13
|
+
program: Command;
|
|
14
|
+
}): Promise<Record<string, unknown>>;
|
|
3
15
|
/**
|
|
4
16
|
* Loads `postxl-schema.json` from the given project path, merges in any
|
|
5
17
|
* per-model / per-enum files under `schema/`, and validates the result
|
|
@@ -33,6 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.loadAndMergeRawSchema = loadAndMergeRawSchema;
|
|
36
37
|
exports.loadProjectSchema = loadProjectSchema;
|
|
37
38
|
const fs = __importStar(require("node:fs/promises"));
|
|
38
39
|
const path = __importStar(require("node:path"));
|
|
@@ -40,14 +41,14 @@ const schema_1 = require("@postxl/schema");
|
|
|
40
41
|
const load_split_schema_files_1 = require("./load-split-schema-files");
|
|
41
42
|
const log_schema_error_1 = require("./log-schema-error");
|
|
42
43
|
/**
|
|
43
|
-
* Loads `postxl-schema.json` from the given project path
|
|
44
|
-
* per-model / per-enum files under `schema
|
|
45
|
-
*
|
|
44
|
+
* Loads `postxl-schema.json` from the given project path and merges in any
|
|
45
|
+
* per-model / per-enum files under `schema/`. Returns the unvalidated merged
|
|
46
|
+
* JSON object — callers run their own `safeParse` so they can format errors
|
|
47
|
+
* however they like.
|
|
46
48
|
*
|
|
47
|
-
*
|
|
48
|
-
* preserves the behavior of the previous in-command schema loader.
|
|
49
|
+
* Errors during file IO / JSON parsing call `program.error`, which exits.
|
|
49
50
|
*/
|
|
50
|
-
async function
|
|
51
|
+
async function loadAndMergeRawSchema(params) {
|
|
51
52
|
const { projectPath, program } = params;
|
|
52
53
|
const schemaPath = path.join(projectPath, 'postxl-schema.json');
|
|
53
54
|
try {
|
|
@@ -72,7 +73,7 @@ async function loadProjectSchema(params) {
|
|
|
72
73
|
...(modelFilesGlob ? { modelFilesGlob } : {}),
|
|
73
74
|
...(enumFilesGlob ? { enumFilesGlob } : {}),
|
|
74
75
|
});
|
|
75
|
-
|
|
76
|
+
return {
|
|
76
77
|
...rawJson,
|
|
77
78
|
models: (0, schema_1.mergeSplitSchemaEntries)({
|
|
78
79
|
inline: rawJson.models,
|
|
@@ -87,6 +88,17 @@ async function loadProjectSchema(params) {
|
|
|
87
88
|
fromFilesPaths: toPathMap(split.enums),
|
|
88
89
|
}),
|
|
89
90
|
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Loads `postxl-schema.json` from the given project path, merges in any
|
|
94
|
+
* per-model / per-enum files under `schema/`, and validates the result
|
|
95
|
+
* against `zProjectSchema`.
|
|
96
|
+
*
|
|
97
|
+
* On validation error, logs the formatted Zod error and exits the process —
|
|
98
|
+
* preserves the behavior of the previous in-command schema loader.
|
|
99
|
+
*/
|
|
100
|
+
async function loadProjectSchema(params) {
|
|
101
|
+
const mergedInput = await loadAndMergeRawSchema(params);
|
|
90
102
|
const result = schema_1.zProjectSchema.safeParse(mergedInput);
|
|
91
103
|
if (!result.success) {
|
|
92
104
|
console.log('\nError parsing postxl-schema.json:\n');
|
package/dist/validate.command.js
CHANGED
|
@@ -38,6 +38,7 @@ const fs = __importStar(require("node:fs/promises"));
|
|
|
38
38
|
const path = __importStar(require("node:path"));
|
|
39
39
|
const schema_1 = require("@postxl/schema");
|
|
40
40
|
const utils_1 = require("@postxl/utils");
|
|
41
|
+
const load_project_schema_1 = require("./helpers/load-project-schema");
|
|
41
42
|
const log_schema_error_1 = require("./helpers/log-schema-error");
|
|
42
43
|
function register(program) {
|
|
43
44
|
program
|
|
@@ -52,40 +53,46 @@ This reads the schema from the specified file or from postxl-schema.json in the
|
|
|
52
53
|
if (!path.isAbsolute(inputPath)) {
|
|
53
54
|
inputPath = path.join(process.cwd(), inputPath);
|
|
54
55
|
}
|
|
55
|
-
// Check if input path exists
|
|
56
|
-
|
|
56
|
+
// Check if input path exists and decide between project-directory and raw-file modes.
|
|
57
|
+
// For a directory or a `postxl-schema.json` file we go through `loadAndMergeRawSchema`
|
|
58
|
+
// so split files under `schema/*.model.json` / `schema/*.enum.json` are merged into
|
|
59
|
+
// the validated input — otherwise validate would disagree with `generate` on
|
|
60
|
+
// multi-file schemas. For any other file path we treat it as a raw schema JSON
|
|
61
|
+
// (the single-file escape hatch for validating arbitrary fragments).
|
|
62
|
+
let jsonSchema;
|
|
57
63
|
try {
|
|
58
64
|
const stats = await fs.stat(inputPath);
|
|
59
65
|
if (stats.isDirectory()) {
|
|
60
|
-
|
|
61
|
-
schemaPath = path.join(inputPath, 'postxl-schema.json');
|
|
66
|
+
jsonSchema = await (0, load_project_schema_1.loadAndMergeRawSchema)({ projectPath: inputPath, program });
|
|
62
67
|
}
|
|
63
68
|
else if (stats.isFile()) {
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
if (path.basename(inputPath) === 'postxl-schema.json') {
|
|
70
|
+
jsonSchema = await (0, load_project_schema_1.loadAndMergeRawSchema)({ projectPath: path.dirname(inputPath), program });
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
try {
|
|
74
|
+
const content = await fs.readFile(inputPath, 'utf-8');
|
|
75
|
+
jsonSchema = JSON.parse(content);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
79
|
+
program.error(`Error reading or parsing JSON from ${inputPath}: ${message}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
66
82
|
}
|
|
67
83
|
else {
|
|
68
84
|
program.error(`Path ${inputPath} is neither a file nor a directory!`);
|
|
69
85
|
}
|
|
70
86
|
}
|
|
71
|
-
catch {
|
|
72
|
-
program.error(`Cannot find path ${inputPath}!`);
|
|
73
|
-
}
|
|
74
|
-
// Check if schema file exists
|
|
75
|
-
try {
|
|
76
|
-
await fs.access(schemaPath);
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
program.error(`Cannot find schema file at ${schemaPath}`);
|
|
80
|
-
}
|
|
81
|
-
// Read and parse JSON
|
|
82
|
-
let jsonSchema;
|
|
83
|
-
try {
|
|
84
|
-
const content = await fs.readFile(schemaPath, 'utf-8');
|
|
85
|
-
jsonSchema = JSON.parse(content);
|
|
86
|
-
}
|
|
87
87
|
catch (error) {
|
|
88
|
-
|
|
88
|
+
// Only mask the missing-path case here — real errors from `loadAndMergeRawSchema`
|
|
89
|
+
// (bad split-file casing, duplicate model names, etc.) must surface so users can
|
|
90
|
+
// act on them. validate is the command they reach for to debug schema problems.
|
|
91
|
+
const code = error?.code;
|
|
92
|
+
if (code === 'ENOENT' || code === 'ENOTDIR') {
|
|
93
|
+
program.error(`Cannot find path ${inputPath}!`);
|
|
94
|
+
}
|
|
95
|
+
throw error;
|
|
89
96
|
}
|
|
90
97
|
// Validate schema
|
|
91
98
|
const result = schema_1.zProjectSchema.safeParse(jsonSchema);
|