1+ import { window , QuickPick , QuickPickItem , QuickInputButton , ThemeIcon , OpenDialogOptions , Uri , commands } from 'vscode' ;
2+ import { downloadTemplate } from 'giget' ;
3+ import { ofetch } from 'ofetch'
4+ import { ProjectTemplate } from '../types'
5+ import { projectRootDirectory , openExternalLink , openFolder } from '../utils' ;
6+
7+ const github : QuickInputButton = {
8+ iconPath : new ThemeIcon ( "github" ) ,
9+ tooltip : "Template Github Repo" ,
10+ }
11+
12+ const docs : QuickInputButton = {
13+ iconPath : new ThemeIcon ( "book" ) ,
14+ tooltip : "Template Docs/Refrence" ,
15+ }
16+
17+
18+ const fetchTemplates = async ( ) => {
19+ try {
20+ let res = await ofetch ( 'https://nuxt.new/data/starters.json' ) ;
21+ return res as ProjectTemplate [ ]
22+
23+ } catch ( error ) {
24+ window . showErrorMessage ( `Failed to fetch Nuxt templates` )
25+ }
26+ }
27+
28+ const fetchTemplate = async ( repo : ProjectTemplate , path : string , projectName : string ) => {
29+ try {
30+ await downloadTemplate ( `${ repo . branch } ` , {
31+ cwd : path ,
32+ registry : "https://raw.githubusercontent.com/nuxt/starter/templates/templates" ,
33+ dir : projectName ,
34+ } )
35+ } catch ( error ) {
36+ console . log ( 'error' , error ) ;
37+ window . showErrorMessage ( `Failed to fetch Nuxt ${ repo . name } template` )
38+ }
39+ }
40+
41+ export async function createProject ( ) {
42+ try {
43+ const templates = await fetchTemplates ( ) as ProjectTemplate [ ] ;
44+
45+ if ( ! templates ) {
46+ return ;
47+ } else {
48+
49+
50+ const picker : QuickPick < QuickPickItem > = window . createQuickPick ( )
51+
52+ picker . canSelectMany = false
53+ picker . ignoreFocusOut = true
54+ picker . matchOnDescription = true
55+ picker . placeholder = "Select a Nuxt template"
56+
57+ const items : QuickPickItem [ ] = templates . map ( ( item ) => {
58+ return {
59+ label : item . name ,
60+ description : item . description ,
61+ buttons : [ github , docs ] ,
62+ package : item ,
63+ }
64+ } )
65+
66+ picker . items = items
67+
68+
69+ picker . onDidChangeSelection ( async ( selection ) => {
70+ if ( selection [ 0 ] ) {
71+ const repo = templates . find ( ( item ) => item . name === selection [ 0 ] . label )
72+ if ( repo ) {
73+ try {
74+
75+ const proName = await window . showInputBox ( {
76+ placeHolder : 'Project Name' ,
77+ prompt : 'Enter a project name' ,
78+ } )
79+
80+ if ( proName ) {
81+
82+ const options : OpenDialogOptions = {
83+ canSelectMany : false ,
84+ openLabel : 'Select' ,
85+ canSelectFiles : false ,
86+ canSelectFolders : true
87+ } ;
88+
89+ window . showOpenDialog ( options ) . then ( async fileUri => {
90+ if ( fileUri && fileUri [ 0 ] ) {
91+ try {
92+ await fetchTemplate ( repo , fileUri [ 0 ] . fsPath , proName )
93+ const result = await window . showInformationMessage ( `Projec created` , 'Open in current window' , 'Open in new window' )
94+ if ( result === 'Open in current window' ) {
95+ let uri = Uri . file ( `${ fileUri [ 0 ] . fsPath } /${ proName } ` ) ;
96+ await openFolder ( uri , proName , false )
97+ }
98+
99+ if ( result === 'Open in new window' ) {
100+ let uri = Uri . file ( `${ fileUri [ 0 ] . fsPath } /${ proName } ` ) ;
101+ await openFolder ( uri , proName , true )
102+ }
103+ } catch ( error ) {
104+ window . showInformationMessage ( `Failed to create Nuxt ${ proName } template` )
105+ }
106+ }
107+ } ) ;
108+
109+ }
110+
111+
112+ } catch ( error ) {
113+ window . showErrorMessage ( `Failed to fetch Nuxt ${ repo . name } template` )
114+ }
115+ }
116+ }
117+ } )
118+
119+ picker . onDidTriggerItemButton ( async ( e ) => {
120+ const selectedItem = e . item as QuickPickItem & { package : ProjectTemplate }
121+ if ( e . button === github ) { openExternalLink ( `https://github.com/${ selectedItem . package . repo } ` ) }
122+
123+ if ( e . button === docs ) { openExternalLink ( selectedItem . package . docs ) }
124+ } )
125+
126+ picker . onDidChangeSelection ( async ( item : any ) => { picker . dispose ( ) } )
127+
128+ picker . show ( )
129+ }
130+
131+ } catch ( error ) {
132+ window . showErrorMessage ( `Failed to fetch Nuxt templates` )
133+ }
134+ }
0 commit comments