@@ -11,26 +11,16 @@ import { fetchComposition, fetchCompositions } from "../utils/fetch"
1111import { getFileDependencies } from "../utils/get-file-dependencies"
1212import { ensureDir } from "../utils/io"
1313import { installCommand } from "../utils/run-command"
14- import { type CompositionFile , addCommandFlagsSchema } from "../utils/schema"
14+ import {
15+ type CompositionFile ,
16+ type Compositions ,
17+ addCommandFlagsSchema ,
18+ } from "../utils/schema"
1519import { uniq } from "../utils/shared"
1620import { tasks } from "../utils/tasks"
1721
1822const debug = createDebug ( "chakra:snippet" )
1923
20- async function transformToJsx ( item : CompositionFile ) {
21- const content = await convertTsxToJsx ( item . file . content )
22- item . file . content = content
23- item . file . name = item . file . name . replace ( ".tsx" , ".jsx" )
24- }
25-
26- function printFileSync ( item : CompositionFile ) {
27- const boxText = boxen ( item . file . content , {
28- headerText : `${ item . file . name } \n` ,
29- borderStyle : "none" ,
30- } )
31- p . log . info ( boxText )
32- }
33-
3424export const SnippetCommand = new Command ( "snippet" )
3525 . description ( "Add snippets to your project for better DX" )
3626 . addCommand (
@@ -39,10 +29,11 @@ export const SnippetCommand = new Command("snippet")
3929 . argument ( "[snippets...]" , "snippets to add" )
4030 . option ( "-d, --dry-run" , "Dry run" )
4131 . option ( "--outdir <dir>" , "Output directory to write the snippets" )
32+ . option ( "--all" , "Add all snippets" )
4233 . option ( "-f, --force" , "Overwrite existing files" )
43- . action ( async ( components : string [ ] , flags : unknown ) => {
34+ . action ( async ( selectedComponents : string [ ] , flags : unknown ) => {
4435 const parsedFlags = addCommandFlagsSchema . parse ( flags )
45- const { dryRun, force } = parsedFlags
36+ const { dryRun, force, all } = parsedFlags
4637
4738 const ctx = await getProjectContext ( { cwd : process . cwd ( ) } )
4839 debug ( "context" , ctx )
@@ -54,16 +45,16 @@ export const SnippetCommand = new Command("snippet")
5445
5546 const items = await fetchCompositions ( )
5647
57- const all = components . length === 0
58-
59- if ( all ) {
60- p . log . info ( "No components selected, Adding all..." )
61- components = items . map ( ( item ) => item . id )
62- }
48+ const inferredComponents = getComponents ( {
49+ components : selectedComponents ,
50+ all,
51+ items,
52+ } )
6353
54+ const components = inferredComponents . items
6455 debug ( "components" , components )
6556
66- p . log . info ( `Adding ${ components . length } snippet(s)...` )
57+ p . log . info ( inferredComponents . message )
6758
6859 const deps = uniq (
6960 components . flatMap ( ( id ) => getFileDependencies ( items , id ) ) ,
@@ -191,3 +182,56 @@ export const SnippetCommand = new Command("snippet")
191182 p . outro ( "🎉 Done!" )
192183 } ) ,
193184 )
185+
186+ async function transformToJsx ( item : CompositionFile ) {
187+ const content = await convertTsxToJsx ( item . file . content )
188+ item . file . content = content
189+ item . file . name = item . file . name . replace ( ".tsx" , ".jsx" )
190+ }
191+
192+ function printFileSync ( item : CompositionFile ) {
193+ const boxText = boxen ( item . file . content , {
194+ headerText : `${ item . file . name } \n` ,
195+ borderStyle : "none" ,
196+ } )
197+ p . log . info ( boxText )
198+ }
199+
200+ const RECOMMENDED_SNIPPETS = [
201+ "provider" ,
202+ "avatar" ,
203+ "button" ,
204+ "checkbox" ,
205+ "radio" ,
206+ "input-group" ,
207+ "slider" ,
208+ "popover" ,
209+ "dialog" ,
210+ "drawer" ,
211+ "tooltip" ,
212+ "field" ,
213+ ]
214+
215+ function getComponents ( opts : {
216+ components : string [ ]
217+ all : boolean | undefined
218+ items : Compositions
219+ } ) {
220+ const { components, all, items } = opts
221+ if ( components . length === 0 && ! all )
222+ return {
223+ message : "No component(s) selected, adding recommended snippets..." ,
224+ items : RECOMMENDED_SNIPPETS ,
225+ }
226+
227+ if ( all )
228+ return {
229+ message : "Adding all snippets..." ,
230+ items : items . map ( ( item ) => item . id ) ,
231+ }
232+
233+ return {
234+ message : `Adding ${ components . length } snippet(s)...` ,
235+ items : components ,
236+ }
237+ }
0 commit comments