-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-item.js
More file actions
161 lines (135 loc) · 4.89 KB
/
Copy pathadd-item.js
File metadata and controls
161 lines (135 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// ts-object-builder.js
const readline = require('readline');
const fs = require('fs');
const path = require('path');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function askQuestion(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
}
// Simple parser for TypeScript interface string
function parseTypeFromString(typeString) {
// Remove interface keyword and clean up
let cleanType = typeString.replace(/interface\s+\w+\s*{/, '').replace(/}$/, '').trim();
const lines = cleanType.split('\n').map(line => line.trim()).filter(line => line);
const schema = {};
for (const line of lines) {
// Parse line like: "id: string," or "title: string"
const match = line.match(/^(\w+)(\?)?\s*:\s*([^;]+)[,;]?$/);
if (match) {
const [, fieldName, optional, fieldType] = match;
schema[fieldName] = {
type: fieldType.trim().replace(/\[\]$/, ''), // Remove [] for arrays
required: !optional,
isArray: fieldType.includes('[]')
};
}
// Handle nested objects like link: { title: string, link: string }
else if (line.includes(': {')) {
const fieldName = line.split(':')[0].trim();
schema[fieldName] = { type: 'object', required: true };
}
}
return schema;
}
async function buildObjectFromSchema(schema, obj = {}) {
for (const [key, field] of Object.entries(schema)) {
if (field.type === 'object') {
// For nested objects, ask for JSON input
console.log(`\nNested object '${key}': Enter as JSON object (e.g., {"title": "text", "link": "url"})`);
const answer = await askQuestion(`Enter ${key}${field.required ? ' (required)' : ''}: `);
try {
obj[key] = answer ? JSON.parse(answer) : {};
} catch (e) {
console.log("Invalid JSON format, setting empty object");
obj[key] = {};
}
} else if (field.isArray) {
console.log(`\nArray field '${key}': Enter as JSON array (e.g., ["item1", "item2"])`);
const answer = await askQuestion(`Enter ${key}${field.required ? ' (required)' : ''}: `);
try {
obj[key] = answer ? JSON.parse(answer) : [];
} catch (e) {
console.log("Invalid JSON format, setting empty array");
obj[key] = [];
}
} else {
const requiredText = field.required ? ' (required)' : ' (optional)';
const answer = await askQuestion(`Enter ${key}${requiredText}: `);
if (answer || field.required) {
obj[key] = answer || '';
}
}
}
return obj;
}
// Function to convert object to JavaScript format (no quotes on keys)
function toJSObject(obj, indent = 0) {
const spaces = ' '.repeat(indent);
if (obj === null) return 'null';
if (Array.isArray(obj)) {
if (obj.length === 0) return '[]';
const items = obj.map(item => toJSObject(item, indent + 1));
return `[\n${items.map(item => ' ' + spaces + item).join(',\n')}\n${spaces}]`;
}
if (typeof obj === 'object') {
const keys = Object.keys(obj);
if (keys.length === 0) return '{}';
const items = keys.map(key => {
const value = toJSObject(obj[key], indent + 1);
return `${key}: ${value}`;
});
return `{\n${items.map(item => ' ' + spaces + item).join(',\n')}\n${spaces}}`;
}
if (typeof obj === 'string') {
return `"${obj}"`;
}
return String(obj);
}
async function main(items) {
console.log("TypeScript Object Builder");
console.log("========================");
const typeString = `
{
id: string,
title: string,
description: string,
detailsDefault?: string,
icon:string,
technology: TechnologyEntryType[],
link:{
title:string,
link:string
}
}
`;
console.log("Parsing type:");
console.log(typeString);
try {
const schema = parseTypeFromString(typeString);
const result = await buildObjectFromSchema(schema);
items.push(result);
const final = {res:items}
console.log("\nGenerated Object:");
console.log(toJSObject(final));
const more = await askQuestion("add more? if yes then type y\n");
if (more==="y"){
await main(items)
}else{
const filename = `result-${Date.now()}.js`;
const fileContent = `module.exports = ${toJSObject(final)};`;
fs.writeFileSync(filename, fileContent);
console.log(`\nResult saved to: ${filename}`);
}
} catch (error) {
console.error("Error:", error.message);
}
rl.close();
}
main([]).catch(console.error);