centoui-cli 1.0.0-alpha.38 → 1.0.0-alpha.40
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/README.md +13 -13
- package/dist/index.d.mts +32 -65
- package/dist/index.mjs +340 -625
- package/package.json +25 -25
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
import { defineCommand, runMain } from "citty";
|
|
3
|
-
import { cancel, confirm, group, intro, isCancel, log,
|
|
4
|
-
import {
|
|
2
|
+
import { cancel, confirm, group, intro, isCancel, log, outro, tasks, text } from "@clack/prompts";
|
|
3
|
+
import { join } from "pathe";
|
|
4
|
+
import { loadConfig } from "c12";
|
|
5
5
|
import fsExtra from "fs-extra";
|
|
6
6
|
import { addDependency, removeDependency } from "nypm";
|
|
7
|
-
import { loadConfig } from "c12";
|
|
8
7
|
//#endregion
|
|
9
8
|
//#region src/constants.ts
|
|
10
9
|
/** CentoUI current package version, sourced directly from package.json. */
|
|
11
|
-
const VERSION = "1.0.0-alpha.
|
|
10
|
+
const VERSION = "1.0.0-alpha.40";
|
|
12
11
|
/** File name for the user-side CentoUI config (created by `centoui init`). */
|
|
13
12
|
const CONFIG_FILE_NAME = "centoui.config.ts";
|
|
14
13
|
/**
|
|
@@ -18,24 +17,7 @@ const CONFIG_FILE_NAME = "centoui.config.ts";
|
|
|
18
17
|
* The path ends at `src/` so that registry and component paths from the
|
|
19
18
|
* registry (e.g. `components/button/button.vue`) can be appended directly.
|
|
20
19
|
*/
|
|
21
|
-
const
|
|
22
|
-
/** Full URL to the registry index file that lists every available component. */
|
|
23
|
-
const REGISTRY_INDEX_URL = `${`${CORE_SRC_BASE_URL}/registry`}/index.json`;
|
|
24
|
-
/**
|
|
25
|
-
* Full URL to the CentoUI CSS theme file.
|
|
26
|
-
* This file is written to the user's project during `centoui init`.
|
|
27
|
-
*/
|
|
28
|
-
const THEME_CSS_URL = `${CORE_SRC_BASE_URL}/defaults/centoui.css`;
|
|
29
|
-
/**
|
|
30
|
-
* Full URL to the utils file.
|
|
31
|
-
* This file is written to the user's project during `centoui init`.
|
|
32
|
-
*/
|
|
33
|
-
const UTILS_FILE_URL = `${CORE_SRC_BASE_URL}/defaults/utils.ts`;
|
|
34
|
-
/**
|
|
35
|
-
* Full URL to the default values file for the CentoUI config.
|
|
36
|
-
* The contents of this file are written to the user's project during `centoui init`.
|
|
37
|
-
*/
|
|
38
|
-
const CONFIG_DEFAULTS_URL = `${CORE_SRC_BASE_URL}/defaults/config.ts`;
|
|
20
|
+
const BASE_URL = `https://raw.githubusercontent.com/favorodera/centoui/refs/tags/v${VERSION}/packages/core/src`;
|
|
39
21
|
/**
|
|
40
22
|
* HTTP headers required when fetching raw content from the GitHub API.
|
|
41
23
|
* These ensure we get the raw file bytes, not GitHub's HTML wrapper.
|
|
@@ -45,701 +27,434 @@ const GITHUB_RAW_FETCH_HEADERS = {
|
|
|
45
27
|
"X-GitHub-Api-Version": "2026-03-10"
|
|
46
28
|
};
|
|
47
29
|
//#endregion
|
|
48
|
-
//#region src/utils/
|
|
30
|
+
//#region src/utils/network.ts
|
|
49
31
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* @param requiredPackages - Map of `packageName → semver version range`.
|
|
58
|
-
* @param cwd - Absolute path to the project root (must contain `package.json`).
|
|
59
|
-
* @param onProgress - Optional callback fired before each `addDependency` call
|
|
60
|
-
* with a `"[n/total] package@version"` string.
|
|
61
|
-
* @returns Human-readable summary (e.g. `"Installed 3 package(s)"`).
|
|
62
|
-
* @throws If reading `package.json` or running the package manager fails.
|
|
32
|
+
* Sends a network request to the CentoUI core package on GitHub.
|
|
33
|
+
* @param path The relative path to the file from the base URL (i.e. core/src).
|
|
34
|
+
* @param responseFormat The format of the response.
|
|
35
|
+
* @param init The request options.
|
|
36
|
+
* @returns The response from the server.
|
|
37
|
+
* @throws If the network request fails or the server returns a non-2xx status.
|
|
63
38
|
*/
|
|
64
|
-
async function
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
39
|
+
async function sendNetworkRequest(path, responseFormat = "text", init) {
|
|
40
|
+
const url = `${BASE_URL}${path}`;
|
|
41
|
+
const resolvedInit = {
|
|
42
|
+
...init,
|
|
43
|
+
headers: {
|
|
44
|
+
...GITHUB_RAW_FETCH_HEADERS,
|
|
45
|
+
...init?.headers
|
|
46
|
+
}
|
|
70
47
|
};
|
|
71
|
-
|
|
72
|
-
if (packagesToInstall.length === 0) return "All packages already up to date";
|
|
48
|
+
let response;
|
|
73
49
|
try {
|
|
74
|
-
|
|
75
|
-
onProgress?.(`[${index + 1}/${packagesToInstall.length}] ${pkg}`);
|
|
76
|
-
await addDependency(pkg, {
|
|
77
|
-
cwd,
|
|
78
|
-
silent: true
|
|
79
|
-
});
|
|
80
|
-
}
|
|
50
|
+
response = await fetch(url, resolvedInit);
|
|
81
51
|
} catch (error) {
|
|
82
|
-
throw new Error(
|
|
52
|
+
throw new Error("Network request failed", { cause: error });
|
|
53
|
+
}
|
|
54
|
+
if (!response.ok) throw new Error(`Server responded with ${response.status} ${response.statusText} (URL: ${url})`);
|
|
55
|
+
let resolvedResponse;
|
|
56
|
+
switch (responseFormat) {
|
|
57
|
+
case "json":
|
|
58
|
+
resolvedResponse = await response.json();
|
|
59
|
+
break;
|
|
60
|
+
case "text":
|
|
61
|
+
resolvedResponse = await response.text();
|
|
62
|
+
break;
|
|
83
63
|
}
|
|
84
|
-
return
|
|
64
|
+
return resolvedResponse;
|
|
85
65
|
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/utils/config.ts
|
|
86
68
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* dependencies are never removed prematurely.
|
|
92
|
-
*
|
|
93
|
-
* @param packagesToConsider - Packages belonging to the component being removed.
|
|
94
|
-
* @param packagesStillNeeded - Union of `packageDeps` from all other installed components.
|
|
95
|
-
* @param cwd - Absolute path to the project root.
|
|
96
|
-
* @param onProgress - Optional callback fired before each `removeDependency` call.
|
|
97
|
-
* @returns Human-readable summary (e.g. `"Removed 2 package(s)"`).
|
|
98
|
-
* @throws If the package manager fails to remove a dependency.
|
|
69
|
+
* Loads the user's CentoUI configuration from `centoui.config.ts`.
|
|
70
|
+
* @param cwd Absolute path to the project root.
|
|
71
|
+
* @returns The user's configuration.
|
|
72
|
+
* @throws If `centoui.config.ts` is not found.
|
|
99
73
|
*/
|
|
100
|
-
async function
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
cwd,
|
|
108
|
-
silent: true
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
} catch (error) {
|
|
112
|
-
throw new Error(`[removeOrphanedPackages] Failed to remove packages: ${error}`);
|
|
113
|
-
}
|
|
114
|
-
return `Removed ${packagesToRemove.length} package(s)`;
|
|
74
|
+
async function loadConfig$1(cwd) {
|
|
75
|
+
const { config, configFile } = await loadConfig({
|
|
76
|
+
cwd,
|
|
77
|
+
name: "centoui"
|
|
78
|
+
});
|
|
79
|
+
if (!configFile) throw new Error(`${CONFIG_FILE_NAME} not found in ${cwd}. Run \`centoui init\` first.`);
|
|
80
|
+
return config;
|
|
115
81
|
}
|
|
116
82
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* @param value - The raw value from the prompt.
|
|
122
|
-
* @returns An error message string if invalid, or `undefined` if valid.
|
|
83
|
+
* Builds the user's CentoUI configuration file content.
|
|
84
|
+
* @param choices The user's configuration choices.
|
|
85
|
+
* @returns The file content.
|
|
86
|
+
* @throws If building fails.
|
|
123
87
|
*/
|
|
124
|
-
function
|
|
125
|
-
|
|
126
|
-
|
|
88
|
+
async function buildUserConfig(choices) {
|
|
89
|
+
try {
|
|
90
|
+
let cleanedContent = (await sendNetworkRequest("/config.ts")).replaceAll(/^import\s+.*$/gm, "").replace(/^\s*export\s+default\s+/, "").replace(/\s+satisfies\s+[^}]*$/, "").trim();
|
|
91
|
+
const firstBrace = cleanedContent.indexOf("{");
|
|
92
|
+
const lastBrace = cleanedContent.lastIndexOf("}");
|
|
93
|
+
cleanedContent = cleanedContent.slice(firstBrace + 1, lastBrace).replace(/^\n/, "").replace(/\n\s*$/, "");
|
|
94
|
+
return `import { defineConfig } from 'centoui'
|
|
95
|
+
|
|
96
|
+
export default defineConfig({
|
|
97
|
+
componentsDir: '${choices.componentsDir}',
|
|
98
|
+
themeFilePath: '${choices.themeFilePath}',
|
|
99
|
+
${cleanedContent}
|
|
100
|
+
})`;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
throw new Error("Failed to build user config", { cause: error });
|
|
103
|
+
}
|
|
127
104
|
}
|
|
128
105
|
//#endregion
|
|
129
|
-
//#region src/utils/file-system
|
|
106
|
+
//#region src/utils/file-system.ts
|
|
130
107
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* `"components/button/button.vue"`). This function strips that leading segment
|
|
136
|
-
* and joins the remainder with the user's configured components directory so
|
|
137
|
-
* that `"components/button/button.vue"` becomes, for example,
|
|
138
|
-
* `"/home/user/my-app/src/components/centoui/button/button.vue"`.
|
|
139
|
-
*
|
|
140
|
-
* @param registryComponentFilePath - Path as it appears in the component's registry entry
|
|
141
|
-
* (always starts with `"components/"`).
|
|
142
|
-
* @param config - The loaded CentoUI project configuration.
|
|
143
|
-
* @param cwd - Absolute path to the project root.
|
|
144
|
-
* @returns Absolute destination path for the file in the user's project.
|
|
145
|
-
*
|
|
146
|
-
* @example
|
|
147
|
-
* // config.componentsDir = 'src/components/centoui', cwd = '/home/user/my-app'
|
|
148
|
-
* mapComponentsRegistryPathToProjectDest('components/button/button.vue', config, cwd)
|
|
149
|
-
* // → '/home/user/my-app/src/components/centoui/button/button.vue'
|
|
108
|
+
* Writes content to a file, creating parent directories if they don't exist.
|
|
109
|
+
* @param path The file path to write to.
|
|
110
|
+
* @param content The content to write to the file.
|
|
111
|
+
* @throws If the file cannot be written to.
|
|
150
112
|
*/
|
|
151
|
-
function
|
|
152
|
-
|
|
153
|
-
|
|
113
|
+
async function writeToFile(path, content) {
|
|
114
|
+
try {
|
|
115
|
+
await fsExtra.outputFile(path, content, "utf8");
|
|
116
|
+
} catch (error) {
|
|
117
|
+
throw new Error(`Failed to write ${path}`, { cause: error });
|
|
118
|
+
}
|
|
154
119
|
}
|
|
155
120
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* target directory tree exists so callers don't have to `mkdir` manually.
|
|
160
|
-
*
|
|
161
|
-
* @param filePath - Absolute path where the file should be written.
|
|
162
|
-
* @param content - UTF-8 string content to write.
|
|
163
|
-
* @throws If the directory cannot be created or the file cannot be written.
|
|
121
|
+
* Creates a directory and any necessary parent directories.
|
|
122
|
+
* @param path The directory path to create.
|
|
123
|
+
* @throws If the directory cannot be created.
|
|
164
124
|
*/
|
|
165
|
-
async function
|
|
125
|
+
async function createDirectory(path) {
|
|
166
126
|
try {
|
|
167
|
-
await fsExtra.
|
|
168
|
-
await fsExtra.writeFile(filePath, content, "utf8");
|
|
127
|
+
await fsExtra.ensureDir(path);
|
|
169
128
|
} catch (error) {
|
|
170
|
-
throw new Error(`
|
|
129
|
+
throw new Error(`Failed to create directory ${path}`, { cause: error });
|
|
171
130
|
}
|
|
172
131
|
}
|
|
173
132
|
/**
|
|
174
133
|
* Prompts the user to confirm before overwriting an existing path.
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
* any prompt — nothing to overwrite, safe to proceed.
|
|
178
|
-
*
|
|
179
|
-
* If the user cancels the prompt (e.g. Ctrl+C), the process exits cleanly
|
|
180
|
-
* with a cancellation message rather than throwing.
|
|
181
|
-
*
|
|
182
|
-
* @param label - Human-readable name shown in the prompt (e.g. `"centoui.config.ts"`).
|
|
183
|
-
* @param path - Absolute path to the file or directory to potentially overwrite.
|
|
184
|
-
* @returns `true` if the caller should write/overwrite, `false` if the user declined.
|
|
134
|
+
* @param path The absolute path to the file or directory to potentially overwrite.
|
|
135
|
+
* @returns Whether the user wants to overwrite the file.
|
|
185
136
|
*/
|
|
186
|
-
async function
|
|
137
|
+
async function confirmOverwrite(path) {
|
|
187
138
|
if (!await fsExtra.pathExists(path)) return true;
|
|
188
139
|
const answer = await confirm({
|
|
189
|
-
|
|
190
|
-
|
|
140
|
+
initialValue: false,
|
|
141
|
+
message: `${path} already exists. Overwrite?`
|
|
191
142
|
});
|
|
192
143
|
if (isCancel(answer)) {
|
|
193
|
-
cancel("Operation cancelled.");
|
|
144
|
+
cancel("Operation cancelled by user.");
|
|
194
145
|
process.exit(0);
|
|
195
146
|
}
|
|
196
147
|
return answer;
|
|
197
148
|
}
|
|
198
149
|
//#endregion
|
|
199
|
-
//#region src/utils/
|
|
150
|
+
//#region src/utils/package.ts
|
|
200
151
|
/**
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* @param
|
|
204
|
-
* @
|
|
205
|
-
* @
|
|
206
|
-
* @throws If the file exists but cannot be imported or does not export a default value.
|
|
152
|
+
* Installs a dependency using nypm.
|
|
153
|
+
* @param name Dependency name.
|
|
154
|
+
* @param version Dependency semver version.
|
|
155
|
+
* @param cwd Current working directory.
|
|
156
|
+
* @returns Status message.
|
|
207
157
|
*/
|
|
208
|
-
async function
|
|
209
|
-
const { config, configFile } = await loadConfig({
|
|
210
|
-
name: "centoui",
|
|
211
|
-
cwd
|
|
212
|
-
});
|
|
213
|
-
if (!configFile) throw new Error(`[loadCentoUIConfig] "${CONFIG_FILE_NAME}" not found in "${cwd}". Run \`centoui init\` first.`);
|
|
214
|
-
return config;
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Fetches the raw content of the default CentoUI config file from GitHub.
|
|
218
|
-
*
|
|
219
|
-
* This file contains the default icon mappings and other shared defaults
|
|
220
|
-
* that are merged into the user's generated config.
|
|
221
|
-
*
|
|
222
|
-
* @returns Raw UTF-8 content of the default config file.
|
|
223
|
-
* @throws If the network request fails or the server returns a non-2xx status.
|
|
224
|
-
*/
|
|
225
|
-
async function fetchDefaultConfig() {
|
|
226
|
-
let response;
|
|
158
|
+
async function installDependency(name, version, cwd) {
|
|
227
159
|
try {
|
|
228
|
-
|
|
160
|
+
const packageJson = await fsExtra.readJson(join(cwd, "package.json")).catch(() => ({}));
|
|
161
|
+
if (name in {
|
|
162
|
+
...packageJson?.dependencies,
|
|
163
|
+
...packageJson?.devDependencies
|
|
164
|
+
}) return `${name} is already installed.`;
|
|
165
|
+
const target = `${name}@${version}`;
|
|
166
|
+
await addDependency(target, {
|
|
167
|
+
cwd,
|
|
168
|
+
silent: true
|
|
169
|
+
});
|
|
170
|
+
return `${target} installed.`;
|
|
229
171
|
} catch (error) {
|
|
230
|
-
throw new Error(`
|
|
172
|
+
throw new Error(`Failed to install ${name}`, { cause: error });
|
|
231
173
|
}
|
|
232
|
-
if (!response.ok) throw new Error(`[fetchDefaultConfigContent] Server returned ${response.status} ${response.statusText} (URL: ${CONFIG_DEFAULTS_URL})`);
|
|
233
|
-
return response.text();
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Extracts the inner config object from the default config file content.
|
|
237
|
-
*
|
|
238
|
-
* Strips the `export default`, `satisfies` clause, and outer braces,
|
|
239
|
-
* returning just the inner properties as a raw string.
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
* // Input:
|
|
243
|
-
* // export default {
|
|
244
|
-
* // icons: { check: 'lucide:check' },
|
|
245
|
-
* // } satisfies Pick<CentoUIConfig, 'icons'>
|
|
246
|
-
* // Output:
|
|
247
|
-
* // " icons: { check: 'lucide:check' },"
|
|
248
|
-
*
|
|
249
|
-
* @param fileContent - The full source of the default config file.
|
|
250
|
-
* @returns The inner config body, or an empty string if no object is found.
|
|
251
|
-
*/
|
|
252
|
-
function extractInnerConfigContent(fileContent) {
|
|
253
|
-
let cleanedConfigContent = fileContent.replace(/^import\s+.*$/gm, "");
|
|
254
|
-
cleanedConfigContent = cleanedConfigContent.replace(/^\s*export\s+default\s+/, "");
|
|
255
|
-
cleanedConfigContent = cleanedConfigContent.replace(/\s+satisfies\s+[^}]*$/, "");
|
|
256
|
-
cleanedConfigContent = cleanedConfigContent.trim();
|
|
257
|
-
const firstBrace = cleanedConfigContent.indexOf("{");
|
|
258
|
-
const lastBrace = cleanedConfigContent.lastIndexOf("}");
|
|
259
|
-
if (firstBrace === -1 || lastBrace === -1 || lastBrace <= firstBrace) return "";
|
|
260
|
-
return cleanedConfigContent.slice(firstBrace + 1, lastBrace).replace(/^\n/, "").replace(/\n\s*$/, "");
|
|
261
174
|
}
|
|
262
175
|
/**
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
* for the built-in Lucide icon set.
|
|
268
|
-
*
|
|
269
|
-
* @param themeFilePath - Relative path (from project root) where the CSS theme file lives.
|
|
270
|
-
* @param componentsDir - Relative path (from project root) where components will be installed.
|
|
271
|
-
* @returns A string containing the complete TypeScript source for the config file.
|
|
272
|
-
*/
|
|
273
|
-
/**
|
|
274
|
-
* Generates the content of a user's default `centoui.config.ts` file.
|
|
275
|
-
*
|
|
276
|
-
* Fetches the default config from GitHub to get the latest default config values (icon mappings, etc.),
|
|
277
|
-
* then merges them with the user-provided paths. The generated file uses
|
|
278
|
-
* `defineConfig` so IDEs can provide type-checking and autocompletion.
|
|
279
|
-
*
|
|
280
|
-
* @param themeFilePath - Relative path (from project root) where the CSS theme file lives.
|
|
281
|
-
* @param componentsDir - Relative path (from project root) where components will be installed.
|
|
282
|
-
* @param utilsFilePath - Relative path (from project root) where the utils file lives.
|
|
283
|
-
* @returns A string containing the complete TypeScript source for the config file.
|
|
176
|
+
* Uninstalls a dependency using nypm.
|
|
177
|
+
* @param name Dependency name.
|
|
178
|
+
* @param cwd Current working directory.
|
|
179
|
+
* @returns Status message.
|
|
284
180
|
*/
|
|
285
|
-
async function
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
181
|
+
async function uninstallDependency(name, cwd) {
|
|
182
|
+
try {
|
|
183
|
+
const packageJson = await fsExtra.readJson(join(cwd, "package.json")).catch(() => ({}));
|
|
184
|
+
if (!(name in {
|
|
185
|
+
...packageJson?.dependencies,
|
|
186
|
+
...packageJson?.devDependencies
|
|
187
|
+
})) return `${name} is not installed.`;
|
|
188
|
+
await removeDependency(name, {
|
|
189
|
+
cwd,
|
|
190
|
+
silent: true
|
|
191
|
+
});
|
|
192
|
+
return `${name} uninstalled.`;
|
|
193
|
+
} catch (error) {
|
|
194
|
+
throw new Error(`Failed to uninstall ${name}`, { cause: error });
|
|
195
|
+
}
|
|
295
196
|
}
|
|
296
197
|
//#endregion
|
|
297
|
-
//#region src/utils/registry
|
|
198
|
+
//#region src/utils/registry.ts
|
|
298
199
|
/** In-process cache so the registry is only fetched once per CLI invocation. */
|
|
299
|
-
let cachedRegistry
|
|
200
|
+
let cachedRegistry;
|
|
300
201
|
/**
|
|
301
|
-
* Fetches the
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
* Every other registry utility calls this internally, so the network round-trip
|
|
305
|
-
* only happens once regardless of how many components are being installed.
|
|
306
|
-
*
|
|
307
|
-
* @returns The full registry object including globals and all component entries.
|
|
308
|
-
* @throws If the network request fails or the server returns a non-2xx status.
|
|
202
|
+
* Fetches the component registry (`index.json`) from GitHub and caches it.
|
|
203
|
+
* @returns The registry including npm dependencies and components.
|
|
309
204
|
*/
|
|
310
|
-
async function
|
|
205
|
+
async function fetchRegistry() {
|
|
311
206
|
if (cachedRegistry) return cachedRegistry;
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
} catch (error) {
|
|
316
|
-
throw new Error(`[fetchFullRegistry] Network request to registry failed: ${error}`);
|
|
317
|
-
}
|
|
318
|
-
if (!response.ok) throw new Error(`[fetchFullRegistry] Registry responded with ${response.status} ${response.statusText} (URL: ${REGISTRY_INDEX_URL})`);
|
|
319
|
-
cachedRegistry = await response.json();
|
|
320
|
-
return cachedRegistry;
|
|
207
|
+
const registry = await sendNetworkRequest("/registry/index.json", "json");
|
|
208
|
+
cachedRegistry = registry;
|
|
209
|
+
return registry;
|
|
321
210
|
}
|
|
322
211
|
/**
|
|
323
|
-
* Recursively resolves a component and
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
* @param componentName - Root component name to start resolving from.
|
|
330
|
-
* @param registry - Pre-fetched registry (pass the result of {@link fetchFullRegistry}).
|
|
331
|
-
* @param visited - Internal visited set used to prevent infinite loops;
|
|
332
|
-
* callers should omit this — it defaults to an empty set.
|
|
333
|
-
* @returns A `Map<componentName, ComponentRegistry>` covering the full tree.
|
|
334
|
-
* @throws If any component in the tree is not found in the registry.
|
|
212
|
+
* Recursively resolves a component and its dependencies into a map.
|
|
213
|
+
* @param name Component name.
|
|
214
|
+
* @param registry The component registry.
|
|
215
|
+
* @param result Internal accumulator map.
|
|
216
|
+
* @returns Map of resolved component names to their entries.
|
|
217
|
+
* @throws If any component is not found.
|
|
335
218
|
*/
|
|
336
|
-
function
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
result.set(componentName, entry);
|
|
343
|
-
for (const dep of entry?.componentDeps || []) for (const [depName, depEntry] of resolveComponentWithDependencies(dep, registry, visited)) result.set(depName, depEntry);
|
|
219
|
+
function resolveComponent(name, registry, result = /* @__PURE__ */ new Map()) {
|
|
220
|
+
if (result.has(name)) return result;
|
|
221
|
+
const entry = registry.components.find((component) => component.name === name);
|
|
222
|
+
if (!entry) throw new Error(`Component ${name} not found in registry.`);
|
|
223
|
+
result.set(name, entry);
|
|
224
|
+
for (const component of entry.componentDependencies || []) resolveComponent(component, registry, result);
|
|
344
225
|
return result;
|
|
345
226
|
}
|
|
227
|
+
//#endregion
|
|
228
|
+
//#region src/commands/add.ts
|
|
346
229
|
/**
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
* Registry file paths (e.g. `"components/button/button.vue"`) are relative
|
|
351
|
-
* to `packages/core/src/`, which is already the base of {@link CORE_SRC_BASE_URL}.
|
|
352
|
-
* This function simply appends the path and downloads the content.
|
|
353
|
-
*
|
|
354
|
-
* @param registryFilePath - Path as it appears in the component's `files` array
|
|
355
|
-
* (e.g. `"components/button/button.vue"`).
|
|
356
|
-
* @returns Raw UTF-8 content of the file.
|
|
357
|
-
* @throws If the network request fails or the server returns a non-2xx status.
|
|
358
|
-
*/
|
|
359
|
-
async function fetchRegistryFileContent(registryFilePath) {
|
|
360
|
-
const url = `${CORE_SRC_BASE_URL}/${registryFilePath}`;
|
|
361
|
-
let response;
|
|
362
|
-
try {
|
|
363
|
-
response = await fetch(url, { headers: GITHUB_RAW_FETCH_HEADERS });
|
|
364
|
-
} catch (error) {
|
|
365
|
-
throw new Error(`[fetchRegistryFileContent] Network request failed for "${registryFilePath}": ${error}`);
|
|
366
|
-
}
|
|
367
|
-
if (!response.ok) throw new Error(`[fetchRegistryFileContent] Server returned ${response.status} ${response.statusText} for "${url}"`);
|
|
368
|
-
return response.text();
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* Fetches the raw content of the CentoUI CSS theme file from GitHub.
|
|
372
|
-
*
|
|
373
|
-
* This is the file written to the user's project during `centoui init` and
|
|
374
|
-
* contains all CSS custom properties and base styles for every component.
|
|
375
|
-
*
|
|
376
|
-
* @returns Raw UTF-8 content of the theme CSS file.
|
|
377
|
-
* @throws If the network request fails or the server returns a non-2xx status.
|
|
378
|
-
*/
|
|
379
|
-
async function fetchThemeCSSContent() {
|
|
380
|
-
let response;
|
|
381
|
-
try {
|
|
382
|
-
response = await fetch(THEME_CSS_URL, { headers: GITHUB_RAW_FETCH_HEADERS });
|
|
383
|
-
} catch (error) {
|
|
384
|
-
throw new Error(`[fetchThemeCSSContent] Network request for theme CSS failed: ${error}`);
|
|
385
|
-
}
|
|
386
|
-
if (!response.ok) throw new Error(`[fetchThemeCSSContent] Server returned ${response.status} ${response.statusText} (URL: ${THEME_CSS_URL})`);
|
|
387
|
-
return response.text();
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* Fetches the raw content of the CentoUI utils file from GitHub.
|
|
391
|
-
*
|
|
392
|
-
* This is the file written to the user's project during `centoui init` and
|
|
393
|
-
* contains all global utils for every component.
|
|
394
|
-
*
|
|
395
|
-
* @returns Raw UTF-8 content of the utils file.
|
|
396
|
-
* @throws If the network request fails or the server returns a non-2xx status.
|
|
230
|
+
* Installs one or more components and their full transitive dependency trees from the registry into the user's project.
|
|
231
|
+
* @returns The Citty command definition that executes the 'add' CLI process.
|
|
397
232
|
*/
|
|
398
|
-
async function
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
233
|
+
async function add() {
|
|
234
|
+
return defineCommand({
|
|
235
|
+
meta: {
|
|
236
|
+
description: "Add one or more components to your project",
|
|
237
|
+
name: "add"
|
|
238
|
+
},
|
|
239
|
+
args: {},
|
|
240
|
+
run: async ({ args }) => {
|
|
241
|
+
const cwd = process.cwd();
|
|
242
|
+
intro("CentoUI — Add components!");
|
|
243
|
+
const requestedComponents = args._;
|
|
244
|
+
if (requestedComponents.length === 0) throw new Error("No components specified. Usage: centoui add <component> [component...]");
|
|
245
|
+
log.step("Loading config.");
|
|
246
|
+
const config = await loadConfig$1(cwd);
|
|
247
|
+
log.step("Fetching registry.");
|
|
248
|
+
const registry = await fetchRegistry();
|
|
249
|
+
log.step("Resolving components");
|
|
250
|
+
const resolvedComponents = /* @__PURE__ */ new Map();
|
|
251
|
+
for (const requested of requestedComponents) {
|
|
252
|
+
const resolvedTree = resolveComponent(requested, registry);
|
|
253
|
+
for (const [name, entry] of resolvedTree) resolvedComponents.set(name, entry);
|
|
254
|
+
}
|
|
255
|
+
const writeDecisions = /* @__PURE__ */ new Map();
|
|
256
|
+
for (const [name] of resolvedComponents) {
|
|
257
|
+
const shouldWrite = await confirmOverwrite(join(cwd, config.componentsDir, name));
|
|
258
|
+
writeDecisions.set(name, shouldWrite);
|
|
259
|
+
}
|
|
260
|
+
const npmDependenciesToInstall = {};
|
|
261
|
+
for (const [name, entry] of resolvedComponents) if (writeDecisions.get(name)) Object.assign(npmDependenciesToInstall, entry.npmDependencies);
|
|
262
|
+
await tasks([...[...resolvedComponents.entries()].filter(([name]) => writeDecisions.get(name)).map(([name, entry]) => {
|
|
263
|
+
return {
|
|
264
|
+
task: async (message) => {
|
|
265
|
+
message(`Installing ${name}.`);
|
|
266
|
+
for (const path of entry.files) {
|
|
267
|
+
message(`Fetching contents from registry.`);
|
|
268
|
+
const content = await sendNetworkRequest(`/components/${path}`);
|
|
269
|
+
const destination = join(cwd, config.componentsDir, path);
|
|
270
|
+
message("Writing to disk.");
|
|
271
|
+
await writeToFile(destination, content);
|
|
272
|
+
}
|
|
273
|
+
return `${name} component installed!`;
|
|
274
|
+
},
|
|
275
|
+
title: `Installing ${name}`
|
|
276
|
+
};
|
|
277
|
+
}), {
|
|
278
|
+
enabled: Object.keys(npmDependenciesToInstall).length > 0,
|
|
279
|
+
task: async (message) => {
|
|
280
|
+
for (const [name, version] of Object.entries(npmDependenciesToInstall)) {
|
|
281
|
+
message(`Installing ${name}.`);
|
|
282
|
+
await installDependency(name, version, cwd);
|
|
283
|
+
}
|
|
284
|
+
return "Dependencies installed!";
|
|
285
|
+
},
|
|
286
|
+
title: "Installing dependencies"
|
|
287
|
+
}]);
|
|
288
|
+
outro("Installation Complete!");
|
|
289
|
+
}
|
|
290
|
+
});
|
|
407
291
|
}
|
|
408
292
|
//#endregion
|
|
409
293
|
//#region src/commands/init.ts
|
|
410
294
|
/**
|
|
411
|
-
* Command: `centoui init`
|
|
412
|
-
*
|
|
413
295
|
* Bootstraps a new CentoUI project in the current working directory.
|
|
414
|
-
*
|
|
415
|
-
* Flow:
|
|
416
|
-
* 1. Prompt the user for the components directory, theme CSS file path, and utils file path.
|
|
417
|
-
* 2. Ask upfront whether to overwrite the config file, theme CSS, components directory paths if they already exist.
|
|
418
|
-
* 3. Write the config file, fetch and write the theme CSS, prepare the
|
|
419
|
-
* components directory, and install global npm dependencies.
|
|
296
|
+
* @returns The Citty command definition that executes the 'init' CLI process.
|
|
420
297
|
*/
|
|
421
298
|
function init() {
|
|
422
299
|
return defineCommand({
|
|
423
300
|
meta: {
|
|
424
|
-
|
|
425
|
-
|
|
301
|
+
description: "Initialize a new CentoUI project",
|
|
302
|
+
name: "init"
|
|
426
303
|
},
|
|
427
|
-
async
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
{
|
|
460
|
-
title: "Fetching config defaults",
|
|
461
|
-
task: async () => {
|
|
462
|
-
if (!shouldWriteConfig) return `Skipped — "${CONFIG_FILE_NAME}" already exists`;
|
|
463
|
-
await writeFileWithDirs(configPath, await buildUserDefaultConfigFileContent(directories.themeFilePath, directories.componentDir, directories.utilsFilePath));
|
|
464
|
-
return `${CONFIG_FILE_NAME} written`;
|
|
465
|
-
}
|
|
304
|
+
run: async () => {
|
|
305
|
+
const cwd = process.cwd();
|
|
306
|
+
intro("CentoUI — Initialize project!");
|
|
307
|
+
const choices = await group({
|
|
308
|
+
componentsDir: () => text({
|
|
309
|
+
initialValue: "src/components/centoui",
|
|
310
|
+
message: "Directory to store components."
|
|
311
|
+
}),
|
|
312
|
+
themeFilePath: () => text({
|
|
313
|
+
initialValue: "src/assets/css/centoui.css",
|
|
314
|
+
message: "Path for the theme CSS file."
|
|
315
|
+
})
|
|
316
|
+
}, { onCancel: () => {
|
|
317
|
+
cancel("Project initialization cancelled.");
|
|
318
|
+
process.exit(0);
|
|
319
|
+
} });
|
|
320
|
+
const configPath = join(cwd, CONFIG_FILE_NAME);
|
|
321
|
+
const themePath = join(cwd, choices.themeFilePath);
|
|
322
|
+
const componentsPath = join(cwd, choices.componentsDir);
|
|
323
|
+
const shouldWriteConfig = await confirmOverwrite(CONFIG_FILE_NAME);
|
|
324
|
+
const shouldWriteTheme = await confirmOverwrite(choices.themeFilePath);
|
|
325
|
+
const shouldWriteComponentsDir = await confirmOverwrite(choices.componentsDir);
|
|
326
|
+
let registry;
|
|
327
|
+
await tasks([
|
|
328
|
+
{
|
|
329
|
+
enabled: shouldWriteConfig,
|
|
330
|
+
task: async (message) => {
|
|
331
|
+
message("Building config content.");
|
|
332
|
+
const userConfigContent = await buildUserConfig(choices);
|
|
333
|
+
message("Writing to disk.");
|
|
334
|
+
await writeToFile(configPath, userConfigContent);
|
|
335
|
+
return "Config created!";
|
|
466
336
|
},
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
337
|
+
title: "Creating config."
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
enabled: shouldWriteTheme,
|
|
341
|
+
task: async (message) => {
|
|
342
|
+
message("Fetching theme from registry.");
|
|
343
|
+
const themeFileContent = await sendNetworkRequest("/theme.css");
|
|
344
|
+
message("Writing to disk.");
|
|
345
|
+
await writeToFile(themePath, themeFileContent);
|
|
346
|
+
return "Theme created!";
|
|
474
347
|
},
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
348
|
+
title: "Creating theme."
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
enabled: shouldWriteComponentsDir,
|
|
352
|
+
task: async (message) => {
|
|
353
|
+
message("Writing to disk.");
|
|
354
|
+
await createDirectory(componentsPath);
|
|
355
|
+
return "Components directory ready!";
|
|
356
|
+
},
|
|
357
|
+
title: "Preparing components directory."
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
task: async () => {
|
|
361
|
+
registry = await fetchRegistry();
|
|
362
|
+
return "Registry loaded!";
|
|
482
363
|
},
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
364
|
+
title: "Loading registry."
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
task: async (message) => {
|
|
368
|
+
for (const [name, version] of Object.entries(registry.npmDependencies)) {
|
|
369
|
+
message(`Installing ${name}.`);
|
|
370
|
+
await installDependency(name, version, cwd);
|
|
488
371
|
}
|
|
372
|
+
return "Dependencies installed!";
|
|
489
373
|
},
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
]);
|
|
495
|
-
note([
|
|
496
|
-
`Config > ${configPath}`,
|
|
497
|
-
`Theme > ${themePath}`,
|
|
498
|
-
`Components > ${componentsPath}`,
|
|
499
|
-
"",
|
|
500
|
-
"Run 'centoui add button' to install your first component."
|
|
501
|
-
].filter(Boolean).join("\n"), "CentoUI initialized");
|
|
502
|
-
outro("All set!");
|
|
503
|
-
} catch (error) {
|
|
504
|
-
log.error(`Initialization failed: ${error}`);
|
|
505
|
-
process.exit(1);
|
|
506
|
-
}
|
|
374
|
+
title: "Installing dependencies."
|
|
375
|
+
}
|
|
376
|
+
]);
|
|
377
|
+
outro("Initialization Complete!");
|
|
507
378
|
}
|
|
508
379
|
});
|
|
509
380
|
}
|
|
510
381
|
//#endregion
|
|
511
|
-
//#region src/utils/components
|
|
512
|
-
/**
|
|
513
|
-
* Scans the configured components directory and returns a sorted list of
|
|
514
|
-
* installed component names.
|
|
515
|
-
*
|
|
516
|
-
* Each direct subdirectory of `config.componentsDir` is treated as one
|
|
517
|
-
* installed component. Non-directory entries (loose files) are ignored.
|
|
518
|
-
*
|
|
519
|
-
* @param config - The loaded CentoUI project configuration.
|
|
520
|
-
* @param cwd - Absolute path to the project root.
|
|
521
|
-
* @returns Sorted array of installed component names, or an empty array if
|
|
522
|
-
* the components directory does not exist yet.
|
|
523
|
-
* @throws If the directory exists but cannot be read.
|
|
524
|
-
*/
|
|
525
|
-
async function listInstalledComponentNames(config, cwd) {
|
|
526
|
-
const componentsDir = join(cwd, config.componentsDir);
|
|
527
|
-
try {
|
|
528
|
-
if (!await fsExtra.pathExists(componentsDir)) return [];
|
|
529
|
-
return (await fsExtra.readdir(componentsDir, { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort();
|
|
530
|
-
} catch (error) {
|
|
531
|
-
throw new Error(`[listInstalledComponentNames] Failed to read components directory "${componentsDir}": ${error}`);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* Returns the absolute path to a component's installation directory inside
|
|
536
|
-
* the user's project.
|
|
537
|
-
*
|
|
538
|
-
* The directory may or may not exist yet — this function does not check.
|
|
539
|
-
* Use {@link checkIsComponentInstalled} if you need to verify existence.
|
|
540
|
-
*
|
|
541
|
-
* @param componentName - The component name in kebab-case (e.g. `"button"`).
|
|
542
|
-
* @param config - The loaded CentoUI project configuration.
|
|
543
|
-
* @param cwd - Absolute path to the project root.
|
|
544
|
-
*/
|
|
545
|
-
function resolveComponentInstallDir(componentName, config, cwd) {
|
|
546
|
-
return join(cwd, config.componentsDir, componentName);
|
|
547
|
-
}
|
|
382
|
+
//#region src/utils/components.ts
|
|
548
383
|
/**
|
|
549
|
-
*
|
|
550
|
-
*
|
|
551
|
-
*
|
|
552
|
-
* @
|
|
553
|
-
* @
|
|
554
|
-
* @param cwd - Absolute path to the project root.
|
|
555
|
-
* @returns `true` if the component directory exists, `false` otherwise.
|
|
384
|
+
* Scans the components directory to determine which CentoUI components are installed.
|
|
385
|
+
* @param config User's CentoUI configuration.
|
|
386
|
+
* @param cwd Absolute path to the project root.
|
|
387
|
+
* @returns Sorted array of installed component names.
|
|
388
|
+
* @throws If the components directory cannot be read.
|
|
556
389
|
*/
|
|
557
|
-
async function
|
|
558
|
-
const componentInstallDir = resolveComponentInstallDir(componentName, config, cwd);
|
|
390
|
+
async function getInstalledComponents(config, cwd) {
|
|
559
391
|
try {
|
|
560
|
-
|
|
392
|
+
const componentsDir = join(cwd, config.componentsDir);
|
|
393
|
+
return (await fsExtra.readdir(componentsDir, { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name).toSorted();
|
|
561
394
|
} catch (error) {
|
|
562
|
-
throw new Error(
|
|
395
|
+
throw new Error("Failed to read components directory", { cause: error });
|
|
563
396
|
}
|
|
564
397
|
}
|
|
565
398
|
//#endregion
|
|
566
|
-
//#region src/commands/add.ts
|
|
567
|
-
/**
|
|
568
|
-
* Command: `centoui add <component> [component...]`
|
|
569
|
-
*
|
|
570
|
-
* Installs one or more components — and their full transitive dependency trees
|
|
571
|
-
* — from the registry into the user's project.
|
|
572
|
-
*
|
|
573
|
-
* Flow:
|
|
574
|
-
* 1. Resolve the full dependency tree for every requested component.
|
|
575
|
-
* 2. Ask the user upfront whether to overwrite any that already exist.
|
|
576
|
-
* 3. Fetch and write the source files for components the user approved.
|
|
577
|
-
* 4. Fetch and write the utils file if it doesn't exist but is required by the components.
|
|
578
|
-
* 5. Install any npm packages required by the components being written.
|
|
579
|
-
*/
|
|
580
|
-
function add() {
|
|
581
|
-
return defineCommand({
|
|
582
|
-
meta: {
|
|
583
|
-
name: "add",
|
|
584
|
-
description: "Add one or more components to your project"
|
|
585
|
-
},
|
|
586
|
-
args: {},
|
|
587
|
-
async run({ args }) {
|
|
588
|
-
try {
|
|
589
|
-
const cwd = process.cwd();
|
|
590
|
-
const requestedNames = args._;
|
|
591
|
-
intro("CentoUI — Add components");
|
|
592
|
-
if (requestedNames.length === 0) throw new Error("No components specified. Usage: centoui add <component> [component...]");
|
|
593
|
-
const config = await loadCentoUIConfig(cwd);
|
|
594
|
-
const registry = await fetchFullRegistry();
|
|
595
|
-
const allComponents = /* @__PURE__ */ new Map();
|
|
596
|
-
for (const name of requestedNames) try {
|
|
597
|
-
const tree = resolveComponentWithDependencies(name, registry);
|
|
598
|
-
for (const [depName, depEntry] of tree) allComponents.set(depName, depEntry);
|
|
599
|
-
} catch (error) {
|
|
600
|
-
throw new Error(`Failed to resolve "${name}": ${error}`);
|
|
601
|
-
}
|
|
602
|
-
const writeDecisions = /* @__PURE__ */ new Map();
|
|
603
|
-
for (const [name] of allComponents) {
|
|
604
|
-
const shouldWrite = await confirmOverwriteIfExists(name, resolveComponentInstallDir(name, config, cwd));
|
|
605
|
-
writeDecisions.set(name, shouldWrite);
|
|
606
|
-
}
|
|
607
|
-
const packageDepsToInstall = {};
|
|
608
|
-
for (const [name, entry] of allComponents) if (writeDecisions.get(name)) Object.assign(packageDepsToInstall, entry.packageDeps);
|
|
609
|
-
const approvedComponents = Array.from(allComponents.entries()).filter(([name]) => writeDecisions.get(name));
|
|
610
|
-
const needsUtils = Array.from(approvedComponents).some(([, entry]) => entry.needsUtils === true);
|
|
611
|
-
const utilsPath = join(cwd, config.utilsFilePath);
|
|
612
|
-
const utilsFileExists = await fsExtra.pathExists(utilsPath);
|
|
613
|
-
await tasks([
|
|
614
|
-
...approvedComponents.map(([name, entry]) => ({
|
|
615
|
-
title: `Installing ${name}`,
|
|
616
|
-
task: async () => {
|
|
617
|
-
for (const registryFilePath of entry.files) {
|
|
618
|
-
const content = await fetchRegistryFileContent(registryFilePath);
|
|
619
|
-
await writeFileWithDirs(mapComponentsRegistryPathToProjectDest(registryFilePath, config, cwd), content);
|
|
620
|
-
}
|
|
621
|
-
return `${name} installed (${entry.files.length} file(s))`;
|
|
622
|
-
}
|
|
623
|
-
})),
|
|
624
|
-
{
|
|
625
|
-
title: "Installing packages",
|
|
626
|
-
task: async (message) => installMissingPackages(packageDepsToInstall, cwd, message)
|
|
627
|
-
},
|
|
628
|
-
...needsUtils && !utilsFileExists ? [{
|
|
629
|
-
title: "Writing utils file",
|
|
630
|
-
task: async () => {
|
|
631
|
-
await writeFileWithDirs(utilsPath, await fetchUtilsFileContent());
|
|
632
|
-
return `${config.utilsFilePath} written`;
|
|
633
|
-
}
|
|
634
|
-
}] : []
|
|
635
|
-
]);
|
|
636
|
-
const skippedNames = Array.from(writeDecisions.entries()).filter(([, shouldWrite]) => !shouldWrite).map(([name]) => name);
|
|
637
|
-
note([
|
|
638
|
-
`Installed > ${approvedComponents.map(([name]) => name).join(", ") || "none"}`,
|
|
639
|
-
skippedNames.length > 0 ? `Skipped > ${skippedNames.join(", ")}` : "",
|
|
640
|
-
"",
|
|
641
|
-
"Import components from your components directory to use them."
|
|
642
|
-
].filter(Boolean).join("\n"), "Component(s) added");
|
|
643
|
-
outro("All set!");
|
|
644
|
-
} catch (error) {
|
|
645
|
-
log.error(`Failed to add component(s): ${error}`);
|
|
646
|
-
process.exit(1);
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
//#endregion
|
|
652
399
|
//#region src/commands/remove.ts
|
|
653
400
|
/**
|
|
654
|
-
* Command: `centoui remove <component>`
|
|
655
|
-
*
|
|
656
401
|
* Removes a single installed component and its files from the user's project.
|
|
657
|
-
*
|
|
658
|
-
* Flow:
|
|
659
|
-
* 1. Confirm the component is actually installed.
|
|
660
|
-
* 2. Fetch the registry and check whether any other installed components
|
|
661
|
-
* declare this one as a `componentDep` — block removal if so.
|
|
662
|
-
* 3. Collect the packages still required by remaining components so we
|
|
663
|
-
* know which of this component's packages become orphaned.
|
|
664
|
-
* 4. Ask for confirmation, then delete the component directory and remove
|
|
665
|
-
* any newly orphaned npm packages.
|
|
666
|
-
* 5. Check if any utils file is no longer needed and remove it if so(with confirmation from the user).
|
|
402
|
+
* @returns The Citty command definition that executes the 'remove' CLI process.
|
|
667
403
|
*/
|
|
668
404
|
function remove() {
|
|
669
405
|
return defineCommand({
|
|
670
406
|
meta: {
|
|
671
|
-
|
|
672
|
-
|
|
407
|
+
description: "Remove an installed component from your project",
|
|
408
|
+
name: "remove"
|
|
673
409
|
},
|
|
674
410
|
args: { component: {
|
|
675
|
-
|
|
411
|
+
description: "Name of the component to remove (e.g. \"button\")",
|
|
676
412
|
required: true,
|
|
677
|
-
|
|
413
|
+
type: "positional"
|
|
678
414
|
} },
|
|
679
|
-
async
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
const list = Array.from(dependents).map((dependent) => ` · ${dependent}`).join("\n");
|
|
701
|
-
throw new Error(`Cannot remove "${componentName}" — the following installed components depend on it:\n${list}`);
|
|
702
|
-
}
|
|
703
|
-
const confirmed = await confirm({
|
|
704
|
-
message: `Remove "${componentName}"?`,
|
|
705
|
-
initialValue: false
|
|
706
|
-
});
|
|
707
|
-
if (isCancel(confirmed) || !confirmed) {
|
|
708
|
-
cancel("Removal cancelled.");
|
|
709
|
-
process.exit(0);
|
|
710
|
-
}
|
|
711
|
-
await tasks([{
|
|
712
|
-
title: `Removing ${componentName}`,
|
|
713
|
-
task: async () => {
|
|
714
|
-
await fsExtra.remove(componentInstallDir);
|
|
715
|
-
return `${componentName} removed`;
|
|
716
|
-
}
|
|
717
|
-
}, {
|
|
718
|
-
title: "Removing orphaned packages",
|
|
719
|
-
task: async (message) => removeOrphanedPackages(targetEntry.packageDeps || {}, packagesStillNeeded, cwd, message)
|
|
720
|
-
}]);
|
|
721
|
-
const removedPackages = Object.keys(targetEntry.packageDeps || {}).filter((pkg) => !(pkg in packagesStillNeeded));
|
|
722
|
-
if (removedPackages.length > 0) note(removedPackages.map((pkg) => ` · ${pkg}`).join("\n"), "Packages removed");
|
|
723
|
-
if (!remainingNames.some((name) => {
|
|
724
|
-
return registry.components.find((c) => c.name === name)?.needsUtils === true;
|
|
725
|
-
})) {
|
|
726
|
-
const utilsPath = join(cwd, config.utilsFilePath);
|
|
727
|
-
if (await fsExtra.pathExists(utilsPath)) {
|
|
728
|
-
const shouldDeleteUtils = await confirm({
|
|
729
|
-
message: "Delete utils file? (no components require it anymore)",
|
|
730
|
-
initialValue: false
|
|
731
|
-
});
|
|
732
|
-
if (!isCancel(shouldDeleteUtils) && shouldDeleteUtils) {
|
|
733
|
-
await fsExtra.remove(utilsPath);
|
|
734
|
-
note(`${config.utilsFilePath} deleted`, "Utils file removed");
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
outro("All set!");
|
|
739
|
-
} catch (error) {
|
|
740
|
-
log.error(`Failed to remove component: ${error}`);
|
|
741
|
-
process.exit(1);
|
|
415
|
+
run: async ({ args }) => {
|
|
416
|
+
const cwd = process.cwd();
|
|
417
|
+
const component = args.component;
|
|
418
|
+
intro("CentoUI — Remove components!");
|
|
419
|
+
log.step("Loading config.");
|
|
420
|
+
const config = await loadConfig$1(cwd);
|
|
421
|
+
log.step("Resolving workspace.");
|
|
422
|
+
const installedComponents = await getInstalledComponents(config, cwd);
|
|
423
|
+
if (!installedComponents.includes(component)) throw new Error(`${component} is not installed.`);
|
|
424
|
+
log.step("Fetching registry.");
|
|
425
|
+
const registry = await fetchRegistry();
|
|
426
|
+
log.step("Resolving components.");
|
|
427
|
+
const componentEntry = registry.components.find((entry) => entry.name === component);
|
|
428
|
+
if (!componentEntry) throw new Error(`${component} not found in registry.`);
|
|
429
|
+
const exceptToBeRemovedComponents = installedComponents.filter((entry) => entry !== component);
|
|
430
|
+
const componentDependents = /* @__PURE__ */ new Set();
|
|
431
|
+
const neededDependencies = /* @__PURE__ */ new Map();
|
|
432
|
+
for (const name of exceptToBeRemovedComponents) {
|
|
433
|
+
const entry = registry.components.find((entry) => entry.name === name);
|
|
434
|
+
if (entry?.componentDependencies?.includes(component)) componentDependents.add(name);
|
|
435
|
+
neededDependencies.set(name, entry?.npmDependencies || {});
|
|
742
436
|
}
|
|
437
|
+
if (componentDependents.size > 0) throw new Error(`Cannot remove ${component} these components depend on it: ${[...componentDependents].join(", ")}`);
|
|
438
|
+
const componentDir = join(cwd, config.componentsDir, component);
|
|
439
|
+
const dependenciesToUninstall = Object.keys(componentEntry?.npmDependencies || {}).filter((name) => !neededDependencies.has(name));
|
|
440
|
+
await tasks([{
|
|
441
|
+
task: async () => {
|
|
442
|
+
await fsExtra.remove(componentDir);
|
|
443
|
+
return `${component} removed!`;
|
|
444
|
+
},
|
|
445
|
+
title: `Removing ${component}.`
|
|
446
|
+
}, {
|
|
447
|
+
enabled: dependenciesToUninstall.length > 0,
|
|
448
|
+
task: async (message) => {
|
|
449
|
+
for (const name of dependenciesToUninstall) {
|
|
450
|
+
message(`Uninstalling ${name}.`);
|
|
451
|
+
await uninstallDependency(name, cwd);
|
|
452
|
+
}
|
|
453
|
+
return "Orphaned dependencies removed!";
|
|
454
|
+
},
|
|
455
|
+
title: `Removing orphaned dependencies`
|
|
456
|
+
}]);
|
|
457
|
+
outro(`${component} removed from your project!`);
|
|
743
458
|
}
|
|
744
459
|
});
|
|
745
460
|
}
|
|
@@ -751,8 +466,8 @@ runMain(defineCommand({
|
|
|
751
466
|
version: VERSION
|
|
752
467
|
},
|
|
753
468
|
subCommands: {
|
|
754
|
-
init,
|
|
755
469
|
add,
|
|
470
|
+
init,
|
|
756
471
|
remove
|
|
757
472
|
}
|
|
758
473
|
}));
|