@omnixdp/typegen 0.2.0 → 0.2.2
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 +1 -1
- package/dist/cli.d.ts +3 -1
- package/dist/cli.js +72 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ npx omnixdp-typegen \
|
|
|
25
25
|
--out src/omnixdp.generated.ts
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
Space arguments can be a space id, slug, or exact name. Typegen exchanges client credentials for short-lived access tokens. Existing `--management-token`, `--business-objects-token`, and `--ratings-and-reviews-token` flags remain supported when you already have bearer tokens. Regenerate the output after changing content types, taxonomy types, data types, or global fields in the admin.
|
|
28
|
+
Space arguments can be a space id, slug, or exact name. Typegen loads `.env` and `.env.local` before evaluating config files, then exchanges client credentials for short-lived access tokens. Existing `--management-token`, `--business-objects-token`, and `--ratings-and-reviews-token` flags remain supported when you already have bearer tokens. Regenerate the output after changing content types, taxonomy types, data types, or global fields in the admin.
|
|
29
29
|
|
|
30
30
|
## Check in CI
|
|
31
31
|
|
package/dist/cli.d.ts
CHANGED
package/dist/cli.js
CHANGED
|
@@ -24,17 +24,22 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
return result;
|
|
25
25
|
};
|
|
26
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.loadEnvFiles = loadEnvFiles;
|
|
28
|
+
exports.loadConfig = loadConfig;
|
|
27
29
|
const promises_1 = require("node:fs/promises");
|
|
30
|
+
const node_module_1 = require("node:module");
|
|
28
31
|
const node_path_1 = require("node:path");
|
|
29
32
|
const node_url_1 = require("node:url");
|
|
30
33
|
const fetch_schema_1 = require("./fetch-schema");
|
|
31
34
|
const generate_1 = require("./generate");
|
|
35
|
+
const requireConfig = (0, node_module_1.createRequire)(__filename);
|
|
32
36
|
async function main() {
|
|
33
37
|
const args = parseArgs(process.argv.slice(2));
|
|
34
38
|
if (args.help) {
|
|
35
39
|
printHelp();
|
|
36
40
|
return;
|
|
37
41
|
}
|
|
42
|
+
await loadEnvFiles();
|
|
38
43
|
const fileConfig = args.config ? await loadConfig(args.config) : await loadDefaultConfig();
|
|
39
44
|
const config = compactConfig({ ...fileConfig, ...args });
|
|
40
45
|
const out = config.out ?? "src/omnixdp.generated.ts";
|
|
@@ -56,6 +61,56 @@ async function main() {
|
|
|
56
61
|
await (0, promises_1.writeFile)(outPath, output, "utf8");
|
|
57
62
|
console.log(`Generated ${out}`);
|
|
58
63
|
}
|
|
64
|
+
async function loadEnvFiles(cwd = process.cwd()) {
|
|
65
|
+
const protectedKeys = new Set(Object.keys(process.env));
|
|
66
|
+
for (const file of [".env", ".env.local"]) {
|
|
67
|
+
const values = await readEnvFile((0, node_path_1.join)(cwd, file));
|
|
68
|
+
for (const [key, value] of Object.entries(values)) {
|
|
69
|
+
if (!protectedKeys.has(key)) {
|
|
70
|
+
process.env[key] = value;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function readEnvFile(path) {
|
|
76
|
+
const content = await (0, promises_1.readFile)(path, "utf8").catch((error) => {
|
|
77
|
+
if (error.code === "ENOENT")
|
|
78
|
+
return null;
|
|
79
|
+
throw error;
|
|
80
|
+
});
|
|
81
|
+
if (content === null)
|
|
82
|
+
return {};
|
|
83
|
+
const values = {};
|
|
84
|
+
for (const line of content.split(/\r?\n/)) {
|
|
85
|
+
const parsed = parseEnvLine(line);
|
|
86
|
+
if (parsed) {
|
|
87
|
+
values[parsed.key] = parsed.value;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return values;
|
|
91
|
+
}
|
|
92
|
+
function parseEnvLine(line) {
|
|
93
|
+
const trimmed = line.trim();
|
|
94
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
95
|
+
return null;
|
|
96
|
+
const normalized = trimmed.startsWith("export ") ? trimmed.slice("export ".length).trimStart() : trimmed;
|
|
97
|
+
const separatorIndex = normalized.indexOf("=");
|
|
98
|
+
if (separatorIndex <= 0)
|
|
99
|
+
return null;
|
|
100
|
+
const key = normalized.slice(0, separatorIndex).trim();
|
|
101
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key))
|
|
102
|
+
return null;
|
|
103
|
+
return { key, value: unquoteEnvValue(normalized.slice(separatorIndex + 1).trim()) };
|
|
104
|
+
}
|
|
105
|
+
function unquoteEnvValue(value) {
|
|
106
|
+
if (value.length >= 2 && value.startsWith("\"") && value.endsWith("\"")) {
|
|
107
|
+
return value.slice(1, -1).replace(/\\n/g, "\n").replace(/\\r/g, "\r").replace(/\\"/g, "\"").replace(/\\\\/g, "\\");
|
|
108
|
+
}
|
|
109
|
+
if (value.length >= 2 && value.startsWith("'") && value.endsWith("'")) {
|
|
110
|
+
return value.slice(1, -1);
|
|
111
|
+
}
|
|
112
|
+
return value.replace(/\s+#.*$/, "");
|
|
113
|
+
}
|
|
59
114
|
function parseArgs(argv) {
|
|
60
115
|
const out = {};
|
|
61
116
|
for (let i = 0; i < argv.length; i += 1) {
|
|
@@ -149,8 +204,18 @@ async function loadConfig(file) {
|
|
|
149
204
|
if (path.endsWith(".json")) {
|
|
150
205
|
return JSON.parse(await (0, promises_1.readFile)(path, "utf8"));
|
|
151
206
|
}
|
|
207
|
+
if (path.endsWith(".cjs") || path.endsWith(".js")) {
|
|
208
|
+
return normalizeLoadedConfig(requireConfig(path));
|
|
209
|
+
}
|
|
152
210
|
const imported = await Promise.resolve(`${(0, node_url_1.pathToFileURL)(path).href}`).then(s => __importStar(require(s)));
|
|
153
|
-
return (imported
|
|
211
|
+
return normalizeLoadedConfig(imported);
|
|
212
|
+
}
|
|
213
|
+
function normalizeLoadedConfig(imported) {
|
|
214
|
+
if (imported && typeof imported === "object") {
|
|
215
|
+
const record = imported;
|
|
216
|
+
return (record.default ?? record.config ?? imported);
|
|
217
|
+
}
|
|
218
|
+
return {};
|
|
154
219
|
}
|
|
155
220
|
function compactConfig(config) {
|
|
156
221
|
return Object.fromEntries(Object.entries(config).filter(([, value]) => value !== undefined && value !== ""));
|
|
@@ -179,7 +244,9 @@ Options:
|
|
|
179
244
|
--check Fail if the output file is stale
|
|
180
245
|
`);
|
|
181
246
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
247
|
+
if (require.main === module) {
|
|
248
|
+
main().catch((error) => {
|
|
249
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
250
|
+
process.exitCode = 1;
|
|
251
|
+
});
|
|
252
|
+
}
|