@seed-design/cli 0.0.0-alpha-20241014090450 → 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 +2 -502
- package/package.json +2 -1
package/bin/index.mjs
CHANGED
|
@@ -1,503 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { cosmiconfig } from "cosmiconfig";
|
|
5
|
-
import path from "path";
|
|
6
|
-
import { z } from "zod";
|
|
7
|
-
var MODULE_NAME = "seed-design";
|
|
8
|
-
var explorer = cosmiconfig(MODULE_NAME, {
|
|
9
|
-
searchPlaces: [`${MODULE_NAME}.json`]
|
|
10
|
-
});
|
|
11
|
-
var rawConfigSchema = z.object({
|
|
12
|
-
$schema: z.string().optional(),
|
|
13
|
-
rsc: z.coerce.boolean().default(false),
|
|
14
|
-
tsx: z.coerce.boolean().default(true),
|
|
15
|
-
css: z.coerce.boolean().default(true),
|
|
16
|
-
path: z.string()
|
|
17
|
-
}).strict();
|
|
18
|
-
var configSchema = rawConfigSchema.extend({
|
|
19
|
-
resolvedUIPaths: z.string()
|
|
20
|
-
});
|
|
21
|
-
async function getConfig(cwd) {
|
|
22
|
-
const config = await getRawConfig(cwd);
|
|
23
|
-
if (!config) {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
return await resolveConfigPaths(cwd, config);
|
|
27
|
-
}
|
|
28
|
-
async function resolveConfigPaths(cwd, config) {
|
|
29
|
-
const seedComponentRootPath = path.resolve(cwd, config.path);
|
|
30
|
-
return configSchema.parse({
|
|
31
|
-
...config,
|
|
32
|
-
resolvedUIPaths: path.join(seedComponentRootPath, "ui")
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
async function getRawConfig(cwd) {
|
|
36
|
-
try {
|
|
37
|
-
const configResult = await explorer.search(cwd);
|
|
38
|
-
if (!configResult) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
return rawConfigSchema.parse(configResult.config);
|
|
42
|
-
} catch (error) {
|
|
43
|
-
console.log(error);
|
|
44
|
-
throw new Error(`Invalid configuration found in ${cwd}/seed-design.json.`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// src/schema.ts
|
|
49
|
-
import { z as z2 } from "zod";
|
|
50
|
-
var registryComponentItemSchema = z2.object({
|
|
51
|
-
/**
|
|
52
|
-
* @description 컴포넌트 이름
|
|
53
|
-
* @example chip-tabs, alert-dialog
|
|
54
|
-
*/
|
|
55
|
-
name: z2.string(),
|
|
56
|
-
description: z2.string().optional(),
|
|
57
|
-
/**
|
|
58
|
-
* @description 컴포넌트 의존성
|
|
59
|
-
* @example @seed-design/react-tabs
|
|
60
|
-
*/
|
|
61
|
-
dependencies: z2.array(z2.string()).optional(),
|
|
62
|
-
/**
|
|
63
|
-
* @description 컴포넌트 개발 의존성
|
|
64
|
-
*/
|
|
65
|
-
devDependencies: z2.array(z2.string()).optional(),
|
|
66
|
-
/**
|
|
67
|
-
* @description 컴포넌트 내부의 Seed Design 컴포넌트 의존성
|
|
68
|
-
* @example action-button
|
|
69
|
-
*/
|
|
70
|
-
innerDependencies: z2.array(z2.string()).optional(),
|
|
71
|
-
/**
|
|
72
|
-
* @description 컴포넌트 코드 스니펫 경로, 여러 파일이 될 수 있어서 배열로 정의
|
|
73
|
-
* @example component/alert-dialog.tsx
|
|
74
|
-
*/
|
|
75
|
-
files: z2.array(z2.string())
|
|
76
|
-
});
|
|
77
|
-
var registryComponentSchema = z2.array(registryComponentItemSchema);
|
|
78
|
-
var omittedRegistryComponentSchema = registryComponentItemSchema.omit({ files: true });
|
|
79
|
-
var registryComponentItemMachineGeneratedSchema = omittedRegistryComponentSchema.extend({
|
|
80
|
-
registries: z2.array(
|
|
81
|
-
z2.object({
|
|
82
|
-
name: z2.string(),
|
|
83
|
-
content: z2.string()
|
|
84
|
-
})
|
|
85
|
-
)
|
|
86
|
-
});
|
|
87
|
-
var registryComponentMachineGeneratedSchema = z2.array(
|
|
88
|
-
registryComponentItemMachineGeneratedSchema
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
// src/utils/get-metadata.ts
|
|
92
|
-
var BASE_URL = false ? "https://component.seed-design.io" : "http://localhost:3000";
|
|
93
|
-
async function fetchRegistryComponentItem(fileNames) {
|
|
94
|
-
try {
|
|
95
|
-
const results = await Promise.all(
|
|
96
|
-
fileNames.map(async (fileName) => {
|
|
97
|
-
const response = await fetch(`${BASE_URL}/__registry__/component/${fileName}.json`);
|
|
98
|
-
return await response.json();
|
|
99
|
-
})
|
|
100
|
-
);
|
|
101
|
-
return results;
|
|
102
|
-
} catch (error) {
|
|
103
|
-
console.log(error);
|
|
104
|
-
throw new Error(`Failed to fetch registry from ${BASE_URL}.`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
async function getRegistryComponentIndex() {
|
|
108
|
-
try {
|
|
109
|
-
const [result] = await fetchRegistryComponentItem(["index"]);
|
|
110
|
-
return registryComponentSchema.parse(result);
|
|
111
|
-
} catch (error) {
|
|
112
|
-
console.log(error);
|
|
113
|
-
throw new Error(`Failed to fetch components from ${BASE_URL}.`);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// src/utils/get-package-manager.ts
|
|
118
|
-
import { detect } from "@antfu/ni";
|
|
119
|
-
async function getPackageManager(targetDir) {
|
|
120
|
-
const packageManager = await detect({ programmatic: true, cwd: targetDir });
|
|
121
|
-
if (packageManager === "yarn@berry")
|
|
122
|
-
return "yarn";
|
|
123
|
-
if (packageManager === "pnpm@6")
|
|
124
|
-
return "pnpm";
|
|
125
|
-
if (packageManager === "bun")
|
|
126
|
-
return "bun";
|
|
127
|
-
return packageManager ?? "npm";
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// src/utils/transformers/index.ts
|
|
131
|
-
import { promises as fs } from "fs";
|
|
132
|
-
import { tmpdir } from "os";
|
|
133
|
-
import path2 from "path";
|
|
134
|
-
|
|
135
|
-
// src/utils/transformers/transform-jsx.ts
|
|
136
|
-
import { transformFromAstSync } from "@babel/core";
|
|
137
|
-
import transformTypescript from "@babel/plugin-transform-typescript";
|
|
138
|
-
import * as recast from "recast";
|
|
139
|
-
import { parse as parse2 } from "@babel/parser";
|
|
140
|
-
var PARSE_OPTIONS = {
|
|
141
|
-
sourceType: "module",
|
|
142
|
-
allowImportExportEverywhere: true,
|
|
143
|
-
allowReturnOutsideFunction: true,
|
|
144
|
-
startLine: 1,
|
|
145
|
-
tokens: true,
|
|
146
|
-
plugins: [
|
|
147
|
-
"asyncGenerators",
|
|
148
|
-
"bigInt",
|
|
149
|
-
"classPrivateMethods",
|
|
150
|
-
"classPrivateProperties",
|
|
151
|
-
"classProperties",
|
|
152
|
-
"classStaticBlock",
|
|
153
|
-
"decimal",
|
|
154
|
-
"decorators-legacy",
|
|
155
|
-
"doExpressions",
|
|
156
|
-
"dynamicImport",
|
|
157
|
-
"exportDefaultFrom",
|
|
158
|
-
"exportNamespaceFrom",
|
|
159
|
-
"functionBind",
|
|
160
|
-
"functionSent",
|
|
161
|
-
"importAssertions",
|
|
162
|
-
"importMeta",
|
|
163
|
-
"nullishCoalescingOperator",
|
|
164
|
-
"numericSeparator",
|
|
165
|
-
"objectRestSpread",
|
|
166
|
-
"optionalCatchBinding",
|
|
167
|
-
"optionalChaining",
|
|
168
|
-
[
|
|
169
|
-
"pipelineOperator",
|
|
170
|
-
{
|
|
171
|
-
proposal: "minimal"
|
|
172
|
-
}
|
|
173
|
-
],
|
|
174
|
-
[
|
|
175
|
-
"recordAndTuple",
|
|
176
|
-
{
|
|
177
|
-
syntaxType: "hash"
|
|
178
|
-
}
|
|
179
|
-
],
|
|
180
|
-
"throwExpressions",
|
|
181
|
-
"topLevelAwait",
|
|
182
|
-
"v8intrinsic",
|
|
183
|
-
"typescript",
|
|
184
|
-
"jsx"
|
|
185
|
-
]
|
|
186
|
-
};
|
|
187
|
-
var transformJsx = async ({ sourceFile, config }) => {
|
|
188
|
-
const output = sourceFile.getFullText();
|
|
189
|
-
if (config.tsx) {
|
|
190
|
-
return output;
|
|
191
|
-
}
|
|
192
|
-
const ast = recast.parse(output, {
|
|
193
|
-
parser: {
|
|
194
|
-
parse: (code) => {
|
|
195
|
-
return parse2(code, PARSE_OPTIONS);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
const result = transformFromAstSync(ast, output, {
|
|
200
|
-
cloneInputAst: false,
|
|
201
|
-
code: false,
|
|
202
|
-
ast: true,
|
|
203
|
-
plugins: [transformTypescript],
|
|
204
|
-
configFile: false
|
|
205
|
-
});
|
|
206
|
-
if (!result || !result.ast) {
|
|
207
|
-
throw new Error("Failed to transform JSX");
|
|
208
|
-
}
|
|
209
|
-
return recast.print(result.ast).code;
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
// src/utils/transformers/transform-rsc.ts
|
|
213
|
-
import { SyntaxKind } from "ts-morph";
|
|
214
|
-
var transformRsc = async ({ sourceFile, config }) => {
|
|
215
|
-
if (config.rsc) {
|
|
216
|
-
return sourceFile;
|
|
217
|
-
}
|
|
218
|
-
const first = sourceFile.getFirstChildByKind(SyntaxKind.ExpressionStatement);
|
|
219
|
-
if (first?.getText() === `"use client";`) {
|
|
220
|
-
first.remove();
|
|
221
|
-
}
|
|
222
|
-
return sourceFile;
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
// src/utils/transformers/transform-css.ts
|
|
226
|
-
var transformCSS = async ({ sourceFile, config }) => {
|
|
227
|
-
if (config.css) {
|
|
228
|
-
return sourceFile;
|
|
229
|
-
}
|
|
230
|
-
const imports = sourceFile.getImportDeclarations();
|
|
231
|
-
const cssImports = imports.filter((i) => i.getModuleSpecifierValue().endsWith(".css"));
|
|
232
|
-
for (const cssImport of cssImports) {
|
|
233
|
-
cssImport.remove();
|
|
234
|
-
}
|
|
235
|
-
return sourceFile;
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
// src/utils/transformers/index.ts
|
|
239
|
-
import { Project, ScriptKind } from "ts-morph";
|
|
240
|
-
var transformers = [transformRsc, transformCSS];
|
|
241
|
-
var project = new Project({
|
|
242
|
-
compilerOptions: {}
|
|
243
|
-
});
|
|
244
|
-
async function createTempSourceFile(filename) {
|
|
245
|
-
const dir = await fs.mkdtemp(path2.join(tmpdir(), "seed-deisgn-"));
|
|
246
|
-
return path2.join(dir, filename);
|
|
247
|
-
}
|
|
248
|
-
async function transform(opts) {
|
|
249
|
-
const tempFile = await createTempSourceFile(opts.filename);
|
|
250
|
-
const sourceFile = project.createSourceFile(tempFile, opts.raw, {
|
|
251
|
-
scriptKind: ScriptKind.TSX
|
|
252
|
-
});
|
|
253
|
-
for (const transformer of transformers) {
|
|
254
|
-
transformer({ sourceFile, ...opts });
|
|
255
|
-
}
|
|
256
|
-
return await transformJsx({
|
|
257
|
-
sourceFile,
|
|
258
|
-
...opts
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// src/commands/add.ts
|
|
263
|
-
import * as p from "@clack/prompts";
|
|
264
|
-
import { execa } from "execa";
|
|
265
|
-
import fs2 from "fs-extra";
|
|
266
|
-
import path3 from "path";
|
|
267
|
-
import color from "picocolors";
|
|
268
|
-
import { z as z3 } from "zod";
|
|
269
|
-
|
|
270
|
-
// src/utils/add-relative-components.ts
|
|
271
|
-
function addRelativeComponents(userSelects, registryIndex) {
|
|
272
|
-
const selectedComponents = /* @__PURE__ */ new Set();
|
|
273
|
-
function addSeedDependencies(componentName) {
|
|
274
|
-
if (selectedComponents.has(componentName))
|
|
275
|
-
return;
|
|
276
|
-
selectedComponents.add(componentName);
|
|
277
|
-
const component = registryIndex.find((c) => c.name === componentName);
|
|
278
|
-
if (!component)
|
|
279
|
-
return;
|
|
280
|
-
if (component.innerDependencies) {
|
|
281
|
-
for (const dep of component.innerDependencies) {
|
|
282
|
-
addSeedDependencies(dep);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
for (const componentName of userSelects) {
|
|
287
|
-
addSeedDependencies(componentName);
|
|
288
|
-
}
|
|
289
|
-
return Array.from(selectedComponents);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
// src/commands/add.ts
|
|
293
|
-
var addOptionsSchema = z3.object({
|
|
294
|
-
components: z3.array(z3.string()).optional(),
|
|
295
|
-
cwd: z3.string(),
|
|
296
|
-
all: z3.boolean()
|
|
297
|
-
// yes: z.boolean(),
|
|
298
|
-
// overwrite: z.boolean(),
|
|
299
|
-
// path: z.string().optional(),
|
|
300
|
-
});
|
|
301
|
-
var addCommand = (cli) => {
|
|
302
|
-
cli.command("add [...components]", "add component").option("-a, --all", "Add all components", {
|
|
303
|
-
default: false
|
|
304
|
-
}).option("-c, --cwd <cwd>", "the working directory. defaults to the current directory.", {
|
|
305
|
-
default: process.cwd()
|
|
306
|
-
}).example("seed-design add box-button").example("seed-design add alert-dialog").action(async (components, opts) => {
|
|
307
|
-
const options = addOptionsSchema.parse({
|
|
308
|
-
components,
|
|
309
|
-
...opts
|
|
310
|
-
});
|
|
311
|
-
const highlight = (text2) => color.cyan(text2);
|
|
312
|
-
const cwd = options.cwd;
|
|
313
|
-
if (!fs2.existsSync(cwd)) {
|
|
314
|
-
p.log.error(`The path ${cwd} does not exist. Please try again.`);
|
|
315
|
-
process.exit(1);
|
|
316
|
-
}
|
|
317
|
-
const registryComponentIndex = await getRegistryComponentIndex();
|
|
318
|
-
let selectedComponents = options.all ? registryComponentIndex.map((registry) => registry.name) : options.components;
|
|
319
|
-
if (!options.components?.length && !options.all) {
|
|
320
|
-
const selects = await p.multiselect({
|
|
321
|
-
message: "Select all components to add",
|
|
322
|
-
options: registryComponentIndex.map((metadata) => {
|
|
323
|
-
return {
|
|
324
|
-
label: metadata.name,
|
|
325
|
-
value: metadata.name,
|
|
326
|
-
hint: metadata.description
|
|
327
|
-
};
|
|
328
|
-
})
|
|
329
|
-
});
|
|
330
|
-
if (p.isCancel(selects)) {
|
|
331
|
-
p.log.error("Aborted.");
|
|
332
|
-
process.exit(0);
|
|
333
|
-
}
|
|
334
|
-
selectedComponents = selects;
|
|
335
|
-
}
|
|
336
|
-
if (!selectedComponents?.length) {
|
|
337
|
-
p.log.error("No components found.");
|
|
338
|
-
process.exit(0);
|
|
339
|
-
}
|
|
340
|
-
const allComponents = addRelativeComponents(selectedComponents, registryComponentIndex);
|
|
341
|
-
const addedComponents = allComponents.filter((c) => !selectedComponents.includes(c));
|
|
342
|
-
const config = await getConfig(cwd);
|
|
343
|
-
const registryComponentItems = await fetchRegistryComponentItem(allComponents);
|
|
344
|
-
p.log.message(`Selection: ${highlight(selectedComponents.join(", "))}`);
|
|
345
|
-
if (addedComponents.length) {
|
|
346
|
-
p.log.message(
|
|
347
|
-
`Inner Dependencies: ${highlight(addedComponents.join(", "))} will be also added.`
|
|
348
|
-
);
|
|
349
|
-
}
|
|
350
|
-
for (const component of registryComponentItems) {
|
|
351
|
-
for (const registry of component.registries) {
|
|
352
|
-
const UIFolderPath = config.resolvedUIPaths;
|
|
353
|
-
if (!fs2.existsSync(UIFolderPath)) {
|
|
354
|
-
await fs2.mkdir(UIFolderPath, { recursive: true });
|
|
355
|
-
}
|
|
356
|
-
let filePath = path3.resolve(UIFolderPath, registry.name);
|
|
357
|
-
const content = await transform({
|
|
358
|
-
filename: registry.name,
|
|
359
|
-
config,
|
|
360
|
-
raw: registry.content
|
|
361
|
-
});
|
|
362
|
-
if (!config.tsx) {
|
|
363
|
-
filePath = filePath.replace(/\.tsx$/, ".jsx");
|
|
364
|
-
filePath = filePath.replace(/\.ts$/, ".js");
|
|
365
|
-
}
|
|
366
|
-
await fs2.writeFile(filePath, content);
|
|
367
|
-
const relativePath = path3.relative(cwd, filePath);
|
|
368
|
-
p.log.info(`Added ${highlight(registry.name)} to ${highlight(relativePath)}`);
|
|
369
|
-
}
|
|
370
|
-
const packageManager = await getPackageManager(cwd);
|
|
371
|
-
const { start, stop } = p.spinner();
|
|
372
|
-
if (component.dependencies?.length) {
|
|
373
|
-
start(color.gray("Installing dependencies"));
|
|
374
|
-
const result = await execa(
|
|
375
|
-
packageManager,
|
|
376
|
-
[packageManager === "npm" ? "install" : "add", ...component.dependencies],
|
|
377
|
-
{
|
|
378
|
-
cwd
|
|
379
|
-
}
|
|
380
|
-
);
|
|
381
|
-
if (result.failed) {
|
|
382
|
-
console.error(result.all);
|
|
383
|
-
process.exit(1);
|
|
384
|
-
} else {
|
|
385
|
-
for (const deps of component.dependencies) {
|
|
386
|
-
p.log.info(`- ${deps}`);
|
|
387
|
-
}
|
|
388
|
-
stop("Dependencies installed.");
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
if (component.devDependencies?.length) {
|
|
392
|
-
start(color.gray("Installing devDependencies"));
|
|
393
|
-
const result = await execa(
|
|
394
|
-
packageManager,
|
|
395
|
-
[packageManager === "npm" ? "install" : "add", "-D", ...component.devDependencies],
|
|
396
|
-
{
|
|
397
|
-
cwd
|
|
398
|
-
}
|
|
399
|
-
);
|
|
400
|
-
if (result.failed) {
|
|
401
|
-
console.error(result.all);
|
|
402
|
-
process.exit(1);
|
|
403
|
-
} else {
|
|
404
|
-
for (const deps of component.devDependencies) {
|
|
405
|
-
p.log.info(`- ${deps}`);
|
|
406
|
-
}
|
|
407
|
-
stop("Dependencies installed.");
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
p.outro("Components added.");
|
|
412
|
-
});
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
// src/utils/get-package-info.ts
|
|
416
|
-
import findup from "findup-sync";
|
|
417
|
-
import fs3 from "fs-extra";
|
|
418
|
-
var PACKAGE_JSON = "package.json";
|
|
419
|
-
function getPackagePath() {
|
|
420
|
-
const packageJsonPath = findup(PACKAGE_JSON);
|
|
421
|
-
if (!packageJsonPath) {
|
|
422
|
-
throw new Error("No package.json file found in the project.");
|
|
423
|
-
}
|
|
424
|
-
return packageJsonPath;
|
|
425
|
-
}
|
|
426
|
-
function getPackageInfo() {
|
|
427
|
-
return fs3.readJSONSync(getPackagePath());
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
// src/index.ts
|
|
431
|
-
import { cac } from "cac";
|
|
432
|
-
|
|
433
|
-
// src/commands/init.ts
|
|
434
|
-
import * as p2 from "@clack/prompts";
|
|
435
|
-
import fs4 from "fs-extra";
|
|
436
|
-
import path4 from "path";
|
|
437
|
-
import color2 from "picocolors";
|
|
438
|
-
import { z as z4 } from "zod";
|
|
439
|
-
var initOptionsSchema = z4.object({
|
|
440
|
-
cwd: z4.string()
|
|
441
|
-
});
|
|
442
|
-
var initCommand = (cli) => {
|
|
443
|
-
cli.command("init", "initialize seed-design.json").option("-c, --cwd <cwd>", "the working directory. defaults to the current directory.", {
|
|
444
|
-
default: process.cwd()
|
|
445
|
-
}).action(async (opts) => {
|
|
446
|
-
const options = initOptionsSchema.parse({ ...opts });
|
|
447
|
-
const highlight = (text2) => color2.cyan(text2);
|
|
448
|
-
const group2 = await p2.group(
|
|
449
|
-
{
|
|
450
|
-
tsx: () => p2.confirm({
|
|
451
|
-
message: `Would you like to use ${highlight("TypeScript")} (recommended)?`,
|
|
452
|
-
initialValue: true
|
|
453
|
-
}),
|
|
454
|
-
rsc: () => p2.confirm({
|
|
455
|
-
message: `Are you using ${highlight("React Server Components")}?`,
|
|
456
|
-
initialValue: false
|
|
457
|
-
}),
|
|
458
|
-
css: () => p2.confirm({
|
|
459
|
-
message: `Would you like to use ${highlight("CSS Modules")}? (If true, CSS import will be added in components)`,
|
|
460
|
-
initialValue: true
|
|
461
|
-
}),
|
|
462
|
-
path: () => p2.text({
|
|
463
|
-
message: `Enter the path to your ${highlight("seed-design directory")}`,
|
|
464
|
-
initialValue: "./seed-design",
|
|
465
|
-
defaultValue: "./seed-design",
|
|
466
|
-
placeholder: "./seed-design"
|
|
467
|
-
})
|
|
468
|
-
},
|
|
469
|
-
{
|
|
470
|
-
onCancel: () => {
|
|
471
|
-
p2.cancel("Operation cancelled.");
|
|
472
|
-
process.exit(0);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
);
|
|
476
|
-
const config = {
|
|
477
|
-
rsc: group2.rsc,
|
|
478
|
-
tsx: group2.tsx,
|
|
479
|
-
css: group2.css,
|
|
480
|
-
path: group2.path
|
|
481
|
-
};
|
|
482
|
-
const { start, stop } = p2.spinner();
|
|
483
|
-
start("Writing seed-design.json...");
|
|
484
|
-
const targetPath = path4.resolve(options.cwd, "seed-design.json");
|
|
485
|
-
await fs4.writeFile(targetPath, `${JSON.stringify(config, null, 2)}
|
|
486
|
-
`, "utf-8");
|
|
487
|
-
const relativePath = path4.relative(process.cwd(), targetPath);
|
|
488
|
-
stop(`seed-design.json written to ${highlight(relativePath)}`);
|
|
489
|
-
});
|
|
490
|
-
};
|
|
491
|
-
|
|
492
|
-
// src/index.ts
|
|
493
|
-
var NAME = "seed-design";
|
|
494
|
-
var CLI = cac(NAME);
|
|
495
|
-
async function main() {
|
|
496
|
-
const packageInfo = getPackageInfo();
|
|
497
|
-
addCommand(CLI);
|
|
498
|
-
initCommand(CLI);
|
|
499
|
-
CLI.version(packageInfo.version || "1.0.0", "-v, --version");
|
|
500
|
-
CLI.help();
|
|
501
|
-
CLI.parse();
|
|
502
|
-
}
|
|
503
|
-
main();
|
|
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"
|