@contentstorage/core 0.3.6 → 0.3.8

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.
@@ -5,7 +5,7 @@ const DEFAULT_CONFIG = {
5
5
  typesOutputFile: path.join('src', 'generated', 'content-types.ts'),
6
6
  };
7
7
  export async function loadConfig() {
8
- const configPath = path.resolve(process.cwd(), 'contentstorage.config.ts'); // Look in user's current working dir
8
+ const configPath = path.resolve(process.cwd(), 'contentstorage.config.js'); // Look in user's current working dir
9
9
  let userConfig = {};
10
10
  if (fs.existsSync(configPath)) {
11
11
  try {
@@ -23,42 +23,40 @@ export async function pullContent() {
23
23
  // const outputPath = path.join(config.contentDir, 'content.json');
24
24
  // await fs.writeFile(outputPath, JSON.stringify(jsonData, null, 2));
25
25
  // console.log(chalk.green(`Content saved successfully to ${outputPath}`));
26
- // Option 2: Assume the response is an ARRAY of objects, save each as a separate file
27
- // This matches the requirement "use the first file from the directory" later
28
- if (!Array.isArray(jsonData)) {
29
- throw new Error(`Expected an array of objects from ${config.contentUrl}, but received type ${typeof jsonData}. Cannot save individual files.`);
30
- }
31
- if (jsonData.length === 0) {
32
- console.log(chalk.yellow('Received empty array. No files to save.'));
26
+ // Check if jsonData is a single, non-null object (and not an array)
27
+ if (typeof jsonData !== 'object' || Array.isArray(jsonData)) {
28
+ console.log(chalk.red(`Expected a single JSON object from ${config.contentUrl}, but received type ${Array.isArray(jsonData) ? 'array' : typeof jsonData}. Cannot save the file.`));
33
29
  return;
34
30
  }
35
- console.log(chalk.gray(`Received ${jsonData.length} items. Saving individually...`));
36
- let filesSavedCount = 0;
37
- for (let i = 0; i < jsonData.length; i++) {
38
- const item = jsonData[i];
39
- // Determine filename: use 'id' or 'slug' if available, otherwise use index
40
- const filename = `${item.id || item.slug || i}.json`;
41
- const outputPath = path.join(config.contentDir, filename);
42
- try {
43
- await fs.writeFile(outputPath, JSON.stringify(item, null, 2));
44
- filesSavedCount++;
31
+ console.log(chalk.green(`Received JSON. Saving it to ${config.contentDir}`));
32
+ // Determine filename for the single object.
33
+ // Use 'id' or 'slug' from the object if available, otherwise use 'data' as a fallback base name.
34
+ const objectData = jsonData; // Use type assertion to access potential properties
35
+ const baseFilename = objectData.id || objectData.slug || 'data';
36
+ const filename = `${baseFilename}.json`;
37
+ const outputPath = path.join(config.contentDir, filename);
38
+ console.log(chalk.gray(`Received a single JSON object. Attempting to save as ${filename}...`));
39
+ try {
40
+ // Ensure the output directory exists
41
+ await fs.mkdir(config.contentDir, { recursive: true });
42
+ // Write the single JSON object to the file
43
+ await fs.writeFile(outputPath, JSON.stringify(jsonData, null, 2));
44
+ console.log(chalk.green(`Successfully saved the object to ${outputPath}`));
45
+ }
46
+ catch (error) {
47
+ console.error(chalk.red('Error fetching or saving content:'));
48
+ if (axios.isAxiosError(error)) {
49
+ console.error(chalk.red(`Status: ${error.response?.status}`));
50
+ console.error(chalk.red(`Data: ${JSON.stringify(error.response?.data)}`));
45
51
  }
46
- catch (writeError) {
47
- console.error(chalk.red(`Error saving file ${filename}:`), writeError.message);
48
- // Optionally decide whether to continue or stop on error
52
+ else {
53
+ console.error(chalk.red(error.message));
49
54
  }
55
+ process.exit(1); // Exit with error code
50
56
  }
51
- console.log(chalk.green(`Successfully saved ${filesSavedCount} files to ${config.contentDir}`));
52
57
  }
53
58
  catch (error) {
54
- console.error(chalk.red('Error fetching or saving content:'));
55
- if (axios.isAxiosError(error)) {
56
- console.error(chalk.red(`Status: ${error.response?.status}`));
57
- console.error(chalk.red(`Data: ${JSON.stringify(error.response?.data)}`));
58
- }
59
- else {
60
- console.error(chalk.red(error.message));
61
- }
59
+ console.error(chalk.red(error.message));
62
60
  process.exit(1); // Exit with error code
63
61
  }
64
62
  }
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.6",
5
+ "version": "0.3.8",
6
6
  "type": "module",
7
7
  "description": "Fetch content from contentstorage and generate TypeScript types",
8
8
  "module": "dist/index.js",