slicejs-cli 3.6.3 → 3.6.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/client.js CHANGED
@@ -1,744 +1,740 @@
1
- #!/usr/bin/env node
2
- import { program } from "commander";
3
- import inquirer from "inquirer";
4
- import initializeProject from "./commands/init/init.js";
5
- import createComponent from "./commands/createComponent/createComponent.js";
6
- import listComponents from "./commands/listComponents/listComponents.js";
7
- import deleteComponent from "./commands/deleteComponent/deleteComponent.js";
8
- import getComponent, { listComponents as listRemoteComponents, syncComponents } from "./commands/getComponent/getComponent.js";
9
- import startServer from "./commands/startServer/startServer.js";
10
- import runDiagnostics from "./commands/doctor/doctor.js";
11
- import versionChecker from "./commands/utils/VersionChecker.js";
12
- import updateManager from "./commands/utils/updateManager.js";
13
- import fs from "fs";
14
- import path from "path";
15
- import { fileURLToPath } from "url";
16
- import { getProjectRoot, getSrcPath, getPath } from "./commands/utils/PathHelper.js";
17
- import { loadConfigSync as sharedLoadConfigSync } from "./commands/utils/loadConfig.js";
18
- import { spawnSync } from "node:child_process";
19
- import validations from "./commands/Validations.js";
20
- import Print from "./commands/Print.js";
21
- import build from './commands/build/build.js';
22
- import { runGenerateTypes } from './commands/types/types.js';
23
- import { cleanBundles, bundleInfo } from './commands/bundle/bundle.js';
24
- import {
25
- isLocalDelegationDisabled,
26
- findNearestLocalCliEntry,
27
- resolveLocalCliCandidate,
28
- shouldDelegateToLocalCli
29
- } from './commands/utils/LocalCliDelegation.js';
30
- import {
31
- detectPackageManager,
32
- getAvailablePackageManagers,
33
- isPackageManagerAvailable,
34
- resolvePackageManager,
35
- runScriptCommand,
36
- SUPPORTED_PACKAGE_MANAGERS
37
- } from './commands/utils/PackageManager.js';
38
- import { SLICE_SCRIPTS } from './commands/utils/sliceScripts.js';
39
-
40
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
41
-
42
- const getCategories = () => {
43
- const config = sharedLoadConfigSync(import.meta.url);
44
- return config && config.paths?.components ? Object.keys(config.paths.components) : [];
45
- };
46
-
47
- // Function to run version check for all commands
48
- async function runWithVersionCheck(commandFunction, ...args) {
49
- try {
50
- updateManager.notifyAvailableUpdates().catch(() => {});
51
-
52
- const result = await commandFunction(...args);
53
-
54
- setTimeout(() => {
55
- versionChecker.checkForUpdates(false);
56
- }, 100);
57
-
58
- return result;
59
- } catch (error) {
60
- Print.error(`Command execution: ${error.message}`);
61
- return false;
62
- }
63
- }
64
-
65
- function maybeDelegateToLocalCli() {
66
- if (isLocalDelegationDisabled(process.env)) {
67
- return;
68
- }
69
-
70
- const currentEntryPath = fileURLToPath(import.meta.url);
71
- const localEntryPath = findNearestLocalCliEntry(process.cwd(), resolveLocalCliCandidate);
72
-
73
- if (!shouldDelegateToLocalCli(currentEntryPath, localEntryPath)) {
74
- return;
75
- }
76
-
77
- const child = spawnSync(
78
- process.execPath,
79
- [localEntryPath, ...process.argv.slice(2)],
80
- {
81
- stdio: 'inherit',
82
- cwd: process.cwd(),
83
- env: process.env
84
- }
85
- );
86
-
87
- process.exit(child.status ?? 1);
88
- }
89
-
90
- maybeDelegateToLocalCli();
91
-
92
- const sliceClient = program;
93
-
94
- try {
95
- const pkgPath = path.join(__dirname, "./package.json");
96
- const pkgRaw = fs.readFileSync(pkgPath, "utf-8");
97
- const pkg = JSON.parse(pkgRaw);
98
- sliceClient.version(pkg.version).description("CLI for managing Slice.js framework components");
99
- } catch {
100
- sliceClient.version("0.0.0").description("CLI for managing Slice.js framework components");
101
- }
102
-
103
- // INIT COMMAND
104
- sliceClient
105
- .command("init")
106
- .description("Initialize a new Slice.js project")
107
- .option("-y, --yes [name]", "Skip prompts and initialize with project name")
108
- .option("--pm <packageManager>", "Package manager to use (pnpm or npm). Auto-detected when omitted")
109
- .action(async (options) => {
110
- let projectName = 'my-slice-app';
111
- if (options.yes) {
112
- projectName = typeof options.yes === 'string' ? options.yes : projectName;
113
- } else {
114
- const answers = await inquirer.prompt([
115
- {
116
- type: 'input',
117
- name: 'projectName',
118
- message: 'What is the name of your project?',
119
- default: projectName,
120
- filter: (input) => input.trim()
121
- .toLowerCase()
122
- .replace(/\s+/g, '-')
123
- .replace(/[^a-z0-9-]/g, '')
124
- .replace(/-+/g, '-')
125
- .replace(/^-|-$/g, ''),
126
- validate: (input) => {
127
- if (!input || !input.trim()) return 'Project name cannot be empty';
128
- if (input.includes('/') || input.includes('\\')) return 'Use a simple name, not a path';
129
- return true;
130
- }
131
- }
132
- ]);
133
- projectName = answers.projectName;
134
- }
135
-
136
- // Resolve the package manager: --pm flag → detection → interactive prompt.
137
- // Detection here has no project root yet (the folder is new), so it relies on
138
- // npm_config_user_agent (npx / pnpm dlx / PM scripts) or a single available binary.
139
- let packageManager = options.pm;
140
- if (packageManager && !SUPPORTED_PACKAGE_MANAGERS.includes(packageManager)) {
141
- Print.error(`Unsupported package manager "${packageManager}". Use one of: ${SUPPORTED_PACKAGE_MANAGERS.join(', ')}`);
142
- return;
143
- }
144
- if (packageManager && !isPackageManagerAvailable(packageManager)) {
145
- Print.error(`Package manager "${packageManager}" is not available on this system`);
146
- return;
147
- }
148
- if (!packageManager) {
149
- const detected = detectPackageManager(null);
150
- if (detected) {
151
- packageManager = detected.name;
152
- } else if (!options.yes) {
153
- const available = getAvailablePackageManagers();
154
- if (available.length === 0) {
155
- Print.error('No package manager found (pnpm or npm). Install one and retry.');
156
- return;
157
- }
158
- const { pm } = await inquirer.prompt([
159
- {
160
- type: 'list',
161
- name: 'pm',
162
- message: 'Which package manager do you want to use?',
163
- choices: available,
164
- default: available.includes('pnpm') ? 'pnpm' : available[0]
165
- }
166
- ]);
167
- packageManager = pm;
168
- } else {
169
- const available = getAvailablePackageManagers();
170
- packageManager = available.includes('pnpm') ? 'pnpm' : available[0];
171
- if (!packageManager) {
172
- Print.error('No package manager found (pnpm or npm). Install one and retry.');
173
- return;
174
- }
175
- }
176
- }
177
-
178
- const projectDir = path.resolve(projectName);
179
-
180
- if (fs.existsSync(projectDir)) {
181
- const contents = fs.readdirSync(projectDir);
182
- if (contents.length > 0) {
183
- if (options.yes) {
184
- Print.error(`Directory "${projectName}" already exists and is not empty. Aborting.`);
185
- return;
186
- }
187
- const { overwrite } = await inquirer.prompt([
188
- {
189
- type: 'confirm',
190
- name: 'overwrite',
191
- message: `Directory "${projectName}" already exists and is not empty. Continue?`,
192
- default: false
193
- }
194
- ]);
195
- if (!overwrite) {
196
- Print.info('Initialization cancelled.');
197
- return;
198
- }
199
- }
200
- } else {
201
- fs.mkdirSync(projectDir, { recursive: true });
202
- }
203
-
204
- process.chdir(projectDir);
205
- process.env.INIT_CWD = projectDir;
206
-
207
- await runWithVersionCheck(async () => {
208
- await initializeProject({ packageManager });
209
- });
210
- });
211
-
212
- // VERSION COMMAND
213
- sliceClient
214
- .command("version")
215
- .alias("v")
216
- .description("Show version information and check for updates")
217
- .action(async () => {
218
- await versionChecker.showVersionInfo();
219
- });
220
-
221
- // BUILD COMMAND
222
- const buildCommand = sliceClient.command("build")
223
- .description("Build Slice.js project for production")
224
- .action(async (options) => {
225
- const prevEnv = process.env.NODE_ENV;
226
- process.env.NODE_ENV = 'production';
227
- try {
228
- await runWithVersionCheck(async () => {
229
- await build(options);
230
- });
231
- } finally {
232
- process.env.NODE_ENV = prevEnv;
233
- }
234
- });
235
-
236
- buildCommand
237
- .command("clean")
238
- .description("Remove all generated bundles")
239
- .action(async () => {
240
- await cleanBundles();
241
- });
242
-
243
- buildCommand
244
- .command("info")
245
- .description("Show information about generated bundles")
246
- .action(async () => {
247
- await bundleInfo();
248
- });
249
-
250
- buildCommand
251
- .option("-a, --analyze", "Analyze project dependencies without bundling")
252
- .option("-v, --verbose", "Show detailed output")
253
- .option("--no-minify", "Disable minification (enabled by default)")
254
- .option("--no-obfuscate", "Disable obfuscation (enabled by default, no prop mangling)")
255
- .option("--preview", "Start preview server after build")
256
- .option("--serve", "Start preview server without building")
257
- .option("--skip-clean", "Skip cleaning dist before build");
258
-
259
- // DEV COMMAND (DEVELOPMENT)
260
- sliceClient
261
- .command("dev")
262
- .description("Start development server with hot reload enabled by default")
263
- .option("-p, --port <port>", "Port for development server")
264
- .option("--no-hmr", "Disable hot module reload (enabled by default)")
265
- .action(async (options) => {
266
- const prevEnv = process.env.NODE_ENV;
267
- process.env.NODE_ENV = 'development';
268
- try {
269
- await runWithVersionCheck(async () => {
270
- await startServer({
271
- mode: 'development',
272
- port: options.port ? parseInt(options.port) : undefined,
273
- watch: options.hmr
274
- });
275
- });
276
- } finally {
277
- process.env.NODE_ENV = prevEnv;
278
- }
279
- });
280
-
281
- // START COMMAND - PRODUCTION MODE
282
- sliceClient
283
- .command("start")
284
- .description("Serve production files from dist/ (requires prior slice build)")
285
- .option("-p, --port <port>", "Port for server")
286
- .action(async (options) => {
287
- const prevEnv = process.env.NODE_ENV;
288
- process.env.NODE_ENV = 'production';
289
- try {
290
- await runWithVersionCheck(async () => {
291
- await startServer({
292
- mode: 'production',
293
- port: options.port ? parseInt(options.port) : undefined
294
- });
295
- });
296
- } finally {
297
- process.env.NODE_ENV = prevEnv;
298
- }
299
- });
300
-
301
- // COMPONENT COMMAND GROUP - For local component management
302
- const componentCommand = sliceClient.command("component").alias("comp").description("Manage local project components");
303
-
304
- // CREATE LOCAL COMPONENT
305
- componentCommand
306
- .command("create [name]")
307
- .alias("new")
308
- .description("Create a new component in your local project")
309
- .option("-c, --category <category>", "Component category (e.g. Visual, Service, AppComponents). Skips the prompt when provided.")
310
- .action(async (name, options) => {
311
- await runWithVersionCheck(async () => {
312
- const categories = getCategories();
313
- if (categories.length === 0) {
314
- Print.error("No categories found in your project configuration");
315
- Print.info("Run 'slice init' to initialize your project first");
316
- Print.commandExample("Initialize project", "slice init");
317
- return;
318
- }
319
-
320
- let componentName = name;
321
- let category = options.category;
322
-
323
- // Prompt only for the values not supplied on the command line. Passing both
324
- // a name and --category runs fully non-interactively (handy for scripts/agents).
325
- const prompts = [];
326
- if (!componentName) {
327
- prompts.push({
328
- type: "input",
329
- name: "componentName",
330
- message: "Enter the component name:",
331
- validate: (input) => {
332
- if (!input) return "Component name cannot be empty";
333
- if (!/^[a-zA-Z][a-zA-Z0-9]*$/.test(input)) {
334
- return "Component name must start with a letter and contain only alphanumeric characters";
335
- }
336
- return true;
337
- },
338
- });
339
- }
340
- if (!category) {
341
- prompts.push({
342
- type: "list",
343
- name: "category",
344
- message: "Select the component category:",
345
- choices: categories,
346
- });
347
- }
348
-
349
- if (prompts.length > 0) {
350
- const answers = await inquirer.prompt(prompts);
351
- componentName = componentName ?? answers.componentName;
352
- category = category ?? answers.category;
353
- }
354
-
355
- // createComponent validates the name and the category (and reports a clear
356
- // error listing valid categories if --category is wrong).
357
- const result = createComponent(componentName, category);
358
- if (result) {
359
- Print.success(`Component '${componentName}' created successfully in category '${category}'`);
360
- Print.info("Listing updated components:");
361
- listComponents();
362
- }
363
- });
364
- });
365
-
366
- // LIST LOCAL COMPONENTS
367
- componentCommand
368
- .command("list")
369
- .alias("ls")
370
- .description("List all components in your local project")
371
- .action(async () => {
372
- await runWithVersionCheck(() => {
373
- listComponents();
374
- return Promise.resolve();
375
- });
376
- });
377
-
378
- // DELETE LOCAL COMPONENT
379
- componentCommand
380
- .command("delete [name]")
381
- .alias("remove")
382
- .description("Delete a component from your local project")
383
- .option("-c, --category <category>", "Component category. Skips the category prompt when provided.")
384
- .option("-y, --yes", "Skip the confirmation prompt (for non-interactive use)")
385
- .action(async (name, options) => {
386
- await runWithVersionCheck(async () => {
387
- const categories = getCategories();
388
- if (categories.length === 0) {
389
- Print.error("No categories available. Check your configuration");
390
- Print.info("Run 'slice init' to initialize your project");
391
- return;
392
- }
393
-
394
- try {
395
- let category = options.category;
396
- let componentName = name;
397
-
398
- // Resolve category (prompt only if not provided)
399
- if (!category) {
400
- const categoryAnswer = await inquirer.prompt([
401
- {
402
- type: "list",
403
- name: "category",
404
- message: "Select the component category:",
405
- choices: categories,
406
- }
407
- ]);
408
- category = categoryAnswer.category;
409
- }
410
-
411
- const config = sharedLoadConfigSync(import.meta.url);
412
- if (!config) {
413
- Print.error("Could not load configuration");
414
- return;
415
- }
416
-
417
- if (!config.paths.components[category]) {
418
- Print.error(`Invalid category: '${category}'`);
419
- Print.info(`Available categories: ${categories.join(", ")}`);
420
- return;
421
- }
422
-
423
- const categoryPath = config.paths.components[category].path;
424
- const fullPath = getSrcPath(import.meta.url, categoryPath);
425
-
426
- if (!fs.existsSync(fullPath)) {
427
- Print.error(`Category path does not exist: ${categoryPath}`);
428
- return;
429
- }
430
-
431
- // Resolve component name (prompt with a list only if not provided)
432
- if (!componentName) {
433
- const components = fs.readdirSync(fullPath).filter(item => {
434
- const itemPath = path.join(fullPath, item);
435
- return fs.statSync(itemPath).isDirectory();
436
- });
437
-
438
- if (components.length === 0) {
439
- Print.info(`No components found in category '${category}'`);
440
- return;
441
- }
442
-
443
- const componentAnswer = await inquirer.prompt([
444
- {
445
- type: "list",
446
- name: "componentName",
447
- message: "Select the component to delete:",
448
- choices: components,
449
- }
450
- ]);
451
- componentName = componentAnswer.componentName;
452
- }
453
-
454
- // Confirm unless --yes was passed (passing name + --category + --yes is fully non-interactive)
455
- if (!options.yes) {
456
- const { confirm } = await inquirer.prompt([
457
- {
458
- type: "confirm",
459
- name: "confirm",
460
- message: `Are you sure you want to delete '${componentName}' from '${category}'?`,
461
- default: false,
462
- }
463
- ]);
464
- if (!confirm) {
465
- Print.info("Delete operation cancelled");
466
- return;
467
- }
468
- }
469
-
470
- // deleteComponent validates the name/category and that the component exists.
471
- if (deleteComponent(componentName, category)) {
472
- Print.success(`Component ${componentName} deleted successfully`);
473
- Print.info("Listing updated components:");
474
- listComponents();
475
- }
476
- } catch (error) {
477
- Print.error(`Deleting component: ${error.message}`);
478
- }
479
- });
480
- });
481
-
482
- // REGISTRY COMMAND GROUP - For component registry operations
483
- const registryCommand = sliceClient.command("registry").alias("reg").description("Manage components from official Slice.js repository");
484
-
485
- // TYPES COMMAND GROUP - TypeScript declarations from static props
486
- const typesCommand = sliceClient.command("types").description("Generate TypeScript declarations from component static props");
487
-
488
- typesCommand
489
- .command("generate")
490
- .description("Generate src/slice-build.generated.d.ts for slice.build autocomplete")
491
- .option("-o, --output <path>", "Custom output path", "src/slice-build.generated.d.ts")
492
- .action(async (options) => {
493
- await runWithVersionCheck(async () => {
494
- const projectRoot = getProjectRoot(import.meta.url);
495
- const outputPath = path.isAbsolute(options.output)
496
- ? options.output
497
- : path.join(projectRoot, options.output);
498
-
499
- await runGenerateTypes({
500
- projectRoot,
501
- outputPath
502
- });
503
- });
504
- });
505
-
506
- // GET COMPONENTS FROM REGISTRY
507
- registryCommand
508
- .command("get [components...]")
509
- .description("Download and install components from official repository")
510
- .option("-f, --force", "Force overwrite existing components")
511
- .option("-s, --service", "Install Service components instead of Visual")
512
- .action(async (components, options) => {
513
- await runWithVersionCheck(async () => {
514
- await getComponent(components, {
515
- force: options.force,
516
- service: options.service
517
- });
518
- });
519
- });
520
-
521
- // LIST REGISTRY COMPONENTS
522
- registryCommand
523
- .command("list")
524
- .alias("ls")
525
- .description("List all available components in the official repository")
526
- .action(async () => {
527
- await runWithVersionCheck(async () => {
528
- await listRemoteComponents();
529
- });
530
- });
531
-
532
- // SYNC COMPONENTS FROM REGISTRY
533
- registryCommand
534
- .command("sync")
535
- .description("Update all local components to latest versions from repository")
536
- .option("-f, --force", "Force update without confirmation")
537
- .action(async (options) => {
538
- await runWithVersionCheck(async () => {
539
- await syncComponents({
540
- force: options.force
541
- });
542
- });
543
- });
544
-
545
- // SHORTCUTS - Top-level convenient commands
546
- sliceClient
547
- .command("get [components...]")
548
- .description("Quick install components from registry")
549
- .option("-f, --force", "Force overwrite existing components")
550
- .option("-s, --service", "Install Service components instead of Visual")
551
- .action(async (components, options) => {
552
- await runWithVersionCheck(async () => {
553
- if (!components || components.length === 0) {
554
- Print.info("Use 'slice registry list' to see available components");
555
- Print.commandExample("Get multiple components", "slice get Button Card Input");
556
- Print.commandExample("Get service component", "slice get FetchManager --service");
557
- Print.commandExample("Browse components", "slice browse");
558
- return;
559
- }
560
-
561
- await getComponent(components, {
562
- force: options.force,
563
- service: options.service
564
- });
565
- });
566
- });
567
-
568
- sliceClient
569
- .command("browse")
570
- .description("Quick browse available components")
571
- .action(async () => {
572
- await runWithVersionCheck(async () => {
573
- await listRemoteComponents();
574
- });
575
- });
576
-
577
- sliceClient
578
- .command("sync")
579
- .description("Quick sync local components to latest versions")
580
- .option("-f, --force", "Force update without confirmation")
581
- .action(async (options) => {
582
- await runWithVersionCheck(async () => {
583
- await syncComponents({
584
- force: options.force
585
- });
586
- });
587
- });
588
-
589
- // LIST COMMAND - Quick shortcut for listing local components
590
- sliceClient
591
- .command("list")
592
- .description("Quick list all local components (alias for component list)")
593
- .action(async () => {
594
- await runWithVersionCheck(() => {
595
- listComponents();
596
- return Promise.resolve();
597
- });
598
- });
599
-
600
- // UPDATE COMMAND
601
- sliceClient
602
- .command("update")
603
- .alias("upgrade")
604
- .description("Update CLI and framework to latest versions")
605
- .option("-y, --yes", "Skip confirmation and update all packages automatically")
606
- .option("--cli", "Update only the Slice.js CLI")
607
- .option("-f, --framework", "Update only the Slice.js Framework")
608
- .option("--update-api", "Also overwrite api/index.js with the framework version (never done by default; creates a .bak backup)")
609
- .action(async (options) => {
610
- await updateManager.checkAndPromptUpdates(options);
611
- });
612
-
613
- // DOCTOR COMMAND - Diagnose project issues
614
- sliceClient
615
- .command("doctor")
616
- .alias("diagnose")
617
- .description("Run diagnostics to check project health")
618
- .action(async () => {
619
- await runWithVersionCheck(async () => {
620
- await runDiagnostics();
621
- });
622
- });
623
-
624
- // POSTINSTALL COMMAND - Manual alternative to postinstall
625
- sliceClient
626
- .command("postinstall")
627
- .description("Configure npm scripts in package.json (alternative to postinstall for --ignore-scripts users)")
628
- .action(() => {
629
- // npm sets npm_config_global; pnpm global installs are detected via PNPM_HOME.
630
- const pnpmHome = process.env.PNPM_HOME;
631
- const cliEntryPath = fileURLToPath(import.meta.url);
632
- const isGlobal = process.env.npm_config_global === 'true'
633
- || (pnpmHome && cliEntryPath.startsWith(pnpmHome));
634
- if (isGlobal) {
635
- console.log('⚠️ Global installation of slicejs-cli detected.');
636
- console.log(' We strongly recommend using a local installation to avoid version mismatches.');
637
- console.log(` Uninstall global: ${pnpmHome ? 'pnpm remove -g slicejs-cli' : 'npm uninstall -g slicejs-cli'}`);
638
- return;
639
- }
640
-
641
- const projectRoot = getProjectRoot(import.meta.url);
642
- const pkgPath = path.join(projectRoot, 'package.json');
643
- const packageManager = resolvePackageManager(projectRoot).name;
644
-
645
- // Shared with post.js and slice init — see commands/utils/sliceScripts.js
646
- const sliceScripts = SLICE_SCRIPTS;
647
-
648
- try {
649
- let pkg = {};
650
- if (fs.existsSync(pkgPath)) {
651
- pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
652
- } else {
653
- pkg = {
654
- name: path.basename(projectRoot),
655
- version: '1.0.0',
656
- description: 'Slice.js project',
657
- scripts: {}
658
- };
659
- }
660
-
661
- pkg.scripts = pkg.scripts || {};
662
- let addedCount = 0;
663
- for (const [script, command] of Object.entries(sliceScripts)) {
664
- if (!pkg.scripts[script]) {
665
- pkg.scripts[script] = command;
666
- addedCount++;
667
- }
668
- }
669
-
670
- fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2), 'utf-8');
671
- console.log(`✅ slicejs-cli installed successfully. Added ${addedCount} package scripts to package.json.`);
672
- console.log(` Run: ${runScriptCommand(packageManager, 'slice:dev')}`);
673
- } catch (err) {
674
- console.log('✅ slicejs-cli installed successfully.');
675
- console.log(' Could not auto-configure scripts:', err.message);
676
- console.log(` Configure scripts manually and run: ${runScriptCommand(packageManager, 'slice:dev')}`);
677
- }
678
- });
679
-
680
- // Enhanced help
681
- sliceClient
682
- .option("--no-version-check", "Skip version check for this command")
683
- .configureHelp({
684
- sortSubcommands: true,
685
- subcommandTerm: (cmd) => cmd.name() + ' ' + cmd.usage()
686
- });
687
-
688
-
689
- // Custom help - SIMPLIFIED for development only
690
- sliceClient.addHelpText('after', `
691
- Common Usage Examples:
692
- slice init - Initialize new Slice.js project
693
- slice dev - Start development server
694
- slice build - Build production output (bundles + dist)
695
- slice start - Start production server
696
- slice get Button Card Input - Install Visual components from registry
697
- slice get FetchManager -s - Install Service component from registry
698
- slice browse - Browse all available components
699
- slice sync - Update local components to latest versions
700
- slice component create - Create new local component
701
- slice list - List all local components
702
- slice doctor - Run project diagnostics
703
- slice types generate - Generate TypeScript typings for slice.build
704
- slice postinstall - Show post-install setup guide
705
-
706
- Command Categories:
707
- init, dev, start - Project lifecycle (development only)
708
- get, browse, sync, list - Quick shortcuts
709
- • component <cmd> - Local component management
710
- registry <cmd> - Official repository operations
711
- types generate - Type declarations from static props
712
- version, update, doctor, setup - Maintenance commands
713
-
714
- Development Workflow:
715
- • slice init - Initialize project
716
- slice dev - Start development server (serves from /src)
717
- • slice build - Build production output to /dist (includes bundles)
718
- • slice start - Serve production build from /dist
719
-
720
- More info: https://slice-js-docs.vercel.app/
721
- `);
722
-
723
- // Default action with better messaging
724
- if (!process.argv.slice(2).length) {
725
- Print.newLine();
726
- Print.info("Start with: slice init");
727
- Print.commandExample("Development", "slice dev");
728
- Print.commandExample("View available components", "slice browse");
729
- Print.info("Use 'slice --help' for full help");
730
- }
731
-
732
- // Error handling for unknown commands
733
- program.on('command:*', () => {
734
- Print.error('Invalid command. See available commands above');
735
- Print.info("Use 'slice --help' for help");
736
- process.exit(1);
737
- });
738
-
739
- // HELP Command
740
- const helpCommand = sliceClient.command("help").description("Display help information for Slice.js CLI").action(() => {
741
- sliceClient.outputHelp();
742
- });
743
-
744
- program.parse();
1
+ #!/usr/bin/env node
2
+ import { program } from "commander";
3
+ import inquirer from "inquirer";
4
+ import initializeProject from "./commands/init/init.js";
5
+ import createComponent from "./commands/createComponent/createComponent.js";
6
+ import listComponents from "./commands/listComponents/listComponents.js";
7
+ import deleteComponent from "./commands/deleteComponent/deleteComponent.js";
8
+ import getComponent, { listComponents as listRemoteComponents, syncComponents } from "./commands/getComponent/getComponent.js";
9
+ import startServer from "./commands/startServer/startServer.js";
10
+ import runDiagnostics from "./commands/doctor/doctor.js";
11
+ import versionChecker from "./commands/utils/VersionChecker.js";
12
+ import updateManager from "./commands/utils/updateManager.js";
13
+ import fs from "fs";
14
+ import path from "path";
15
+ import { fileURLToPath } from "url";
16
+ import { getProjectRoot, getSrcPath, getPath } from "./commands/utils/PathHelper.js";
17
+ import { loadConfigSync as sharedLoadConfigSync } from "./commands/utils/loadConfig.js";
18
+ import { spawnSync } from "node:child_process";
19
+ import validations from "./commands/Validations.js";
20
+ import Print from "./commands/Print.js";
21
+ import build from './commands/build/build.js';
22
+ import { runGenerateTypes } from './commands/types/types.js';
23
+ import { cleanBundles, bundleInfo } from './commands/bundle/bundle.js';
24
+ import {
25
+ isLocalDelegationDisabled,
26
+ findNearestLocalCliEntry,
27
+ resolveLocalCliCandidate,
28
+ shouldDelegateToLocalCli
29
+ } from './commands/utils/LocalCliDelegation.js';
30
+ import {
31
+ detectPackageManager,
32
+ getAvailablePackageManagers,
33
+ isPackageManagerAvailable,
34
+ resolvePackageManager,
35
+ runScriptCommand,
36
+ SUPPORTED_PACKAGE_MANAGERS
37
+ } from './commands/utils/PackageManager.js';
38
+ import { SLICE_SCRIPTS, verifySliceScriptsInPackageJson } from './commands/utils/sliceScripts.js';
39
+
40
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
41
+
42
+ const getCategories = () => {
43
+ const config = sharedLoadConfigSync(import.meta.url);
44
+ return config && config.paths?.components ? Object.keys(config.paths.components) : [];
45
+ };
46
+
47
+ // Helper to validate port number
48
+ function parsePort(value) {
49
+ if (!value) return undefined;
50
+ const port = parseInt(value, 10);
51
+ if (isNaN(port) || port < 1 || port > 65535) {
52
+ Print.error(`Invalid port: "${value}". Must be a number between 1 and 65535.`);
53
+ process.exit(1);
54
+ }
55
+ return port;
56
+ }
57
+
58
+ async function withNodeEnv(env, fn) {
59
+ const prevEnv = process.env.NODE_ENV;
60
+ process.env.NODE_ENV = env;
61
+ listComponents();
62
+ try {
63
+ return await fn();
64
+ } finally {
65
+ process.env.NODE_ENV = prevEnv;
66
+ }
67
+ }
68
+
69
+ // Function to run version check for all commands
70
+ async function runWithVersionCheck(commandFunction, ...args) {
71
+ try {
72
+ const globalOpts = program.opts();
73
+ const skipVersionCheck = globalOpts.noVersionCheck === false;
74
+
75
+ if (!skipVersionCheck) {
76
+ updateManager.notifyAvailableUpdates().catch(() => {});
77
+ }
78
+
79
+ const result = await commandFunction(...args);
80
+
81
+ if (!skipVersionCheck) {
82
+ setTimeout(() => {
83
+ versionChecker.checkForUpdates(false);
84
+ }, 100);
85
+ }
86
+
87
+ return result;
88
+ } catch (error) {
89
+ Print.error(`Command execution: ${error.message}`);
90
+ console.error(error.stack);
91
+ return false;
92
+ }
93
+ }
94
+
95
+ function maybeDelegateToLocalCli() {
96
+ if (isLocalDelegationDisabled(process.env)) {
97
+ return;
98
+ }
99
+
100
+ const currentEntryPath = fileURLToPath(import.meta.url);
101
+ const localEntryPath = findNearestLocalCliEntry(process.cwd(), resolveLocalCliCandidate);
102
+
103
+ if (!shouldDelegateToLocalCli(currentEntryPath, localEntryPath)) {
104
+ return;
105
+ }
106
+
107
+ const child = spawnSync(
108
+ process.execPath,
109
+ [localEntryPath, ...process.argv.slice(2)],
110
+ {
111
+ stdio: 'inherit',
112
+ cwd: process.cwd(),
113
+ env: { ...process.env, SLICE_NO_LOCAL_DELEGATION: '1' }
114
+ }
115
+ );
116
+
117
+ process.exit(child.status ?? 1);
118
+ }
119
+
120
+ maybeDelegateToLocalCli();
121
+
122
+ const sliceClient = program;
123
+
124
+ try {
125
+ const pkgPath = path.join(__dirname, "./package.json");
126
+ const pkgRaw = fs.readFileSync(pkgPath, "utf-8");
127
+ const pkg = JSON.parse(pkgRaw);
128
+ sliceClient.version(pkg.version).description("CLI for managing Slice.js framework components");
129
+ } catch {
130
+ sliceClient.version("0.0.0").description("CLI for managing Slice.js framework components");
131
+ }
132
+
133
+ // INIT COMMAND
134
+ sliceClient
135
+ .command("init")
136
+ .description("Initialize a new Slice.js project")
137
+ .argument("[name]", "Project name (defaults to my-slice-app)")
138
+ .option("-y, --yes", "Skip prompts")
139
+ .option("--pm <packageManager>", "Package manager to use (pnpm or npm). Auto-detected when omitted")
140
+ .action(async (name, options) => {
141
+ let projectName = name ? name.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '').replace(/-+/g, '-').replace(/^-|-$/g, '') : 'my-slice-app';
142
+ if (!options.yes) {
143
+ const answers = await inquirer.prompt([
144
+ {
145
+ type: 'input',
146
+ name: 'projectName',
147
+ message: 'What is the name of your project?',
148
+ default: projectName,
149
+ filter: (input) => input.trim()
150
+ .toLowerCase()
151
+ .replace(/\s+/g, '-')
152
+ .replace(/[^a-z0-9-]/g, '')
153
+ .replace(/-+/g, '-')
154
+ .replace(/^-|-$/g, ''),
155
+ validate: (input) => {
156
+ if (!input || !input.trim()) return 'Project name cannot be empty';
157
+ if (input.includes('/') || input.includes('\\')) return 'Use a simple name, not a path';
158
+ return true;
159
+ }
160
+ }
161
+ ]);
162
+ projectName = answers.projectName;
163
+ }
164
+
165
+ // Resolve the package manager: --pm flag → detection → interactive prompt.
166
+ // Detection here has no project root yet (the folder is new), so it relies on
167
+ // npm_config_user_agent (npx / pnpm dlx / PM scripts) or a single available binary.
168
+ let packageManager = options.pm;
169
+ if (packageManager && !SUPPORTED_PACKAGE_MANAGERS.includes(packageManager)) {
170
+ Print.error(`Unsupported package manager "${packageManager}". Use one of: ${SUPPORTED_PACKAGE_MANAGERS.join(', ')}`);
171
+ return;
172
+ }
173
+ if (packageManager && !isPackageManagerAvailable(packageManager)) {
174
+ Print.error(`Package manager "${packageManager}" is not available on this system`);
175
+ return;
176
+ }
177
+ if (!packageManager) {
178
+ const detected = detectPackageManager(null);
179
+ if (detected) {
180
+ packageManager = detected.name;
181
+ } else if (!options.yes) {
182
+ const available = getAvailablePackageManagers();
183
+ if (available.length === 0) {
184
+ Print.error('No package manager found (pnpm or npm). Install one and retry.');
185
+ return;
186
+ }
187
+ const { pm } = await inquirer.prompt([
188
+ {
189
+ type: 'list',
190
+ name: 'pm',
191
+ message: 'Which package manager do you want to use?',
192
+ choices: available,
193
+ default: available.includes('pnpm') ? 'pnpm' : available[0]
194
+ }
195
+ ]);
196
+ packageManager = pm;
197
+ } else {
198
+ const available = getAvailablePackageManagers();
199
+ packageManager = available.includes('pnpm') ? 'pnpm' : available[0];
200
+ if (!packageManager) {
201
+ Print.error('No package manager found (pnpm or npm). Install one and retry.');
202
+ return;
203
+ }
204
+ }
205
+ }
206
+
207
+ const projectDir = path.resolve(projectName);
208
+
209
+ if (fs.existsSync(projectDir)) {
210
+ const contents = fs.readdirSync(projectDir);
211
+ if (contents.length > 0) {
212
+ if (options.yes) {
213
+ Print.error(`Directory "${projectName}" already exists and is not empty. Aborting.`);
214
+ return;
215
+ }
216
+ const { overwrite } = await inquirer.prompt([
217
+ {
218
+ type: 'confirm',
219
+ name: 'overwrite',
220
+ message: `Directory "${projectName}" already exists and is not empty. Continue?`,
221
+ default: false
222
+ }
223
+ ]);
224
+ if (!overwrite) {
225
+ Print.info('Initialization cancelled.');
226
+ return;
227
+ }
228
+ }
229
+ } else {
230
+ fs.mkdirSync(projectDir, { recursive: true });
231
+ }
232
+
233
+ process.chdir(projectDir);
234
+ process.env.INIT_CWD = projectDir;
235
+
236
+ await runWithVersionCheck(async () => {
237
+ await initializeProject({ packageManager });
238
+ });
239
+ });
240
+
241
+ // VERSION COMMAND
242
+ sliceClient
243
+ .command("version")
244
+ .alias("v")
245
+ .description("Show version information and check for updates")
246
+ .action(async () => {
247
+ await versionChecker.showVersionInfo();
248
+ });
249
+
250
+ // BUILD COMMAND
251
+ const buildCommand = sliceClient.command("build")
252
+ .description("Build Slice.js project for production")
253
+ .action(async (options) => {
254
+ await withNodeEnv('production', () => runWithVersionCheck(async () => {
255
+ await build(options);
256
+ }));
257
+ });
258
+
259
+ buildCommand
260
+ .command("clean")
261
+ .description("Remove all generated bundles")
262
+ .action(async () => {
263
+ await cleanBundles();
264
+ });
265
+
266
+ buildCommand
267
+ .command("info")
268
+ .description("Show information about generated bundles")
269
+ .action(async () => {
270
+ await bundleInfo();
271
+ });
272
+
273
+ buildCommand
274
+ .option("-a, --analyze", "Analyze project dependencies without bundling")
275
+ .option("-v, --verbose", "Show detailed output")
276
+ .option("--no-minify", "Disable minification (enabled by default)")
277
+ .option("--no-obfuscate", "Disable obfuscation (enabled by default, no prop mangling)")
278
+ .option("--preview", "Start preview server after build")
279
+ .option("--serve", "Start preview server without building")
280
+ .option("--skip-clean", "Skip cleaning dist before build");
281
+
282
+ // DEV COMMAND (DEVELOPMENT)
283
+ sliceClient
284
+ .command("dev")
285
+ .description("Start development server with hot reload enabled by default")
286
+ .option("-p, --port <port>", "Port for development server")
287
+ .option("--no-hmr", "Disable hot module reload (enabled by default)")
288
+ .action(async (options) => {
289
+ await withNodeEnv('development', () => runWithVersionCheck(async () => {
290
+ await startServer({
291
+ mode: 'development',
292
+ port: parsePort(options.port),
293
+ watch: options.hmr
294
+ });
295
+ }));
296
+ });
297
+
298
+ // START COMMAND - PRODUCTION MODE
299
+ sliceClient
300
+ .command("start")
301
+ .description("Serve production files from dist/ (requires prior slice build)")
302
+ .option("-p, --port <port>", "Port for server")
303
+ .action(async (options) => {
304
+ await withNodeEnv('production', () => runWithVersionCheck(async () => {
305
+ await startServer({
306
+ mode: 'production',
307
+ port: parsePort(options.port)
308
+ });
309
+ }));
310
+ });
311
+
312
+ // COMPONENT COMMAND GROUP - For local component management
313
+ const componentCommand = sliceClient.command("component").alias("comp").description("Manage local project components");
314
+
315
+ // CREATE LOCAL COMPONENT
316
+ componentCommand
317
+ .command("create [name]")
318
+ .alias("new")
319
+ .description("Create a new component in your local project")
320
+ .option("-c, --category <category>", "Component category (e.g. Visual, Service, AppComponents). Skips the prompt when provided.")
321
+ .action(async (name, options) => {
322
+ await runWithVersionCheck(async () => {
323
+ const categories = getCategories();
324
+ if (categories.length === 0) {
325
+ Print.error("No categories found in your project configuration");
326
+ Print.info("Run 'slice init' to initialize your project first");
327
+ Print.commandExample("Initialize project", "slice init");
328
+ return;
329
+ }
330
+
331
+ let componentName = name;
332
+ let category = options.category;
333
+
334
+ // Prompt only for the values not supplied on the command line. Passing both
335
+ // a name and --category runs fully non-interactively (handy for scripts/agents).
336
+ const prompts = [];
337
+ if (!componentName) {
338
+ prompts.push({
339
+ type: "input",
340
+ name: "componentName",
341
+ message: "Enter the component name:",
342
+ validate: (input) => {
343
+ if (!input) return "Component name cannot be empty";
344
+ if (!/^[a-zA-Z][a-zA-Z0-9]*$/.test(input)) {
345
+ return "Component name must start with a letter and contain only alphanumeric characters";
346
+ }
347
+ return true;
348
+ },
349
+ });
350
+ }
351
+ if (!category) {
352
+ prompts.push({
353
+ type: "list",
354
+ name: "category",
355
+ message: "Select the component category:",
356
+ choices: categories,
357
+ });
358
+ }
359
+
360
+ if (prompts.length > 0) {
361
+ const answers = await inquirer.prompt(prompts);
362
+ componentName = componentName ?? answers.componentName;
363
+ category = category ?? answers.category;
364
+ }
365
+
366
+ // createComponent validates the name and the category (and reports a clear
367
+ // error listing valid categories if --category is wrong).
368
+ const result = createComponent(componentName, category);
369
+ if (result) {
370
+ Print.success(`Component '${componentName}' created successfully in category '${category}'`);
371
+ Print.info("Listing updated components:");
372
+ listComponents();
373
+ }
374
+ });
375
+ });
376
+
377
+ // LIST LOCAL COMPONENTS
378
+ componentCommand
379
+ .command("list")
380
+ .alias("ls")
381
+ .description("List all components in your local project")
382
+ .action(async () => {
383
+ await runWithVersionCheck(() => {
384
+ listComponents();
385
+ return Promise.resolve();
386
+ });
387
+ });
388
+
389
+ // DELETE LOCAL COMPONENT
390
+ componentCommand
391
+ .command("delete [name]")
392
+ .alias("remove")
393
+ .description("Delete a component from your local project")
394
+ .option("-c, --category <category>", "Component category. Skips the category prompt when provided.")
395
+ .option("-y, --yes", "Skip the confirmation prompt (for non-interactive use)")
396
+ .action(async (name, options) => {
397
+ await runWithVersionCheck(async () => {
398
+ const categories = getCategories();
399
+ if (categories.length === 0) {
400
+ Print.error("No categories available. Check your configuration");
401
+ Print.info("Run 'slice init' to initialize your project");
402
+ return;
403
+ }
404
+
405
+ try {
406
+ let category = options.category;
407
+ let componentName = name;
408
+
409
+ // Resolve category (prompt only if not provided)
410
+ if (!category) {
411
+ const categoryAnswer = await inquirer.prompt([
412
+ {
413
+ type: "list",
414
+ name: "category",
415
+ message: "Select the component category:",
416
+ choices: categories,
417
+ }
418
+ ]);
419
+ category = categoryAnswer.category;
420
+ }
421
+
422
+ const config = sharedLoadConfigSync(import.meta.url);
423
+ if (!config) {
424
+ Print.error("Could not load configuration");
425
+ return;
426
+ }
427
+
428
+ if (!config.paths.components[category]) {
429
+ Print.error(`Invalid category: '${category}'`);
430
+ Print.info(`Available categories: ${categories.join(", ")}`);
431
+ return;
432
+ }
433
+
434
+ const categoryPath = config.paths.components[category].path;
435
+ const fullPath = getSrcPath(import.meta.url, categoryPath);
436
+
437
+ if (!fs.existsSync(fullPath)) {
438
+ Print.error(`Category path does not exist: ${categoryPath}`);
439
+ return;
440
+ }
441
+
442
+ // Resolve component name (prompt with a list only if not provided)
443
+ if (!componentName) {
444
+ const components = fs.readdirSync(fullPath).filter(item => {
445
+ const itemPath = path.join(fullPath, item);
446
+ return fs.statSync(itemPath).isDirectory();
447
+ });
448
+
449
+ if (components.length === 0) {
450
+ Print.info(`No components found in category '${category}'`);
451
+ return;
452
+ }
453
+
454
+ const componentAnswer = await inquirer.prompt([
455
+ {
456
+ type: "list",
457
+ name: "componentName",
458
+ message: "Select the component to delete:",
459
+ choices: components,
460
+ }
461
+ ]);
462
+ componentName = componentAnswer.componentName;
463
+ }
464
+
465
+ // Confirm unless --yes was passed (passing name + --category + --yes is fully non-interactive)
466
+ if (!options.yes) {
467
+ const { confirm } = await inquirer.prompt([
468
+ {
469
+ type: "confirm",
470
+ name: "confirm",
471
+ message: `Are you sure you want to delete '${componentName}' from '${category}'?`,
472
+ default: false,
473
+ }
474
+ ]);
475
+ if (!confirm) {
476
+ Print.info("Delete operation cancelled");
477
+ return;
478
+ }
479
+ }
480
+
481
+ // deleteComponent validates the name/category and that the component exists.
482
+ if (deleteComponent(componentName, category)) {
483
+ Print.success(`Component ${componentName} deleted successfully`);
484
+ Print.info("Listing updated components:");
485
+ listComponents();
486
+ }
487
+ } catch (error) {
488
+ Print.error(`Deleting component: ${error.message}`);
489
+ }
490
+ });
491
+ });
492
+
493
+ // REGISTRY COMMAND GROUP - For component registry operations
494
+ const registryCommand = sliceClient.command("registry").alias("reg").description("Manage components from official Slice.js repository");
495
+
496
+ // TYPES COMMAND GROUP - TypeScript declarations from static props
497
+ const typesCommand = sliceClient.command("types").description("Generate TypeScript declarations from component static props");
498
+
499
+ typesCommand
500
+ .command("generate")
501
+ .description("Generate src/slice-build.generated.d.ts for slice.build autocomplete")
502
+ .option("-o, --output <path>", "Custom output path", "src/slice-build.generated.d.ts")
503
+ .action(async (options) => {
504
+ await runWithVersionCheck(async () => {
505
+ const projectRoot = getProjectRoot(import.meta.url);
506
+ const outputPath = path.isAbsolute(options.output)
507
+ ? options.output
508
+ : path.join(projectRoot, options.output);
509
+
510
+ await runGenerateTypes({
511
+ projectRoot,
512
+ outputPath
513
+ });
514
+ });
515
+ });
516
+
517
+ // GET COMPONENTS FROM REGISTRY
518
+ registryCommand
519
+ .command("get [components...]")
520
+ .description("Download and install components from official repository")
521
+ .option("-f, --force", "Force overwrite existing components")
522
+ .option("-s, --service", "Install Service components instead of Visual")
523
+ .action(async (components, options) => {
524
+ await runWithVersionCheck(async () => {
525
+ await getComponent(components, {
526
+ force: options.force,
527
+ service: options.service
528
+ });
529
+ });
530
+ });
531
+
532
+ // LIST REGISTRY COMPONENTS
533
+ registryCommand
534
+ .command("list")
535
+ .alias("ls")
536
+ .description("List all available components in the official repository")
537
+ .action(async () => {
538
+ await runWithVersionCheck(async () => {
539
+ await listRemoteComponents();
540
+ });
541
+ });
542
+
543
+ // SYNC COMPONENTS FROM REGISTRY
544
+ registryCommand
545
+ .command("sync")
546
+ .description("Update all local components to latest versions from repository")
547
+ .option("-f, --force", "Force update without confirmation")
548
+ .action(async (options) => {
549
+ await runWithVersionCheck(async () => {
550
+ await syncComponents({
551
+ force: options.force
552
+ });
553
+ });
554
+ });
555
+
556
+ // SHORTCUTS - Top-level convenient commands
557
+ sliceClient
558
+ .command("get [components...]")
559
+ .description("Quick install components from registry")
560
+ .option("-f, --force", "Force overwrite existing components")
561
+ .option("-s, --service", "Install Service components instead of Visual")
562
+ .action(async (components, options) => {
563
+ await runWithVersionCheck(async () => {
564
+ if (!components || components.length === 0) {
565
+ Print.info("Use 'slice registry list' to see available components");
566
+ Print.commandExample("Get multiple components", "slice get Button Card Input");
567
+ Print.commandExample("Get service component", "slice get FetchManager --service");
568
+ Print.commandExample("Browse components", "slice browse");
569
+ return;
570
+ }
571
+
572
+ await getComponent(components, {
573
+ force: options.force,
574
+ service: options.service
575
+ });
576
+ });
577
+ });
578
+
579
+ sliceClient
580
+ .command("browse")
581
+ .description("Quick browse available components")
582
+ .action(async () => {
583
+ await runWithVersionCheck(async () => {
584
+ await listRemoteComponents();
585
+ });
586
+ });
587
+
588
+ sliceClient
589
+ .command("sync")
590
+ .description("Quick sync local components to latest versions")
591
+ .option("-f, --force", "Force update without confirmation")
592
+ .action(async (options) => {
593
+ await runWithVersionCheck(async () => {
594
+ await syncComponents({
595
+ force: options.force
596
+ });
597
+ });
598
+ });
599
+
600
+ // LIST COMMAND - Quick shortcut for listing local components
601
+ sliceClient
602
+ .command("list")
603
+ .description("Quick list all local components (alias for component list)")
604
+ .action(async () => {
605
+ await runWithVersionCheck(() => {
606
+ listComponents();
607
+ return Promise.resolve();
608
+ });
609
+ });
610
+
611
+ // DOCTOR COMMAND - Diagnose project issues
612
+ sliceClient
613
+ .command("doctor")
614
+ .alias("diagnose")
615
+ .description("Run diagnostics to check project health")
616
+ .action(async () => {
617
+ await runWithVersionCheck(async () => {
618
+ await runDiagnostics();
619
+ });
620
+ });
621
+
622
+ // POSTINSTALL COMMAND - Configure npm scripts in existing project
623
+ sliceClient
624
+ .command("postinstall")
625
+ .description("Configure npm scripts in package.json (useful when adding slicejs-cli to an existing project)")
626
+ .action(() => {
627
+ const pnpmHome = process.env.PNPM_HOME;
628
+ const cliEntryPath = fileURLToPath(import.meta.url);
629
+ const isGlobal = process.env.npm_config_global === 'true'
630
+ || (pnpmHome && cliEntryPath.startsWith(pnpmHome));
631
+ if (isGlobal) {
632
+ console.log('⚠️ Global installation of slicejs-cli detected.');
633
+ console.log(' We strongly recommend using a local installation to avoid version mismatches.');
634
+ console.log(` Uninstall global: ${pnpmHome ? 'pnpm remove -g slicejs-cli' : 'npm uninstall -g slicejs-cli'}`);
635
+ return;
636
+ }
637
+
638
+ const projectRoot = getProjectRoot(import.meta.url);
639
+ const pkgPath = path.join(projectRoot, 'package.json');
640
+ const packageManager = resolvePackageManager(projectRoot).name;
641
+
642
+ const sliceScripts = SLICE_SCRIPTS;
643
+
644
+ try {
645
+ let pkg = {};
646
+ if (fs.existsSync(pkgPath)) {
647
+ pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
648
+ } else {
649
+ pkg = {
650
+ name: path.basename(projectRoot),
651
+ version: '1.0.0',
652
+ description: 'Slice.js project',
653
+ scripts: {}
654
+ };
655
+ }
656
+
657
+ pkg.scripts = pkg.scripts || {};
658
+ let addedCount = 0;
659
+ for (const [script, command] of Object.entries(sliceScripts)) {
660
+ if (!pkg.scripts[script]) {
661
+ pkg.scripts[script] = command;
662
+ addedCount++;
663
+ }
664
+ }
665
+
666
+ fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2), 'utf-8');
667
+ console.log(`✅ slicejs-cli installed successfully. Added ${addedCount} package scripts to package.json.`);
668
+ console.log(` Run: ${runScriptCommand(packageManager, 'slice:dev')}`);
669
+ } catch (err) {
670
+ console.log('✅ slicejs-cli installed successfully.');
671
+ console.log(' Could not auto-configure scripts:', err.message);
672
+ console.log(` Configure scripts manually and run: ${runScriptCommand(packageManager, 'slice:dev')}`);
673
+ }
674
+ });
675
+
676
+ // Enhanced help
677
+ sliceClient
678
+ .option("--no-version-check", "Skip version check for this command")
679
+ .configureHelp({
680
+ sortSubcommands: true,
681
+ subcommandTerm: (cmd) => cmd.name() + ' ' + cmd.usage()
682
+ });
683
+
684
+
685
+ // Custom help - SIMPLIFIED for development only
686
+ sliceClient.addHelpText('after', `
687
+ Common Usage Examples:
688
+ slice init - Initialize new Slice.js project
689
+ slice dev - Start development server
690
+ slice build - Build production output (bundles + dist)
691
+ slice start - Start production server
692
+ slice get Button Card Input - Install Visual components from registry
693
+ slice get FetchManager -s - Install Service component from registry
694
+ slice browse - Browse all available components
695
+ slice sync - Update local components to latest versions
696
+ slice component create - Create new local component
697
+ slice list - List all local components
698
+ slice doctor - Run project diagnostics
699
+ slice types generate - Generate TypeScript typings for slice.build
700
+ slice postinstall - Configure npm scripts in package.json
701
+
702
+ Command Categories:
703
+ init, dev, start - Project lifecycle (development only)
704
+ get, browse, sync, list - Quick shortcuts
705
+ • component <cmd> - Local component management
706
+ registry <cmd> - Official repository operations
707
+ types generate - Type declarations from static props
708
+ version, doctor, setup - Maintenance commands
709
+
710
+ Development Workflow:
711
+ slice init - Initialize project
712
+ slice dev - Start development server (serves from /src)
713
+ • slice build - Build production output to /dist (includes bundles)
714
+ slice start - Serve production build from /dist
715
+
716
+ More info: https://slice-js-docs.vercel.app/
717
+ `);
718
+
719
+ // Default action with better messaging
720
+ if (!process.argv.slice(2).length) {
721
+ Print.newLine();
722
+ Print.info("Start with: slice init");
723
+ Print.commandExample("Development", "slice dev");
724
+ Print.commandExample("View available components", "slice browse");
725
+ Print.info("Use 'slice --help' for full help");
726
+ }
727
+
728
+ // Error handling for unknown commands
729
+ program.on('command:*', () => {
730
+ Print.error('Invalid command. See available commands above');
731
+ Print.info("Use 'slice --help' for help");
732
+ process.exit(1);
733
+ });
734
+
735
+ // HELP Command
736
+ const helpCommand = sliceClient.command("help").description("Display help information for Slice.js CLI").action(() => {
737
+ sliceClient.outputHelp();
738
+ });
739
+
740
+ program.parse();