@foundrykit/cli 1.1.0 → 2.0.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foundrykit/cli",
3
- "version": "1.1.0",
3
+ "version": "2.0.1",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",
@@ -25,7 +25,7 @@
25
25
  "glob": "^10.3.10",
26
26
  "inquirer": "^9.2.15",
27
27
  "node-fetch": "^3.3.0",
28
- "@foundrykit/types": "1.0.9"
28
+ "@foundrykit/types": "2.0.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/node": "^20.19.9",
@@ -225,6 +225,9 @@ export async function addSingleComponent(
225
225
  // Install the component files
226
226
  await installComponentFiles(componentData, options);
227
227
 
228
+ // Track the installed component in config
229
+ await trackInstalledComponent(componentData);
230
+
228
231
  console.log(chalk.green(`āœ… ${componentData.name} added successfully!`));
229
232
 
230
233
  // Show usage example
@@ -355,6 +358,25 @@ export async function installComponentFiles(
355
358
  await updateConfigVersion();
356
359
  }
357
360
 
361
+ /**
362
+ * Record an installed component in foundrykit.config.json
363
+ */
364
+ export async function trackInstalledComponent(
365
+ componentData: ComponentData
366
+ ): Promise<void> {
367
+ const configPath = path.join(process.cwd(), 'foundrykit.config.json');
368
+ if (!fs.existsSync(configPath)) return;
369
+
370
+ const config = await fs.readJson(configPath);
371
+ config.installedComponents = config.installedComponents ?? {};
372
+ config.installedComponents[componentData.id] = {
373
+ id: componentData.id,
374
+ version: componentData.version ?? null,
375
+ installedAt: new Date().toISOString(),
376
+ };
377
+ await fs.writeJson(configPath, config, { spaces: 2 });
378
+ }
379
+
358
380
  /**
359
381
  * Handle package dependencies - check and auto-install if needed
360
382
  */
@@ -1,29 +1,185 @@
1
1
  import { Command } from 'commander';
2
2
  import chalk from 'chalk';
3
- import { addSingleComponent, type AddOptions } from './add.js';
3
+ import inquirer from 'inquirer';
4
+ import fs from 'fs-extra';
5
+ import path from 'path';
6
+ import { addSingleComponent, hasConflictingFiles, type AddOptions } from './add.js';
7
+ import { getRegistry } from '../lib/registry.js';
8
+ import type { InstalledComponent } from '@foundrykit/types';
4
9
 
5
10
  interface UpdateOptions extends AddOptions {
6
- path?: string;
11
+ opt?: boolean;
7
12
  }
8
13
 
9
14
  export function updateCommand() {
10
15
  return new Command('update')
11
- .description('Update an existing component or block')
12
- .argument('<id>', 'Component or block ID')
16
+ .description('Update installed components')
17
+ .argument('[id]', 'Component or block ID (omit to update all installed)')
18
+ .option('--opt', 'interactively select which components to update, showing available upgrades')
13
19
  .option('-p, --path <path>', 'Custom installation path')
14
- .action(async (id: string, options: UpdateOptions) => {
15
- try {
16
- console.log(chalk.blue(`šŸ”„ Updating ${id}...`));
17
- await addSingleComponent(id, {
18
- ...options,
19
- overwrite: true,
20
- });
21
- } catch (error) {
22
- console.error(chalk.red(`āŒ Failed to update ${id}:`));
23
- console.error(
24
- chalk.red(error instanceof Error ? error.message : String(error))
25
- );
20
+ .action(async (id: string | undefined, options: UpdateOptions) => {
21
+ if (id) {
22
+ // Update single named component
23
+ try {
24
+ console.log(chalk.blue(`šŸ”„ Updating ${id}...`));
25
+ await addSingleComponent(id, { ...options, overwrite: true });
26
+ } catch (error) {
27
+ console.error(chalk.red(`āŒ Failed to update ${id}:`));
28
+ console.error(chalk.red(error instanceof Error ? error.message : String(error)));
29
+ process.exit(1);
30
+ }
31
+ return;
32
+ }
33
+
34
+ // Load installed components from config
35
+ const configPath = path.join(process.cwd(), 'foundrykit.config.json');
36
+ if (!fs.existsSync(configPath)) {
37
+ console.error(chalk.red('āŒ FoundryKit not initialized. Run "foundrykit init" first.'));
26
38
  process.exit(1);
27
39
  }
40
+
41
+ const config = await fs.readJson(configPath);
42
+ let installed: Record<string, InstalledComponent> = config.installedComponents ?? {};
43
+ let installedIds = Object.keys(installed);
44
+
45
+ if (installedIds.length === 0) {
46
+ // No tracking data yet — fall back to scanning the filesystem
47
+ console.log(chalk.gray('No install tracking found. Scanning filesystem for installed components...'));
48
+ const registry = getRegistry();
49
+ let allComponents;
50
+ try {
51
+ allComponents = await registry.getComponents();
52
+ } catch (error) {
53
+ console.error(chalk.red('āŒ Failed to fetch registry:'));
54
+ console.error(chalk.red(error instanceof Error ? error.message : String(error)));
55
+ process.exit(1);
56
+ }
57
+
58
+ for (const component of allComponents) {
59
+ try {
60
+ const componentData = await registry.getComponent(component.id);
61
+ if (await hasConflictingFiles(componentData, options)) {
62
+ installed[component.id] = {
63
+ id: component.id,
64
+ version: null,
65
+ installedAt: new Date().toISOString(),
66
+ };
67
+ }
68
+ } catch {
69
+ // skip unfetchable components
70
+ }
71
+ }
72
+
73
+ installedIds = Object.keys(installed);
74
+
75
+ if (installedIds.length === 0) {
76
+ console.log(chalk.yellow('No installed components found. Use "foundrykit add" to install components.'));
77
+ return;
78
+ }
79
+
80
+ console.log(chalk.gray(`Found ${installedIds.length} installed component(s) via filesystem scan.`));
81
+ }
82
+
83
+ if (options.opt) {
84
+ // --opt: fetch registry versions, show which have updates, let user pick
85
+ console.log(chalk.blue(`šŸ” Checking for updates across ${installedIds.length} installed component(s)...`));
86
+
87
+ const registry = getRegistry();
88
+ const withUpdates: Array<{ id: string; installed: string | null; latest: string | null }> = [];
89
+ const upToDate: string[] = [];
90
+
91
+ for (const compId of installedIds) {
92
+ try {
93
+ const data = await registry.getComponent(compId);
94
+ const installedVersion = installed[compId]?.version ?? null;
95
+ const latestVersion = data.version ?? null;
96
+
97
+ const hasUpdate =
98
+ latestVersion !== null &&
99
+ installedVersion !== null &&
100
+ latestVersion !== installedVersion;
101
+
102
+ const unknownVersion = installedVersion === null || latestVersion === null;
103
+
104
+ if (hasUpdate || unknownVersion) {
105
+ withUpdates.push({ id: compId, installed: installedVersion, latest: latestVersion });
106
+ } else {
107
+ upToDate.push(compId);
108
+ }
109
+ } catch {
110
+ // If we can't fetch, still offer the option to re-install
111
+ withUpdates.push({ id: compId, installed: installed[compId]?.version ?? null, latest: null });
112
+ }
113
+ }
114
+
115
+ if (upToDate.length > 0) {
116
+ console.log(chalk.gray(`\nāœ“ Already up to date: ${upToDate.join(', ')}`));
117
+ }
118
+
119
+ if (withUpdates.length === 0) {
120
+ console.log(chalk.green('\nāœ… All components are up to date.'));
121
+ return;
122
+ }
123
+
124
+ const choices = withUpdates.map(({ id: compId, installed: inst, latest }) => {
125
+ let versionLabel: string;
126
+ if (inst === null && latest === null) {
127
+ versionLabel = chalk.gray('(version unknown)');
128
+ } else if (latest === null) {
129
+ versionLabel = chalk.gray(`(installed: ${inst} — registry version unknown)`);
130
+ } else if (inst === null) {
131
+ versionLabel = chalk.cyan(`(latest: ${latest} — install version not tracked)`);
132
+ } else {
133
+ versionLabel = chalk.cyan(`(${inst} → ${latest})`);
134
+ }
135
+ return {
136
+ name: `${compId} ${versionLabel}`,
137
+ value: compId,
138
+ checked: true,
139
+ };
140
+ });
141
+
142
+ const { selectedIds } = await inquirer.prompt([
143
+ {
144
+ type: 'checkbox',
145
+ name: 'selectedIds',
146
+ message: 'Select components to update:',
147
+ choices,
148
+ pageSize: 20,
149
+ },
150
+ ]);
151
+
152
+ if (selectedIds.length === 0) {
153
+ console.log(chalk.yellow('No components selected.'));
154
+ return;
155
+ }
156
+
157
+ await runUpdates(selectedIds, options);
158
+ } else {
159
+ // No flag: update everything
160
+ console.log(chalk.blue(`šŸ”„ Updating all ${installedIds.length} installed component(s)...`));
161
+ await runUpdates(installedIds, options);
162
+ }
28
163
  });
29
164
  }
165
+
166
+ async function runUpdates(ids: string[], options: AddOptions): Promise<void> {
167
+ let failed = 0;
168
+ for (const compId of ids) {
169
+ try {
170
+ console.log(chalk.blue(`\nšŸ”„ Updating ${compId}...`));
171
+ await addSingleComponent(compId, { ...options, overwrite: true });
172
+ } catch (error) {
173
+ console.error(chalk.red(`āŒ Failed to update ${compId}:`));
174
+ console.error(chalk.red(error instanceof Error ? error.message : String(error)));
175
+ failed++;
176
+ }
177
+ }
178
+
179
+ if (failed > 0) {
180
+ console.log(chalk.yellow(`\n⚠ ${failed} component(s) failed to update.`));
181
+ process.exit(1);
182
+ } else {
183
+ console.log(chalk.green(`\nāœ… ${ids.length} component(s) updated successfully!`));
184
+ }
185
+ }