@@ -8,10 +8,12 @@ $.verbose = true;
88async function downloadGitFolder (
99 GitURL : string ,
1010 branchName ?: string ,
11- folderOrFilePath ?: string
11+ folderOrFilePath ?: string ,
12+ targetFolder = '.'
1213) {
13- const tempFolder = path . join ( os . tmpdir ( ) , new URL ( GitURL ) . pathname ) ,
14- targetFolder = process . cwd ( ) ;
14+ targetFolder = path . resolve ( targetFolder ) ;
15+
16+ const tempFolder = path . join ( os . tmpdir ( ) , new URL ( GitURL ) . pathname ) ;
1517
1618 await fs . remove ( tempFolder ) ;
1719 await fs . mkdirp ( tempFolder ) ;
@@ -39,10 +41,8 @@ async function downloadGitFolder(
3941 if ( sourceStat . isFile ( ) ) {
4042 const fileName = path . basename ( sourcePath ) ;
4143
42- await fs . copy ( sourcePath , path . join ( targetFolder , fileName ) , {
43- overwrite : true
44- } ) ;
45- } else await fs . copy ( sourcePath , targetFolder , { overwrite : true } ) ;
44+ await fs . copy ( sourcePath , path . join ( targetFolder , fileName ) ) ;
45+ } else await fs . copy ( sourcePath , targetFolder ) ;
4646}
4747
4848async function listSubmodules ( ) {
@@ -70,25 +70,79 @@ Note: You may want to commit these changes with:
7070 git commit -m "Remove submodule ${ submodulePath } "` ) ;
7171}
7272
73+ async function uploadFolder (
74+ sourceFolder : string ,
75+ GitURL : string ,
76+ targetBranch : string ,
77+ targetFolder ? : string
78+ ) {
79+ sourceFolder = path . resolve ( sourceFolder ) ;
80+
81+ if ( targetFolder ) {
82+ const tempFolder = path . join ( os . tmpdir ( ) , new URL ( GitURL ) . pathname ) ;
83+
84+ await fs . remove ( tempFolder ) ;
85+ await fs . mkdirp ( tempFolder ) ;
86+ cd ( tempFolder ) ;
87+
88+ await $ `git clone - b ${targetBranch } ${GitURL } . `;
89+
90+ targetFolder = path.join(tempFolder, targetFolder);
91+
92+ await fs.remove(targetFolder);
93+ await fs.mkdirp(targetFolder);
94+ await fs.copy(sourceFolder, targetFolder);
95+ await fs.remove(path.join(targetFolder, '.git'));
96+
97+ await $` git add . `;
98+ await $` git commit - m "upload by Git-utility CLI" `;
99+ await $` git push origin $ { targetBranch } `;
100+ } else {
101+ cd(sourceFolder);
102+
103+ await $` git init `;
104+ await $` git remote add origin $ { GitURL } `;
105+ await $` git checkout - b $ { targetBranch } `;
106+ await $` git add . `;
107+ await $` git commit - m "upload by Git-utility CLI" `;
108+ await $` git push -- set - upstream origin ${targetBranch } - f `;
109+ await fs . remove ( '.git' ) ;
110+ }
111+ }
112+
73113Command . execute (
74114 < Command name = "xgit" >
75115 < Command
76116 name = "download"
77- parameters = "<GitURL> [branchName] [folderOrFilePath]"
117+ parameters = "<GitURL> [branchName] [folderOrFilePath] [targetFolder] "
78118 description = "Download folders or files from a Git repository"
79119 executor = { (
80120 _ ,
81121 GitURL : string ,
82122 branchName = 'main' ,
83- folderOrFilePath ? : string
123+ folderOrFilePath ? : string ,
124+ targetFolder ? : string
84125 ) =>
85126 downloadGitFolder (
86127 GitURL ,
87128 branchName as string ,
88- folderOrFilePath
129+ folderOrFilePath ,
130+ targetFolder
89131 )
90132 }
91133 />
134+ < Command
135+ name = "upload"
136+ parameters = "<sourceFolder> <GitURL> <targetBranch> [targetFolder]"
137+ description = "Upload a folder to a Git repository"
138+ executor = { (
139+ _ ,
140+ sourceFolder : string ,
141+ GitURL : string ,
142+ targetBranch : string ,
143+ targetFolder ? : string
144+ ) => uploadFolder ( sourceFolder , GitURL , targetBranch , targetFolder ) }
145+ />
92146 < Command name = "submodule" description = "Manage Git submodules" >
93147 < Command
94148 name = "remove"
0 commit comments