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.
@@ -1,471 +1,471 @@
1
- // commands/utils/updateManager.js
2
-
3
- import { exec } from "child_process";
4
- import { promisify } from "util";
5
- import inquirer from "inquirer";
6
- import ora from "ora";
7
- import Print from "../Print.js";
8
- import versionChecker from "./VersionChecker.js";
9
- import { getProjectRoot, getApiPath, getPath } from "../utils/PathHelper.js";
10
- import { resolvePackageManager, installCommand } from "../utils/PackageManager.js";
11
- import path from "path";
12
- import { fileURLToPath } from "url";
13
- import fs from "fs-extra";
14
-
15
- const execAsync = promisify(exec);
16
-
17
- export class UpdateManager {
18
- constructor() {
19
- this.packagesToUpdate = [];
20
- }
21
-
22
- getPackageManager() {
23
- if (!this._packageManager) {
24
- this._packageManager = resolvePackageManager(getProjectRoot(import.meta.url)).name;
25
- }
26
- return this._packageManager;
27
- }
28
-
29
- async detectCliInstall() {
30
- try {
31
- const moduleDir = path.dirname(fileURLToPath(import.meta.url));
32
- const cliRoot = path.join(moduleDir, '../../');
33
- const projectRoot = getProjectRoot(import.meta.url);
34
- const packageManager = this.getPackageManager();
35
- const localNodeModules = path.join(projectRoot, 'node_modules');
36
-
37
- if (cliRoot.startsWith(localNodeModules)) {
38
- return { type: 'local', cliRoot, projectRoot, packageManager };
39
- }
40
-
41
- // Global pnpm installs live under PNPM_HOME — `npm config get prefix`
42
- // knows nothing about them (and npm may not even exist on the machine).
43
- const pnpmHome = process.env.PNPM_HOME;
44
- if (pnpmHome && cliRoot.startsWith(pnpmHome)) {
45
- return { type: 'global', cliRoot, projectRoot, packageManager: 'pnpm' };
46
- }
47
-
48
- let globalPrefix = '';
49
- try {
50
- const { stdout } = await execAsync('npm config get prefix');
51
- globalPrefix = stdout.toString().trim();
52
- } catch {}
53
- const globalNodeModules = globalPrefix ? path.join(globalPrefix, 'node_modules') : '';
54
- if (globalNodeModules && cliRoot.startsWith(globalNodeModules)) {
55
- return { type: 'global', cliRoot, projectRoot, packageManager: 'npm' };
56
- }
57
-
58
- return { type: 'unknown', cliRoot, projectRoot, packageManager };
59
- } catch (error) {
60
- return { type: 'unknown', packageManager: this.getPackageManager() };
61
- }
62
- }
63
-
64
- /**
65
- * Check for available updates and return structured info
66
- */
67
- async checkForUpdates(options = {}) {
68
- const { silentErrors = false } = options;
69
-
70
- try {
71
- const updateInfo = await versionChecker.checkForUpdates(true); // Silent mode
72
-
73
- if (!updateInfo) {
74
- return null;
75
- }
76
-
77
- const updates = [];
78
-
79
- if (updateInfo.cli.status === 'outdated') {
80
- updates.push({
81
- name: 'slicejs-cli',
82
- displayName: 'Slice.js CLI',
83
- current: updateInfo.cli.current,
84
- latest: updateInfo.cli.latest,
85
- type: 'cli'
86
- });
87
- }
88
-
89
- if (updateInfo.framework.status === 'outdated') {
90
- updates.push({
91
- name: 'slicejs-web-framework',
92
- displayName: 'Slice.js Framework',
93
- current: updateInfo.framework.current,
94
- latest: updateInfo.framework.latest,
95
- type: 'framework'
96
- });
97
- }
98
-
99
- return {
100
- hasUpdates: updates.length > 0,
101
- updates,
102
- allCurrent: updateInfo.cli.status === 'current' && updateInfo.framework.status === 'current'
103
- };
104
- } catch (error) {
105
- if (!silentErrors) {
106
- Print.error(`Checking for updates: ${error.message}`);
107
- }
108
- return null;
109
- }
110
- }
111
-
112
- async notifyAvailableUpdates() {
113
- const updateInfo = await this.checkForUpdates({ silentErrors: true });
114
-
115
- if (!updateInfo || !updateInfo.hasUpdates) {
116
- return false;
117
- }
118
-
119
- this.displayUpdates(updateInfo);
120
- Print.info("Run 'slice update' to install updates when convenient.");
121
- return true;
122
- }
123
-
124
- /**
125
- * Display available updates in a formatted way
126
- */
127
- displayUpdates(updateInfo) {
128
- if (!updateInfo || !updateInfo.hasUpdates) {
129
- return;
130
- }
131
-
132
- console.log('');
133
- Print.warning('📦 Available Updates:');
134
- console.log('');
135
-
136
- updateInfo.updates.forEach(pkg => {
137
- console.log(` ${pkg.type === 'cli' ? '🔧' : '⚡'} ${pkg.displayName}`);
138
- console.log(` ${pkg.current} → ${pkg.latest}`);
139
- });
140
-
141
- console.log('');
142
- console.log(' 📚 Changelog: https://github.com/VKneider/slice.js/releases');
143
- console.log('');
144
- }
145
-
146
- /**
147
- * Prompt user to select which packages to update
148
- */
149
- async promptForUpdates(updateInfo, options = {}) {
150
- if (!updateInfo || !updateInfo.hasUpdates) {
151
- return [];
152
- }
153
-
154
- // If --yes flag is set, return all updates
155
- if (options.yes) {
156
- return updateInfo.updates.map(pkg => pkg.name);
157
- }
158
-
159
- // If specific package flags are set
160
- if (options.cli || options.framework) {
161
- const selected = [];
162
- if (options.cli) {
163
- const cliUpdate = updateInfo.updates.find(pkg => pkg.type === 'cli');
164
- if (cliUpdate) selected.push(cliUpdate.name);
165
- }
166
- if (options.framework) {
167
- const frameworkUpdate = updateInfo.updates.find(pkg => pkg.type === 'framework');
168
- if (frameworkUpdate) selected.push(frameworkUpdate.name);
169
- }
170
- return selected;
171
- }
172
-
173
- // Interactive selection
174
- const choices = updateInfo.updates.map(pkg => ({
175
- name: `${pkg.displayName} (${pkg.current} → ${pkg.latest})`,
176
- value: pkg.name,
177
- checked: true
178
- }));
179
-
180
- const answers = await inquirer.prompt([
181
- {
182
- type: 'checkbox',
183
- name: 'packages',
184
- message: 'Which packages do you want to update?',
185
- choices,
186
- validate: (answer) => {
187
- if (answer.length === 0) {
188
- return 'You must select at least one package';
189
- }
190
- return true;
191
- }
192
- }
193
- ]);
194
-
195
- return answers.packages;
196
- }
197
-
198
- async buildUpdatePlan(packages) {
199
- const plan = [];
200
- const info = await this.detectCliInstall();
201
- const pm = info.packageManager || this.getPackageManager();
202
- for (const pkg of packages) {
203
- if (pkg === 'slicejs-cli' && info.type === 'global') {
204
- plan.push({ package: pkg, target: 'global', command: installCommand(pm, 'slicejs-cli@latest', { global: true }) });
205
- } else {
206
- plan.push({ package: pkg, target: 'project', command: installCommand(pm, `${pkg}@latest`) });
207
- }
208
- }
209
- return plan;
210
- }
211
-
212
- /**
213
- * Execute npm update command for a specific package
214
- */
215
- async updatePackage(packageName) {
216
- try {
217
- const pm = this.getPackageManager();
218
- let installCmd = installCommand(pm, `${packageName}@latest`);
219
- let options = {};
220
-
221
- if (packageName === 'slicejs-cli') {
222
- const info = await this.detectCliInstall();
223
- if (info.type === 'global') {
224
- const globalPm = info.packageManager || pm;
225
- installCmd = installCommand(globalPm, 'slicejs-cli@latest', { global: true });
226
- } else {
227
- options.cwd = info.projectRoot || getProjectRoot(import.meta.url);
228
- }
229
- } else {
230
- options.cwd = getProjectRoot(import.meta.url);
231
- }
232
-
233
- // Install directly — npm/pnpm upgrade in place. (We used to uninstall
234
- // first, which left the project without the package whenever the
235
- // subsequent install failed, e.g. offline or under a release-age policy.)
236
- const { stdout, stderr } = await execAsync(installCmd, options);
237
-
238
- return {
239
- success: true,
240
- packageName,
241
- stdout,
242
- stderr
243
- };
244
- } catch (error) {
245
- return {
246
- success: false,
247
- packageName,
248
- error: error.message
249
- };
250
- }
251
- }
252
-
253
- /**
254
- * Update multiple packages with progress indication
255
- */
256
- async installUpdates(packages) {
257
- const results = [];
258
-
259
- for (const packageName of packages) {
260
- const spinner = ora(`Updating ${packageName}...`).start();
261
-
262
- try {
263
- const result = await this.updatePackage(packageName);
264
-
265
- if (result.success) {
266
- spinner.succeed(`${packageName} updated successfully`);
267
- results.push({ packageName, success: true });
268
- } else {
269
- spinner.fail(`Error updating ${packageName}`);
270
- Print.error(`Details: ${result.error}`);
271
- results.push({ packageName, success: false, error: result.error });
272
- }
273
- } catch (error) {
274
- spinner.fail(`Error updating ${packageName}`);
275
- Print.error(`Details: ${error.message}`);
276
- results.push({ packageName, success: false, error: error.message });
277
- }
278
- }
279
-
280
- return results;
281
- }
282
-
283
- /**
284
- * Main method to check and prompt for updates
285
- */
286
- async checkAndPromptUpdates(options = {}) {
287
- const spinner = ora('Checking for updates...').start();
288
-
289
- try {
290
- const updateInfo = await this.checkForUpdates();
291
- spinner.stop();
292
-
293
- if (!updateInfo) {
294
- Print.error('Could not check for updates. Verify your internet connection.');
295
- return false;
296
- }
297
-
298
- if (updateInfo.allCurrent || !updateInfo.hasUpdates) {
299
- Print.success('✅ All components are up to date!');
300
- // --update-api works even when no package update runs.
301
- if (options.updateApi) {
302
- await this.updateApiIndexIfNeeded(options);
303
- }
304
- return true;
305
- }
306
-
307
- // Display available updates
308
- this.displayUpdates(updateInfo);
309
-
310
- // Get packages to update
311
- const packagesToUpdate = await this.promptForUpdates(updateInfo, options);
312
-
313
- if (!packagesToUpdate || packagesToUpdate.length === 0) {
314
- Print.info('No packages selected for update.');
315
- return false;
316
- }
317
-
318
- // Show plan and confirm installation if not auto-confirmed
319
- let plan = await this.buildUpdatePlan(packagesToUpdate);
320
- console.log('');
321
- Print.info('🧭 Update plan:');
322
- plan.forEach(item => {
323
- const where = item.target === 'global' ? 'GLOBAL' : 'PROJECT';
324
- console.log(` • ${item.package} → ${where}`);
325
- console.log(` ${item.command}`);
326
- });
327
- console.log('');
328
-
329
- const cliInfo = await this.detectCliInstall();
330
- if (cliInfo.type === 'global' && !packagesToUpdate.includes('slicejs-cli')) {
331
- if (!options.yes && !options.cli) {
332
- const { addCli } = await inquirer.prompt([
333
- {
334
- type: 'confirm',
335
- name: 'addCli',
336
- message: 'Global CLI detected. Add the global CLI update to the plan?',
337
- default: true
338
- }
339
- ]);
340
- if (addCli) {
341
- packagesToUpdate.push('slicejs-cli');
342
- plan = await this.buildUpdatePlan(packagesToUpdate);
343
- console.log('');
344
- Print.info('🧭 Updated plan:');
345
- plan.forEach(item => {
346
- const where = item.target === 'global' ? 'GLOBAL' : 'PROJECT';
347
- console.log(` • ${item.package} → ${where}`);
348
- console.log(` ${item.command}`);
349
- });
350
- console.log('');
351
- }
352
- } else {
353
- Print.warning('Global CLI detected. It is recommended to update slicejs-cli globally to keep aligned with the framework.');
354
- console.log(` Suggestion: ${installCommand(cliInfo.packageManager || this.getPackageManager(), 'slicejs-cli@latest', { global: true })}`);
355
- console.log('');
356
- }
357
- }
358
-
359
- if (!options.yes && !options.cli && !options.framework) {
360
- const { confirm } = await inquirer.prompt([
361
- {
362
- type: 'confirm',
363
- name: 'confirm',
364
- message: 'Do you want to proceed with the update according to the plan shown?',
365
- default: true
366
- }
367
- ]);
368
-
369
- if (!confirm) {
370
- Print.info('Update cancelled.');
371
- return false;
372
- }
373
- }
374
-
375
- console.log(''); // Line break
376
- Print.info('📥 Installing updates...');
377
- console.log('');
378
-
379
- // Install updates
380
- const results = await this.installUpdates(packagesToUpdate);
381
-
382
- // Summary
383
- console.log('');
384
- const successCount = results.filter(r => r.success).length;
385
- const failCount = results.filter(r => !r.success).length;
386
-
387
- if (failCount === 0) {
388
- Print.success(`✅ ${successCount} package(s) updated successfully!`);
389
- } else {
390
- Print.warning(`⚠️ ${successCount} successful, ${failCount} failed`);
391
- }
392
-
393
- if (successCount > 0) {
394
- console.log('');
395
- Print.info('💡 It is recommended to restart the development server if it is running.');
396
- }
397
-
398
- const frameworkUpdated = results.find(r => r.packageName === 'slicejs-web-framework' && r.success);
399
- if (frameworkUpdated || options.updateApi) {
400
- await this.updateApiIndexIfNeeded(options);
401
- }
402
-
403
- return failCount === 0;
404
-
405
- } catch (error) {
406
- spinner.stop();
407
- Print.error(`Error during update: ${error.message}`);
408
- return false;
409
- }
410
- }
411
-
412
- async updateApiIndexIfNeeded(options = {}) {
413
- try {
414
- const projectApiPath = getApiPath(import.meta.url, 'index.js');
415
- const frameworkApiPath = getPath(import.meta.url, 'node_modules', 'slicejs-web-framework', 'api', 'index.js');
416
-
417
- if (!await fs.pathExists(projectApiPath) || !await fs.pathExists(frameworkApiPath)) {
418
- return;
419
- }
420
-
421
- const projectContent = await fs.readFile(projectApiPath, 'utf-8');
422
- const frameworkContent = await fs.readFile(frameworkApiPath, 'utf-8');
423
-
424
- if (projectContent === frameworkContent) {
425
- return;
426
- }
427
-
428
- Print.warning('⚠️ Detected changes in framework api/index.js.');
429
-
430
- // Overwriting api/index.js is opt-in: `--update-api` is the only way
431
- // to auto-confirm. `-y/--yes` deliberately does NOT imply it — the
432
- // project file may carry local changes, so a blanket "yes to updates"
433
- // must not silently replace it.
434
- let confirmUpdate = options.updateApi === true;
435
- if (!confirmUpdate && options.yes === true) {
436
- Print.info('Skipping api/index.js update (not updated by default).');
437
- Print.info('Run "slice update --update-api" to update it (a .bak backup is created).');
438
- return;
439
- }
440
- if (!confirmUpdate) {
441
- const answers = await inquirer.prompt([
442
- {
443
- type: 'confirm',
444
- name: 'confirm',
445
- message: 'Update your project api/index.js to the latest framework version?',
446
- default: false
447
- }
448
- ]);
449
- confirmUpdate = answers.confirm;
450
- }
451
-
452
- if (!confirmUpdate) {
453
- Print.info('Skipping api/index.js update.');
454
- return;
455
- }
456
-
457
- const backupPath = `${projectApiPath}.bak`;
458
- await fs.copy(projectApiPath, backupPath);
459
- await fs.writeFile(projectApiPath, frameworkContent, 'utf-8');
460
- Print.success('✅ api/index.js updated from framework template.');
461
- Print.info(`Backup created at ${backupPath}`);
462
- } catch (error) {
463
- Print.error(`Failed to update api/index.js: ${error.message}`);
464
- }
465
- }
466
- }
467
-
468
- // Singleton instance
469
- const updateManager = new UpdateManager();
470
-
471
- export default updateManager;
1
+ // commands/utils/updateManager.js
2
+
3
+ import { exec } from "child_process";
4
+ import { promisify } from "util";
5
+ import inquirer from "inquirer";
6
+ import ora from "ora";
7
+ import Print from "../Print.js";
8
+ import versionChecker from "./VersionChecker.js";
9
+ import { getProjectRoot, getApiPath, getPath } from "../utils/PathHelper.js";
10
+ import { resolvePackageManager, installCommand } from "../utils/PackageManager.js";
11
+ import path from "path";
12
+ import { fileURLToPath } from "url";
13
+ import fs from "fs-extra";
14
+
15
+ const execAsync = promisify(exec);
16
+
17
+ export class UpdateManager {
18
+ constructor() {
19
+ this.packagesToUpdate = [];
20
+ }
21
+
22
+ getPackageManager() {
23
+ if (!this._packageManager) {
24
+ this._packageManager = resolvePackageManager(getProjectRoot(import.meta.url)).name;
25
+ }
26
+ return this._packageManager;
27
+ }
28
+
29
+ async detectCliInstall() {
30
+ try {
31
+ const moduleDir = path.dirname(fileURLToPath(import.meta.url));
32
+ const cliRoot = path.join(moduleDir, '../../');
33
+ const projectRoot = getProjectRoot(import.meta.url);
34
+ const packageManager = this.getPackageManager();
35
+ const localNodeModules = path.join(projectRoot, 'node_modules');
36
+
37
+ if (cliRoot.startsWith(localNodeModules)) {
38
+ return { type: 'local', cliRoot, projectRoot, packageManager };
39
+ }
40
+
41
+ // Global pnpm installs live under PNPM_HOME — `npm config get prefix`
42
+ // knows nothing about them (and npm may not even exist on the machine).
43
+ const pnpmHome = process.env.PNPM_HOME;
44
+ if (pnpmHome && cliRoot.startsWith(pnpmHome)) {
45
+ return { type: 'global', cliRoot, projectRoot, packageManager: 'pnpm' };
46
+ }
47
+
48
+ let globalPrefix = '';
49
+ try {
50
+ const { stdout } = await execAsync('npm config get prefix');
51
+ globalPrefix = stdout.toString().trim();
52
+ } catch {}
53
+ const globalNodeModules = globalPrefix ? path.join(globalPrefix, 'node_modules') : '';
54
+ if (globalNodeModules && cliRoot.startsWith(globalNodeModules)) {
55
+ return { type: 'global', cliRoot, projectRoot, packageManager: 'npm' };
56
+ }
57
+
58
+ return { type: 'unknown', cliRoot, projectRoot, packageManager };
59
+ } catch (error) {
60
+ return { type: 'unknown', packageManager: this.getPackageManager() };
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Check for available updates and return structured info
66
+ */
67
+ async checkForUpdates(options = {}) {
68
+ const { silentErrors = false } = options;
69
+
70
+ try {
71
+ const updateInfo = await versionChecker.checkForUpdates(true); // Silent mode
72
+
73
+ if (!updateInfo) {
74
+ return null;
75
+ }
76
+
77
+ const updates = [];
78
+
79
+ if (updateInfo.cli.status === 'outdated') {
80
+ updates.push({
81
+ name: 'slicejs-cli',
82
+ displayName: 'Slice.js CLI',
83
+ current: updateInfo.cli.current,
84
+ latest: updateInfo.cli.latest,
85
+ type: 'cli'
86
+ });
87
+ }
88
+
89
+ if (updateInfo.framework.status === 'outdated') {
90
+ updates.push({
91
+ name: 'slicejs-web-framework',
92
+ displayName: 'Slice.js Framework',
93
+ current: updateInfo.framework.current,
94
+ latest: updateInfo.framework.latest,
95
+ type: 'framework'
96
+ });
97
+ }
98
+
99
+ return {
100
+ hasUpdates: updates.length > 0,
101
+ updates,
102
+ allCurrent: updateInfo.cli.status === 'current' && updateInfo.framework.status === 'current'
103
+ };
104
+ } catch (error) {
105
+ if (!silentErrors) {
106
+ Print.error(`Checking for updates: ${error.message}`);
107
+ }
108
+ return null;
109
+ }
110
+ }
111
+
112
+ async notifyAvailableUpdates() {
113
+ const updateInfo = await this.checkForUpdates({ silentErrors: true });
114
+
115
+ if (!updateInfo || !updateInfo.hasUpdates) {
116
+ return false;
117
+ }
118
+
119
+ this.displayUpdates(updateInfo);
120
+ Print.info("Use your package manager to install updates: npm/pnpm update slicejs-cli slicejs-web-framework");
121
+ return true;
122
+ }
123
+
124
+ /**
125
+ * Display available updates in a formatted way
126
+ */
127
+ displayUpdates(updateInfo) {
128
+ if (!updateInfo || !updateInfo.hasUpdates) {
129
+ return;
130
+ }
131
+
132
+ console.log('');
133
+ Print.warning('📦 Available Updates:');
134
+ console.log('');
135
+
136
+ updateInfo.updates.forEach(pkg => {
137
+ console.log(` ${pkg.type === 'cli' ? '🔧' : '⚡'} ${pkg.displayName}`);
138
+ console.log(` ${pkg.current} → ${pkg.latest}`);
139
+ });
140
+
141
+ console.log('');
142
+ console.log(' 📚 Changelog: https://github.com/VKneider/slice.js/releases');
143
+ console.log('');
144
+ }
145
+
146
+ /**
147
+ * Prompt user to select which packages to update
148
+ */
149
+ async promptForUpdates(updateInfo, options = {}) {
150
+ if (!updateInfo || !updateInfo.hasUpdates) {
151
+ return [];
152
+ }
153
+
154
+ // If --yes flag is set, return all updates
155
+ if (options.yes) {
156
+ return updateInfo.updates.map(pkg => pkg.name);
157
+ }
158
+
159
+ // If specific package flags are set
160
+ if (options.cli || options.framework) {
161
+ const selected = [];
162
+ if (options.cli) {
163
+ const cliUpdate = updateInfo.updates.find(pkg => pkg.type === 'cli');
164
+ if (cliUpdate) selected.push(cliUpdate.name);
165
+ }
166
+ if (options.framework) {
167
+ const frameworkUpdate = updateInfo.updates.find(pkg => pkg.type === 'framework');
168
+ if (frameworkUpdate) selected.push(frameworkUpdate.name);
169
+ }
170
+ return selected;
171
+ }
172
+
173
+ // Interactive selection
174
+ const choices = updateInfo.updates.map(pkg => ({
175
+ name: `${pkg.displayName} (${pkg.current} → ${pkg.latest})`,
176
+ value: pkg.name,
177
+ checked: true
178
+ }));
179
+
180
+ const answers = await inquirer.prompt([
181
+ {
182
+ type: 'checkbox',
183
+ name: 'packages',
184
+ message: 'Which packages do you want to update?',
185
+ choices,
186
+ validate: (answer) => {
187
+ if (answer.length === 0) {
188
+ return 'You must select at least one package';
189
+ }
190
+ return true;
191
+ }
192
+ }
193
+ ]);
194
+
195
+ return answers.packages;
196
+ }
197
+
198
+ async buildUpdatePlan(packages) {
199
+ const plan = [];
200
+ const info = await this.detectCliInstall();
201
+ const pm = info.packageManager || this.getPackageManager();
202
+ for (const pkg of packages) {
203
+ if (pkg === 'slicejs-cli' && info.type === 'global') {
204
+ plan.push({ package: pkg, target: 'global', command: installCommand(pm, 'slicejs-cli@latest', { global: true }) });
205
+ } else {
206
+ plan.push({ package: pkg, target: 'project', command: installCommand(pm, `${pkg}@latest`) });
207
+ }
208
+ }
209
+ return plan;
210
+ }
211
+
212
+ /**
213
+ * Execute npm update command for a specific package
214
+ */
215
+ async updatePackage(packageName) {
216
+ try {
217
+ const pm = this.getPackageManager();
218
+ let installCmd = installCommand(pm, `${packageName}@latest`);
219
+ let options = {};
220
+
221
+ if (packageName === 'slicejs-cli') {
222
+ const info = await this.detectCliInstall();
223
+ if (info.type === 'global') {
224
+ const globalPm = info.packageManager || pm;
225
+ installCmd = installCommand(globalPm, 'slicejs-cli@latest', { global: true });
226
+ } else {
227
+ options.cwd = info.projectRoot || getProjectRoot(import.meta.url);
228
+ }
229
+ } else {
230
+ options.cwd = getProjectRoot(import.meta.url);
231
+ }
232
+
233
+ // Install directly — npm/pnpm upgrade in place. (We used to uninstall
234
+ // first, which left the project without the package whenever the
235
+ // subsequent install failed, e.g. offline or under a release-age policy.)
236
+ const { stdout, stderr } = await execAsync(installCmd, options);
237
+
238
+ return {
239
+ success: true,
240
+ packageName,
241
+ stdout,
242
+ stderr
243
+ };
244
+ } catch (error) {
245
+ return {
246
+ success: false,
247
+ packageName,
248
+ error: error.message
249
+ };
250
+ }
251
+ }
252
+
253
+ /**
254
+ * Update multiple packages with progress indication
255
+ */
256
+ async installUpdates(packages) {
257
+ const results = [];
258
+
259
+ for (const packageName of packages) {
260
+ const spinner = ora(`Updating ${packageName}...`).start();
261
+
262
+ try {
263
+ const result = await this.updatePackage(packageName);
264
+
265
+ if (result.success) {
266
+ spinner.succeed(`${packageName} updated successfully`);
267
+ results.push({ packageName, success: true });
268
+ } else {
269
+ spinner.fail(`Error updating ${packageName}`);
270
+ Print.error(`Details: ${result.error}`);
271
+ results.push({ packageName, success: false, error: result.error });
272
+ }
273
+ } catch (error) {
274
+ spinner.fail(`Error updating ${packageName}`);
275
+ Print.error(`Details: ${error.message}`);
276
+ results.push({ packageName, success: false, error: error.message });
277
+ }
278
+ }
279
+
280
+ return results;
281
+ }
282
+
283
+ /**
284
+ * Main method to check and prompt for updates
285
+ */
286
+ async checkAndPromptUpdates(options = {}) {
287
+ const spinner = ora('Checking for updates...').start();
288
+
289
+ try {
290
+ const updateInfo = await this.checkForUpdates();
291
+ spinner.stop();
292
+
293
+ if (!updateInfo) {
294
+ Print.error('Could not check for updates. Verify your internet connection.');
295
+ return false;
296
+ }
297
+
298
+ if (updateInfo.allCurrent || !updateInfo.hasUpdates) {
299
+ Print.success('✅ All components are up to date!');
300
+ // --update-api works even when no package update runs.
301
+ if (options.updateApi) {
302
+ await this.updateApiIndexIfNeeded(options);
303
+ }
304
+ return true;
305
+ }
306
+
307
+ // Display available updates
308
+ this.displayUpdates(updateInfo);
309
+
310
+ // Get packages to update
311
+ const packagesToUpdate = await this.promptForUpdates(updateInfo, options);
312
+
313
+ if (!packagesToUpdate || packagesToUpdate.length === 0) {
314
+ Print.info('No packages selected for update.');
315
+ return false;
316
+ }
317
+
318
+ // Show plan and confirm installation if not auto-confirmed
319
+ let plan = await this.buildUpdatePlan(packagesToUpdate);
320
+ console.log('');
321
+ Print.info('🧭 Update plan:');
322
+ plan.forEach(item => {
323
+ const where = item.target === 'global' ? 'GLOBAL' : 'PROJECT';
324
+ console.log(` • ${item.package} → ${where}`);
325
+ console.log(` ${item.command}`);
326
+ });
327
+ console.log('');
328
+
329
+ const cliInfo = await this.detectCliInstall();
330
+ if (cliInfo.type === 'global' && !packagesToUpdate.includes('slicejs-cli')) {
331
+ if (!options.yes && !options.cli) {
332
+ const { addCli } = await inquirer.prompt([
333
+ {
334
+ type: 'confirm',
335
+ name: 'addCli',
336
+ message: 'Global CLI detected. Add the global CLI update to the plan?',
337
+ default: true
338
+ }
339
+ ]);
340
+ if (addCli) {
341
+ packagesToUpdate.push('slicejs-cli');
342
+ plan = await this.buildUpdatePlan(packagesToUpdate);
343
+ console.log('');
344
+ Print.info('🧭 Updated plan:');
345
+ plan.forEach(item => {
346
+ const where = item.target === 'global' ? 'GLOBAL' : 'PROJECT';
347
+ console.log(` • ${item.package} → ${where}`);
348
+ console.log(` ${item.command}`);
349
+ });
350
+ console.log('');
351
+ }
352
+ } else {
353
+ Print.warning('Global CLI detected. It is recommended to update slicejs-cli globally to keep aligned with the framework.');
354
+ console.log(` Suggestion: ${installCommand(cliInfo.packageManager || this.getPackageManager(), 'slicejs-cli@latest', { global: true })}`);
355
+ console.log('');
356
+ }
357
+ }
358
+
359
+ if (!options.yes && !options.cli && !options.framework) {
360
+ const { confirm } = await inquirer.prompt([
361
+ {
362
+ type: 'confirm',
363
+ name: 'confirm',
364
+ message: 'Do you want to proceed with the update according to the plan shown?',
365
+ default: true
366
+ }
367
+ ]);
368
+
369
+ if (!confirm) {
370
+ Print.info('Update cancelled.');
371
+ return false;
372
+ }
373
+ }
374
+
375
+ console.log(''); // Line break
376
+ Print.info('📥 Installing updates...');
377
+ console.log('');
378
+
379
+ // Install updates
380
+ const results = await this.installUpdates(packagesToUpdate);
381
+
382
+ // Summary
383
+ console.log('');
384
+ const successCount = results.filter(r => r.success).length;
385
+ const failCount = results.filter(r => !r.success).length;
386
+
387
+ if (failCount === 0) {
388
+ Print.success(`✅ ${successCount} package(s) updated successfully!`);
389
+ } else {
390
+ Print.warning(`⚠️ ${successCount} successful, ${failCount} failed`);
391
+ }
392
+
393
+ if (successCount > 0) {
394
+ console.log('');
395
+ Print.info('💡 It is recommended to restart the development server if it is running.');
396
+ }
397
+
398
+ const frameworkUpdated = results.find(r => r.packageName === 'slicejs-web-framework' && r.success);
399
+ if (frameworkUpdated || options.updateApi) {
400
+ await this.updateApiIndexIfNeeded(options);
401
+ }
402
+
403
+ return failCount === 0;
404
+
405
+ } catch (error) {
406
+ spinner.stop();
407
+ Print.error(`Error during update: ${error.message}`);
408
+ return false;
409
+ }
410
+ }
411
+
412
+ async updateApiIndexIfNeeded(options = {}) {
413
+ try {
414
+ const projectApiPath = getApiPath(import.meta.url, 'index.js');
415
+ const frameworkApiPath = getPath(import.meta.url, 'node_modules', 'slicejs-web-framework', 'api', 'index.js');
416
+
417
+ if (!await fs.pathExists(projectApiPath) || !await fs.pathExists(frameworkApiPath)) {
418
+ return;
419
+ }
420
+
421
+ const projectContent = await fs.readFile(projectApiPath, 'utf-8');
422
+ const frameworkContent = await fs.readFile(frameworkApiPath, 'utf-8');
423
+
424
+ if (projectContent === frameworkContent) {
425
+ return;
426
+ }
427
+
428
+ Print.warning('⚠️ Detected changes in framework api/index.js.');
429
+
430
+ // Overwriting api/index.js is opt-in: `--update-api` is the only way
431
+ // to auto-confirm. `-y/--yes` deliberately does NOT imply it — the
432
+ // project file may carry local changes, so a blanket "yes to updates"
433
+ // must not silently replace it.
434
+ let confirmUpdate = options.updateApi === true;
435
+ if (!confirmUpdate && options.yes === true) {
436
+ Print.info('Skipping api/index.js update (not updated by default).');
437
+ Print.info('Use --update-api to overwrite api/index.js from the framework template.');
438
+ return;
439
+ }
440
+ if (!confirmUpdate) {
441
+ const answers = await inquirer.prompt([
442
+ {
443
+ type: 'confirm',
444
+ name: 'confirm',
445
+ message: 'Update your project api/index.js to the latest framework version?',
446
+ default: false
447
+ }
448
+ ]);
449
+ confirmUpdate = answers.confirm;
450
+ }
451
+
452
+ if (!confirmUpdate) {
453
+ Print.info('Skipping api/index.js update.');
454
+ return;
455
+ }
456
+
457
+ const backupPath = `${projectApiPath}.bak`;
458
+ await fs.copy(projectApiPath, backupPath);
459
+ await fs.writeFile(projectApiPath, frameworkContent, 'utf-8');
460
+ Print.success('✅ api/index.js updated from framework template.');
461
+ Print.info(`Backup created at ${backupPath}`);
462
+ } catch (error) {
463
+ Print.error(`Failed to update api/index.js: ${error.message}`);
464
+ }
465
+ }
466
+ }
467
+
468
+ // Singleton instance
469
+ const updateManager = new UpdateManager();
470
+
471
+ export default updateManager;