@contentstorage/core 0.3.3 → 0.3.5
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/lib/configLoader.js +10 -15
- package/dist/scripts/generate-types.js +22 -27
- package/dist/scripts/pull-content.js +24 -29
- package/package.json +2 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -9
package/dist/lib/configLoader.js
CHANGED
|
@@ -1,23 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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:
|
|
11
|
-
typesOutputFile:
|
|
4
|
+
contentDir: path.join('src', 'assets', 'content'),
|
|
5
|
+
typesOutputFile: path.join('src', 'generated', 'content-types.ts'),
|
|
12
6
|
};
|
|
13
|
-
function loadConfig() {
|
|
14
|
-
const configPath =
|
|
7
|
+
export 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 (
|
|
10
|
+
if (fs.existsSync(configPath)) {
|
|
17
11
|
try {
|
|
18
12
|
// Use require for JS config file
|
|
19
13
|
const loadedModule = require(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:
|
|
47
|
-
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
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 = 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
|
|
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 =
|
|
40
|
-
console.log(
|
|
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
|
|
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 =
|
|
49
|
+
const typeDeclarations = jsonToTS.default(jsonObject, {
|
|
56
50
|
rootName: rootTypeName,
|
|
57
51
|
});
|
|
58
52
|
if (!typeDeclarations || typeDeclarations.length === 0) {
|
|
@@ -60,15 +54,16 @@ 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 =
|
|
64
|
-
await
|
|
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
|
|
67
|
-
console.log(
|
|
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(
|
|
71
|
-
console.error(
|
|
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
|
}
|
|
69
|
+
generateTypes();
|
|
@@ -1,29 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
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 = 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
|
|
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
|
|
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,36 +29,37 @@ 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(
|
|
32
|
+
console.log(chalk.yellow('Received empty array. No files to save.'));
|
|
39
33
|
return;
|
|
40
34
|
}
|
|
41
|
-
console.log(
|
|
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 =
|
|
41
|
+
const outputPath = path.join(config.contentDir, filename);
|
|
48
42
|
try {
|
|
49
|
-
await
|
|
43
|
+
await fs.writeFile(outputPath, JSON.stringify(item, null, 2));
|
|
50
44
|
filesSavedCount++;
|
|
51
45
|
}
|
|
52
46
|
catch (writeError) {
|
|
53
|
-
console.error(
|
|
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(
|
|
51
|
+
console.log(chalk.green(`Successfully saved ${filesSavedCount} files to ${config.contentDir}`));
|
|
58
52
|
}
|
|
59
53
|
catch (error) {
|
|
60
|
-
console.error(
|
|
61
|
-
if (
|
|
62
|
-
console.error(
|
|
63
|
-
console.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)}`));
|
|
64
58
|
}
|
|
65
59
|
else {
|
|
66
|
-
console.error(
|
|
60
|
+
console.error(chalk.red(error.message));
|
|
67
61
|
}
|
|
68
62
|
process.exit(1); // Exit with error code
|
|
69
63
|
}
|
|
70
64
|
}
|
|
65
|
+
pullContent();
|
package/package.json
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
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.5",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"description": "Fetch content from contentstorage and generate TypeScript types",
|
|
7
8
|
"module": "dist/index.js",
|
|
8
9
|
"types": "dist/index.d.ts",
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
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; } });
|