@contentstorage/core 0.3.10 → 0.3.11
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/index.d.ts +1 -3
- package/dist/index.js +1 -2
- package/dist/scripts/generate-types.js +34 -26
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { defineConfig };
|
|
1
|
+
export {};
|
|
@@ -7,52 +7,56 @@ import chalk from 'chalk'; // Optional: for colored output
|
|
|
7
7
|
import { loadConfig } from '../lib/configLoader.js';
|
|
8
8
|
export async function generateTypes() {
|
|
9
9
|
console.log(chalk.blue('Starting type generation...'));
|
|
10
|
-
const config = await loadConfig();
|
|
11
|
-
console.log(chalk.gray(`
|
|
10
|
+
const config = await loadConfig(); // Ensure loadConfig provides languageCodes
|
|
11
|
+
console.log(chalk.gray(`Content will be read from directory: ${config.contentDir}`));
|
|
12
12
|
console.log(chalk.gray(`Saving TypeScript types to: ${config.typesOutputFile}`));
|
|
13
13
|
try {
|
|
14
|
-
//
|
|
15
|
-
|
|
14
|
+
// Validate languageCodes from config
|
|
15
|
+
if (!config.languageCodes ||
|
|
16
|
+
!Array.isArray(config.languageCodes) ||
|
|
17
|
+
config.languageCodes.length === 0) {
|
|
18
|
+
throw new Error('config.languageCodes is missing, not an array, or empty. Cannot determine which JSON file to use for type generation.');
|
|
19
|
+
}
|
|
20
|
+
const firstLanguageCode = config.languageCodes[0];
|
|
21
|
+
const targetFilename = `${firstLanguageCode}.json`;
|
|
22
|
+
const jsonFilePath = path.join(config.contentDir, targetFilename);
|
|
23
|
+
console.log(chalk.gray(`Attempting to generate types using the JSON file for the first configured language code ('${firstLanguageCode}').`));
|
|
24
|
+
console.log(chalk.gray(`Target file: ${jsonFilePath}`));
|
|
25
|
+
// Read the specific JSON file
|
|
26
|
+
let jsonContent;
|
|
16
27
|
try {
|
|
17
|
-
|
|
28
|
+
jsonContent = await fs.readFile(jsonFilePath, 'utf-8');
|
|
18
29
|
}
|
|
19
30
|
catch (err) {
|
|
20
31
|
if (err.code === 'ENOENT') {
|
|
21
|
-
throw new Error(`
|
|
32
|
+
throw new Error(`Target JSON file not found: ${jsonFilePath}. Ensure content for language code '${firstLanguageCode}' has been pulled and exists at this location.`);
|
|
22
33
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// Filter for JSON files and sort them (optional, but good for consistency)
|
|
26
|
-
const jsonFiles = files
|
|
27
|
-
.filter((file) => file.toLowerCase().endsWith('.json'))
|
|
28
|
-
.sort();
|
|
29
|
-
if (jsonFiles.length === 0) {
|
|
30
|
-
throw new Error(`No JSON files found in ${config.contentDir}.`);
|
|
34
|
+
// Re-throw other fs.readFile errors (e.g., permission issues)
|
|
35
|
+
throw new Error(`Failed to read file ${jsonFilePath}: ${err.message}`);
|
|
31
36
|
}
|
|
32
|
-
const firstJsonFile = jsonFiles[0];
|
|
33
|
-
const jsonFilePath = path.join(config.contentDir, firstJsonFile);
|
|
34
|
-
console.log(chalk.gray(`Using first JSON file for type generation: ${firstJsonFile}`));
|
|
35
|
-
// Read the first JSON file
|
|
36
|
-
const jsonContent = await fs.readFile(jsonFilePath, 'utf-8');
|
|
37
37
|
// Parse the JSON content
|
|
38
38
|
let jsonObject;
|
|
39
39
|
try {
|
|
40
40
|
jsonObject = JSON.parse(jsonContent);
|
|
41
41
|
}
|
|
42
42
|
catch (parseError) {
|
|
43
|
-
throw new Error(`Failed to parse JSON file ${
|
|
43
|
+
throw new Error(`Failed to parse JSON from file ${targetFilename}: ${parseError.message}`);
|
|
44
44
|
}
|
|
45
45
|
// Generate TypeScript interfaces using json-to-ts
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
const rootTypeName = 'RootContentItem'; // Or derive from filename, or make configurable
|
|
46
|
+
// The root type name for the generated interface. You might want to make this configurable.
|
|
47
|
+
const rootTypeName = 'RootContentItem';
|
|
49
48
|
const typeDeclarations = jsonToTS.default(jsonObject, {
|
|
50
49
|
rootName: rootTypeName,
|
|
51
50
|
});
|
|
51
|
+
// If your previous code `jsonToTS.default(...)` was correct for your setup,
|
|
52
|
+
// please revert the line above to:
|
|
53
|
+
// const typeDeclarations: string[] = jsonToTS.default(jsonObject, {
|
|
54
|
+
// rootName: rootTypeName,
|
|
55
|
+
// });
|
|
52
56
|
if (!typeDeclarations || typeDeclarations.length === 0) {
|
|
53
|
-
throw new Error(`Could not generate types from ${
|
|
57
|
+
throw new Error(`Could not generate types from ${targetFilename}. The file might be empty, malformed, or not produce any types.`);
|
|
54
58
|
}
|
|
55
|
-
const outputContent = typeDeclarations.join('\n\n'); // Add extra newline between interfaces
|
|
59
|
+
const outputContent = typeDeclarations.join('\n\n'); // Add extra newline between interfaces for readability
|
|
56
60
|
// Ensure the output directory exists
|
|
57
61
|
const outputDir = path.dirname(config.typesOutputFile);
|
|
58
62
|
await fs.mkdir(outputDir, { recursive: true });
|
|
@@ -61,8 +65,12 @@ export async function generateTypes() {
|
|
|
61
65
|
console.log(chalk.green(`TypeScript types generated successfully at ${config.typesOutputFile}`));
|
|
62
66
|
}
|
|
63
67
|
catch (error) {
|
|
64
|
-
console.error(chalk.red('
|
|
68
|
+
console.error(chalk.red.bold('\nError generating TypeScript types:'));
|
|
65
69
|
console.error(chalk.red(error.message));
|
|
70
|
+
// It's good practice to log the stack for unexpected errors if not already done by a higher-level handler
|
|
71
|
+
// if (error.stack) {
|
|
72
|
+
// console.error(chalk.gray(error.stack));
|
|
73
|
+
// }
|
|
66
74
|
process.exit(1); // Exit with error code
|
|
67
75
|
}
|
|
68
76
|
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@contentstorage/core",
|
|
3
3
|
"author": "Kaido Hussar <kaidohus@gmail.com>",
|
|
4
4
|
"homepage": "https://contentstorage.app",
|
|
5
|
-
"version": "0.3.
|
|
5
|
+
"version": "0.3.11",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"description": "Fetch content from contentstorage and generate TypeScript types",
|
|
8
8
|
"module": "dist/index.js",
|