draft-cli 0.0.2
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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +86 -0
- package/dist/chunk-TY6E2FMX.js +191 -0
- package/dist/react.d.ts +2 -0
- package/dist/react.js +16 -0
- package/dist/vue.d.ts +2 -0
- package/dist/vue.js +16 -0
- package/package.json +50 -0
- package/src/bin/react.ts +28 -0
- package/src/bin/vue.ts +28 -0
- package/src/commands/add.ts +164 -0
- package/src/commands/init.ts +42 -0
- package/src/utils/config.ts +38 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ted
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Draft UI CLI
|
|
2
|
+
|
|
3
|
+
[](https://preflower.github.io/draft-ui/)
|
|
4
|
+
|
|
5
|
+
A command-line tool for adding components to your Vue and React projects. Inspired by [shadcn/ui](https://ui.shadcn.com/).
|
|
6
|
+
|
|
7
|
+
## Documentation
|
|
8
|
+
|
|
9
|
+
For full documentation, visit [preflower.github.io/draft-ui](https://preflower.github.io/draft-ui/).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
You can install the CLI globally or use it with your favorite package manager:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# Global installation
|
|
17
|
+
npm install -g draft-ui
|
|
18
|
+
# or
|
|
19
|
+
pnpm add -g draft-ui
|
|
20
|
+
|
|
21
|
+
# Or use it directly without installation
|
|
22
|
+
pnpm dlx draft-ui init
|
|
23
|
+
npx draft-ui init
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
The CLI provides two main entry points depending on your framework: `draft-vue` and `draft-react`.
|
|
29
|
+
|
|
30
|
+
### Initialize
|
|
31
|
+
|
|
32
|
+
Initialize your project and create a `components.json` file:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# For Vue projects
|
|
36
|
+
pnpm draft-vue init
|
|
37
|
+
|
|
38
|
+
# For React projects
|
|
39
|
+
pnpm draft-react init
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Add Components
|
|
43
|
+
|
|
44
|
+
Add a component to your project:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# For Vue
|
|
48
|
+
pnpm draft-vue add button
|
|
49
|
+
|
|
50
|
+
# For React
|
|
51
|
+
pnpm draft-react add button
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Configuration
|
|
55
|
+
|
|
56
|
+
The CLI uses a `components.json` file in your project root to manage paths and settings.
|
|
57
|
+
|
|
58
|
+
### Schema
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"style": "default",
|
|
63
|
+
"rsc": false,
|
|
64
|
+
"tsx": true,
|
|
65
|
+
"tailwind": {
|
|
66
|
+
"config": "tailwind.config.js",
|
|
67
|
+
"css": "src/index.css",
|
|
68
|
+
"baseColor": "slate",
|
|
69
|
+
"cssVariables": true
|
|
70
|
+
},
|
|
71
|
+
"aliases": {
|
|
72
|
+
"components": "@/components",
|
|
73
|
+
"utils": "@/lib/utils",
|
|
74
|
+
"ui": "@/components/ui",
|
|
75
|
+
"draft": "@/components/draft"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Credits
|
|
81
|
+
|
|
82
|
+
This project is heavily inspired by [shadcn/ui](https://ui.shadcn.com/).
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
MIT
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// src/commands/add.ts
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
import path2 from "path";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import fs2 from "fs-extra";
|
|
6
|
+
import ora from "ora";
|
|
7
|
+
|
|
8
|
+
// src/utils/config.ts
|
|
9
|
+
import path from "path";
|
|
10
|
+
import fs from "fs-extra";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
var configSchema = z.object({
|
|
13
|
+
style: z.string(),
|
|
14
|
+
rsc: z.boolean(),
|
|
15
|
+
tsx: z.boolean(),
|
|
16
|
+
tailwind: z.object({
|
|
17
|
+
config: z.string(),
|
|
18
|
+
css: z.string(),
|
|
19
|
+
baseColor: z.string(),
|
|
20
|
+
cssVariables: z.boolean()
|
|
21
|
+
}),
|
|
22
|
+
aliases: z.object({
|
|
23
|
+
components: z.string(),
|
|
24
|
+
utils: z.string(),
|
|
25
|
+
ui: z.string().optional(),
|
|
26
|
+
draft: z.string().optional()
|
|
27
|
+
})
|
|
28
|
+
});
|
|
29
|
+
async function getConfig(cwd) {
|
|
30
|
+
const configPath = path.resolve(cwd, "components.json");
|
|
31
|
+
if (!await fs.pathExists(configPath)) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const config = await fs.readJson(configPath);
|
|
36
|
+
return configSchema.parse(config);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new Error(`Invalid configuration: ${error}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/commands/add.ts
|
|
43
|
+
var require2 = createRequire(import.meta.url);
|
|
44
|
+
var pkg = {};
|
|
45
|
+
try {
|
|
46
|
+
pkg = require2("../../package.json");
|
|
47
|
+
} catch {
|
|
48
|
+
try {
|
|
49
|
+
pkg = require2("../package.json");
|
|
50
|
+
} catch {
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function addComponent(componentName, type) {
|
|
54
|
+
const cwd = process.cwd();
|
|
55
|
+
const config = await getConfig(cwd);
|
|
56
|
+
if (!config) {
|
|
57
|
+
console.log(chalk.red("\nNo components.json found. Please run init first (simulated).\n"));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const spinner = ora(`Adding ${componentName}...`).start();
|
|
61
|
+
try {
|
|
62
|
+
const monorepoRoot = path2.resolve(import.meta.url.replace("file://", ""), "../../../../");
|
|
63
|
+
const sourceDir = path2.resolve(
|
|
64
|
+
monorepoRoot,
|
|
65
|
+
"packages",
|
|
66
|
+
type,
|
|
67
|
+
"src",
|
|
68
|
+
"components",
|
|
69
|
+
componentName
|
|
70
|
+
);
|
|
71
|
+
if (!await fs2.pathExists(sourceDir)) {
|
|
72
|
+
if (typeof pkg.register === "string") {
|
|
73
|
+
spinner.text = `Checking registry ${pkg.register}...`;
|
|
74
|
+
try {
|
|
75
|
+
const registryUrl = `${pkg.register}/packages/${type}/src/registry.json`;
|
|
76
|
+
const registryRes = await fetch(registryUrl);
|
|
77
|
+
if (!registryRes.ok) {
|
|
78
|
+
throw new Error(`Failed to fetch registry from ${registryUrl}: ${registryRes.statusText}`);
|
|
79
|
+
}
|
|
80
|
+
const registry = await registryRes.json();
|
|
81
|
+
const componentMeta = registry.components[componentName];
|
|
82
|
+
if (componentMeta == null) {
|
|
83
|
+
spinner.fail(chalk.red(`Component ${componentName} not found in registry.`));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (!componentMeta.files || !Array.isArray(componentMeta.files)) {
|
|
87
|
+
spinner.fail(chalk.red(`Component ${componentName} has no files listed in registry.`));
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
spinner.text = `Fetching ${componentName} files...`;
|
|
91
|
+
const titleCaseComponentName = componentName.charAt(0).toUpperCase() + componentName.slice(1);
|
|
92
|
+
const targetBase2 = (config.aliases.draft ?? config.aliases.ui ?? config.aliases.components).replace("@/", "src/");
|
|
93
|
+
const targetDir2 = path2.resolve(cwd, targetBase2, titleCaseComponentName);
|
|
94
|
+
await fs2.ensureDir(targetDir2);
|
|
95
|
+
for (const file of componentMeta.files) {
|
|
96
|
+
const fileUrl = `${pkg.register}/packages/${type}/src/components/${componentName}/${file}`;
|
|
97
|
+
const fileRes = await fetch(fileUrl);
|
|
98
|
+
if (!fileRes.ok) {
|
|
99
|
+
throw new Error(`Failed to fetch file ${file}: ${fileRes.statusText}`);
|
|
100
|
+
}
|
|
101
|
+
const content = await fileRes.text();
|
|
102
|
+
await fs2.writeFile(path2.resolve(targetDir2, file), content);
|
|
103
|
+
}
|
|
104
|
+
if (componentMeta.dependencies != null && componentMeta.dependencies.length > 0) {
|
|
105
|
+
spinner.text = `Installing dependencies for ${componentName}: ${componentMeta.dependencies.join(", ")}...`;
|
|
106
|
+
const { execa } = await import("execa");
|
|
107
|
+
try {
|
|
108
|
+
await execa("pnpm", ["add", ...componentMeta.dependencies], { cwd });
|
|
109
|
+
} catch (err) {
|
|
110
|
+
spinner.warn(chalk.yellow(`Component added, but failed to install dependencies: ${err.message}`));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
spinner.succeed(chalk.green(`Component ${componentName} added from registry.`));
|
|
114
|
+
return;
|
|
115
|
+
} catch (error) {
|
|
116
|
+
spinner.fail(chalk.red(`Failed to fetch from registry: ${error.message}`));
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
spinner.fail(chalk.red(`Component ${componentName} not found for ${type}.`));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const targetBase = (config.aliases.draft ?? config.aliases.ui ?? config.aliases.components).replace("@/", "src/");
|
|
124
|
+
const targetDir = path2.resolve(cwd, targetBase, componentName);
|
|
125
|
+
await fs2.ensureDir(targetDir);
|
|
126
|
+
await fs2.copy(sourceDir, targetDir);
|
|
127
|
+
const registryPath = path2.resolve(monorepoRoot, "packages", type, "src", "registry.json");
|
|
128
|
+
if (await fs2.pathExists(registryPath)) {
|
|
129
|
+
const registry = await fs2.readJson(registryPath);
|
|
130
|
+
const componentMeta = registry.components[componentName];
|
|
131
|
+
if (componentMeta?.dependencies?.length > 0) {
|
|
132
|
+
spinner.text = `Installing dependencies for ${componentName}: ${componentMeta.dependencies.join(", ")}...`;
|
|
133
|
+
const { execa } = await import("execa");
|
|
134
|
+
try {
|
|
135
|
+
await execa("pnpm", ["add", ...componentMeta.dependencies], { cwd });
|
|
136
|
+
spinner.succeed(chalk.green(`Component ${componentName} and its dependencies added successfully.`));
|
|
137
|
+
return;
|
|
138
|
+
} catch (err) {
|
|
139
|
+
spinner.warn(chalk.yellow(`Component copied, but failed to install dependencies: ${err.message}`));
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
spinner.succeed(chalk.green(`Component ${componentName} added successfully to ${targetDir}`));
|
|
145
|
+
} catch (error) {
|
|
146
|
+
spinner.fail(chalk.red(`Failed to add component: ${error}`));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/commands/init.ts
|
|
151
|
+
import path3 from "path";
|
|
152
|
+
import chalk2 from "chalk";
|
|
153
|
+
import fs3 from "fs-extra";
|
|
154
|
+
import ora2 from "ora";
|
|
155
|
+
var DEFAULT_CONFIG = {
|
|
156
|
+
style: "default",
|
|
157
|
+
rsc: false,
|
|
158
|
+
tsx: true,
|
|
159
|
+
tailwind: {
|
|
160
|
+
config: "tailwind.config.js",
|
|
161
|
+
css: "src/index.css",
|
|
162
|
+
baseColor: "slate",
|
|
163
|
+
cssVariables: true
|
|
164
|
+
},
|
|
165
|
+
aliases: {
|
|
166
|
+
components: "@/components",
|
|
167
|
+
utils: "@/lib/utils",
|
|
168
|
+
ui: "@/components/ui",
|
|
169
|
+
draft: "@/components/draft"
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
async function initProject() {
|
|
173
|
+
const cwd = process.cwd();
|
|
174
|
+
const configPath = path3.resolve(cwd, "components.json");
|
|
175
|
+
if (await fs3.pathExists(configPath)) {
|
|
176
|
+
console.log(chalk2.yellow("\ncomponents.json already exists.\n"));
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const spinner = ora2("Initializing project...").start();
|
|
180
|
+
try {
|
|
181
|
+
await fs3.writeJson(configPath, DEFAULT_CONFIG, { spaces: 2 });
|
|
182
|
+
spinner.succeed(chalk2.green("Initialized components.json successfully!"));
|
|
183
|
+
} catch (error) {
|
|
184
|
+
spinner.fail(chalk2.red(`Failed to initialize project: ${error}`));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export {
|
|
189
|
+
addComponent,
|
|
190
|
+
initProject
|
|
191
|
+
};
|
package/dist/react.d.ts
ADDED
package/dist/react.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addComponent,
|
|
3
|
+
initProject
|
|
4
|
+
} from "./chunk-TY6E2FMX.js";
|
|
5
|
+
|
|
6
|
+
// src/bin/react.ts
|
|
7
|
+
import { Command } from "commander";
|
|
8
|
+
var program = new Command();
|
|
9
|
+
program.name("draft-react").description("CLI for adding React components").version("0.0.1");
|
|
10
|
+
program.command("add").description("add a component to your project").argument("<component>", "the component to add").action(async (component) => {
|
|
11
|
+
await addComponent(component, "react");
|
|
12
|
+
});
|
|
13
|
+
program.command("init").description("initialize your project and create a components.json file").action(async () => {
|
|
14
|
+
await initProject();
|
|
15
|
+
});
|
|
16
|
+
program.parse();
|
package/dist/vue.d.ts
ADDED
package/dist/vue.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addComponent,
|
|
3
|
+
initProject
|
|
4
|
+
} from "./chunk-TY6E2FMX.js";
|
|
5
|
+
|
|
6
|
+
// src/bin/vue.ts
|
|
7
|
+
import { Command } from "commander";
|
|
8
|
+
var program = new Command();
|
|
9
|
+
program.name("draft-vue").description("CLI for adding Vue components").version("0.0.1");
|
|
10
|
+
program.command("add").description("add a component to your project").argument("<component>", "the component to add").action(async (component) => {
|
|
11
|
+
await addComponent(component, "vue");
|
|
12
|
+
});
|
|
13
|
+
program.command("init").description("initialize your project and create a components.json file").action(async () => {
|
|
14
|
+
await initProject();
|
|
15
|
+
});
|
|
16
|
+
program.parse();
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "draft-cli",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "A command-line tool for adding components to your Vue and React projects. Inspired by shadcn/ui.",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"ui-kit",
|
|
9
|
+
"vue",
|
|
10
|
+
"react",
|
|
11
|
+
"cli",
|
|
12
|
+
"shadcn"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"homepage": "https://preflower.github.io/draft-ui/",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/preflower/draft-ui.git",
|
|
19
|
+
"directory": "packages/cli"
|
|
20
|
+
},
|
|
21
|
+
"bin": {
|
|
22
|
+
"draft-vue": "./dist/vue.js",
|
|
23
|
+
"draft-react": "./dist/react.js"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public",
|
|
27
|
+
"provenance": true
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"chalk": "^5.3.0",
|
|
31
|
+
"commander": "^11.1.0",
|
|
32
|
+
"execa": "^8.0.1",
|
|
33
|
+
"fs-extra": "^11.2.0",
|
|
34
|
+
"ora": "^7.0.1",
|
|
35
|
+
"prompts": "^2.4.2",
|
|
36
|
+
"zod": "^3.22.4"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/fs-extra": "^11.0.4",
|
|
40
|
+
"@types/node": "^20.10.5",
|
|
41
|
+
"@types/prompts": "^2.4.9",
|
|
42
|
+
"tsup": "^8.0.1",
|
|
43
|
+
"typescript": "^5.3.3"
|
|
44
|
+
},
|
|
45
|
+
"register": "https://raw.githubusercontent.com/preflower/draft-ui/main",
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsup src/bin/vue.ts src/bin/react.ts --format esm --clean --dts",
|
|
48
|
+
"dev": "tsup src/bin/vue.ts src/bin/react.ts --format esm --watch --clean"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/bin/react.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Command } from 'commander'
|
|
2
|
+
|
|
3
|
+
import { addComponent } from '../commands/add.js'
|
|
4
|
+
import { initProject } from '../commands/init.js'
|
|
5
|
+
|
|
6
|
+
const program = new Command()
|
|
7
|
+
|
|
8
|
+
program
|
|
9
|
+
.name('draft-react')
|
|
10
|
+
.description('CLI for adding React components')
|
|
11
|
+
.version('0.0.1')
|
|
12
|
+
|
|
13
|
+
program
|
|
14
|
+
.command('add')
|
|
15
|
+
.description('add a component to your project')
|
|
16
|
+
.argument('<component>', 'the component to add')
|
|
17
|
+
.action(async (component) => {
|
|
18
|
+
await addComponent(component, 'react')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
program
|
|
22
|
+
.command('init')
|
|
23
|
+
.description('initialize your project and create a components.json file')
|
|
24
|
+
.action(async () => {
|
|
25
|
+
await initProject()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
program.parse()
|
package/src/bin/vue.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Command } from 'commander'
|
|
2
|
+
|
|
3
|
+
import { addComponent } from '../commands/add.js'
|
|
4
|
+
import { initProject } from '../commands/init.js'
|
|
5
|
+
|
|
6
|
+
const program = new Command()
|
|
7
|
+
|
|
8
|
+
program
|
|
9
|
+
.name('draft-vue')
|
|
10
|
+
.description('CLI for adding Vue components')
|
|
11
|
+
.version('0.0.1')
|
|
12
|
+
|
|
13
|
+
program
|
|
14
|
+
.command('add')
|
|
15
|
+
.description('add a component to your project')
|
|
16
|
+
.argument('<component>', 'the component to add')
|
|
17
|
+
.action(async (component) => {
|
|
18
|
+
await addComponent(component, 'vue')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
program
|
|
22
|
+
.command('init')
|
|
23
|
+
.description('initialize your project and create a components.json file')
|
|
24
|
+
.action(async () => {
|
|
25
|
+
await initProject()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
program.parse()
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { createRequire } from 'module'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
|
|
4
|
+
import chalk from 'chalk'
|
|
5
|
+
import fs from 'fs-extra'
|
|
6
|
+
import ora from 'ora'
|
|
7
|
+
|
|
8
|
+
import { getConfig } from '../utils/config.js'
|
|
9
|
+
|
|
10
|
+
const require = createRequire(import.meta.url)
|
|
11
|
+
|
|
12
|
+
interface RegistryComponent {
|
|
13
|
+
dependencies: string[]
|
|
14
|
+
files?: string[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface Registry {
|
|
18
|
+
components: Record<string, RegistryComponent>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface PackageJson {
|
|
22
|
+
register?: string
|
|
23
|
+
[key: string]: unknown
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let pkg: PackageJson = {}
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
pkg = require('../../package.json') as PackageJson
|
|
30
|
+
} catch {
|
|
31
|
+
try {
|
|
32
|
+
pkg = require('../package.json') as PackageJson
|
|
33
|
+
} catch {
|
|
34
|
+
// pkg is already {}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function addComponent (componentName: string, type: 'vue' | 'react') {
|
|
39
|
+
const cwd = process.cwd()
|
|
40
|
+
const config = await getConfig(cwd)
|
|
41
|
+
|
|
42
|
+
if (!config) {
|
|
43
|
+
console.log(chalk.red('\nNo components.json found. Please run init first (simulated).\n'))
|
|
44
|
+
// For now, we'll just use defaults if no config is found to demonstrate
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const spinner = ora(`Adding ${componentName}...`).start()
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
// Determine source path within monorepo
|
|
52
|
+
// In a real scenario, this would fetch from a registry or use a relative path in dev
|
|
53
|
+
// For this demo, we'll assume the CLI is run from the monorepo root or we know where the packages are
|
|
54
|
+
const monorepoRoot = path.resolve(import.meta.url.replace('file://', ''), '../../../../')
|
|
55
|
+
const sourceDir = path.resolve(
|
|
56
|
+
monorepoRoot,
|
|
57
|
+
'packages',
|
|
58
|
+
type,
|
|
59
|
+
'src',
|
|
60
|
+
'components',
|
|
61
|
+
componentName
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
if (!(await fs.pathExists(sourceDir))) {
|
|
65
|
+
if (typeof pkg.register === 'string') {
|
|
66
|
+
spinner.text = `Checking registry ${pkg.register}...`
|
|
67
|
+
try {
|
|
68
|
+
const registryUrl = `${pkg.register}/packages/${type}/src/registry.json`
|
|
69
|
+
const registryRes = await fetch(registryUrl)
|
|
70
|
+
if (!registryRes.ok) {
|
|
71
|
+
throw new Error(`Failed to fetch registry from ${registryUrl}: ${registryRes.statusText}`)
|
|
72
|
+
}
|
|
73
|
+
const registry = (await registryRes.json()) as Registry
|
|
74
|
+
const componentMeta = registry.components[componentName]
|
|
75
|
+
|
|
76
|
+
if (componentMeta == null) {
|
|
77
|
+
spinner.fail(chalk.red(`Component ${componentName} not found in registry.`))
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!componentMeta.files || !Array.isArray(componentMeta.files)) {
|
|
82
|
+
spinner.fail(chalk.red(`Component ${componentName} has no files listed in registry.`))
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
spinner.text = `Fetching ${componentName} files...`
|
|
87
|
+
|
|
88
|
+
const titleCaseComponentName = componentName.charAt(0).toUpperCase() + componentName.slice(1)
|
|
89
|
+
|
|
90
|
+
// Determine target path using config aliases
|
|
91
|
+
// Use the ui alias if available, otherwise fallback to components
|
|
92
|
+
const targetBase = (config.aliases.draft ?? config.aliases.ui ?? config.aliases.components).replace('@/', 'src/')
|
|
93
|
+
const targetDir = path.resolve(cwd, targetBase, titleCaseComponentName) // Use TitleCase for directory
|
|
94
|
+
|
|
95
|
+
await fs.ensureDir(targetDir)
|
|
96
|
+
|
|
97
|
+
for (const file of componentMeta.files) {
|
|
98
|
+
const fileUrl = `${pkg.register}/packages/${type}/src/components/${componentName}/${file}`
|
|
99
|
+
const fileRes = await fetch(fileUrl)
|
|
100
|
+
if (!fileRes.ok) {
|
|
101
|
+
throw new Error(`Failed to fetch file ${file}: ${fileRes.statusText}`)
|
|
102
|
+
}
|
|
103
|
+
const content = await fileRes.text()
|
|
104
|
+
await fs.writeFile(path.resolve(targetDir, file), content)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Handle dependencies
|
|
108
|
+
if (componentMeta.dependencies != null && componentMeta.dependencies.length > 0) {
|
|
109
|
+
spinner.text = `Installing dependencies for ${componentName}: ${componentMeta.dependencies.join(', ')}...`
|
|
110
|
+
const { execa } = await import('execa')
|
|
111
|
+
try {
|
|
112
|
+
await execa('pnpm', ['add', ...componentMeta.dependencies], { cwd })
|
|
113
|
+
} catch (err) {
|
|
114
|
+
spinner.warn(chalk.yellow(`Component added, but failed to install dependencies: ${(err as Error).message}`))
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
spinner.succeed(chalk.green(`Component ${componentName} added from registry.`))
|
|
119
|
+
return
|
|
120
|
+
} catch (error) {
|
|
121
|
+
spinner.fail(chalk.red(`Failed to fetch from registry: ${(error as Error).message}`))
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
spinner.fail(chalk.red(`Component ${componentName} not found for ${type}.`))
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Determine target path using config aliases
|
|
131
|
+
// Use the ui alias if available, otherwise fallback to components
|
|
132
|
+
const targetBase = (config.aliases.draft ?? config.aliases.ui ?? config.aliases.components).replace('@/', 'src/')
|
|
133
|
+
const targetDir = path.resolve(cwd, targetBase, componentName)
|
|
134
|
+
|
|
135
|
+
await fs.ensureDir(targetDir)
|
|
136
|
+
await fs.copy(sourceDir, targetDir)
|
|
137
|
+
|
|
138
|
+
// Handle dependencies from registry.json
|
|
139
|
+
const registryPath = path.resolve(monorepoRoot, 'packages', type, 'src', 'registry.json')
|
|
140
|
+
if (await fs.pathExists(registryPath)) {
|
|
141
|
+
const registry = await fs.readJson(registryPath)
|
|
142
|
+
const componentMeta = registry.components[componentName]
|
|
143
|
+
|
|
144
|
+
if (componentMeta?.dependencies?.length > 0) {
|
|
145
|
+
spinner.text = `Installing dependencies for ${componentName}: ${componentMeta.dependencies.join(', ')}...`
|
|
146
|
+
|
|
147
|
+
// Detect package manager (assuming pnpm for now as per project requirements)
|
|
148
|
+
const { execa } = await import('execa')
|
|
149
|
+
try {
|
|
150
|
+
await execa('pnpm', ['add', ...componentMeta.dependencies], { cwd })
|
|
151
|
+
spinner.succeed(chalk.green(`Component ${componentName} and its dependencies added successfully.`))
|
|
152
|
+
return
|
|
153
|
+
} catch (err) {
|
|
154
|
+
spinner.warn(chalk.yellow(`Component copied, but failed to install dependencies: ${(err as Error).message}`))
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
spinner.succeed(chalk.green(`Component ${componentName} added successfully to ${targetDir}`))
|
|
161
|
+
} catch (error) {
|
|
162
|
+
spinner.fail(chalk.red(`Failed to add component: ${error}`))
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
|
|
3
|
+
import chalk from 'chalk'
|
|
4
|
+
import fs from 'fs-extra'
|
|
5
|
+
import ora from 'ora'
|
|
6
|
+
|
|
7
|
+
const DEFAULT_CONFIG = {
|
|
8
|
+
style: 'default',
|
|
9
|
+
rsc: false,
|
|
10
|
+
tsx: true,
|
|
11
|
+
tailwind: {
|
|
12
|
+
config: 'tailwind.config.js',
|
|
13
|
+
css: 'src/index.css',
|
|
14
|
+
baseColor: 'slate',
|
|
15
|
+
cssVariables: true
|
|
16
|
+
},
|
|
17
|
+
aliases: {
|
|
18
|
+
components: '@/components',
|
|
19
|
+
utils: '@/lib/utils',
|
|
20
|
+
ui: '@/components/ui',
|
|
21
|
+
draft: '@/components/draft'
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function initProject () {
|
|
26
|
+
const cwd = process.cwd()
|
|
27
|
+
const configPath = path.resolve(cwd, 'components.json')
|
|
28
|
+
|
|
29
|
+
if (await fs.pathExists(configPath)) {
|
|
30
|
+
console.log(chalk.yellow('\ncomponents.json already exists.\n'))
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const spinner = ora('Initializing project...').start()
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
await fs.writeJson(configPath, DEFAULT_CONFIG, { spaces: 2 })
|
|
38
|
+
spinner.succeed(chalk.green('Initialized components.json successfully!'))
|
|
39
|
+
} catch (error) {
|
|
40
|
+
spinner.fail(chalk.red(`Failed to initialize project: ${error}`))
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
|
|
3
|
+
import fs from 'fs-extra'
|
|
4
|
+
import { z } from 'zod'
|
|
5
|
+
|
|
6
|
+
export const configSchema = z.object({
|
|
7
|
+
style: z.string(),
|
|
8
|
+
rsc: z.boolean(),
|
|
9
|
+
tsx: z.boolean(),
|
|
10
|
+
tailwind: z.object({
|
|
11
|
+
config: z.string(),
|
|
12
|
+
css: z.string(),
|
|
13
|
+
baseColor: z.string(),
|
|
14
|
+
cssVariables: z.boolean()
|
|
15
|
+
}),
|
|
16
|
+
aliases: z.object({
|
|
17
|
+
components: z.string(),
|
|
18
|
+
utils: z.string(),
|
|
19
|
+
ui: z.string().optional(),
|
|
20
|
+
draft: z.string().optional()
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
export type Config = z.infer<typeof configSchema>;
|
|
25
|
+
|
|
26
|
+
export async function getConfig (cwd: string) {
|
|
27
|
+
const configPath = path.resolve(cwd, 'components.json')
|
|
28
|
+
if (!(await fs.pathExists(configPath))) {
|
|
29
|
+
return null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const config = await fs.readJson(configPath)
|
|
34
|
+
return configSchema.parse(config)
|
|
35
|
+
} catch (error) {
|
|
36
|
+
throw new Error(`Invalid configuration: ${error}`)
|
|
37
|
+
}
|
|
38
|
+
}
|