@contentstorage/core 0.3.4 → 0.3.6

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 CHANGED
@@ -1,4 +1,2 @@
1
- export { loadConfig } from './lib/configLoader';
2
- export type { AppConfig } from './lib/configLoader';
3
- export { generateTypes } from './scripts/generate-types';
4
- export { pullContent } from './scripts/pull-content';
1
+ import { AppConfig } from './lib/configLoader.js';
2
+ export { AppConfig };
package/dist/index.js CHANGED
@@ -1,9 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pullContent = exports.generateTypes = exports.loadConfig = void 0;
4
- var configLoader_1 = require("./lib/configLoader");
5
- Object.defineProperty(exports, "loadConfig", { enumerable: true, get: function () { return configLoader_1.loadConfig; } });
6
- var generate_types_1 = require("./scripts/generate-types");
7
- Object.defineProperty(exports, "generateTypes", { enumerable: true, get: function () { return generate_types_1.generateTypes; } });
8
- var pull_content_1 = require("./scripts/pull-content");
9
- Object.defineProperty(exports, "pullContent", { enumerable: true, get: function () { return pull_content_1.pullContent; } });
1
+ export {};
@@ -3,4 +3,4 @@ export interface AppConfig {
3
3
  contentDir: string;
4
4
  typesOutputFile: string;
5
5
  }
6
- export declare function loadConfig(): AppConfig;
6
+ export declare function loadConfig(): Promise<AppConfig>;
@@ -1,23 +1,18 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.loadConfig = loadConfig;
7
- const path_1 = __importDefault(require("path"));
8
- const fs_1 = __importDefault(require("fs"));
1
+ import path from 'path';
2
+ import fs from 'fs';
9
3
  const DEFAULT_CONFIG = {
10
- contentDir: path_1.default.join('src', 'assets', 'content'),
11
- typesOutputFile: path_1.default.join('src', 'generated', 'content-types.ts'),
4
+ contentDir: path.join('src', 'assets', 'content'),
5
+ typesOutputFile: path.join('src', 'generated', 'content-types.ts'),
12
6
  };
13
- function loadConfig() {
14
- const configPath = path_1.default.resolve(process.cwd(), 'contentstorage.config.ts'); // Look in user's current working dir
7
+ export async function loadConfig() {
8
+ const configPath = path.resolve(process.cwd(), 'contentstorage.config.ts'); // Look in user's current working dir
15
9
  let userConfig = {};
16
- if (fs_1.default.existsSync(configPath)) {
10
+ if (fs.existsSync(configPath)) {
17
11
  try {
18
12
  // Use require for JS config file
19
- const loadedModule = require(configPath);
13
+ const loadedModule = await import(configPath);
20
14
  userConfig = loadedModule.default || loadedModule;
15
+ console.log('Loaded config', JSON.stringify(userConfig));
21
16
  console.log(`Loaded configuration from ${configPath}`);
22
17
  }
23
18
  catch (error) {
@@ -43,8 +38,8 @@ function loadConfig() {
43
38
  // Resolve paths relative to the user's project root (process.cwd())
44
39
  const finalConfig = {
45
40
  contentUrl: mergedConfig.contentUrl,
46
- contentDir: path_1.default.resolve(process.cwd(), mergedConfig.contentDir),
47
- typesOutputFile: path_1.default.resolve(process.cwd(), mergedConfig.typesOutputFile),
41
+ contentDir: path.resolve(process.cwd(), mergedConfig.contentDir),
42
+ typesOutputFile: path.resolve(process.cwd(), mergedConfig.typesOutputFile),
48
43
  };
49
44
  return finalConfig;
50
45
  }
@@ -1,26 +1,20 @@
1
1
  #!/usr/bin/env node
2
- "use strict";
3
2
  // ^ Shebang ensures the script is executed with Node.js
4
- var __importDefault = (this && this.__importDefault) || function (mod) {
5
- return (mod && mod.__esModule) ? mod : { "default": mod };
6
- };
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.generateTypes = generateTypes;
9
- const promises_1 = __importDefault(require("fs/promises"));
10
- const path_1 = __importDefault(require("path"));
11
- const json_to_ts_1 = __importDefault(require("json-to-ts")); // Import the library
12
- const chalk_1 = __importDefault(require("chalk")); // Optional: for colored output
13
- const configLoader_1 = require("../lib/configLoader");
14
- async function generateTypes() {
15
- console.log(chalk_1.default.blue('Starting type generation...'));
16
- const config = (0, configLoader_1.loadConfig)();
17
- console.log(chalk_1.default.gray(`Reading JSON files from: ${config.contentDir}`));
18
- console.log(chalk_1.default.gray(`Saving TypeScript types to: ${config.typesOutputFile}`));
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+ import jsonToTS from 'json-to-ts'; // Import the library
6
+ import chalk from 'chalk'; // Optional: for colored output
7
+ import { loadConfig } from '../lib/configLoader.js';
8
+ export async function generateTypes() {
9
+ console.log(chalk.blue('Starting type generation...'));
10
+ const config = await loadConfig();
11
+ console.log(chalk.gray(`Reading JSON files from: ${config.contentDir}`));
12
+ console.log(chalk.gray(`Saving TypeScript types to: ${config.typesOutputFile}`));
19
13
  try {
20
14
  // Read the content directory
21
15
  let files;
22
16
  try {
23
- files = await promises_1.default.readdir(config.contentDir);
17
+ files = await fs.readdir(config.contentDir);
24
18
  }
25
19
  catch (err) {
26
20
  if (err.code === 'ENOENT') {
@@ -36,10 +30,10 @@ async function generateTypes() {
36
30
  throw new Error(`No JSON files found in ${config.contentDir}.`);
37
31
  }
38
32
  const firstJsonFile = jsonFiles[0];
39
- const jsonFilePath = path_1.default.join(config.contentDir, firstJsonFile);
40
- console.log(chalk_1.default.gray(`Using first JSON file for type generation: ${firstJsonFile}`));
33
+ const jsonFilePath = path.join(config.contentDir, firstJsonFile);
34
+ console.log(chalk.gray(`Using first JSON file for type generation: ${firstJsonFile}`));
41
35
  // Read the first JSON file
42
- const jsonContent = await promises_1.default.readFile(jsonFilePath, 'utf-8');
36
+ const jsonContent = await fs.readFile(jsonFilePath, 'utf-8');
43
37
  // Parse the JSON content
44
38
  let jsonObject;
45
39
  try {
@@ -52,7 +46,7 @@ async function generateTypes() {
52
46
  // jsonToTS returns an array of strings, each being a line of the interface/type
53
47
  // We need to give the root type a name.
54
48
  const rootTypeName = 'RootContentItem'; // Or derive from filename, or make configurable
55
- const typeDeclarations = (0, json_to_ts_1.default)(jsonObject, {
49
+ const typeDeclarations = jsonToTS.default(jsonObject, {
56
50
  rootName: rootTypeName,
57
51
  });
58
52
  if (!typeDeclarations || typeDeclarations.length === 0) {
@@ -60,15 +54,15 @@ async function generateTypes() {
60
54
  }
61
55
  const outputContent = typeDeclarations.join('\n\n'); // Add extra newline between interfaces
62
56
  // Ensure the output directory exists
63
- const outputDir = path_1.default.dirname(config.typesOutputFile);
64
- await promises_1.default.mkdir(outputDir, { recursive: true });
57
+ const outputDir = path.dirname(config.typesOutputFile);
58
+ await fs.mkdir(outputDir, { recursive: true });
65
59
  // Write the generated types to the output file
66
- await promises_1.default.writeFile(config.typesOutputFile, outputContent, 'utf-8');
67
- console.log(chalk_1.default.green(`TypeScript types generated successfully at ${config.typesOutputFile}`));
60
+ await fs.writeFile(config.typesOutputFile, outputContent, 'utf-8');
61
+ console.log(chalk.green(`TypeScript types generated successfully at ${config.typesOutputFile}`));
68
62
  }
69
63
  catch (error) {
70
- console.error(chalk_1.default.red('Error generating TypeScript types:'));
71
- console.error(chalk_1.default.red(error.message));
64
+ console.error(chalk.red('Error generating TypeScript types:'));
65
+ console.error(chalk.red(error.message));
72
66
  process.exit(1); // Exit with error code
73
67
  }
74
68
  }
@@ -1,29 +1,23 @@
1
1
  #!/usr/bin/env node
2
- "use strict";
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod };
5
- };
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.pullContent = pullContent;
8
- const axios_1 = __importDefault(require("axios"));
9
- const promises_1 = __importDefault(require("fs/promises"));
10
- const path_1 = __importDefault(require("path"));
11
- const chalk_1 = __importDefault(require("chalk"));
12
- const configLoader_1 = require("../lib/configLoader");
13
- async function pullContent() {
14
- console.log(chalk_1.default.blue('Starting content pull...'));
15
- const config = (0, configLoader_1.loadConfig)();
16
- console.log(chalk_1.default.gray(`Getting content from: ${config.contentUrl}`));
17
- console.log(chalk_1.default.gray(`Saving content to: ${config.contentDir}`));
2
+ import axios from 'axios';
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+ import chalk from 'chalk';
6
+ import { loadConfig } from '../lib/configLoader.js';
7
+ export async function pullContent() {
8
+ console.log(chalk.blue('Starting content pull...'));
9
+ const config = await loadConfig();
10
+ console.log(chalk.gray(`Getting content from: ${config.contentUrl}`));
11
+ console.log(chalk.gray(`Saving content to: ${config.contentDir}`));
18
12
  try {
19
13
  // Fetch data
20
- const response = await axios_1.default.get(config.contentUrl);
14
+ const response = await axios.get(config.contentUrl);
21
15
  const jsonData = response.data; // Assume axios parses JSON automatically
22
16
  if (!jsonData) {
23
17
  throw new Error('No data received from the URL.');
24
18
  }
25
19
  // Ensure the output directory exists
26
- await promises_1.default.mkdir(config.contentDir, { recursive: true });
20
+ await fs.mkdir(config.contentDir, { recursive: true });
27
21
  // --- How to save the data? ---
28
22
  // Option 1: Save the entire response as one file (e.g., content.json)
29
23
  // const outputPath = path.join(config.contentDir, 'content.json');
@@ -35,35 +29,35 @@ async function pullContent() {
35
29
  throw new Error(`Expected an array of objects from ${config.contentUrl}, but received type ${typeof jsonData}. Cannot save individual files.`);
36
30
  }
37
31
  if (jsonData.length === 0) {
38
- console.log(chalk_1.default.yellow('Received empty array. No files to save.'));
32
+ console.log(chalk.yellow('Received empty array. No files to save.'));
39
33
  return;
40
34
  }
41
- console.log(chalk_1.default.gray(`Received ${jsonData.length} items. Saving individually...`));
35
+ console.log(chalk.gray(`Received ${jsonData.length} items. Saving individually...`));
42
36
  let filesSavedCount = 0;
43
37
  for (let i = 0; i < jsonData.length; i++) {
44
38
  const item = jsonData[i];
45
39
  // Determine filename: use 'id' or 'slug' if available, otherwise use index
46
40
  const filename = `${item.id || item.slug || i}.json`;
47
- const outputPath = path_1.default.join(config.contentDir, filename);
41
+ const outputPath = path.join(config.contentDir, filename);
48
42
  try {
49
- await promises_1.default.writeFile(outputPath, JSON.stringify(item, null, 2));
43
+ await fs.writeFile(outputPath, JSON.stringify(item, null, 2));
50
44
  filesSavedCount++;
51
45
  }
52
46
  catch (writeError) {
53
- console.error(chalk_1.default.red(`Error saving file ${filename}:`), writeError.message);
47
+ console.error(chalk.red(`Error saving file ${filename}:`), writeError.message);
54
48
  // Optionally decide whether to continue or stop on error
55
49
  }
56
50
  }
57
- console.log(chalk_1.default.green(`Successfully saved ${filesSavedCount} files to ${config.contentDir}`));
51
+ console.log(chalk.green(`Successfully saved ${filesSavedCount} files to ${config.contentDir}`));
58
52
  }
59
53
  catch (error) {
60
- console.error(chalk_1.default.red('Error fetching or saving content:'));
61
- if (axios_1.default.isAxiosError(error)) {
62
- console.error(chalk_1.default.red(`Status: ${error.response?.status}`));
63
- console.error(chalk_1.default.red(`Data: ${JSON.stringify(error.response?.data)}`));
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)}`));
64
58
  }
65
59
  else {
66
- console.error(chalk_1.default.red(error.message));
60
+ console.error(chalk.red(error.message));
67
61
  }
68
62
  process.exit(1); // Exit with error code
69
63
  }
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.4",
5
+ "version": "0.3.6",
6
6
  "type": "module",
7
7
  "description": "Fetch content from contentstorage and generate TypeScript types",
8
8
  "module": "dist/index.js",