algolia-codegen 0.1.1 → 0.1.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 +5 -3
- package/dist/{chunk-4FWQJC54.js → chunk-R66ECGLW.js} +50 -25
- package/dist/cli.cjs +65 -33
- package/dist/cli.js +8 -1
- package/dist/index.cjs +58 -33
- package/dist/index.js +1 -1
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -180,8 +180,9 @@ type AlgoliaCodegenGeneratorConfig = {
|
|
|
180
180
|
|
|
181
181
|
## How It Works
|
|
182
182
|
|
|
183
|
-
1. **Loads
|
|
184
|
-
2. **
|
|
183
|
+
1. **Loads Environment Variables**: Automatically loads `.env` file if present in the current directory
|
|
184
|
+
2. **Loads Configuration**: Reads the `algolia-codegen.ts` config file (or custom path), supporting both TypeScript and JavaScript
|
|
185
|
+
3. **Processes Each Path**: For each file path specified in the config:
|
|
185
186
|
- Connects to Algolia using the provided credentials
|
|
186
187
|
- Fetches a sample record from the specified index
|
|
187
188
|
- Analyzes the record structure and generates TypeScript types
|
|
@@ -201,7 +202,8 @@ Each generated file contains all types found in the index, including nested type
|
|
|
201
202
|
- Each index must have at least one record for the script to work
|
|
202
203
|
- The script processes files sequentially and continues even if one fails
|
|
203
204
|
- Make sure your config file exports a default object
|
|
204
|
-
-
|
|
205
|
+
- TypeScript config files (`.ts`) are automatically supported - no compilation needed
|
|
206
|
+
- Environment variables from `.env` file are automatically loaded if present in the current directory
|
|
205
207
|
- Each generated file contains all types found in the index (including nested types) in a single file
|
|
206
208
|
- Types are automatically sorted by dependencies to ensure correct ordering
|
|
207
209
|
- The generator handles arrays, nested objects, optional fields, and null values
|
|
@@ -1,8 +1,27 @@
|
|
|
1
|
-
// src/utils/config
|
|
1
|
+
// src/utils/load-config.ts
|
|
2
2
|
import { existsSync } from "fs";
|
|
3
3
|
import { resolve } from "path";
|
|
4
4
|
import { pathToFileURL } from "url";
|
|
5
5
|
|
|
6
|
+
// src/utils/load-typescript-config.ts
|
|
7
|
+
import { readFileSync } from "fs";
|
|
8
|
+
async function loadTypeScriptConfig(resolvedPath) {
|
|
9
|
+
const esbuild = await import("esbuild");
|
|
10
|
+
const source = readFileSync(resolvedPath, "utf-8");
|
|
11
|
+
const result = esbuild.transformSync(source, {
|
|
12
|
+
loader: "ts",
|
|
13
|
+
format: "esm",
|
|
14
|
+
target: "node18",
|
|
15
|
+
sourcefile: resolvedPath
|
|
16
|
+
});
|
|
17
|
+
if (!result.code) {
|
|
18
|
+
throw new Error("Failed to transform TypeScript config file");
|
|
19
|
+
}
|
|
20
|
+
const dataUrl = `data:text/javascript;charset=utf-8,${encodeURIComponent(result.code)}`;
|
|
21
|
+
const module = await import(dataUrl);
|
|
22
|
+
return module;
|
|
23
|
+
}
|
|
24
|
+
|
|
6
25
|
// src/utils/validations/generator-config.ts
|
|
7
26
|
function validateGeneratorConfig(config, path) {
|
|
8
27
|
if (typeof config !== "object" || config === null || Array.isArray(config)) {
|
|
@@ -112,7 +131,7 @@ Received: ${typeof generates}`
|
|
|
112
131
|
}
|
|
113
132
|
}
|
|
114
133
|
|
|
115
|
-
// src/utils/config
|
|
134
|
+
// src/utils/load-config.ts
|
|
116
135
|
async function loadConfig(configPath) {
|
|
117
136
|
const defaultConfigPath = "algolia-codegen.ts";
|
|
118
137
|
const finalConfigPath = configPath || defaultConfigPath;
|
|
@@ -125,33 +144,39 @@ Please create a config file or specify a different path using --config option.`
|
|
|
125
144
|
}
|
|
126
145
|
const configUrl = pathToFileURL(resolvedPath).href;
|
|
127
146
|
let configModule;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
147
|
+
if (resolvedPath.endsWith(".ts")) {
|
|
148
|
+
try {
|
|
149
|
+
configModule = await loadTypeScriptConfig(resolvedPath);
|
|
150
|
+
} catch (tsError) {
|
|
151
|
+
const jsPath = resolvedPath.replace(/\.ts$/, ".js");
|
|
152
|
+
if (existsSync(jsPath)) {
|
|
153
|
+
const jsUrl = pathToFileURL(jsPath).href;
|
|
154
|
+
try {
|
|
155
|
+
configModule = await import(jsUrl);
|
|
156
|
+
} catch (jsImportError) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
`Failed to import config file: ${resolvedPath}
|
|
159
|
+
Tried both .ts (via esbuild) and .js extensions.
|
|
160
|
+
TypeScript error: ${tsError instanceof Error ? tsError.message : String(tsError)}
|
|
161
|
+
JavaScript error: ${jsImportError instanceof Error ? jsImportError.message : String(jsImportError)}`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
137
165
|
throw new Error(
|
|
138
|
-
`Failed to
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
Note: If using TypeScript config, you may need to compile it first or use a tool like tsx.`
|
|
166
|
+
`Failed to load TypeScript config file: ${resolvedPath}
|
|
167
|
+
Error: ${tsError instanceof Error ? tsError.message : String(tsError)}
|
|
168
|
+
Make sure esbuild is installed as a dependency.`
|
|
142
169
|
);
|
|
143
170
|
}
|
|
144
|
-
}
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
try {
|
|
174
|
+
configModule = await import(configUrl);
|
|
175
|
+
} catch (importError) {
|
|
145
176
|
const errorMessage = importError instanceof Error ? importError.message : String(importError);
|
|
146
|
-
const isTypeScriptError = resolvedPath.endsWith(".ts") && (errorMessage.includes("Cannot find module") || errorMessage.includes("Unknown file extension"));
|
|
147
177
|
throw new Error(
|
|
148
178
|
`Failed to import config file: ${resolvedPath}
|
|
149
|
-
|
|
150
|
-
Please either:
|
|
151
|
-
1. Compile your config to JavaScript (.js)
|
|
152
|
-
2. Use a tool like tsx to run the CLI: tsx algolia-codegen
|
|
153
|
-
3. Or use a JavaScript config file instead
|
|
154
|
-
` : `Error: ${errorMessage}`)
|
|
179
|
+
Error: ${errorMessage}`
|
|
155
180
|
);
|
|
156
181
|
}
|
|
157
182
|
}
|
|
@@ -173,7 +198,7 @@ Received: ${typeof config}`
|
|
|
173
198
|
}
|
|
174
199
|
|
|
175
200
|
// src/utils/fetch-algolia-data.ts
|
|
176
|
-
import algoliasearch from "algoliasearch";
|
|
201
|
+
import { algoliasearch } from "algoliasearch";
|
|
177
202
|
import { existsSync as existsSync2, writeFileSync, mkdirSync } from "fs";
|
|
178
203
|
import { resolve as resolve2, dirname } from "path";
|
|
179
204
|
|
|
@@ -462,8 +487,8 @@ Set overwrite: true in config to allow overwriting existing files.`
|
|
|
462
487
|
results = await client.search([
|
|
463
488
|
{
|
|
464
489
|
indexName: generatorConfig.indexName,
|
|
465
|
-
query: "",
|
|
466
490
|
params: {
|
|
491
|
+
query: "",
|
|
467
492
|
hitsPerPage: 1
|
|
468
493
|
}
|
|
469
494
|
}
|
package/dist/cli.cjs
CHANGED
|
@@ -24,13 +24,35 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
));
|
|
25
25
|
|
|
26
26
|
// src/cli.ts
|
|
27
|
+
var import_dotenv = require("dotenv");
|
|
28
|
+
var import_fs4 = require("fs");
|
|
29
|
+
var import_path3 = require("path");
|
|
27
30
|
var import_commander = require("commander");
|
|
28
31
|
|
|
29
|
-
// src/utils/config
|
|
30
|
-
var
|
|
32
|
+
// src/utils/load-config.ts
|
|
33
|
+
var import_fs2 = require("fs");
|
|
31
34
|
var import_path = require("path");
|
|
32
35
|
var import_url = require("url");
|
|
33
36
|
|
|
37
|
+
// src/utils/load-typescript-config.ts
|
|
38
|
+
var import_fs = require("fs");
|
|
39
|
+
async function loadTypeScriptConfig(resolvedPath) {
|
|
40
|
+
const esbuild = await import("esbuild");
|
|
41
|
+
const source = (0, import_fs.readFileSync)(resolvedPath, "utf-8");
|
|
42
|
+
const result = esbuild.transformSync(source, {
|
|
43
|
+
loader: "ts",
|
|
44
|
+
format: "esm",
|
|
45
|
+
target: "node18",
|
|
46
|
+
sourcefile: resolvedPath
|
|
47
|
+
});
|
|
48
|
+
if (!result.code) {
|
|
49
|
+
throw new Error("Failed to transform TypeScript config file");
|
|
50
|
+
}
|
|
51
|
+
const dataUrl = `data:text/javascript;charset=utf-8,${encodeURIComponent(result.code)}`;
|
|
52
|
+
const module2 = await import(dataUrl);
|
|
53
|
+
return module2;
|
|
54
|
+
}
|
|
55
|
+
|
|
34
56
|
// src/utils/validations/generator-config.ts
|
|
35
57
|
function validateGeneratorConfig(config, path) {
|
|
36
58
|
if (typeof config !== "object" || config === null || Array.isArray(config)) {
|
|
@@ -140,12 +162,12 @@ Received: ${typeof generates}`
|
|
|
140
162
|
}
|
|
141
163
|
}
|
|
142
164
|
|
|
143
|
-
// src/utils/config
|
|
165
|
+
// src/utils/load-config.ts
|
|
144
166
|
async function loadConfig(configPath) {
|
|
145
167
|
const defaultConfigPath = "algolia-codegen.ts";
|
|
146
168
|
const finalConfigPath = configPath || defaultConfigPath;
|
|
147
169
|
const resolvedPath = (0, import_path.resolve)(process.cwd(), finalConfigPath);
|
|
148
|
-
if (!(0,
|
|
170
|
+
if (!(0, import_fs2.existsSync)(resolvedPath)) {
|
|
149
171
|
throw new Error(
|
|
150
172
|
`Config file not found: ${resolvedPath}
|
|
151
173
|
Please create a config file or specify a different path using --config option.`
|
|
@@ -153,33 +175,39 @@ Please create a config file or specify a different path using --config option.`
|
|
|
153
175
|
}
|
|
154
176
|
const configUrl = (0, import_url.pathToFileURL)(resolvedPath).href;
|
|
155
177
|
let configModule;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
178
|
+
if (resolvedPath.endsWith(".ts")) {
|
|
179
|
+
try {
|
|
180
|
+
configModule = await loadTypeScriptConfig(resolvedPath);
|
|
181
|
+
} catch (tsError) {
|
|
182
|
+
const jsPath = resolvedPath.replace(/\.ts$/, ".js");
|
|
183
|
+
if ((0, import_fs2.existsSync)(jsPath)) {
|
|
184
|
+
const jsUrl = (0, import_url.pathToFileURL)(jsPath).href;
|
|
185
|
+
try {
|
|
186
|
+
configModule = await import(jsUrl);
|
|
187
|
+
} catch (jsImportError) {
|
|
188
|
+
throw new Error(
|
|
189
|
+
`Failed to import config file: ${resolvedPath}
|
|
190
|
+
Tried both .ts (via esbuild) and .js extensions.
|
|
191
|
+
TypeScript error: ${tsError instanceof Error ? tsError.message : String(tsError)}
|
|
192
|
+
JavaScript error: ${jsImportError instanceof Error ? jsImportError.message : String(jsImportError)}`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
165
196
|
throw new Error(
|
|
166
|
-
`Failed to
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
Note: If using TypeScript config, you may need to compile it first or use a tool like tsx.`
|
|
197
|
+
`Failed to load TypeScript config file: ${resolvedPath}
|
|
198
|
+
Error: ${tsError instanceof Error ? tsError.message : String(tsError)}
|
|
199
|
+
Make sure esbuild is installed as a dependency.`
|
|
170
200
|
);
|
|
171
201
|
}
|
|
172
|
-
}
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
try {
|
|
205
|
+
configModule = await import(configUrl);
|
|
206
|
+
} catch (importError) {
|
|
173
207
|
const errorMessage = importError instanceof Error ? importError.message : String(importError);
|
|
174
|
-
const isTypeScriptError = resolvedPath.endsWith(".ts") && (errorMessage.includes("Cannot find module") || errorMessage.includes("Unknown file extension"));
|
|
175
208
|
throw new Error(
|
|
176
209
|
`Failed to import config file: ${resolvedPath}
|
|
177
|
-
|
|
178
|
-
Please either:
|
|
179
|
-
1. Compile your config to JavaScript (.js)
|
|
180
|
-
2. Use a tool like tsx to run the CLI: tsx algolia-codegen
|
|
181
|
-
3. Or use a JavaScript config file instead
|
|
182
|
-
` : `Error: ${errorMessage}`)
|
|
210
|
+
Error: ${errorMessage}`
|
|
183
211
|
);
|
|
184
212
|
}
|
|
185
213
|
}
|
|
@@ -201,8 +229,8 @@ Received: ${typeof config}`
|
|
|
201
229
|
}
|
|
202
230
|
|
|
203
231
|
// src/utils/fetch-algolia-data.ts
|
|
204
|
-
var import_algoliasearch =
|
|
205
|
-
var
|
|
232
|
+
var import_algoliasearch = require("algoliasearch");
|
|
233
|
+
var import_fs3 = require("fs");
|
|
206
234
|
var import_path2 = require("path");
|
|
207
235
|
|
|
208
236
|
// src/utils/generate-typescript-types.ts
|
|
@@ -465,7 +493,7 @@ async function fetchAlgoliaData(filePath, generatorConfig, overwrite) {
|
|
|
465
493
|
console.log(`
|
|
466
494
|
Processing file: ${filePath}`);
|
|
467
495
|
const resolvedPath = (0, import_path2.resolve)(process.cwd(), filePath);
|
|
468
|
-
if ((0,
|
|
496
|
+
if ((0, import_fs3.existsSync)(resolvedPath) && !overwrite) {
|
|
469
497
|
throw new Error(
|
|
470
498
|
`File already exists: ${resolvedPath}
|
|
471
499
|
Set overwrite: true in config to allow overwriting existing files.`
|
|
@@ -475,7 +503,7 @@ Set overwrite: true in config to allow overwriting existing files.`
|
|
|
475
503
|
console.log(`App ID: ${generatorConfig.appId}`);
|
|
476
504
|
let client;
|
|
477
505
|
try {
|
|
478
|
-
client = (0, import_algoliasearch.
|
|
506
|
+
client = (0, import_algoliasearch.algoliasearch)(
|
|
479
507
|
generatorConfig.appId,
|
|
480
508
|
generatorConfig.searchKey
|
|
481
509
|
);
|
|
@@ -490,8 +518,8 @@ Set overwrite: true in config to allow overwriting existing files.`
|
|
|
490
518
|
results = await client.search([
|
|
491
519
|
{
|
|
492
520
|
indexName: generatorConfig.indexName,
|
|
493
|
-
query: "",
|
|
494
521
|
params: {
|
|
522
|
+
query: "",
|
|
495
523
|
hitsPerPage: 1
|
|
496
524
|
}
|
|
497
525
|
}
|
|
@@ -532,10 +560,10 @@ Set overwrite: true in config to allow overwriting existing files.`
|
|
|
532
560
|
console.log(`ObjectID: ${sampleHit.objectID || "N/A"}`);
|
|
533
561
|
const fileContent = generateTypeScriptTypes(sampleHit, generatorConfig);
|
|
534
562
|
const dir = (0, import_path2.dirname)(resolvedPath);
|
|
535
|
-
if (!(0,
|
|
536
|
-
(0,
|
|
563
|
+
if (!(0, import_fs3.existsSync)(dir)) {
|
|
564
|
+
(0, import_fs3.mkdirSync)(dir, { recursive: true });
|
|
537
565
|
}
|
|
538
|
-
(0,
|
|
566
|
+
(0, import_fs3.writeFileSync)(resolvedPath, fileContent, "utf-8");
|
|
539
567
|
console.log(`Generated file: ${filePath}`);
|
|
540
568
|
}
|
|
541
569
|
|
|
@@ -579,6 +607,10 @@ Error processing file: ${filePath}`);
|
|
|
579
607
|
};
|
|
580
608
|
|
|
581
609
|
// src/cli.ts
|
|
610
|
+
var envPath = (0, import_path3.resolve)(process.cwd(), ".env");
|
|
611
|
+
if ((0, import_fs4.existsSync)(envPath)) {
|
|
612
|
+
(0, import_dotenv.config)({ path: envPath });
|
|
613
|
+
}
|
|
582
614
|
var program = new import_commander.Command();
|
|
583
615
|
program.name("algolia-codegen").description("Generate TypeScript types from Algolia index").option("-c, --config <path>", "Config file path").action(async (options) => {
|
|
584
616
|
await main(options.config);
|
package/dist/cli.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
main
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-R66ECGLW.js";
|
|
5
5
|
|
|
6
6
|
// src/cli.ts
|
|
7
|
+
import { config as loadDotenv } from "dotenv";
|
|
8
|
+
import { existsSync } from "fs";
|
|
9
|
+
import { resolve } from "path";
|
|
7
10
|
import { Command } from "commander";
|
|
11
|
+
var envPath = resolve(process.cwd(), ".env");
|
|
12
|
+
if (existsSync(envPath)) {
|
|
13
|
+
loadDotenv({ path: envPath });
|
|
14
|
+
}
|
|
8
15
|
var program = new Command();
|
|
9
16
|
program.name("algolia-codegen").description("Generate TypeScript types from Algolia index").option("-c, --config <path>", "Config file path").action(async (options) => {
|
|
10
17
|
await main(options.config);
|
package/dist/index.cjs
CHANGED
|
@@ -37,11 +37,30 @@ __export(src_exports, {
|
|
|
37
37
|
});
|
|
38
38
|
module.exports = __toCommonJS(src_exports);
|
|
39
39
|
|
|
40
|
-
// src/utils/config
|
|
41
|
-
var
|
|
40
|
+
// src/utils/load-config.ts
|
|
41
|
+
var import_fs2 = require("fs");
|
|
42
42
|
var import_path = require("path");
|
|
43
43
|
var import_url = require("url");
|
|
44
44
|
|
|
45
|
+
// src/utils/load-typescript-config.ts
|
|
46
|
+
var import_fs = require("fs");
|
|
47
|
+
async function loadTypeScriptConfig(resolvedPath) {
|
|
48
|
+
const esbuild = await import("esbuild");
|
|
49
|
+
const source = (0, import_fs.readFileSync)(resolvedPath, "utf-8");
|
|
50
|
+
const result = esbuild.transformSync(source, {
|
|
51
|
+
loader: "ts",
|
|
52
|
+
format: "esm",
|
|
53
|
+
target: "node18",
|
|
54
|
+
sourcefile: resolvedPath
|
|
55
|
+
});
|
|
56
|
+
if (!result.code) {
|
|
57
|
+
throw new Error("Failed to transform TypeScript config file");
|
|
58
|
+
}
|
|
59
|
+
const dataUrl = `data:text/javascript;charset=utf-8,${encodeURIComponent(result.code)}`;
|
|
60
|
+
const module2 = await import(dataUrl);
|
|
61
|
+
return module2;
|
|
62
|
+
}
|
|
63
|
+
|
|
45
64
|
// src/utils/validations/generator-config.ts
|
|
46
65
|
function validateGeneratorConfig(config, path) {
|
|
47
66
|
if (typeof config !== "object" || config === null || Array.isArray(config)) {
|
|
@@ -151,12 +170,12 @@ Received: ${typeof generates}`
|
|
|
151
170
|
}
|
|
152
171
|
}
|
|
153
172
|
|
|
154
|
-
// src/utils/config
|
|
173
|
+
// src/utils/load-config.ts
|
|
155
174
|
async function loadConfig(configPath) {
|
|
156
175
|
const defaultConfigPath = "algolia-codegen.ts";
|
|
157
176
|
const finalConfigPath = configPath || defaultConfigPath;
|
|
158
177
|
const resolvedPath = (0, import_path.resolve)(process.cwd(), finalConfigPath);
|
|
159
|
-
if (!(0,
|
|
178
|
+
if (!(0, import_fs2.existsSync)(resolvedPath)) {
|
|
160
179
|
throw new Error(
|
|
161
180
|
`Config file not found: ${resolvedPath}
|
|
162
181
|
Please create a config file or specify a different path using --config option.`
|
|
@@ -164,33 +183,39 @@ Please create a config file or specify a different path using --config option.`
|
|
|
164
183
|
}
|
|
165
184
|
const configUrl = (0, import_url.pathToFileURL)(resolvedPath).href;
|
|
166
185
|
let configModule;
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
186
|
+
if (resolvedPath.endsWith(".ts")) {
|
|
187
|
+
try {
|
|
188
|
+
configModule = await loadTypeScriptConfig(resolvedPath);
|
|
189
|
+
} catch (tsError) {
|
|
190
|
+
const jsPath = resolvedPath.replace(/\.ts$/, ".js");
|
|
191
|
+
if ((0, import_fs2.existsSync)(jsPath)) {
|
|
192
|
+
const jsUrl = (0, import_url.pathToFileURL)(jsPath).href;
|
|
193
|
+
try {
|
|
194
|
+
configModule = await import(jsUrl);
|
|
195
|
+
} catch (jsImportError) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
`Failed to import config file: ${resolvedPath}
|
|
198
|
+
Tried both .ts (via esbuild) and .js extensions.
|
|
199
|
+
TypeScript error: ${tsError instanceof Error ? tsError.message : String(tsError)}
|
|
200
|
+
JavaScript error: ${jsImportError instanceof Error ? jsImportError.message : String(jsImportError)}`
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
176
204
|
throw new Error(
|
|
177
|
-
`Failed to
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
Note: If using TypeScript config, you may need to compile it first or use a tool like tsx.`
|
|
205
|
+
`Failed to load TypeScript config file: ${resolvedPath}
|
|
206
|
+
Error: ${tsError instanceof Error ? tsError.message : String(tsError)}
|
|
207
|
+
Make sure esbuild is installed as a dependency.`
|
|
181
208
|
);
|
|
182
209
|
}
|
|
183
|
-
}
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
try {
|
|
213
|
+
configModule = await import(configUrl);
|
|
214
|
+
} catch (importError) {
|
|
184
215
|
const errorMessage = importError instanceof Error ? importError.message : String(importError);
|
|
185
|
-
const isTypeScriptError = resolvedPath.endsWith(".ts") && (errorMessage.includes("Cannot find module") || errorMessage.includes("Unknown file extension"));
|
|
186
216
|
throw new Error(
|
|
187
217
|
`Failed to import config file: ${resolvedPath}
|
|
188
|
-
|
|
189
|
-
Please either:
|
|
190
|
-
1. Compile your config to JavaScript (.js)
|
|
191
|
-
2. Use a tool like tsx to run the CLI: tsx algolia-codegen
|
|
192
|
-
3. Or use a JavaScript config file instead
|
|
193
|
-
` : `Error: ${errorMessage}`)
|
|
218
|
+
Error: ${errorMessage}`
|
|
194
219
|
);
|
|
195
220
|
}
|
|
196
221
|
}
|
|
@@ -212,8 +237,8 @@ Received: ${typeof config}`
|
|
|
212
237
|
}
|
|
213
238
|
|
|
214
239
|
// src/utils/fetch-algolia-data.ts
|
|
215
|
-
var import_algoliasearch =
|
|
216
|
-
var
|
|
240
|
+
var import_algoliasearch = require("algoliasearch");
|
|
241
|
+
var import_fs3 = require("fs");
|
|
217
242
|
var import_path2 = require("path");
|
|
218
243
|
|
|
219
244
|
// src/utils/generate-typescript-types.ts
|
|
@@ -476,7 +501,7 @@ async function fetchAlgoliaData(filePath, generatorConfig, overwrite) {
|
|
|
476
501
|
console.log(`
|
|
477
502
|
Processing file: ${filePath}`);
|
|
478
503
|
const resolvedPath = (0, import_path2.resolve)(process.cwd(), filePath);
|
|
479
|
-
if ((0,
|
|
504
|
+
if ((0, import_fs3.existsSync)(resolvedPath) && !overwrite) {
|
|
480
505
|
throw new Error(
|
|
481
506
|
`File already exists: ${resolvedPath}
|
|
482
507
|
Set overwrite: true in config to allow overwriting existing files.`
|
|
@@ -486,7 +511,7 @@ Set overwrite: true in config to allow overwriting existing files.`
|
|
|
486
511
|
console.log(`App ID: ${generatorConfig.appId}`);
|
|
487
512
|
let client;
|
|
488
513
|
try {
|
|
489
|
-
client = (0, import_algoliasearch.
|
|
514
|
+
client = (0, import_algoliasearch.algoliasearch)(
|
|
490
515
|
generatorConfig.appId,
|
|
491
516
|
generatorConfig.searchKey
|
|
492
517
|
);
|
|
@@ -501,8 +526,8 @@ Set overwrite: true in config to allow overwriting existing files.`
|
|
|
501
526
|
results = await client.search([
|
|
502
527
|
{
|
|
503
528
|
indexName: generatorConfig.indexName,
|
|
504
|
-
query: "",
|
|
505
529
|
params: {
|
|
530
|
+
query: "",
|
|
506
531
|
hitsPerPage: 1
|
|
507
532
|
}
|
|
508
533
|
}
|
|
@@ -543,10 +568,10 @@ Set overwrite: true in config to allow overwriting existing files.`
|
|
|
543
568
|
console.log(`ObjectID: ${sampleHit.objectID || "N/A"}`);
|
|
544
569
|
const fileContent = generateTypeScriptTypes(sampleHit, generatorConfig);
|
|
545
570
|
const dir = (0, import_path2.dirname)(resolvedPath);
|
|
546
|
-
if (!(0,
|
|
547
|
-
(0,
|
|
571
|
+
if (!(0, import_fs3.existsSync)(dir)) {
|
|
572
|
+
(0, import_fs3.mkdirSync)(dir, { recursive: true });
|
|
548
573
|
}
|
|
549
|
-
(0,
|
|
574
|
+
(0, import_fs3.writeFileSync)(resolvedPath, fileContent, "utf-8");
|
|
550
575
|
console.log(`Generated file: ${filePath}`);
|
|
551
576
|
}
|
|
552
577
|
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "algolia-codegen",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Generate TypeScript types from Algolia indices",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "nightlightmare",
|
|
@@ -37,13 +37,15 @@
|
|
|
37
37
|
"dist"
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"algoliasearch": "^
|
|
41
|
-
"commander": "^
|
|
40
|
+
"algoliasearch": "^5.46.2",
|
|
41
|
+
"commander": "^14.0.2",
|
|
42
|
+
"dotenv": "^17.2.3",
|
|
43
|
+
"esbuild": "^0.27.0",
|
|
44
|
+
"tsx": "^4.21.0"
|
|
42
45
|
},
|
|
43
46
|
"devDependencies": {
|
|
44
47
|
"@types/node": "^25.0.3",
|
|
45
48
|
"tsup": "^8.0.0",
|
|
46
|
-
"tsx": "^4.21.0",
|
|
47
49
|
"typescript": "^5.4.0"
|
|
48
50
|
},
|
|
49
51
|
"engines": {
|