@tgrv/void-cli 1.0.4 → 1.0.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.
Files changed (2) hide show
  1. package/bin/void.js +108 -92
  2. package/package.json +1 -1
package/bin/void.js CHANGED
@@ -235,6 +235,91 @@ export default plugin;
235
235
  return buildOutputDir;
236
236
  }
237
237
 
238
+ function runAdd(pluginName, appDir) {
239
+ const configInfo = findConfig(appDir);
240
+ if (!configInfo) {
241
+ console.error(`${cross} ${colors.red}Error: void.config.json not found. Run 'void init' first.${colors.reset}`);
242
+ process.exit(1);
243
+ }
244
+
245
+ const config = JSON.parse(fs.readFileSync(configInfo.configPath, "utf8"));
246
+
247
+ // Look up local build directory
248
+ let localBuildDir = null;
249
+
250
+ const scanLocations = [
251
+ path.join(workspaceDir, "plugins"),
252
+ path.join(workspaceDir, "packages"),
253
+ path.join(workspaceDir, "sdk")
254
+ ];
255
+
256
+ for (const root of scanLocations) {
257
+ if (fs.existsSync(root)) {
258
+ const subdirs = fs.readdirSync(root);
259
+ for (const subdir of subdirs) {
260
+ const pPath = path.join(root, subdir);
261
+ if (fs.statSync(pPath).isDirectory()) {
262
+ // Scans nested scopes (e.g. plugins/math/@tgrv/void-math)
263
+ const entries = fs.readdirSync(pPath);
264
+ for (const entry of entries) {
265
+ if (entry.startsWith("@")) {
266
+ const scopePath = path.join(pPath, entry);
267
+ const subfolders = fs.readdirSync(scopePath);
268
+ for (const name of subfolders) {
269
+ const pkgPath = path.join(scopePath, name, "package.json");
270
+ if (fs.existsSync(pkgPath)) {
271
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
272
+ if (pkg.name === pluginName) {
273
+ localBuildDir = path.join(scopePath, name);
274
+ break;
275
+ }
276
+ }
277
+ }
278
+ }
279
+ if (localBuildDir) break;
280
+ }
281
+ }
282
+ if (localBuildDir) break;
283
+ }
284
+ }
285
+ if (localBuildDir) break;
286
+ }
287
+
288
+ const targetPluginDir = path.join(appDir, "node_modules", pluginName);
289
+ ensureDir(path.dirname(targetPluginDir));
290
+
291
+ if (localBuildDir) {
292
+ console.log(`${info} Installing local build of '${colors.bold}${pluginName}${colors.reset}' from ${localBuildDir} to ${targetPluginDir}...`);
293
+ if (fs.existsSync(targetPluginDir)) {
294
+ fs.rmSync(targetPluginDir, { recursive: true, force: true });
295
+ }
296
+ copyFolderRecursive(localBuildDir, targetPluginDir);
297
+ } else {
298
+ console.log(`${info} Installing '${colors.bold}${pluginName}${colors.reset}' from NPM registry...`);
299
+ const npmSuccess = runCommand(`npm install ${pluginName}`, { cwd: appDir });
300
+ if (!npmSuccess) {
301
+ console.error(`${cross} ${colors.red}Error: Failed to install package '${pluginName}' via npm.${colors.reset}`);
302
+ process.exit(1);
303
+ }
304
+ }
305
+
306
+ // Update package.json of the application
307
+ const pkgPath = path.join(appDir, "package.json");
308
+ if (fs.existsSync(pkgPath)) {
309
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
310
+ pkg.dependencies = pkg.dependencies || {};
311
+ pkg.dependencies[pluginName] = "1.0.0";
312
+ fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
313
+ }
314
+
315
+ // Update void.config.json
316
+ config.plugins = config.plugins || {};
317
+ config.plugins[pluginName] = "^1.0.0";
318
+ fs.writeFileSync(configInfo.configPath, JSON.stringify(config, null, 2));
319
+
320
+ console.log(`${tick} ${colors.green}Successfully added '${pluginName}'!${colors.reset}\n`);
321
+ }
322
+
238
323
  async function main() {
239
324
  switch (command) {
240
325
  case "init": {
@@ -244,9 +329,11 @@ async function main() {
244
329
 
245
330
  console.log(`\n${info} Initializing Void project at: ${colors.bold}${targetDir}${colors.reset}`);
246
331
 
332
+ const hasExistingPkg = fs.existsSync(path.join(targetDir, "package.json"));
333
+
247
334
  // Initialize package.json if not exists
248
335
  const pkgPath = path.join(targetDir, "package.json");
249
- if (!fs.existsSync(pkgPath)) {
336
+ if (!hasExistingPkg) {
250
337
  const defaultPkg = {
251
338
  name: path.basename(targetDir),
252
339
  version: "1.0.0",
@@ -266,10 +353,11 @@ async function main() {
266
353
  fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
267
354
  }
268
355
 
269
- // Write default application app.js
270
- const appPath = path.join(targetDir, "app.js");
271
- if (!fs.existsSync(appPath)) {
272
- const appContent = `import math from "@tgrv/void-math";
356
+ // If it's a fresh project, write default app.js
357
+ if (!hasExistingPkg) {
358
+ const appPath = path.join(targetDir, "app.js");
359
+ if (!fs.existsSync(appPath)) {
360
+ const appContent = `import math from "@tgrv/void-math";
273
361
 
274
362
  console.log("=== Void Application ===");
275
363
 
@@ -279,7 +367,8 @@ try {
279
367
  console.error("Error running application:", e.message);
280
368
  }
281
369
  `;
282
- fs.writeFileSync(appPath, appContent);
370
+ fs.writeFileSync(appPath, appContent);
371
+ }
283
372
  }
284
373
 
285
374
  // Install void-runtime via NPM
@@ -289,11 +378,19 @@ try {
289
378
  console.error(`${cross} ${colors.red}Failed to install @tgrv/void-runtime via npm.${colors.reset}`);
290
379
  }
291
380
 
381
+ // If it's a fresh project, also install @tgrv/void-math using the shared installer logic
382
+ if (!hasExistingPkg) {
383
+ runAdd("@tgrv/void-math", targetDir);
384
+ }
385
+
292
386
  console.log(`${tick} ${colors.green}Successfully initialized Void project!${colors.reset}`);
293
- console.log(`\nTo add plugins, you can run:`);
294
- console.log(` ${colors.cyan}npx void add <plugin-name>${colors.reset}`);
295
- console.log(`For example:`);
296
- console.log(` ${colors.cyan}npx void add @tgrv/void-math${colors.reset}\n`);
387
+ if (!hasExistingPkg) {
388
+ console.log(`\nTo run the starter app, execute:`);
389
+ console.log(` ${colors.cyan}node app.js${colors.reset}\n`);
390
+ } else {
391
+ console.log(`\nTo add plugins, you can run:`);
392
+ console.log(` ${colors.cyan}npx void add <plugin-name>${colors.reset}\n`);
393
+ }
297
394
  break;
298
395
  }
299
396
 
@@ -385,88 +482,7 @@ try {
385
482
  console.error(`${cross} ${colors.red}Error: Please specify the plugin to add. e.g. void add @tgrv/void-math${colors.reset}`);
386
483
  process.exit(1);
387
484
  }
388
-
389
- const configInfo = findConfig(process.cwd());
390
- if (!configInfo) {
391
- console.error(`${cross} ${colors.red}Error: void.config.json not found. Run 'void init' first.${colors.reset}`);
392
- process.exit(1);
393
- }
394
-
395
- const config = JSON.parse(fs.readFileSync(configInfo.configPath, "utf8"));
396
-
397
- // Look up local build directory inside the plugins/ or packages/ directory tree
398
- let localBuildDir = null;
399
-
400
- const scanLocations = [
401
- path.join(workspaceDir, "plugins"),
402
- path.join(workspaceDir, "packages")
403
- ];
404
-
405
- for (const root of scanLocations) {
406
- if (fs.existsSync(root)) {
407
- const subdirs = fs.readdirSync(root);
408
- for (const subdir of subdirs) {
409
- const pPath = path.join(root, subdir);
410
- if (fs.statSync(pPath).isDirectory()) {
411
- // Scans nested scopes (e.g. plugins/math/@tgrv/void-math)
412
- const entries = fs.readdirSync(pPath);
413
- for (const entry of entries) {
414
- if (entry.startsWith("@")) {
415
- const scopePath = path.join(pPath, entry);
416
- const subfolders = fs.readdirSync(scopePath);
417
- for (const name of subfolders) {
418
- const pkgPath = path.join(scopePath, name, "package.json");
419
- if (fs.existsSync(pkgPath)) {
420
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
421
- if (pkg.name === pluginName) {
422
- localBuildDir = path.join(scopePath, name);
423
- break;
424
- }
425
- }
426
- }
427
- }
428
- if (localBuildDir) break;
429
- }
430
- }
431
- if (localBuildDir) break;
432
- }
433
- }
434
- if (localBuildDir) break;
435
- }
436
-
437
- const targetPluginDir = path.join(process.cwd(), "node_modules", pluginName);
438
- ensureDir(path.dirname(targetPluginDir));
439
-
440
- if (localBuildDir) {
441
- console.log(`${info} Installing local build of '${colors.bold}${pluginName}${colors.reset}' from ${localBuildDir} to ${targetPluginDir}...`);
442
- if (fs.existsSync(targetPluginDir)) {
443
- fs.rmSync(targetPluginDir, { recursive: true, force: true });
444
- }
445
- copyFolderRecursive(localBuildDir, targetPluginDir);
446
- } else {
447
- console.log(`${info} Installing '${colors.bold}${pluginName}${colors.reset}' from NPM registry...`);
448
- const npmSuccess = runCommand(`npm install ${pluginName}`, { cwd: process.cwd() });
449
- if (!npmSuccess) {
450
- console.error(`${cross} ${colors.red}Error: Failed to install package '${pluginName}' via npm.${colors.reset}`);
451
- process.exit(1);
452
- }
453
- }
454
-
455
- // Update package.json of the application
456
- const pkgPath = path.join(process.cwd(), "package.json");
457
- if (fs.existsSync(pkgPath)) {
458
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
459
- pkg.dependencies = pkg.dependencies || {};
460
- pkg.dependencies[pluginName] = "1.0.0";
461
- fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
462
- }
463
-
464
- // Update void.config.json
465
- config.plugins = config.plugins || {};
466
- config.plugins[pluginName] = "^1.0.0";
467
- fs.writeFileSync(configInfo.configPath, JSON.stringify(config, null, 2));
468
-
469
- console.log(`${tick} ${colors.green}Successfully added '${pluginName}'!${colors.reset}\n`);
485
+ runAdd(pluginName, process.cwd());
470
486
  break;
471
487
  }
472
488
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tgrv/void-cli",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "void": "./bin/void.js"