11import { defineCommand } from 'citty' ;
2+ import * as fs from 'fs' ;
3+ import * as path from 'path' ;
4+ import * as os from 'os' ;
5+ import assert from 'assert' ;
6+ import { exec } from 'child_process' ;
27
38export const newCommand = defineCommand ( {
49 meta : {
@@ -25,12 +30,68 @@ export const newCommand = defineCommand({
2530 } ,
2631 } ,
2732 async run ( ctx ) {
28- if ( ! ctx . args . directory ) console . warn ( `No directory provided, defaulitng to ${ process . cwd ( ) } ` ) ;
33+ const targetDir = ctx . args . directory ;
34+
35+ console . log ( `Downloading template to ${ targetDir } ...` ) ;
36+
37+ // Download the zipball
2938 const client = new ( await import ( '@octokit/rest' ) ) . Octokit ( ) ;
3039 const response = await client . repos . downloadZipballArchive ( {
3140 owner : 'VSC-NeuroPilot' ,
3241 repo : 'neuropilot-companion-template' ,
3342 ref : 'master' ,
3443 } ) ;
44+
45+ // Create a temporary file
46+ const tempDir = os . tmpdir ( ) ;
47+ const tempZipPath = path . join ( tempDir , `neuropilot-template-${ Date . now ( ) } .zip` ) ;
48+
49+ // Write the zip data to the temp file
50+ // The response.data is a Buffer or ArrayBuffer
51+ fs . writeFileSync ( tempZipPath , Buffer . from ( response . data as ArrayBuffer ) ) ;
52+
53+ // Extract the zip file
54+ const AdmZip = ( await import ( 'adm-zip' ) ) . default ;
55+ const zip = new AdmZip ( tempZipPath ) ;
56+ const tempExtractDir = path . join ( tempDir , `neuropilot-template-extract-${ Date . now ( ) } ` ) ;
57+ zip . extractAllTo ( tempExtractDir , true ) ;
58+
59+ // GitHub wraps the content in a folder named like "repo-ref"
60+ // Find that folder and copy its contents to the target directory
61+ const extractedContents = fs . readdirSync ( tempExtractDir ) ;
62+ assert ( extractedContents . length !== 0 , 'Extracted archive is empty' ) ;
63+ const wrappedFolder = extractedContents [ 0 ] ! ; // Should be the only item
64+ const sourceDir = path . join ( tempExtractDir , wrappedFolder ) ;
65+
66+ // Create target directory if it doesn't exist
67+ if ( ! fs . existsSync ( targetDir ) ) {
68+ fs . mkdirSync ( targetDir , { recursive : true } ) ;
69+ }
70+
71+ // Copy contents from source to target
72+ const copyRecursive = ( src : string , dest : string ) => {
73+ const items = fs . readdirSync ( src ) ;
74+ for ( const item of items ) {
75+ const srcPath = path . join ( src , item ) ;
76+ const destPath = path . join ( dest , item ) ;
77+
78+ if ( fs . statSync ( srcPath ) . isDirectory ( ) ) {
79+ fs . mkdirSync ( destPath , { recursive : true } ) ;
80+ copyRecursive ( srcPath , destPath ) ;
81+ } else {
82+ fs . copyFileSync ( srcPath , destPath ) ;
83+ }
84+ }
85+ } ;
86+
87+ copyRecursive ( sourceDir , targetDir ) ;
88+
89+ // Clean up temporary files
90+ fs . rmSync ( tempZipPath ) ;
91+ fs . rmSync ( tempExtractDir , { recursive : true } ) ;
92+
93+ console . log ( 'Template downloaded successfully!' ) ;
94+
95+ // Handle git init and npm install based on args...
3596 } ,
3697} ) ;
0 commit comments