@mexty/cli 1.1.0 ā 1.3.0
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/commands/create.d.ts +9 -0
- package/dist/commands/create.d.ts.map +1 -0
- package/dist/commands/create.js +118 -0
- package/dist/commands/create.js.map +1 -0
- package/dist/commands/delete.d.ts +2 -0
- package/dist/commands/delete.d.ts.map +1 -0
- package/dist/commands/delete.js +59 -0
- package/dist/commands/delete.js.map +1 -0
- package/dist/commands/fork.d.ts +2 -0
- package/dist/commands/fork.d.ts.map +1 -0
- package/dist/commands/fork.js +57 -0
- package/dist/commands/fork.js.map +1 -0
- package/dist/commands/login.d.ts +2 -0
- package/dist/commands/login.d.ts.map +1 -0
- package/dist/commands/login.js +90 -0
- package/dist/commands/login.js.map +1 -0
- package/dist/commands/publish.d.ts +2 -0
- package/dist/commands/publish.d.ts.map +1 -0
- package/dist/commands/publish.js +144 -0
- package/dist/commands/publish.js.map +1 -0
- package/dist/commands/save.d.ts +2 -0
- package/dist/commands/save.d.ts.map +1 -0
- package/dist/commands/save.js +162 -0
- package/dist/commands/save.js.map +1 -0
- package/dist/commands/sync.d.ts +2 -0
- package/dist/commands/sync.d.ts.map +1 -0
- package/dist/commands/sync.js +250 -0
- package/dist/commands/sync.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/api.d.ts +81 -0
- package/dist/utils/api.d.ts.map +1 -0
- package/dist/utils/api.js +161 -0
- package/dist/utils/api.js.map +1 -0
- package/dist/utils/auth.d.ts +4 -0
- package/dist/utils/auth.d.ts.map +1 -0
- package/dist/utils/auth.js +27 -0
- package/dist/utils/auth.js.map +1 -0
- package/dist/utils/git.d.ts +42 -0
- package/dist/utils/git.d.ts.map +1 -0
- package/dist/utils/git.js +171 -0
- package/dist/utils/git.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/sync.ts +145 -79
- package/src/utils/api.ts +65 -32
package/src/commands/sync.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
import chalk from
|
2
|
-
import fs from
|
3
|
-
import path from
|
4
|
-
import { apiClient } from
|
1
|
+
import chalk from "chalk";
|
2
|
+
import fs from "fs";
|
3
|
+
import path from "path";
|
4
|
+
import { apiClient } from "../utils/api";
|
5
5
|
|
6
6
|
interface BlockRegistryEntry {
|
7
7
|
blockId: string;
|
@@ -26,14 +26,16 @@ interface AuthorNamespaceRegistry {
|
|
26
26
|
|
27
27
|
export async function syncCommand(): Promise<void> {
|
28
28
|
try {
|
29
|
-
console.log(chalk.blue(
|
29
|
+
console.log(chalk.blue("š Syncing block registry..."));
|
30
30
|
|
31
31
|
// Fetch registry from server
|
32
|
-
console.log(chalk.yellow(
|
33
|
-
const response = await fetch(
|
34
|
-
|
32
|
+
console.log(chalk.yellow("š” Fetching registry from server..."));
|
33
|
+
const response = await fetch("https://api.mexty.ai/api/blocks/registry");
|
34
|
+
|
35
35
|
if (!response.ok) {
|
36
|
-
throw new Error(
|
36
|
+
throw new Error(
|
37
|
+
`Server responded with ${response.status}: ${response.statusText}`
|
38
|
+
);
|
37
39
|
}
|
38
40
|
|
39
41
|
const data = await response.json();
|
@@ -48,25 +50,43 @@ export async function syncCommand(): Promise<void> {
|
|
48
50
|
console.log(chalk.gray(` Last updated: ${meta.lastUpdated}`));
|
49
51
|
|
50
52
|
if (Object.keys(registry).length === 0) {
|
51
|
-
console.log(chalk.yellow(
|
52
|
-
console.log(
|
53
|
+
console.log(chalk.yellow("ā ļø No components found in registry."));
|
54
|
+
console.log(
|
55
|
+
chalk.gray(
|
56
|
+
' Make sure you have built some blocks first using "mexty publish"'
|
57
|
+
)
|
58
|
+
);
|
53
59
|
return;
|
54
60
|
}
|
55
61
|
|
56
62
|
// Display available components
|
57
|
-
console.log(chalk.blue(
|
63
|
+
console.log(chalk.blue("\nš Available components (global namespace):"));
|
58
64
|
for (const [componentName, entry] of Object.entries(registry)) {
|
59
|
-
console.log(
|
65
|
+
console.log(
|
66
|
+
chalk.green(
|
67
|
+
` ${componentName}${
|
68
|
+
entry.author ? chalk.gray(` (by ${entry.author})`) : ""
|
69
|
+
}`
|
70
|
+
)
|
71
|
+
);
|
60
72
|
console.log(chalk.gray(` Block ID: ${entry.blockId}`));
|
61
73
|
console.log(chalk.gray(` Title: ${entry.title}`));
|
62
|
-
console.log(
|
63
|
-
|
64
|
-
|
74
|
+
console.log(
|
75
|
+
chalk.gray(
|
76
|
+
` Description: ${entry.description.substring(0, 60)}${
|
77
|
+
entry.description.length > 60 ? "..." : ""
|
78
|
+
}`
|
79
|
+
)
|
80
|
+
);
|
81
|
+
console.log(
|
82
|
+
chalk.gray(` Tags: ${entry.tags?.join(", ") || "none"}`)
|
83
|
+
);
|
84
|
+
console.log("");
|
65
85
|
}
|
66
86
|
|
67
87
|
// Display author namespaces
|
68
88
|
if (Object.keys(authorRegistry).length > 0) {
|
69
|
-
console.log(chalk.blue(
|
89
|
+
console.log(chalk.blue("\nš¤ Author namespaces:"));
|
70
90
|
for (const [author, components] of Object.entries(authorRegistry)) {
|
71
91
|
console.log(chalk.cyan(` @${author}:`));
|
72
92
|
for (const [componentName, entry] of Object.entries(components)) {
|
@@ -74,25 +94,30 @@ export async function syncCommand(): Promise<void> {
|
|
74
94
|
console.log(chalk.gray(` Block ID: ${entry.blockId}`));
|
75
95
|
console.log(chalk.gray(` Title: ${entry.title}`));
|
76
96
|
}
|
77
|
-
console.log(
|
97
|
+
console.log("");
|
78
98
|
}
|
79
99
|
}
|
80
100
|
|
81
101
|
// Generate named exports file and author entry files
|
82
102
|
const mextBlockPath = findMextBlockPath();
|
83
103
|
if (mextBlockPath) {
|
84
|
-
console.log(chalk.blue(
|
104
|
+
console.log(chalk.blue("š§ Updating mext-block exports..."));
|
85
105
|
await generateNamedExports(mextBlockPath, registry, authorRegistry);
|
86
106
|
await generateAuthorEntryFiles(mextBlockPath, authorRegistry);
|
87
107
|
console.log(chalk.green(`ā
Exports updated in ${mextBlockPath}`));
|
88
|
-
console.log(
|
108
|
+
console.log(
|
109
|
+
chalk.gray(
|
110
|
+
` Generated ${
|
111
|
+
Object.keys(authorRegistry).length
|
112
|
+
} author entry files`
|
113
|
+
)
|
114
|
+
);
|
89
115
|
} else {
|
90
|
-
console.log(chalk.yellow(
|
91
|
-
console.log(chalk.gray(
|
116
|
+
console.log(chalk.yellow("ā ļø mext-block package not found locally."));
|
117
|
+
console.log(chalk.gray(" Named exports file not generated."));
|
92
118
|
}
|
93
119
|
|
94
|
-
console.log(chalk.green(
|
95
|
-
|
120
|
+
console.log(chalk.green("\nš Registry sync completed!"));
|
96
121
|
} catch (error: any) {
|
97
122
|
console.error(chalk.red(`ā Failed to sync registry: ${error.message}`));
|
98
123
|
process.exit(1);
|
@@ -105,16 +130,21 @@ export async function syncCommand(): Promise<void> {
|
|
105
130
|
function findMextBlockPath(): string | null {
|
106
131
|
// Look for mext-block in common locations
|
107
132
|
const possiblePaths = [
|
108
|
-
path.join(process.cwd(),
|
109
|
-
path.join(process.cwd(),
|
110
|
-
path.join(process.cwd(),
|
133
|
+
path.join(process.cwd(), "..", "mext-block"),
|
134
|
+
path.join(process.cwd(), "mext-block"),
|
135
|
+
path.join(process.cwd(), "..", "..", "mext-block"),
|
111
136
|
];
|
112
137
|
|
113
138
|
for (const possiblePath of possiblePaths) {
|
114
|
-
if (fs.existsSync(path.join(possiblePath,
|
139
|
+
if (fs.existsSync(path.join(possiblePath, "package.json"))) {
|
115
140
|
try {
|
116
|
-
const packageJson = JSON.parse(
|
117
|
-
|
141
|
+
const packageJson = JSON.parse(
|
142
|
+
fs.readFileSync(path.join(possiblePath, "package.json"), "utf8")
|
143
|
+
);
|
144
|
+
if (
|
145
|
+
packageJson.name === "@mexty/block" ||
|
146
|
+
packageJson.name === "@mexty/block"
|
147
|
+
) {
|
118
148
|
return possiblePath;
|
119
149
|
}
|
120
150
|
} catch (error) {
|
@@ -129,9 +159,13 @@ function findMextBlockPath(): string | null {
|
|
129
159
|
/**
|
130
160
|
* Generate named exports file for the mext-block package
|
131
161
|
*/
|
132
|
-
async function generateNamedExports(
|
133
|
-
|
134
|
-
|
162
|
+
async function generateNamedExports(
|
163
|
+
mextBlockPath: string,
|
164
|
+
registry: BlockRegistry,
|
165
|
+
authorRegistry: AuthorNamespaceRegistry
|
166
|
+
): Promise<void> {
|
167
|
+
const namedExportsPath = path.join(mextBlockPath, "src", "namedExports.ts");
|
168
|
+
|
135
169
|
// Generate the file content
|
136
170
|
const content = `// Auto-generated file - DO NOT EDIT MANUALLY
|
137
171
|
// Generated on: ${new Date().toISOString()}
|
@@ -142,22 +176,27 @@ import { createNamedBlock } from './components/NamedBlock';
|
|
142
176
|
import { createAuthorBlock } from './components/AuthorBlock';
|
143
177
|
|
144
178
|
// ===== GLOBAL NAMESPACE COMPONENTS =====
|
145
|
-
${Object.entries(registry)
|
146
|
-
|
179
|
+
${Object.entries(registry)
|
180
|
+
.map(
|
181
|
+
([componentName, entry]) =>
|
182
|
+
`// ${entry.title}${entry.author ? ` (by ${entry.author})` : ""}
|
147
183
|
// Block ID: ${entry.blockId}
|
148
184
|
// Description: ${entry.description}
|
149
|
-
// Tags: ${entry.tags?.join(
|
185
|
+
// Tags: ${entry.tags?.join(", ") || "none"}
|
150
186
|
export const ${componentName} = createNamedBlock('${componentName}');`
|
151
|
-
)
|
187
|
+
)
|
188
|
+
.join("\n\n")}
|
152
189
|
|
153
190
|
// Export all global components as an object for convenience
|
154
191
|
export const NamedComponents = {
|
155
|
-
${Object.keys(registry)
|
192
|
+
${Object.keys(registry)
|
193
|
+
.map((componentName) => ` ${componentName},`)
|
194
|
+
.join("\n")}
|
156
195
|
};
|
157
196
|
|
158
197
|
// Note: Author-specific components are now available via direct imports:
|
159
198
|
// import { ComponentName } from '@mexty/block/authorname'
|
160
|
-
// Available authors: ${Object.keys(authorRegistry).join(
|
199
|
+
// Available authors: ${Object.keys(authorRegistry).join(", ")}
|
161
200
|
|
162
201
|
// Registry metadata
|
163
202
|
export const registryMetadata = {
|
@@ -165,31 +204,40 @@ export const registryMetadata = {
|
|
165
204
|
totalAuthors: ${Object.keys(authorRegistry).length},
|
166
205
|
lastGenerated: '${new Date().toISOString()}',
|
167
206
|
components: {
|
168
|
-
${Object.entries(registry)
|
169
|
-
|
207
|
+
${Object.entries(registry)
|
208
|
+
.map(
|
209
|
+
([componentName, entry]) =>
|
210
|
+
` ${componentName}: {
|
170
211
|
blockId: '${entry.blockId}',
|
171
212
|
title: '${entry.title}',
|
172
213
|
description: '${entry.description.replace(/'/g, "\\'")}',
|
173
|
-
author: '${entry.author ||
|
174
|
-
tags: [${entry.tags?.map(tag => `'${tag}'`).join(
|
214
|
+
author: '${entry.author || ""}',
|
215
|
+
tags: [${entry.tags?.map((tag) => `'${tag}'`).join(", ") || ""}],
|
175
216
|
lastUpdated: '${entry.lastUpdated}'
|
176
217
|
},`
|
177
|
-
)
|
218
|
+
)
|
219
|
+
.join("\n")}
|
178
220
|
},
|
179
221
|
authors: {
|
180
|
-
${Object.entries(authorRegistry)
|
181
|
-
|
182
|
-
|
183
|
-
`
|
222
|
+
${Object.entries(authorRegistry)
|
223
|
+
.map(
|
224
|
+
([author, components]) =>
|
225
|
+
` ${author}: {
|
226
|
+
${Object.entries(components)
|
227
|
+
.map(
|
228
|
+
([componentName, entry]) =>
|
229
|
+
` ${componentName}: {
|
184
230
|
blockId: '${entry.blockId}',
|
185
231
|
title: '${entry.title}',
|
186
232
|
description: '${entry.description.replace(/'/g, "\\'")}',
|
187
|
-
tags: [${entry.tags?.map(tag => `'${tag}'`).join(
|
233
|
+
tags: [${entry.tags?.map((tag) => `'${tag}'`).join(", ") || ""}],
|
188
234
|
lastUpdated: '${entry.lastUpdated}'
|
189
235
|
},`
|
190
|
-
)
|
236
|
+
)
|
237
|
+
.join("\n")}
|
191
238
|
},`
|
192
|
-
)
|
239
|
+
)
|
240
|
+
.join("\n")}
|
193
241
|
}
|
194
242
|
};
|
195
243
|
`;
|
@@ -201,17 +249,18 @@ ${Object.entries(components).map(([componentName, entry]) =>
|
|
201
249
|
}
|
202
250
|
|
203
251
|
// Write the file
|
204
|
-
fs.writeFileSync(namedExportsPath, content,
|
252
|
+
fs.writeFileSync(namedExportsPath, content, "utf8");
|
205
253
|
|
206
254
|
// Update the main index.ts to include these exports
|
207
|
-
const indexPath = path.join(mextBlockPath,
|
255
|
+
const indexPath = path.join(mextBlockPath, "src", "index.ts");
|
208
256
|
if (fs.existsSync(indexPath)) {
|
209
|
-
let indexContent = fs.readFileSync(indexPath,
|
210
|
-
|
257
|
+
let indexContent = fs.readFileSync(indexPath, "utf8");
|
258
|
+
|
211
259
|
// Add export for named exports if not already present
|
212
|
-
if (!indexContent.includes(
|
213
|
-
indexContent +=
|
214
|
-
|
260
|
+
if (!indexContent.includes("export * from './namedExports'")) {
|
261
|
+
indexContent +=
|
262
|
+
"\n// Auto-generated named exports\nexport * from './namedExports';\n";
|
263
|
+
fs.writeFileSync(indexPath, indexContent, "utf8");
|
215
264
|
}
|
216
265
|
}
|
217
266
|
}
|
@@ -219,24 +268,27 @@ ${Object.entries(components).map(([componentName, entry]) =>
|
|
219
268
|
/**
|
220
269
|
* Generate author-specific entry files for direct imports
|
221
270
|
*/
|
222
|
-
async function generateAuthorEntryFiles(
|
223
|
-
|
224
|
-
|
271
|
+
async function generateAuthorEntryFiles(
|
272
|
+
mextBlockPath: string,
|
273
|
+
authorRegistry: AuthorNamespaceRegistry
|
274
|
+
): Promise<void> {
|
275
|
+
const authorsDir = path.join(mextBlockPath, "src", "authors");
|
276
|
+
|
225
277
|
// Clean up existing author files
|
226
278
|
if (fs.existsSync(authorsDir)) {
|
227
279
|
fs.rmSync(authorsDir, { recursive: true, force: true });
|
228
280
|
}
|
229
|
-
|
281
|
+
|
230
282
|
// Create authors directory
|
231
283
|
fs.mkdirSync(authorsDir, { recursive: true });
|
232
|
-
|
284
|
+
|
233
285
|
// Generate an entry file for each author
|
234
286
|
for (const [author, components] of Object.entries(authorRegistry)) {
|
235
287
|
const authorDir = path.join(authorsDir, author);
|
236
288
|
fs.mkdirSync(authorDir, { recursive: true });
|
237
|
-
|
238
|
-
const authorEntryPath = path.join(authorDir,
|
239
|
-
|
289
|
+
|
290
|
+
const authorEntryPath = path.join(authorDir, "index.ts");
|
291
|
+
|
240
292
|
// Generate the author's entry file content
|
241
293
|
const content = `// Auto-generated author entry file for ${author}
|
242
294
|
// Generated on: ${new Date().toISOString()}
|
@@ -244,17 +296,22 @@ async function generateAuthorEntryFiles(mextBlockPath: string, authorRegistry: A
|
|
244
296
|
|
245
297
|
import { createAuthorBlock } from '../../components/AuthorBlock';
|
246
298
|
|
247
|
-
${Object.entries(components)
|
248
|
-
|
299
|
+
${Object.entries(components)
|
300
|
+
.map(
|
301
|
+
([componentName, entry]) =>
|
302
|
+
`// ${entry.title}
|
249
303
|
// Block ID: ${entry.blockId}
|
250
304
|
// Description: ${entry.description}
|
251
|
-
// Tags: ${entry.tags?.join(
|
305
|
+
// Tags: ${entry.tags?.join(", ") || "none"}
|
252
306
|
export const ${componentName} = createAuthorBlock('${author}', '${componentName}');`
|
253
|
-
)
|
307
|
+
)
|
308
|
+
.join("\n\n")}
|
254
309
|
|
255
310
|
// Export all components as default for convenience
|
256
311
|
export default {
|
257
|
-
${Object.keys(components)
|
312
|
+
${Object.keys(components)
|
313
|
+
.map((componentName) => ` ${componentName},`)
|
314
|
+
.join("\n")}
|
258
315
|
};
|
259
316
|
|
260
317
|
// Author metadata
|
@@ -263,22 +320,31 @@ export const authorMetadata = {
|
|
263
320
|
totalComponents: ${Object.keys(components).length},
|
264
321
|
lastGenerated: '${new Date().toISOString()}',
|
265
322
|
components: {
|
266
|
-
${Object.entries(components)
|
267
|
-
|
323
|
+
${Object.entries(components)
|
324
|
+
.map(
|
325
|
+
([componentName, entry]) =>
|
326
|
+
` ${componentName}: {
|
268
327
|
blockId: '${entry.blockId}',
|
269
328
|
title: '${entry.title}',
|
270
329
|
description: '${entry.description.replace(/'/g, "\\'")}',
|
271
|
-
tags: [${entry.tags?.map(tag => `'${tag}'`).join(
|
330
|
+
tags: [${entry.tags?.map((tag) => `'${tag}'`).join(", ") || ""}],
|
272
331
|
lastUpdated: '${entry.lastUpdated}'
|
273
332
|
},`
|
274
|
-
)
|
333
|
+
)
|
334
|
+
.join("\n")}
|
275
335
|
}
|
276
336
|
};
|
277
337
|
`;
|
278
338
|
|
279
339
|
// Write the author entry file
|
280
|
-
fs.writeFileSync(authorEntryPath, content,
|
281
|
-
|
282
|
-
console.log(
|
340
|
+
fs.writeFileSync(authorEntryPath, content, "utf8");
|
341
|
+
|
342
|
+
console.log(
|
343
|
+
chalk.gray(
|
344
|
+
` Created entry file for ${author} (${
|
345
|
+
Object.keys(components).length
|
346
|
+
} components)`
|
347
|
+
)
|
348
|
+
);
|
283
349
|
}
|
284
|
-
}
|
350
|
+
}
|
package/src/utils/api.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
import axios, { AxiosInstance, AxiosResponse } from
|
2
|
-
import chalk from
|
3
|
-
import fs from
|
4
|
-
import path from
|
5
|
-
import os from
|
1
|
+
import axios, { AxiosInstance, AxiosResponse } from "axios";
|
2
|
+
import chalk from "chalk";
|
3
|
+
import fs from "fs";
|
4
|
+
import path from "path";
|
5
|
+
import os from "os";
|
6
6
|
|
7
7
|
export interface Block {
|
8
8
|
_id: string;
|
@@ -14,13 +14,13 @@ export interface Block {
|
|
14
14
|
courseName?: string;
|
15
15
|
allowedBrickTypes: string[];
|
16
16
|
allowedBlockTypes?: string[];
|
17
|
-
scope: (
|
17
|
+
scope: ("library" | "user-store" | "published-store")[];
|
18
18
|
content: any[];
|
19
19
|
// Fork tracking
|
20
20
|
forkedId?: string;
|
21
21
|
bundlePath?: string;
|
22
22
|
federationUrl?: string;
|
23
|
-
buildStatus?:
|
23
|
+
buildStatus?: "pending" | "building" | "success" | "failed";
|
24
24
|
buildError?: string;
|
25
25
|
lastBuilt?: Date;
|
26
26
|
createdAt: Date;
|
@@ -73,15 +73,15 @@ class ApiClient {
|
|
73
73
|
private baseUrl: string;
|
74
74
|
private tokenPath: string;
|
75
75
|
|
76
|
-
constructor(baseUrl: string =
|
76
|
+
constructor(baseUrl: string = "https://api.mexty.ai") {
|
77
77
|
this.baseUrl = baseUrl;
|
78
|
-
this.tokenPath = path.join(os.homedir(),
|
79
|
-
|
78
|
+
this.tokenPath = path.join(os.homedir(), ".mext", "auth.json");
|
79
|
+
|
80
80
|
this.client = axios.create({
|
81
81
|
baseURL: baseUrl,
|
82
82
|
timeout: 30000,
|
83
83
|
headers: {
|
84
|
-
|
84
|
+
"Content-Type": "application/json",
|
85
85
|
},
|
86
86
|
});
|
87
87
|
|
@@ -102,13 +102,27 @@ class ApiClient {
|
|
102
102
|
(response) => response,
|
103
103
|
(error) => {
|
104
104
|
if (error.response?.status === 401) {
|
105
|
-
console.error(
|
105
|
+
console.error(
|
106
|
+
chalk.red(
|
107
|
+
"Authentication required. Please login first: mexty login"
|
108
|
+
)
|
109
|
+
);
|
106
110
|
this.clearStoredToken();
|
107
111
|
} else if (error.response) {
|
108
|
-
console.error(
|
112
|
+
console.error(
|
113
|
+
chalk.red(
|
114
|
+
`API Error ${error.response.status}: ${
|
115
|
+
error.response.data?.error || error.message
|
116
|
+
}`
|
117
|
+
)
|
118
|
+
);
|
109
119
|
} else if (error.request) {
|
110
|
-
console.error(
|
111
|
-
|
120
|
+
console.error(
|
121
|
+
chalk.red("Network Error: Could not reach MEXT server")
|
122
|
+
);
|
123
|
+
console.error(
|
124
|
+
chalk.yellow(`Make sure the server is running at ${this.baseUrl}`)
|
125
|
+
);
|
112
126
|
} else {
|
113
127
|
console.error(chalk.red(`Request Error: ${error.message}`));
|
114
128
|
}
|
@@ -120,7 +134,7 @@ class ApiClient {
|
|
120
134
|
private getStoredToken(): string | null {
|
121
135
|
try {
|
122
136
|
if (fs.existsSync(this.tokenPath)) {
|
123
|
-
const authData = JSON.parse(fs.readFileSync(this.tokenPath,
|
137
|
+
const authData = JSON.parse(fs.readFileSync(this.tokenPath, "utf8"));
|
124
138
|
return authData.token || null;
|
125
139
|
}
|
126
140
|
} catch (error) {
|
@@ -135,16 +149,18 @@ class ApiClient {
|
|
135
149
|
if (!fs.existsSync(authDir)) {
|
136
150
|
fs.mkdirSync(authDir, { recursive: true });
|
137
151
|
}
|
138
|
-
|
152
|
+
|
139
153
|
const authData = {
|
140
154
|
token,
|
141
155
|
user,
|
142
|
-
timestamp: new Date().toISOString()
|
156
|
+
timestamp: new Date().toISOString(),
|
143
157
|
};
|
144
|
-
|
158
|
+
|
145
159
|
fs.writeFileSync(this.tokenPath, JSON.stringify(authData, null, 2));
|
146
160
|
} catch (error: any) {
|
147
|
-
console.warn(
|
161
|
+
console.warn(
|
162
|
+
chalk.yellow(`Warning: Could not store auth token: ${error.message}`)
|
163
|
+
);
|
148
164
|
}
|
149
165
|
}
|
150
166
|
|
@@ -165,7 +181,7 @@ class ApiClient {
|
|
165
181
|
public getStoredUser(): any {
|
166
182
|
try {
|
167
183
|
if (fs.existsSync(this.tokenPath)) {
|
168
|
-
const authData = JSON.parse(fs.readFileSync(this.tokenPath,
|
184
|
+
const authData = JSON.parse(fs.readFileSync(this.tokenPath, "utf8"));
|
169
185
|
return authData.user || null;
|
170
186
|
}
|
171
187
|
} catch (error) {
|
@@ -175,12 +191,18 @@ class ApiClient {
|
|
175
191
|
}
|
176
192
|
|
177
193
|
async createBlock(data: CreateBlockRequest): Promise<Block> {
|
178
|
-
const response: AxiosResponse<Block> = await this.client.post(
|
194
|
+
const response: AxiosResponse<Block> = await this.client.post(
|
195
|
+
"/api/blocks",
|
196
|
+
data
|
197
|
+
);
|
179
198
|
return response.data;
|
180
199
|
}
|
181
200
|
|
182
201
|
async forkBlock(data: ForkBlockRequest): Promise<Block> {
|
183
|
-
const response: AxiosResponse<Block> = await this.client.post(
|
202
|
+
const response: AxiosResponse<Block> = await this.client.post(
|
203
|
+
"/api/blocks/fork",
|
204
|
+
data
|
205
|
+
);
|
184
206
|
return response.data;
|
185
207
|
}
|
186
208
|
|
@@ -189,18 +211,23 @@ class ApiClient {
|
|
189
211
|
}
|
190
212
|
|
191
213
|
async getBlock(blockId: string): Promise<Block> {
|
192
|
-
const response: AxiosResponse<Block> = await this.client.get(
|
214
|
+
const response: AxiosResponse<Block> = await this.client.get(
|
215
|
+
`/api/blocks/${blockId}`
|
216
|
+
);
|
193
217
|
return response.data;
|
194
218
|
}
|
195
219
|
|
196
220
|
async saveAndBundle(data: SaveAndBundleRequest): Promise<any> {
|
197
|
-
const response = await this.client.post(
|
221
|
+
const response = await this.client.post(
|
222
|
+
"/api/blocks/save-and-bundle",
|
223
|
+
data
|
224
|
+
);
|
198
225
|
return response.data;
|
199
226
|
}
|
200
227
|
|
201
228
|
async healthCheck(): Promise<boolean> {
|
202
229
|
try {
|
203
|
-
await this.client.get(
|
230
|
+
await this.client.get("/api/health");
|
204
231
|
return true;
|
205
232
|
} catch (error) {
|
206
233
|
return false;
|
@@ -208,24 +235,30 @@ class ApiClient {
|
|
208
235
|
}
|
209
236
|
|
210
237
|
async requestOTP(email: string): Promise<AuthResponse> {
|
211
|
-
const response: AxiosResponse<AuthResponse> = await this.client.post(
|
238
|
+
const response: AxiosResponse<AuthResponse> = await this.client.post(
|
239
|
+
"/api/auth/request-otp",
|
240
|
+
{ email }
|
241
|
+
);
|
212
242
|
return response.data;
|
213
243
|
}
|
214
244
|
|
215
245
|
async verifyOTP(email: string, otp: string): Promise<AuthResponse> {
|
216
|
-
const response: AxiosResponse<AuthResponse> = await this.client.post(
|
246
|
+
const response: AxiosResponse<AuthResponse> = await this.client.post(
|
247
|
+
"/api/auth/verify-otp",
|
248
|
+
{ email, otp }
|
249
|
+
);
|
217
250
|
const data = response.data;
|
218
|
-
|
251
|
+
|
219
252
|
if (data.success && data.token && data.user) {
|
220
253
|
this.storeToken(data.token, data.user);
|
221
254
|
}
|
222
|
-
|
255
|
+
|
223
256
|
return data;
|
224
257
|
}
|
225
258
|
|
226
259
|
async logout(): Promise<void> {
|
227
260
|
try {
|
228
|
-
await this.client.post(
|
261
|
+
await this.client.post("/api/auth/logout");
|
229
262
|
} catch (error) {
|
230
263
|
// Ignore logout errors
|
231
264
|
} finally {
|
@@ -239,4 +272,4 @@ class ApiClient {
|
|
239
272
|
}
|
240
273
|
}
|
241
274
|
|
242
|
-
export const apiClient = new ApiClient();
|
275
|
+
export const apiClient = new ApiClient();
|