@seed-design/cli 0.0.0-alpha-20241014082802 → 0.0.0-alpha-20241014090620
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.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{cosmiconfig as Z}from"cosmiconfig";import
|
|
3
|
-
`,"utf-8");let S=
|
|
2
|
+
import{cosmiconfig as Z}from"cosmiconfig";import $ from"path";import{z as u}from"zod";var k="seed-design",ee=Z(k,{searchPlaces:[`${k}.json`]}),M=u.object({$schema:u.string().optional(),rsc:u.coerce.boolean().default(!1),tsx:u.coerce.boolean().default(!0),css:u.coerce.boolean().default(!0),path:u.string()}).strict(),te=M.extend({resolvedUIPaths:u.string()});async function O(e){let t=await re(e);return t?await oe(e,t):null}async function oe(e,t){let r=$.resolve(e,t.path);return te.parse({...t,resolvedUIPaths:$.join(r,"ui")})}async function re(e){try{let t=await ee.search(e);return t?M.parse(t.config):null}catch(t){throw console.log(t),new Error(`Invalid configuration found in ${e}/seed-design.json.`)}}import{z as s}from"zod";var A=s.object({name:s.string(),description:s.string().optional(),dependencies:s.array(s.string()).optional(),devDependencies:s.array(s.string()).optional(),innerDependencies:s.array(s.string()).optional(),files:s.array(s.string())}),E=s.array(A),ne=A.omit({files:!0}),se=ne.extend({registries:s.array(s.object({name:s.string(),content:s.string()}))}),De=s.array(se);var I="https://component.seed-design.io";async function R(e){try{return await Promise.all(e.map(async r=>await(await fetch(`${I}/__registry__/component/${r}.json`)).json()))}catch(t){throw console.log(t),new Error(`Failed to fetch registry from ${I}.`)}}async function D(){try{let[e]=await R(["index"]);return E.parse(e)}catch(e){throw console.log(e),new Error(`Failed to fetch components from ${I}.`)}}import{detect as ie}from"@antfu/ni";async function F(e){let t=await ie({programmatic:!0,cwd:e});return t==="yarn@berry"?"yarn":t==="pnpm@6"?"pnpm":t==="bun"?"bun":t??"npm"}import{promises as le}from"fs";import{tmpdir as de}from"os";import J from"path";import{transformFromAstSync as ae}from"@babel/core";import ce from"@babel/plugin-transform-typescript";import*as v from"recast";import{parse as pe}from"@babel/parser";var me={sourceType:"module",allowImportExportEverywhere:!0,allowReturnOutsideFunction:!0,startLine:1,tokens:!0,plugins:["asyncGenerators","bigInt","classPrivateMethods","classPrivateProperties","classProperties","classStaticBlock","decimal","decorators-legacy","doExpressions","dynamicImport","exportDefaultFrom","exportNamespaceFrom","functionBind","functionSent","importAssertions","importMeta","nullishCoalescingOperator","numericSeparator","objectRestSpread","optionalCatchBinding","optionalChaining",["pipelineOperator",{proposal:"minimal"}],["recordAndTuple",{syntaxType:"hash"}],"throwExpressions","topLevelAwait","v8intrinsic","typescript","jsx"]},G=async({sourceFile:e,config:t})=>{let r=e.getFullText();if(t.tsx)return r;let n=v.parse(r,{parser:{parse:c=>pe(c,me)}}),o=ae(n,r,{cloneInputAst:!1,code:!1,ast:!0,plugins:[ce],configFile:!1});if(!o||!o.ast)throw new Error("Failed to transform JSX");return v.print(o.ast).code};import{SyntaxKind as fe}from"ts-morph";var z=async({sourceFile:e,config:t})=>{if(t.rsc)return e;let r=e.getFirstChildByKind(fe.ExpressionStatement);return r?.getText()==='"use client";'&&r.remove(),e};var N=async({sourceFile:e,config:t})=>{if(t.css)return e;let n=e.getImportDeclarations().filter(o=>o.getModuleSpecifierValue().endsWith(".css"));for(let o of n)o.remove();return e};import{Project as ge,ScriptKind as ue}from"ts-morph";var ye=[z,N],he=new ge({compilerOptions:{}});async function Ce(e){let t=await le.mkdtemp(J.join(de(),"seed-deisgn-"));return J.join(t,e)}async function _(e){let t=await Ce(e.filename),r=he.createSourceFile(t,e.raw,{scriptKind:ue.TSX});for(let n of ye)n({sourceFile:r,...e});return await G({sourceFile:r,...e})}import*as i from"@clack/prompts";import{execa as U}from"execa";import P from"fs-extra";import B from"path";import j from"picocolors";import{z as x}from"zod";function V(e,t){let r=new Set;function n(o){if(r.has(o))return;r.add(o);let c=t.find(f=>f.name===o);if(c&&c.innerDependencies)for(let f of c.innerDependencies)n(f)}for(let o of e)n(o);return Array.from(r)}var xe=x.object({components:x.array(x.string()).optional(),cwd:x.string(),all:x.boolean()}),K=e=>{e.command("add [...components]","add component").option("-a, --all","Add all components",{default:!1}).option("-c, --cwd <cwd>","the working directory. defaults to the current directory.",{default:process.cwd()}).example("seed-design add box-button").example("seed-design add alert-dialog").action(async(t,r)=>{let n=xe.parse({components:t,...r}),o=a=>j.cyan(a),c=n.cwd;P.existsSync(c)||(i.log.error(`The path ${c} does not exist. Please try again.`),process.exit(1));let f=await D(),d=n.all?f.map(a=>a.name):n.components;if(!n.components?.length&&!n.all){let a=await i.multiselect({message:"Select all components to add",options:f.map(l=>({label:l.name,value:l.name,hint:l.description}))});i.isCancel(a)&&(i.log.error("Aborted."),process.exit(0)),d=a}d?.length||(i.log.error("No components found."),process.exit(0));let h=V(d,f),S=h.filter(a=>!d.includes(a)),C=await O(c),H=await R(h);i.log.message(`Selection: ${o(d.join(", "))}`),S.length&&i.log.message(`Inner Dependencies: ${o(S.join(", "))} will be also added.`);for(let a of H){for(let m of a.registries){let g=C.resolvedUIPaths;P.existsSync(g)||await P.mkdir(g,{recursive:!0});let y=B.resolve(g,m.name),Q=await _({filename:m.name,config:C,raw:m.content});C.tsx||(y=y.replace(/\.tsx$/,".jsx"),y=y.replace(/\.ts$/,".js")),await P.writeFile(y,Q);let Y=B.relative(c,y);i.log.info(`Added ${o(m.name)} to ${o(Y)}`)}let l=await F(c),{start:b,stop:T}=i.spinner();if(a.dependencies?.length){b(j.gray("Installing dependencies"));let m=await U(l,[l==="npm"?"install":"add",...a.dependencies],{cwd:c});if(m.failed)console.error(m.all),process.exit(1);else{for(let g of a.dependencies)i.log.info(`- ${g}`);T("Dependencies installed.")}}if(a.devDependencies?.length){b(j.gray("Installing devDependencies"));let m=await U(l,[l==="npm"?"install":"add","-D",...a.devDependencies],{cwd:c});if(m.failed)console.error(m.all),process.exit(1);else{for(let g of a.devDependencies)i.log.info(`- ${g}`);T("Dependencies installed.")}}}i.outro("Components added.")})};import we from"findup-sync";import Se from"fs-extra";var ve="package.json";function Pe(){let e=we(ve);if(!e)throw new Error("No package.json file found in the project.");return e}function L(){return Se.readJSONSync(Pe())}import{cac as be}from"cac";import*as p from"@clack/prompts";import Ie from"fs-extra";import W from"path";import Re from"picocolors";import{z as X}from"zod";var je=X.object({cwd:X.string()}),q=e=>{e.command("init","initialize seed-design.json").option("-c, --cwd <cwd>","the working directory. defaults to the current directory.",{default:process.cwd()}).action(async t=>{let r=je.parse({...t}),n=C=>Re.cyan(C),o=await p.group({tsx:()=>p.confirm({message:`Would you like to use ${n("TypeScript")} (recommended)?`,initialValue:!0}),rsc:()=>p.confirm({message:`Are you using ${n("React Server Components")}?`,initialValue:!1}),css:()=>p.confirm({message:`Would you like to use ${n("CSS Modules")}? (If true, CSS import will be added in components)`,initialValue:!0}),path:()=>p.text({message:`Enter the path to your ${n("seed-design directory")}`,initialValue:"./seed-design",defaultValue:"./seed-design",placeholder:"./seed-design"})},{onCancel:()=>{p.cancel("Operation cancelled."),process.exit(0)}}),c={rsc:o.rsc,tsx:o.tsx,css:o.css,path:o.path},{start:f,stop:d}=p.spinner();f("Writing seed-design.json...");let h=W.resolve(r.cwd,"seed-design.json");await Ie.writeFile(h,`${JSON.stringify(c,null,2)}
|
|
3
|
+
`,"utf-8");let S=W.relative(process.cwd(),h);d(`seed-design.json written to ${n(S)}`)})};var Te="seed-design",w=be(Te);async function $e(){let e=L();K(w),q(w),w.version(e.version||"1.0.0","-v, --version"),w.help(),w.parse()}$e();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seed-design/cli",
|
|
3
|
-
"version": "0.0.0-alpha-
|
|
3
|
+
"version": "0.0.0-alpha-20241014090620",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"node": ">=18"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
|
+
"prepack": "yarn build",
|
|
22
23
|
"build": "ENV=prod node ./build.mjs",
|
|
23
24
|
"dev": "ENV=dev node ./dev.mjs",
|
|
24
25
|
"test": "yarn vitest"
|
package/src/commands/add.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getConfig } from "@/src/utils/get-config";
|
|
2
|
-
import {
|
|
2
|
+
import { fetchRegistryComponentItem, getRegistryComponentIndex } from "@/src/utils/get-metadata";
|
|
3
3
|
import { getPackageManager } from "@/src/utils/get-package-manager";
|
|
4
4
|
import { transform } from "@/src/utils/transformers";
|
|
5
5
|
import * as p from "@clack/prompts";
|
|
@@ -45,10 +45,10 @@ export const addCommand = (cli: CAC) => {
|
|
|
45
45
|
process.exit(1);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
const
|
|
48
|
+
const registryComponentIndex = await getRegistryComponentIndex();
|
|
49
49
|
|
|
50
50
|
let selectedComponents: string[] = options.all
|
|
51
|
-
?
|
|
51
|
+
? registryComponentIndex.map((registry) => registry.name)
|
|
52
52
|
: options.components;
|
|
53
53
|
|
|
54
54
|
if (!options.components?.length && !options.all) {
|
|
@@ -57,7 +57,7 @@ export const addCommand = (cli: CAC) => {
|
|
|
57
57
|
string
|
|
58
58
|
>({
|
|
59
59
|
message: "Select all components to add",
|
|
60
|
-
options:
|
|
60
|
+
options: registryComponentIndex.map((metadata) => {
|
|
61
61
|
return {
|
|
62
62
|
label: metadata.name,
|
|
63
63
|
value: metadata.name,
|
|
@@ -79,10 +79,10 @@ export const addCommand = (cli: CAC) => {
|
|
|
79
79
|
process.exit(0);
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
const allComponents = addRelativeComponents(selectedComponents,
|
|
82
|
+
const allComponents = addRelativeComponents(selectedComponents, registryComponentIndex);
|
|
83
83
|
const addedComponents = allComponents.filter((c) => !selectedComponents.includes(c));
|
|
84
84
|
const config = await getConfig(cwd);
|
|
85
|
-
const
|
|
85
|
+
const registryComponentItems = await fetchRegistryComponentItem(allComponents);
|
|
86
86
|
|
|
87
87
|
p.log.message(`Selection: ${highlight(selectedComponents.join(", "))}`);
|
|
88
88
|
if (addedComponents.length) {
|
|
@@ -91,8 +91,8 @@ export const addCommand = (cli: CAC) => {
|
|
|
91
91
|
);
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
for (const
|
|
95
|
-
for (const registry of
|
|
94
|
+
for (const component of registryComponentItems) {
|
|
95
|
+
for (const registry of component.registries) {
|
|
96
96
|
const UIFolderPath = config.resolvedUIPaths;
|
|
97
97
|
|
|
98
98
|
if (!fs.existsSync(UIFolderPath)) {
|
|
@@ -122,12 +122,12 @@ export const addCommand = (cli: CAC) => {
|
|
|
122
122
|
const { start, stop } = p.spinner();
|
|
123
123
|
|
|
124
124
|
// Install dependencies.
|
|
125
|
-
if (
|
|
125
|
+
if (component.dependencies?.length) {
|
|
126
126
|
start(color.gray("Installing dependencies"));
|
|
127
127
|
|
|
128
128
|
const result = await execa(
|
|
129
129
|
packageManager,
|
|
130
|
-
[packageManager === "npm" ? "install" : "add", ...
|
|
130
|
+
[packageManager === "npm" ? "install" : "add", ...component.dependencies],
|
|
131
131
|
{
|
|
132
132
|
cwd,
|
|
133
133
|
},
|
|
@@ -137,7 +137,7 @@ export const addCommand = (cli: CAC) => {
|
|
|
137
137
|
console.error(result.all);
|
|
138
138
|
process.exit(1);
|
|
139
139
|
} else {
|
|
140
|
-
for (const deps of
|
|
140
|
+
for (const deps of component.dependencies) {
|
|
141
141
|
p.log.info(`- ${deps}`);
|
|
142
142
|
}
|
|
143
143
|
stop("Dependencies installed.");
|
|
@@ -145,12 +145,12 @@ export const addCommand = (cli: CAC) => {
|
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
// Install devDependencies.
|
|
148
|
-
if (
|
|
148
|
+
if (component.devDependencies?.length) {
|
|
149
149
|
start(color.gray("Installing devDependencies"));
|
|
150
150
|
|
|
151
151
|
const result = await execa(
|
|
152
152
|
packageManager,
|
|
153
|
-
[packageManager === "npm" ? "install" : "add", "-D", ...
|
|
153
|
+
[packageManager === "npm" ? "install" : "add", "-D", ...component.devDependencies],
|
|
154
154
|
{
|
|
155
155
|
cwd,
|
|
156
156
|
},
|
|
@@ -160,7 +160,7 @@ export const addCommand = (cli: CAC) => {
|
|
|
160
160
|
console.error(result.all);
|
|
161
161
|
process.exit(1);
|
|
162
162
|
} else {
|
|
163
|
-
for (const deps of
|
|
163
|
+
for (const deps of component.devDependencies) {
|
|
164
164
|
p.log.info(`- ${deps}`);
|
|
165
165
|
}
|
|
166
166
|
stop("Dependencies installed.");
|
package/src/schema.ts
CHANGED
|
@@ -2,21 +2,43 @@ import { z } from "zod";
|
|
|
2
2
|
|
|
3
3
|
// TODO: Extract this to a shared package.
|
|
4
4
|
// INFO: also used in component-docs
|
|
5
|
-
export const
|
|
5
|
+
export const registryComponentItemSchema = z.object({
|
|
6
|
+
/**
|
|
7
|
+
* @description 컴포넌트 이름
|
|
8
|
+
* @example chip-tabs, alert-dialog
|
|
9
|
+
*/
|
|
6
10
|
name: z.string(),
|
|
11
|
+
|
|
7
12
|
description: z.string().optional(),
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @description 컴포넌트 의존성
|
|
16
|
+
* @example @seed-design/react-tabs
|
|
17
|
+
*/
|
|
8
18
|
dependencies: z.array(z.string()).optional(),
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @description 컴포넌트 개발 의존성
|
|
22
|
+
*/
|
|
9
23
|
devDependencies: z.array(z.string()).optional(),
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @description 컴포넌트 내부의 Seed Design 컴포넌트 의존성
|
|
27
|
+
* @example action-button
|
|
28
|
+
*/
|
|
10
29
|
innerDependencies: z.array(z.string()).optional(),
|
|
11
|
-
snippets: z.array(z.string()),
|
|
12
|
-
type: z.enum(["component"]),
|
|
13
|
-
});
|
|
14
30
|
|
|
15
|
-
|
|
31
|
+
/**
|
|
32
|
+
* @description 컴포넌트 코드 스니펫 경로, 여러 파일이 될 수 있어서 배열로 정의
|
|
33
|
+
* @example component/alert-dialog.tsx
|
|
34
|
+
*/
|
|
35
|
+
files: z.array(z.string()),
|
|
36
|
+
});
|
|
16
37
|
|
|
17
|
-
const
|
|
38
|
+
export const registryComponentSchema = z.array(registryComponentItemSchema);
|
|
18
39
|
|
|
19
|
-
|
|
40
|
+
const omittedRegistryComponentSchema = registryComponentItemSchema.omit({ files: true });
|
|
41
|
+
export const registryComponentItemMachineGeneratedSchema = omittedRegistryComponentSchema.extend({
|
|
20
42
|
registries: z.array(
|
|
21
43
|
z.object({
|
|
22
44
|
name: z.string(),
|
|
@@ -25,10 +47,15 @@ export const componentMetadataSchemaWithRegistry = omittedComponentMetadataIndex
|
|
|
25
47
|
),
|
|
26
48
|
});
|
|
27
49
|
|
|
28
|
-
export const
|
|
50
|
+
export const registryComponentMachineGeneratedSchema = z.array(
|
|
51
|
+
registryComponentItemMachineGeneratedSchema,
|
|
52
|
+
);
|
|
29
53
|
|
|
30
|
-
export type
|
|
31
|
-
export type
|
|
32
|
-
export type
|
|
33
|
-
typeof
|
|
54
|
+
export type RegistryComponentItem = z.infer<typeof registryComponentItemSchema>;
|
|
55
|
+
export type RegistryComponent = z.infer<typeof registryComponentSchema>;
|
|
56
|
+
export type RegistryComponentItemMachineGenerated = z.infer<
|
|
57
|
+
typeof registryComponentItemMachineGeneratedSchema
|
|
58
|
+
>;
|
|
59
|
+
export type RegistryComponentMachineGenerated = z.infer<
|
|
60
|
+
typeof registryComponentMachineGeneratedSchema
|
|
34
61
|
>;
|
|
@@ -1,36 +1,31 @@
|
|
|
1
1
|
import { describe, expect, test } from "vitest";
|
|
2
2
|
import { addRelativeComponents } from "../utils/add-relative-components";
|
|
3
|
-
import type {
|
|
3
|
+
import type { RegistryComponent } from "@/src/schema";
|
|
4
4
|
|
|
5
|
-
const config:
|
|
5
|
+
const config: RegistryComponent = [
|
|
6
6
|
{
|
|
7
7
|
name: "a",
|
|
8
|
-
|
|
9
|
-
type: "component",
|
|
8
|
+
files: ["a.tsx"],
|
|
10
9
|
},
|
|
11
10
|
{
|
|
12
11
|
name: "b",
|
|
13
12
|
innerDependencies: ["a"],
|
|
14
|
-
|
|
15
|
-
type: "component",
|
|
13
|
+
files: ["b.tsx"],
|
|
16
14
|
},
|
|
17
15
|
{
|
|
18
16
|
name: "c",
|
|
19
17
|
innerDependencies: ["b"],
|
|
20
|
-
|
|
21
|
-
type: "component",
|
|
18
|
+
files: ["c.tsx"],
|
|
22
19
|
},
|
|
23
20
|
{
|
|
24
21
|
name: "d",
|
|
25
22
|
innerDependencies: ["a", "b"],
|
|
26
|
-
|
|
27
|
-
type: "component",
|
|
23
|
+
files: ["d.tsx"],
|
|
28
24
|
},
|
|
29
25
|
{
|
|
30
26
|
name: "e",
|
|
31
27
|
innerDependencies: ["d"],
|
|
32
|
-
|
|
33
|
-
type: "component",
|
|
28
|
+
files: ["d.tsx"],
|
|
34
29
|
},
|
|
35
30
|
];
|
|
36
31
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RegistryComponentMachineGenerated } from "@/src/schema";
|
|
2
2
|
|
|
3
3
|
export function addRelativeComponents(
|
|
4
4
|
userSelects: string[],
|
|
5
|
-
|
|
5
|
+
registryIndex: RegistryComponentMachineGenerated,
|
|
6
6
|
) {
|
|
7
7
|
const selectedComponents = new Set<string>();
|
|
8
8
|
|
|
@@ -11,7 +11,7 @@ export function addRelativeComponents(
|
|
|
11
11
|
|
|
12
12
|
selectedComponents.add(componentName);
|
|
13
13
|
|
|
14
|
-
const component =
|
|
14
|
+
const component = registryIndex.find((c) => c.name === componentName);
|
|
15
15
|
if (!component) return;
|
|
16
16
|
|
|
17
17
|
if (component.innerDependencies) {
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
componentMetadataIndexSchema,
|
|
3
|
-
type ComponentMetadataWithRegistrySchema,
|
|
4
|
-
} from "@/src/schema";
|
|
1
|
+
import { registryComponentSchema, type RegistryComponentMachineGenerated } from "@/src/schema";
|
|
5
2
|
|
|
6
3
|
const BASE_URL =
|
|
7
4
|
process.env.NODE_ENV === "prod" ? "https://component.seed-design.io" : "http://localhost:3000";
|
|
8
5
|
|
|
9
|
-
export async function
|
|
6
|
+
export async function fetchRegistryComponentItem(
|
|
10
7
|
fileNames?: string[],
|
|
11
|
-
): Promise<
|
|
8
|
+
): Promise<RegistryComponentMachineGenerated> {
|
|
12
9
|
try {
|
|
13
10
|
const results = await Promise.all(
|
|
14
11
|
fileNames.map(async (fileName) => {
|
|
@@ -24,11 +21,11 @@ export async function fetchComponentMetadatas(
|
|
|
24
21
|
}
|
|
25
22
|
}
|
|
26
23
|
|
|
27
|
-
export async function
|
|
24
|
+
export async function getRegistryComponentIndex() {
|
|
28
25
|
try {
|
|
29
|
-
const [result] = await
|
|
26
|
+
const [result] = await fetchRegistryComponentItem(["index"]);
|
|
30
27
|
|
|
31
|
-
return
|
|
28
|
+
return registryComponentSchema.parse(result);
|
|
32
29
|
} catch (error) {
|
|
33
30
|
console.log(error);
|
|
34
31
|
throw new Error(`Failed to fetch components from ${BASE_URL}.`);
|