@vue-skuilder/cli 0.1.6 → 0.1.7

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.
Files changed (52) hide show
  1. package/dist/cli.js +8 -0
  2. package/dist/cli.js.map +1 -1
  3. package/dist/commands/studio.d.ts +3 -0
  4. package/dist/commands/studio.d.ts.map +1 -0
  5. package/dist/commands/studio.js +396 -0
  6. package/dist/commands/studio.js.map +1 -0
  7. package/dist/commands/unpack.d.ts +3 -0
  8. package/dist/commands/unpack.d.ts.map +1 -0
  9. package/dist/commands/unpack.js +228 -0
  10. package/dist/commands/unpack.js.map +1 -0
  11. package/dist/studio-ui-assets/assets/BrowseView-BJbixGOU.js +2 -0
  12. package/dist/studio-ui-assets/assets/BrowseView-BJbixGOU.js.map +1 -0
  13. package/dist/studio-ui-assets/assets/BrowseView-CM4HBO4j.css +1 -0
  14. package/dist/studio-ui-assets/assets/BulkImportView-DB6DYDJU.js +2 -0
  15. package/dist/studio-ui-assets/assets/BulkImportView-DB6DYDJU.js.map +1 -0
  16. package/dist/studio-ui-assets/assets/BulkImportView-g4wQUfPA.css +1 -0
  17. package/dist/studio-ui-assets/assets/CourseEditorView-BIlhlhw1.js +2 -0
  18. package/dist/studio-ui-assets/assets/CourseEditorView-BIlhlhw1.js.map +1 -0
  19. package/dist/studio-ui-assets/assets/CourseEditorView-WuPNLVKp.css +1 -0
  20. package/dist/studio-ui-assets/assets/CreateCardView-CyNOKCkm.css +1 -0
  21. package/dist/studio-ui-assets/assets/CreateCardView-DPjPvzzt.js +2 -0
  22. package/dist/studio-ui-assets/assets/CreateCardView-DPjPvzzt.js.map +1 -0
  23. package/dist/studio-ui-assets/assets/edit-ui.es-DiUxqbgF.js +330 -0
  24. package/dist/studio-ui-assets/assets/edit-ui.es-DiUxqbgF.js.map +1 -0
  25. package/dist/studio-ui-assets/assets/index--zY88pg6.css +14 -0
  26. package/dist/studio-ui-assets/assets/index-BnAv1C72.js +287 -0
  27. package/dist/studio-ui-assets/assets/index-BnAv1C72.js.map +1 -0
  28. package/dist/studio-ui-assets/assets/index-DHMXQY3-.js +192 -0
  29. package/dist/studio-ui-assets/assets/index-DHMXQY3-.js.map +1 -0
  30. package/dist/studio-ui-assets/assets/materialdesignicons-webfont-B7mPwVP_.ttf +0 -0
  31. package/dist/studio-ui-assets/assets/materialdesignicons-webfont-CSr8KVlo.eot +0 -0
  32. package/dist/studio-ui-assets/assets/materialdesignicons-webfont-Dp5v-WZN.woff2 +0 -0
  33. package/dist/studio-ui-assets/assets/materialdesignicons-webfont-PXm3-2wK.woff +0 -0
  34. package/dist/studio-ui-assets/assets/vue-DZcMATiC.js +28 -0
  35. package/dist/studio-ui-assets/assets/vue-DZcMATiC.js.map +1 -0
  36. package/dist/studio-ui-assets/assets/vuetify-qg7mRxy_.js +6 -0
  37. package/dist/studio-ui-assets/assets/vuetify-qg7mRxy_.js.map +1 -0
  38. package/dist/studio-ui-assets/index.html +16 -0
  39. package/dist/utils/NodeFileSystemAdapter.d.ts +14 -0
  40. package/dist/utils/NodeFileSystemAdapter.d.ts.map +1 -0
  41. package/dist/utils/NodeFileSystemAdapter.js +55 -0
  42. package/dist/utils/NodeFileSystemAdapter.js.map +1 -0
  43. package/dist/utils/template.d.ts +1 -1
  44. package/dist/utils/template.d.ts.map +1 -1
  45. package/dist/utils/template.js +19 -5
  46. package/dist/utils/template.js.map +1 -1
  47. package/package.json +7 -4
  48. package/src/cli.ts +8 -0
  49. package/src/commands/studio.ts +497 -0
  50. package/src/commands/unpack.ts +259 -0
  51. package/src/utils/NodeFileSystemAdapter.ts +72 -0
  52. package/src/utils/template.ts +26 -7
@@ -0,0 +1,497 @@
1
+ import { Command } from 'commander';
2
+ import path from 'path';
3
+ import fs from 'fs';
4
+ import chalk from 'chalk';
5
+ import { spawn } from 'child_process';
6
+ import { fileURLToPath } from 'url';
7
+ import { dirname } from 'path';
8
+ import http from 'http';
9
+ import { CouchDBManager } from '@vue-skuilder/common/docker';
10
+ // TODO: Re-enable once module import issues are resolved
11
+ // import { StaticToCouchDBMigrator, validateStaticCourse } from '@vue-skuilder/db';
12
+
13
+ const __filename = fileURLToPath(import.meta.url);
14
+ const __dirname = dirname(__filename);
15
+
16
+ export function createStudioCommand(): Command {
17
+ return new Command('studio')
18
+ .description('Launch studio mode for editing a static course')
19
+ .argument('[coursePath]', 'Path to static course directory', '.')
20
+ .option('-p, --port <port>', 'CouchDB port for studio session', '5985')
21
+ .option('--no-browser', 'Skip automatic browser launch')
22
+ .action(launchStudio);
23
+ }
24
+
25
+ interface StudioOptions {
26
+ port: string;
27
+ browser: boolean;
28
+ }
29
+
30
+ // Global references for cleanup
31
+ let couchDBManager: CouchDBManager | null = null;
32
+ let studioUIServer: http.Server | null = null;
33
+
34
+ async function launchStudio(coursePath: string, options: StudioOptions) {
35
+ try {
36
+ console.log(chalk.cyan(`🎨 Launching Skuilder Studio...`));
37
+
38
+ // Phase 2: Course Detection & Validation
39
+ const resolvedPath = path.resolve(coursePath);
40
+ console.log(chalk.gray(`📁 Course path: ${resolvedPath}`));
41
+
42
+ if (!(await validateSuiCourse(resolvedPath))) {
43
+ console.error(chalk.red(`❌ Not a valid standalone-ui course directory`));
44
+ console.log(chalk.yellow(`💡 Studio mode requires a vue-skuilder course with:`));
45
+ console.log(chalk.yellow(` - package.json with @vue-skuilder/* dependencies`));
46
+ console.log(
47
+ chalk.yellow(` - static-data/ OR public/static-courses/ directory with course content`)
48
+ );
49
+ process.exit(1);
50
+ }
51
+
52
+ console.log(chalk.green(`✅ Valid standalone-ui course detected`));
53
+
54
+ // Phase 1: CouchDB Management
55
+ const studioDatabaseName = generateStudioDatabaseName(resolvedPath);
56
+ console.log(chalk.cyan(`🗄️ Starting studio CouchDB instance: ${studioDatabaseName}`));
57
+
58
+ couchDBManager = await startStudioCouchDB(studioDatabaseName, parseInt(options.port));
59
+
60
+ // Phase 4: Populate CouchDB with course data
61
+ console.log(chalk.cyan(`📦 Unpacking course data to studio database...`));
62
+ const unpackResult = await unpackCourseToStudio(
63
+ resolvedPath,
64
+ couchDBManager.getConnectionDetails()
65
+ );
66
+
67
+ // Phase 7: Launch studio-ui server
68
+ console.log(chalk.cyan(`🌐 Starting studio-ui server...`));
69
+ console.log(
70
+ chalk.gray(
71
+ ` Debug: Unpack result - Database: "${unpackResult.databaseName}", Course ID: "${unpackResult.courseId}"`
72
+ )
73
+ );
74
+ const studioUIPort = await startStudioUIServer(
75
+ couchDBManager.getConnectionDetails(),
76
+ unpackResult
77
+ );
78
+
79
+ console.log(chalk.green(`✅ Studio session ready!`));
80
+ console.log(chalk.white(`🎨 Studio URL: http://localhost:${studioUIPort}`));
81
+ console.log(chalk.gray(` Database: ${studioDatabaseName} on port ${options.port}`));
82
+ if (options.browser) {
83
+ console.log(chalk.cyan(`🌐 Opening browser...`));
84
+ await openBrowser(`http://localhost:${studioUIPort}`);
85
+ }
86
+ console.log(chalk.gray(` Press Ctrl+C to stop studio session`));
87
+
88
+ // Keep process alive and handle cleanup
89
+ process.on('SIGINT', () => {
90
+ void (async () => {
91
+ console.log(chalk.cyan(`\n🔄 Stopping studio session...`));
92
+ await stopStudioSession();
93
+ console.log(chalk.green(`✅ Studio session stopped`));
94
+ process.exit(0);
95
+ })();
96
+ });
97
+
98
+ process.on('SIGTERM', () => {
99
+ void (async () => {
100
+ console.log(chalk.cyan(`\n🔄 Stopping studio session...`));
101
+ await stopStudioSession();
102
+ process.exit(0);
103
+ })();
104
+ });
105
+
106
+ // Keep process alive
107
+ await new Promise(() => {});
108
+ } catch (error) {
109
+ console.error(chalk.red(`❌ Studio launch failed:`), error);
110
+ process.exit(1);
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Phase 2: Validate that the given path contains a standalone-ui course
116
+ */
117
+ async function validateSuiCourse(coursePath: string): Promise<boolean> {
118
+ try {
119
+ // Check if directory exists
120
+ if (!fs.existsSync(coursePath)) {
121
+ return false;
122
+ }
123
+
124
+ // Check for package.json
125
+ const packageJsonPath = path.join(coursePath, 'package.json');
126
+ if (!fs.existsSync(packageJsonPath)) {
127
+ return false;
128
+ }
129
+
130
+ // Read and validate package.json
131
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
132
+
133
+ // Check for vue-skuilder course indicators (either standalone-ui or required packages)
134
+ const hasStandaloneUi =
135
+ (packageJson.dependencies && packageJson.dependencies['@vue-skuilder/standalone-ui']) ||
136
+ (packageJson.devDependencies && packageJson.devDependencies['@vue-skuilder/standalone-ui']);
137
+
138
+ const hasRequiredPackages =
139
+ packageJson.dependencies &&
140
+ packageJson.dependencies['@vue-skuilder/common-ui'] &&
141
+ packageJson.dependencies['@vue-skuilder/courses'] &&
142
+ packageJson.dependencies['@vue-skuilder/db'];
143
+
144
+ if (!hasStandaloneUi && !hasRequiredPackages) {
145
+ return false;
146
+ }
147
+
148
+ // Check for course content directory (static-data OR public/static-courses)
149
+ const staticDataPath = path.join(coursePath, 'static-data');
150
+ const publicStaticCoursesPath = path.join(coursePath, 'public', 'static-courses');
151
+
152
+ if (!fs.existsSync(staticDataPath) && !fs.existsSync(publicStaticCoursesPath)) {
153
+ return false;
154
+ }
155
+
156
+ return true;
157
+ } catch {
158
+ return false;
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Generate a unique database name for this studio session
164
+ */
165
+ function generateStudioDatabaseName(coursePath: string): string {
166
+ const courseName = path.basename(coursePath);
167
+ const timestamp = Date.now();
168
+ return `studio-${courseName.toLowerCase().replace(/[^a-z0-9]/g, '-')}-${timestamp}`;
169
+ }
170
+
171
+ /**
172
+ * Phase 1: Start CouchDB for studio session
173
+ */
174
+ async function startStudioCouchDB(_databaseName: string, port: number): Promise<CouchDBManager> {
175
+ const manager = new CouchDBManager(
176
+ {
177
+ mode: 'blank',
178
+ port: port,
179
+ containerName: `skuilder-studio-${port}`,
180
+ },
181
+ {
182
+ onLog: (message) => console.log(chalk.gray(` ${message}`)),
183
+ onError: (error) => console.error(chalk.red(` Error: ${error}`)),
184
+ }
185
+ );
186
+
187
+ try {
188
+ await manager.start();
189
+
190
+ const connectionDetails = manager.getConnectionDetails();
191
+ console.log(chalk.green(`✅ CouchDB studio instance ready`));
192
+ console.log(chalk.gray(` URL: ${connectionDetails.url}`));
193
+ console.log(chalk.gray(` Username: ${connectionDetails.username}`));
194
+ console.log(chalk.gray(` Password: ${connectionDetails.password}`));
195
+
196
+ return manager;
197
+ } catch (error: unknown) {
198
+ const errorMessage = error instanceof Error ? error.message : String(error);
199
+ console.error(chalk.red(`Failed to start CouchDB: ${errorMessage}`));
200
+ throw error;
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Stop entire studio session (CouchDB + UI server)
206
+ */
207
+ async function stopStudioSession(): Promise<void> {
208
+ // Stop studio-ui server
209
+ if (studioUIServer) {
210
+ try {
211
+ studioUIServer.close();
212
+ console.log(chalk.green(`✅ Studio-UI server stopped`));
213
+ } catch (error: unknown) {
214
+ const errorMessage = error instanceof Error ? error.message : String(error);
215
+ console.error(chalk.red(`Error stopping studio-ui server: ${errorMessage}`));
216
+ }
217
+ studioUIServer = null;
218
+ }
219
+
220
+ // Stop CouchDB
221
+ if (couchDBManager) {
222
+ try {
223
+ await couchDBManager.remove(); // This stops and removes the container
224
+ console.log(chalk.green(`✅ CouchDB studio instance cleaned up`));
225
+ } catch (error: unknown) {
226
+ const errorMessage = error instanceof Error ? error.message : String(error);
227
+ console.error(chalk.red(`Error cleaning up CouchDB: ${errorMessage}`));
228
+ }
229
+ couchDBManager = null;
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Phase 7: Start studio-ui static file server
235
+ */
236
+ interface ConnectionDetails {
237
+ url: string;
238
+ username: string;
239
+ password: string;
240
+ }
241
+
242
+ interface UnpackResult {
243
+ databaseName: string;
244
+ courseId: string;
245
+ }
246
+
247
+ async function startStudioUIServer(connectionDetails: ConnectionDetails, unpackResult: UnpackResult): Promise<number> {
248
+ const studioAssetsPath = path.join(__dirname, '..', 'studio-ui-assets');
249
+
250
+ if (!fs.existsSync(studioAssetsPath)) {
251
+ throw new Error('Studio-UI assets not found. Please rebuild the CLI package.');
252
+ }
253
+
254
+ // Find available port starting from 7174
255
+ let port = 7174;
256
+ while (port < 7200) {
257
+ try {
258
+ await new Promise<void>((resolve, reject) => {
259
+ const server = http.createServer((req, res) => {
260
+ let filePath = path.join(
261
+ studioAssetsPath,
262
+ req.url === '/' ? 'index.html' : req.url || ''
263
+ );
264
+
265
+ // Security: prevent directory traversal
266
+ if (!filePath.startsWith(studioAssetsPath)) {
267
+ res.writeHead(403);
268
+ res.end('Forbidden');
269
+ return;
270
+ }
271
+
272
+ // Check if file exists
273
+ if (!fs.existsSync(filePath)) {
274
+ // If it's not a file, serve index.html for SPA routing
275
+ filePath = path.join(studioAssetsPath, 'index.html');
276
+ }
277
+
278
+ // Determine content type
279
+ const ext = path.extname(filePath);
280
+ const contentType =
281
+ {
282
+ '.html': 'text/html',
283
+ '.js': 'text/javascript',
284
+ '.css': 'text/css',
285
+ '.woff2': 'font/woff2',
286
+ '.woff': 'font/woff',
287
+ '.ttf': 'font/ttf',
288
+ '.eot': 'application/vnd.ms-fontobject',
289
+ }[ext] || 'application/octet-stream';
290
+
291
+ res.writeHead(200, { 'Content-Type': contentType });
292
+
293
+ // For HTML files, inject CouchDB connection details
294
+ if (ext === '.html') {
295
+ let html = fs.readFileSync(filePath, 'utf8');
296
+
297
+ // Inject connection details as script tag before </head>
298
+ const connectionScript = `
299
+ <script>
300
+ window.STUDIO_CONFIG = {
301
+ couchdb: {
302
+ url: '${connectionDetails.url}',
303
+ username: '${connectionDetails.username}',
304
+ password: '${connectionDetails.password}'
305
+ },
306
+ database: {
307
+ name: '${unpackResult.databaseName}',
308
+ courseId: '${unpackResult.courseId}'
309
+ }
310
+ };
311
+ </script>
312
+ `;
313
+ html = html.replace('</head>', connectionScript + '</head>');
314
+ res.end(html);
315
+ } else {
316
+ // Serve static files
317
+ const stream = fs.createReadStream(filePath);
318
+ stream.pipe(res);
319
+ }
320
+ });
321
+
322
+ server.listen(port, '127.0.0.1', () => {
323
+ studioUIServer = server;
324
+ resolve();
325
+ });
326
+
327
+ server.on('error', (err: NodeJS.ErrnoException) => {
328
+ if (err.code === 'EADDRINUSE') {
329
+ reject(err);
330
+ } else {
331
+ reject(err);
332
+ }
333
+ });
334
+ });
335
+
336
+ // Port is available
337
+ console.log(chalk.green(`✅ Studio-UI server running on port ${port}`));
338
+ return port;
339
+ } catch (error: unknown) {
340
+ if (error instanceof Error && 'code' in error && error.code === 'EADDRINUSE') {
341
+ port++;
342
+ continue;
343
+ } else {
344
+ throw error;
345
+ }
346
+ }
347
+ }
348
+
349
+ throw new Error('Unable to find an available port for studio-ui server');
350
+ }
351
+
352
+ /**
353
+ * Open browser to studio URL
354
+ */
355
+ async function openBrowser(url: string): Promise<void> {
356
+ const { spawn } = await import('child_process');
357
+
358
+ let command: string;
359
+ let args: string[];
360
+
361
+ switch (process.platform) {
362
+ case 'darwin':
363
+ command = 'open';
364
+ args = [url];
365
+ break;
366
+ case 'win32':
367
+ command = 'start';
368
+ args = ['', url];
369
+ break;
370
+ default:
371
+ command = 'xdg-open';
372
+ args = [url];
373
+ break;
374
+ }
375
+
376
+ try {
377
+ spawn(command, args, { detached: true, stdio: 'ignore' });
378
+ } catch {
379
+ console.log(chalk.yellow(`⚠️ Could not automatically open browser. Please visit: ${url}`));
380
+ }
381
+ }
382
+
383
+ /**
384
+ * Phase 4: Unpack course data to studio CouchDB
385
+ */
386
+ async function unpackCourseToStudio(
387
+ coursePath: string,
388
+ connectionDetails: ConnectionDetails
389
+ ): Promise<{ databaseName: string; courseId: string }> {
390
+ return new Promise((resolve, reject) => {
391
+ // Find the course data directory (static-data OR public/static-courses)
392
+ let courseDataPath = path.join(coursePath, 'static-data');
393
+ if (!fs.existsSync(courseDataPath)) {
394
+ // Try public/static-courses directory
395
+ const publicStaticPath = path.join(coursePath, 'public', 'static-courses');
396
+ if (fs.existsSync(publicStaticPath)) {
397
+ // Find the first course directory inside public/static-courses
398
+ const courses = fs
399
+ .readdirSync(publicStaticPath, { withFileTypes: true })
400
+ .filter((dirent) => dirent.isDirectory())
401
+ .map((dirent) => dirent.name);
402
+
403
+ if (courses.length > 0) {
404
+ courseDataPath = path.join(publicStaticPath, courses[0]);
405
+ } else {
406
+ reject(new Error('No course directories found in public/static-courses/'));
407
+ return;
408
+ }
409
+ } else {
410
+ reject(new Error('No course data found in static-data/ or public/static-courses/'));
411
+ return;
412
+ }
413
+ }
414
+
415
+ console.log(chalk.gray(` Course data path: ${courseDataPath}`));
416
+
417
+ // Build the unpack command arguments
418
+ const args = [
419
+ 'unpack',
420
+ courseDataPath,
421
+ '--server',
422
+ connectionDetails.url,
423
+ '--username',
424
+ connectionDetails.username,
425
+ '--password',
426
+ connectionDetails.password,
427
+ ];
428
+
429
+ console.log(chalk.gray(` Running: skuilder ${args.join(' ')}`));
430
+
431
+ // Spawn the unpack command as a child process
432
+ const unpackProcess = spawn('node', [path.join(__dirname, '..', 'cli.js'), ...args], {
433
+ stdio: ['pipe', 'pipe', 'pipe'],
434
+ cwd: process.cwd(),
435
+ });
436
+
437
+ let stdout = '';
438
+ let stderr = '';
439
+
440
+ unpackProcess.stdout?.on('data', (data) => {
441
+ const output = data.toString();
442
+ stdout += output;
443
+ // Forward output with indentation
444
+ process.stdout.write(chalk.gray(` ${output.replace(/\n/g, '\n ')}`));
445
+ });
446
+
447
+ unpackProcess.stderr?.on('data', (data) => {
448
+ const output = data.toString();
449
+ stderr += output;
450
+ // Forward error output with indentation
451
+ process.stderr.write(chalk.gray(` ${output.replace(/\n/g, '\n ')}`));
452
+ });
453
+
454
+ unpackProcess.on('close', (code) => {
455
+ if (code === 0) {
456
+ console.log(chalk.green(`✅ Course data unpacked successfully`));
457
+
458
+ // Parse the output to extract database name and course ID
459
+ console.log(chalk.gray(` Debug: Parsing unpack output...`));
460
+
461
+ const databaseMatch = stdout.match(/Database: ([\w-_]+)/);
462
+ const courseIdMatch = stdout.match(/Course: .* \(([a-f0-9]+)\)/);
463
+
464
+ const fullDatabaseName = databaseMatch ? databaseMatch[1] : '';
465
+ const courseId = courseIdMatch ? courseIdMatch[1] : '';
466
+
467
+ // Extract the course database ID by removing 'coursedb-' prefix
468
+ const databaseName = fullDatabaseName.startsWith('coursedb-')
469
+ ? fullDatabaseName.substring('coursedb-'.length)
470
+ : fullDatabaseName;
471
+
472
+ console.log(
473
+ chalk.gray(
474
+ ` Debug: Parsed - Full DB: "${fullDatabaseName}", Course DB ID: "${databaseName}", Course ID: "${courseId}"`
475
+ )
476
+ );
477
+
478
+ if (!databaseName || !courseId) {
479
+ console.warn(
480
+ chalk.yellow(`⚠️ Could not parse database name or course ID from unpack output`)
481
+ );
482
+ console.log(chalk.gray(` Raw stdout length: ${stdout.length} chars`));
483
+ }
484
+
485
+ resolve({ databaseName, courseId });
486
+ } else {
487
+ console.error(chalk.red(`❌ Failed to unpack course data (exit code: ${code})`));
488
+ reject(new Error(`Unpack failed with code ${code}\nstdout: ${stdout}\nstderr: ${stderr}`));
489
+ }
490
+ });
491
+
492
+ unpackProcess.on('error', (error) => {
493
+ console.error(chalk.red(`❌ Failed to start unpack process: ${error.message}`));
494
+ reject(error);
495
+ });
496
+ });
497
+ }