11import { existsSync } from 'fs' ;
22import { promisify } from 'util' ;
3- import { program } from 'commander' ;
43import { fileURLToPath } from 'url' ;
54import { coerce , satisfies } from 'semver' ;
65import { mkdir , readFile , rm } from 'fs/promises' ;
@@ -9,10 +8,12 @@ import { exec as rawExec, spawn } from 'child_process';
98
109export type ExecuteResults = [ string , string ] ;
1110
12- const exec = promisify ( rawExec ) ;
11+ export type CloneSparseOptions = {
12+ globs : boolean ;
13+ force : boolean ;
14+ } ;
1315
14- const createAppender = ( array : string [ ] ) => ( chunk : string ) =>
15- array . push ( chunk ) ;
16+ const exec = promisify ( rawExec ) ;
1617
1718const getInstallDirectory = ( ) : string =>
1819 dirname ( fileURLToPath ( import . meta. url ) ) ;
@@ -48,31 +49,17 @@ const executeCommand = async (
4849 return [ stdout , stderr ] ;
4950} ;
5051
51- const executeGitCommand = (
52- cwd : string ,
53- args : string [ ] = [ ]
54- ) : Promise < ExecuteResults > =>
52+ const executeGitCommand = ( cwd : string , args : string [ ] = [ ] ) : Promise < void > =>
5553 new Promise (
56- (
57- resolve : ( value : ExecuteResults ) => void ,
58- reject : ( reason ?: string | Error ) => void
59- ) => {
60- const [ stdout , stderr ] = [ [ ] , [ ] ] ;
54+ ( resolve : ( ) => void , reject : ( reason ?: string | Error ) => void ) => {
6155 const process = spawn ( 'git' , args , { cwd } ) ;
6256
63- process . stdout . setEncoding ( 'utf-8' ) ;
64- process . stderr . setEncoding ( 'utf-8' ) ;
65- process . stdout . on ( 'data' , createAppender ( stdout ) ) ;
66- process . stderr . on ( 'data' , createAppender ( stderr ) ) ;
67-
68- process . on ( 'error' , ( ) =>
69- reject ( `Error occurred during execution:\n\n${ stderr . join ( '\n' ) } ` )
70- ) ;
57+ process . on ( 'error' , ( ) => reject ( `Error occurred during execution!` ) ) ;
7158 process . on ( 'close' , ( code : number ) => {
7259 if ( code !== 0 ) {
73- reject ( `Exited with code ${ code } :\n\n ${ stderr . join ( '\n' ) } ` ) ;
60+ reject ( `Exited with code ${ code } ! ` ) ;
7461 } else {
75- resolve ( [ stdout . join ( '\n' ) , stderr . join ( '\n' ) ] ) ;
62+ resolve ( ) ;
7663 }
7764 } ) ;
7865 }
@@ -84,7 +71,8 @@ export const getPackageVersion = async (): Promise<string> =>
8471export async function cloneSparse (
8572 cwd : string ,
8673 repoUrl : string ,
87- paths : string [ ]
74+ paths : string [ ] ,
75+ opts ?: CloneSparseOptions
8876) : Promise < void > {
8977 try {
9078 if ( ! repoUrl || ! cwd || ! paths . length ) {
@@ -110,7 +98,7 @@ export async function cloneSparse(
11098 }
11199
112100 const workingCopyDir = basename ( cwd ) ;
113- const { globs, force } = program . opts ( ) ;
101+ const { globs, force } = opts ;
114102 if ( existsSync ( cwd ) && force ) {
115103 console . log ( 'Removing existing working copy...' ) ;
116104 await rm ( workingCopyDir , {
@@ -122,7 +110,6 @@ export async function cloneSparse(
122110 }
123111
124112 console . log ( 'Pre-flight checks passed, performing bare clone...' ) ;
125- paths = paths . map ( ( path ) => path . replace ( / " / g, '' ) . replace ( / ^ \. / , '' ) ) ;
126113 await executeGitCommand ( workingCopyParent , [
127114 'clone' ,
128115 '-n' ,
@@ -133,6 +120,7 @@ export async function cloneSparse(
133120 workingCopyDir
134121 ] ) ;
135122 console . log ( 'Adding desired paths to sparse-checkout...' ) ;
123+ paths = paths . map ( ( path ) => path . replace ( / " / g, '' ) . replace ( / ^ \. / , '' ) ) ;
136124 if ( ! globs ) {
137125 await executeGitCommand ( cwd , [
138126 'sparse-checkout' ,
0 commit comments