@astralkit/cli 3.1.0 → 4.1.0
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/bin/index.js +155 -156
- package/package.json +42 -48
- package/src/commands/add.js +155 -479
- package/src/commands/diff.js +57 -107
- package/src/commands/init.js +108 -148
- package/src/commands/list.js +46 -186
- package/src/commands/login.js +133 -157
- package/src/commands/remove.js +42 -118
- package/src/commands/search.js +58 -156
- package/src/commands/update.js +90 -120
- package/src/lib/ai-rules.js +49 -50
- package/src/lib/api.js +152 -197
- package/src/lib/auth.js +98 -142
- package/src/lib/config.js +127 -163
- package/src/lib/pkg-manager.js +25 -65
- package/src/lib/validators.js +50 -87
package/src/commands/remove.js
CHANGED
|
@@ -1,145 +1,69 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import pc from 'picocolors';
|
|
4
|
+
import * as p from '@clack/prompts';
|
|
5
|
+
import { getProjectConfig, detectFramework } from '../lib/config.js';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
* Remove command — cleanly uninstall a component, booster, or workflow.
|
|
9
|
-
*/
|
|
10
|
-
async function removeCommand(slug, options) {
|
|
7
|
+
export async function removeCommand(name, options) {
|
|
11
8
|
const config = getProjectConfig();
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
// Build a list of paths this slug might live in
|
|
15
|
-
const candidates = [];
|
|
16
|
-
|
|
17
|
-
// Component paths
|
|
9
|
+
const framework = config?.framework || detectFramework();
|
|
18
10
|
const componentsDir = config?.componentsDir || 'src/components/ui';
|
|
19
|
-
for (const ext of ['.tsx', '.jsx', '.html']) {
|
|
20
|
-
const p = path.join(cwd, componentsDir, `${slug}${ext}`);
|
|
21
|
-
if (fs.existsSync(p)) {
|
|
22
|
-
candidates.push({ type: 'component', path: p, relative: `${componentsDir}/${slug}${ext}` });
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
11
|
|
|
26
|
-
//
|
|
27
|
-
const
|
|
28
|
-
`src/lib/${slug}`,
|
|
29
|
-
`lib/${slug}`,
|
|
30
|
-
];
|
|
31
|
-
const boosterAdapterDirs = [
|
|
32
|
-
`src/components/${slug}`,
|
|
33
|
-
`components/${slug}`,
|
|
34
|
-
];
|
|
12
|
+
// Find all files associated with this name
|
|
13
|
+
const found = [];
|
|
35
14
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
for (const dir of boosterAdapterDirs) {
|
|
43
|
-
const p = path.join(cwd, dir);
|
|
44
|
-
if (fs.existsSync(p) && fs.statSync(p).isDirectory()) {
|
|
45
|
-
candidates.push({ type: 'booster-adapter', path: p, relative: `${dir}/` });
|
|
46
|
-
}
|
|
15
|
+
// Check component
|
|
16
|
+
for (const ext of ['.tsx', '.jsx', '.html']) {
|
|
17
|
+
const filePath = path.resolve(process.cwd(), componentsDir, `${name}${ext}`);
|
|
18
|
+
if (fs.existsSync(filePath)) found.push({ label: 'Component', path: filePath });
|
|
47
19
|
}
|
|
48
20
|
|
|
49
|
-
//
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
candidates.push({ type: 'workflow', path: workflowDir, relative: `${slug}/` });
|
|
55
|
-
}
|
|
56
|
-
}
|
|
21
|
+
// Check booster dirs
|
|
22
|
+
const coreDir = path.resolve(process.cwd(), `src/lib/${name}`);
|
|
23
|
+
const adapterDir = path.resolve(process.cwd(), `src/components/${name}`);
|
|
24
|
+
if (fs.existsSync(coreDir)) found.push({ label: 'Booster (core)', path: coreDir, isDir: true });
|
|
25
|
+
if (fs.existsSync(adapterDir)) found.push({ label: 'Booster (adapter)', path: adapterDir, isDir: true });
|
|
57
26
|
|
|
58
|
-
if (
|
|
59
|
-
|
|
60
|
-
console.log(
|
|
61
|
-
console.log(chalk.gray(` ${componentsDir}/${slug}.*`));
|
|
62
|
-
console.log(chalk.gray(` src/lib/${slug}/`));
|
|
63
|
-
console.log(chalk.gray(` src/components/${slug}/`));
|
|
64
|
-
console.log(chalk.gray(` ${slug}/`));
|
|
27
|
+
if (found.length === 0) {
|
|
28
|
+
p.log.warn(`No installed files found for "${name}".`);
|
|
29
|
+
console.log(pc.gray('It may have been installed in a custom directory.'));
|
|
65
30
|
return;
|
|
66
31
|
}
|
|
67
32
|
|
|
68
|
-
|
|
69
|
-
console.log(
|
|
70
|
-
|
|
71
|
-
for (const
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (fs.statSync(c.path).isDirectory()) {
|
|
78
|
-
const files = getAllFiles(c.path);
|
|
79
|
-
console.log(chalk.red(` ${label}: ${c.relative}`));
|
|
80
|
-
for (const f of files.slice(0, 10)) {
|
|
81
|
-
console.log(chalk.gray(` ${f}`));
|
|
82
|
-
}
|
|
83
|
-
if (files.length > 10) {
|
|
84
|
-
console.log(chalk.gray(` ... and ${files.length - 10} more files`));
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
console.log(chalk.red(` ${label}: ${c.relative}`));
|
|
33
|
+
console.log();
|
|
34
|
+
console.log(pc.yellow(`Found files for "${name}":`));
|
|
35
|
+
console.log();
|
|
36
|
+
for (const item of found) {
|
|
37
|
+
const rel = path.relative(process.cwd(), item.path);
|
|
38
|
+
console.log(` ${pc.cyan(item.label)}: ${pc.gray(rel)}`);
|
|
39
|
+
if (item.isDir && fs.existsSync(item.path)) {
|
|
40
|
+
const files = fs.readdirSync(item.path);
|
|
41
|
+
files.forEach(f => console.log(pc.gray(` ${f}`)));
|
|
88
42
|
}
|
|
89
43
|
}
|
|
90
|
-
|
|
91
44
|
console.log();
|
|
92
45
|
|
|
93
|
-
// Confirm
|
|
94
46
|
if (!options.force) {
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (!confirm) {
|
|
103
|
-
console.log(chalk.gray('Cancelled.'));
|
|
47
|
+
const confirm = await p.confirm({
|
|
48
|
+
message: `Remove ${found.length} item(s)?`,
|
|
49
|
+
initialValue: false,
|
|
50
|
+
});
|
|
51
|
+
if (p.isCancel(confirm) || !confirm) {
|
|
52
|
+
p.cancel('Cancelled.');
|
|
104
53
|
return;
|
|
105
54
|
}
|
|
106
55
|
}
|
|
107
56
|
|
|
108
|
-
|
|
109
|
-
let removed = 0;
|
|
110
|
-
for (const c of candidates) {
|
|
57
|
+
for (const item of found) {
|
|
111
58
|
try {
|
|
112
|
-
if (
|
|
113
|
-
fs.rmSync(
|
|
59
|
+
if (item.isDir) {
|
|
60
|
+
fs.rmSync(item.path, { recursive: true, force: true });
|
|
114
61
|
} else {
|
|
115
|
-
fs.unlinkSync(
|
|
62
|
+
fs.unlinkSync(item.path);
|
|
116
63
|
}
|
|
117
|
-
console.log(
|
|
118
|
-
removed++;
|
|
64
|
+
console.log(pc.green(' ✓ Removed'), pc.gray(path.relative(process.cwd(), item.path)));
|
|
119
65
|
} catch (err) {
|
|
120
|
-
console.log(
|
|
66
|
+
console.log(pc.red(` ✗ Failed to remove ${item.path}:`), pc.gray(err.message));
|
|
121
67
|
}
|
|
122
68
|
}
|
|
123
|
-
|
|
124
|
-
console.log();
|
|
125
|
-
console.log(chalk.green(`Removed ${removed} item(s) for "${slug}".`));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Get all file paths (relative) in a directory recursively.
|
|
130
|
-
*/
|
|
131
|
-
function getAllFiles(dir, base = '') {
|
|
132
|
-
const results = [];
|
|
133
|
-
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
134
|
-
const rel = base ? `${base}/${entry.name}` : entry.name;
|
|
135
|
-
if (entry.name === 'node_modules' || entry.name === '.next') continue;
|
|
136
|
-
if (entry.isDirectory()) {
|
|
137
|
-
results.push(...getAllFiles(path.join(dir, entry.name), rel));
|
|
138
|
-
} else {
|
|
139
|
-
results.push(rel);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return results;
|
|
143
69
|
}
|
|
144
|
-
|
|
145
|
-
module.exports = { removeCommand };
|
package/src/commands/search.js
CHANGED
|
@@ -1,156 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
console.log(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
console.log(chalk.gray('Run'), chalk.cyan('astralkit add <component>'), chalk.gray('to install.'));
|
|
60
|
-
|
|
61
|
-
} catch (err) {
|
|
62
|
-
spinner.fail('Search failed.');
|
|
63
|
-
console.error(chalk.red(err.message));
|
|
64
|
-
process.exit(1);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Search boosters.
|
|
70
|
-
*/
|
|
71
|
-
async function searchBoostersCommand(query, options) {
|
|
72
|
-
const spinner = ora(`Searching boosters for "${query}"...`).start();
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
const data = await listBoosters({
|
|
76
|
-
search: query,
|
|
77
|
-
framework: options.framework,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
spinner.stop();
|
|
81
|
-
|
|
82
|
-
if (!data.boosters || data.boosters.length === 0) {
|
|
83
|
-
console.log(chalk.yellow(`No boosters found for "${query}".`));
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
console.log(chalk.blue.bold(`Found ${data.boosters.length} booster(s):`));
|
|
88
|
-
console.log();
|
|
89
|
-
|
|
90
|
-
for (const b of data.boosters) {
|
|
91
|
-
const proTag = b.is_pro ? chalk.magenta(' PRO') : '';
|
|
92
|
-
const cat = b.category ? chalk.gray(` (${b.category})`) : '';
|
|
93
|
-
console.log(` ${chalk.cyan(b.slug)}${proTag}${cat}`);
|
|
94
|
-
if (b.description) {
|
|
95
|
-
console.log(` ${chalk.gray(b.description.length > 80 ? b.description.slice(0, 80) + '...' : b.description)}`);
|
|
96
|
-
}
|
|
97
|
-
if (b.frameworks?.length) {
|
|
98
|
-
console.log(` ${chalk.gray(`Frameworks: ${b.frameworks.join(', ')}`)}`);
|
|
99
|
-
}
|
|
100
|
-
console.log();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
console.log(chalk.gray('Run'), chalk.cyan('astralkit add --booster <name>'), chalk.gray('to install.'));
|
|
104
|
-
|
|
105
|
-
} catch (err) {
|
|
106
|
-
spinner.fail('Search failed.');
|
|
107
|
-
console.error(chalk.red(err.message));
|
|
108
|
-
process.exit(1);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Search workflows.
|
|
114
|
-
*/
|
|
115
|
-
async function searchWorkflowsCommand(query, options) {
|
|
116
|
-
const spinner = ora(`Searching workflows for "${query}"...`).start();
|
|
117
|
-
|
|
118
|
-
try {
|
|
119
|
-
const data = await listWorkflows({
|
|
120
|
-
search: query,
|
|
121
|
-
framework: options.framework,
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
spinner.stop();
|
|
125
|
-
|
|
126
|
-
if (!data.workflows || data.workflows.length === 0) {
|
|
127
|
-
console.log(chalk.yellow(`No workflows found for "${query}".`));
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
console.log(chalk.blue.bold(`Found ${data.workflows.length} workflow(s):`));
|
|
132
|
-
console.log();
|
|
133
|
-
|
|
134
|
-
for (const w of data.workflows) {
|
|
135
|
-
const proTag = w.is_pro ? chalk.magenta(' PRO') : '';
|
|
136
|
-
const cat = w.category ? chalk.gray(` (${w.category})`) : '';
|
|
137
|
-
console.log(` ${chalk.cyan(w.slug)}${proTag}${cat}`);
|
|
138
|
-
if (w.description) {
|
|
139
|
-
console.log(` ${chalk.gray(w.description.length > 80 ? w.description.slice(0, 80) + '...' : w.description)}`);
|
|
140
|
-
}
|
|
141
|
-
if (w.frameworks?.length) {
|
|
142
|
-
console.log(` ${chalk.gray(`Frameworks: ${w.frameworks.join(', ')}`)}`);
|
|
143
|
-
}
|
|
144
|
-
console.log();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
console.log(chalk.gray('Run'), chalk.cyan('astralkit add --workflow <name>'), chalk.gray('to scaffold.'));
|
|
148
|
-
|
|
149
|
-
} catch (err) {
|
|
150
|
-
spinner.fail('Search failed.');
|
|
151
|
-
console.error(chalk.red(err.message));
|
|
152
|
-
process.exit(1);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
module.exports = { searchCommand };
|
|
1
|
+
import pc from 'picocolors';
|
|
2
|
+
import * as p from '@clack/prompts';
|
|
3
|
+
import { listComponents, listBoosters, listWorkflows } from '../lib/api.js';
|
|
4
|
+
|
|
5
|
+
const FRAMEWORK_LABELS = { nextjs: 'Next.js', react: 'React', html: 'HTML' };
|
|
6
|
+
|
|
7
|
+
export async function searchCommand(query, options) {
|
|
8
|
+
const ora = (await import('ora')).default;
|
|
9
|
+
const spinner = ora(`Searching for "${pc.cyan(query)}"...`).start();
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const filters = { search: query, framework: options.framework };
|
|
13
|
+
let results = [];
|
|
14
|
+
|
|
15
|
+
if (options.boosters) {
|
|
16
|
+
results = await listBoosters(filters);
|
|
17
|
+
} else if (options.workflows) {
|
|
18
|
+
results = await listWorkflows(filters);
|
|
19
|
+
} else {
|
|
20
|
+
// Search all three in parallel
|
|
21
|
+
const [components, boosters, workflows] = await Promise.allSettled([
|
|
22
|
+
listComponents(filters),
|
|
23
|
+
listBoosters(filters),
|
|
24
|
+
listWorkflows(filters),
|
|
25
|
+
]);
|
|
26
|
+
if (components.status === 'fulfilled') results.push(...(components.value || []).map(i => ({ ...i, _type: 'component' })));
|
|
27
|
+
if (boosters.status === 'fulfilled') results.push(...(boosters.value || []).map(i => ({ ...i, _type: 'booster' })));
|
|
28
|
+
if (workflows.status === 'fulfilled') results.push(...(workflows.value || []).map(i => ({ ...i, _type: 'workflow' })));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
spinner.stop();
|
|
32
|
+
|
|
33
|
+
if (!results.length) {
|
|
34
|
+
p.log.info(`No results for "${query}".`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.log();
|
|
39
|
+
console.log(pc.bold(` ${results.length} result(s) for "${query}"\n`));
|
|
40
|
+
|
|
41
|
+
for (const item of results) {
|
|
42
|
+
const typeLabel = item._type ? pc.gray(` [${item._type}]`) : '';
|
|
43
|
+
const proTag = item.is_pro ? pc.magenta(' [PRO]') : pc.green(' [FREE]');
|
|
44
|
+
const fw = item.frameworks?.map(f => FRAMEWORK_LABELS[f] || f).join(', ') || '';
|
|
45
|
+
console.log(` ${pc.cyan(item.slug || item.name)}${typeLabel}${proTag}`);
|
|
46
|
+
if (item.description) console.log(pc.gray(` ${item.description}`));
|
|
47
|
+
if (fw) console.log(pc.gray(` Frameworks: ${fw}`));
|
|
48
|
+
console.log();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
console.log(pc.gray(` astralkit add <slug> to install`));
|
|
52
|
+
} catch (err) {
|
|
53
|
+
spinner.fail('Search failed.');
|
|
54
|
+
if (err.code === 'AUTH_REQUIRED') p.log.warn(err.message);
|
|
55
|
+
else console.error(pc.red(err.message));
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/commands/update.js
CHANGED
|
@@ -1,120 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Write updated files (with path traversal protection)
|
|
96
|
-
const updateSpinner = ora('Updating files...').start();
|
|
97
|
-
|
|
98
|
-
for (const file of [...changedFiles, ...newFiles]) {
|
|
99
|
-
if (!isSafePath(targetDir, file.path)) {
|
|
100
|
-
console.log(chalk.red(` Skipping suspicious file path: ${file.path}`));
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
const filePath = path.resolve(targetDir, file.path);
|
|
104
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
105
|
-
fs.writeFileSync(filePath, file.content);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
updateSpinner.succeed(`Updated ${chalk.cyan(data.name || componentSlug)}`);
|
|
109
|
-
|
|
110
|
-
for (const file of [...changedFiles, ...newFiles]) {
|
|
111
|
-
console.log(chalk.gray(` ${file.path}`));
|
|
112
|
-
}
|
|
113
|
-
} catch (err) {
|
|
114
|
-
spinner.fail('Update failed.');
|
|
115
|
-
console.error(chalk.red(err.message));
|
|
116
|
-
process.exit(1);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
module.exports = { updateCommand };
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import pc from 'picocolors';
|
|
4
|
+
import * as p from '@clack/prompts';
|
|
5
|
+
import { getComponent } from '../lib/api.js';
|
|
6
|
+
import { getProjectConfig, detectFramework } from '../lib/config.js';
|
|
7
|
+
import { isSafePath } from '../lib/validators.js';
|
|
8
|
+
|
|
9
|
+
export async function updateCommand(componentSlug, options) {
|
|
10
|
+
const ora = (await import('ora')).default;
|
|
11
|
+
const config = getProjectConfig();
|
|
12
|
+
const framework = options.framework || config?.framework || detectFramework();
|
|
13
|
+
const componentsDir = options.dir || config?.componentsDir || 'src/components/ui';
|
|
14
|
+
|
|
15
|
+
const spinner = ora(`Fetching latest ${pc.cyan(componentSlug)}...`).start();
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const data = await getComponent(componentSlug, framework);
|
|
19
|
+
spinner.stop();
|
|
20
|
+
|
|
21
|
+
if (!data.files?.length) {
|
|
22
|
+
p.log.warn(`No ${framework} version available for "${componentSlug}".`);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const dir = path.resolve(process.cwd(), componentsDir);
|
|
27
|
+
|
|
28
|
+
// Check what's actually changing
|
|
29
|
+
const toUpdate = [];
|
|
30
|
+
const toCreate = [];
|
|
31
|
+
|
|
32
|
+
for (const file of data.files) {
|
|
33
|
+
const filePath = path.resolve(dir, file.path);
|
|
34
|
+
if (!fs.existsSync(filePath)) {
|
|
35
|
+
toCreate.push(file);
|
|
36
|
+
} else {
|
|
37
|
+
const localContent = fs.readFileSync(filePath, 'utf8');
|
|
38
|
+
if (localContent !== file.content) toUpdate.push(file);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (toUpdate.length === 0 && toCreate.length === 0) {
|
|
43
|
+
p.log.success(`${pc.cyan(componentSlug)} is already up to date.`);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Show what will change
|
|
48
|
+
console.log();
|
|
49
|
+
if (toCreate.length > 0) {
|
|
50
|
+
console.log(pc.gray(' New files:'));
|
|
51
|
+
toCreate.forEach(f => console.log(pc.green(` + ${f.path}`)));
|
|
52
|
+
}
|
|
53
|
+
if (toUpdate.length > 0) {
|
|
54
|
+
console.log(pc.gray(' Updated files:'));
|
|
55
|
+
toUpdate.forEach(f => console.log(pc.yellow(` ~ ${f.path}`)));
|
|
56
|
+
}
|
|
57
|
+
console.log();
|
|
58
|
+
|
|
59
|
+
if (!options.force) {
|
|
60
|
+
const confirm = await p.confirm({
|
|
61
|
+
message: `Update ${pc.cyan(componentSlug)}?`,
|
|
62
|
+
initialValue: true,
|
|
63
|
+
});
|
|
64
|
+
if (p.isCancel(confirm) || !confirm) {
|
|
65
|
+
p.cancel('Cancelled.');
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
71
|
+
|
|
72
|
+
for (const file of [...toCreate, ...toUpdate]) {
|
|
73
|
+
if (!isSafePath(dir, file.path)) {
|
|
74
|
+
console.log(pc.red(` Skipping suspicious path: ${file.path}`));
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const filePath = path.resolve(dir, file.path);
|
|
78
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
79
|
+
fs.writeFileSync(filePath, file.content);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
p.outro(pc.green(`✅ ${data.name || componentSlug} updated`));
|
|
83
|
+
|
|
84
|
+
} catch (err) {
|
|
85
|
+
spinner.fail('Failed to update component.');
|
|
86
|
+
if (err.code === 'AUTH_REQUIRED' || err.code === 'PRO_REQUIRED') p.log.warn(err.message);
|
|
87
|
+
else console.error(pc.red(err.message));
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
}
|