2
2
* @module Git-Tools
3
3
*/
4
4
const { execSync } = require ( 'child_process' ) ;
5
- const { writeFileSync , mkdirSync , readdirSync , lstatSync , copyFileSync , existsSync } = require ( 'fs' ) ;
6
- const { join } = require ( 'path' ) ;
5
+ const fs = require ( 'node: fs' ) ;
6
+ const { join } = require ( 'node: path' ) ;
7
7
const randomize = require ( 'randomatic' ) ;
8
8
const { createTempFolder } = require ( './temp' ) ;
9
9
const git = require ( 'simple-git' ) ;
@@ -14,7 +14,7 @@ const commitLength = 20;
14
14
* initializes and empty git repository
15
15
* into a temp folder within the base temp folder
16
16
*
17
- * @param {String } nameOfFileToCommit - name
17
+ * @param {string } nameOfFileToCommit - name
18
18
*/
19
19
const createRepo = async ( nameOfFileToCommit = '' ) => {
20
20
const path = createTempFolder ( ) ;
@@ -30,46 +30,52 @@ const createRepo = async (nameOfFileToCommit = '') => {
30
30
} ;
31
31
const createBareRepo = createRepo ;
32
32
33
- // mkdirSync, readdirSync, lstatSync, copyFileSync
34
33
/**
35
34
* Copy folder recursively
36
35
*
37
36
* @private
38
- * @param {String } src
39
- * @param {String } dest
37
+ * @param {string } src
38
+ * @param {string } dest
39
+ * @returns {Promise }
40
40
*/
41
- const copyFolder = ( src , dest ) => {
42
- if ( ! existsSync ( dest ) ) {
43
- mkdirSync ( dest ) ;
44
- }
45
- readdirSync ( src ) . map ( file => {
46
- if ( lstatSync ( join ( src , file ) ) . isFile ( ) ) {
47
- copyFileSync ( join ( src , file ) , join ( dest , file ) ) ;
48
- }
49
- else {
50
- copyFolder ( join ( src , file ) , join ( dest , file ) ) ;
41
+ async function copyFolder ( src , dest ) {
42
+ try {
43
+ await fs . promises . mkdir ( dest , { recursive : true } ) ;
44
+
45
+ const entries = await fs . promises . readdir ( src , { withFileTypes : true } ) ;
46
+
47
+ for ( const entry of entries ) {
48
+ const srcPath = join ( src , entry . name ) ;
49
+ const destPath = join ( dest , entry . name ) ;
50
+
51
+ entry . isDirectory ( ) ?
52
+ await copyFolder ( srcPath , destPath ) :
53
+ await fs . promises . copyFile ( srcPath , destPath ) ;
51
54
}
52
- } ) ;
53
- } ;
55
+ }
56
+ catch ( error ) {
57
+ console . error ( 'An error occurred:' , error ) ;
58
+ }
59
+ }
54
60
55
61
/**
56
62
* Replicates a repository
57
63
*
58
- * @param {String } repoPath
59
- * @returns {String } path to new repo
64
+ * @param {string } repoPath
65
+ * @returns {Promise<string> } path to new repo
60
66
*/
61
- const duplicateRepo = ( repoPath ) => {
67
+ const duplicateRepo = async ( repoPath ) => {
62
68
const newPath = createTempFolder ( ) ;
63
- copyFolder ( repoPath , newPath ) ;
69
+ await copyFolder ( repoPath , newPath ) ;
64
70
return newPath ;
65
71
} ;
66
72
67
73
/**
68
74
* Adds remote from src to target repo
69
75
*
70
- * @param {String } srcRepoPath
71
- * @param {String } targetRepoPath
72
- * @param {String } remoteName
76
+ * @param {string } srcRepoPath
77
+ * @param {string } targetRepoPath
78
+ * @param {string } remoteName
73
79
* @return {Promise }
74
80
*/
75
81
const addRemote = ( srcRepoPath , targetRepoPath , remoteName = 'origin' ) => {
@@ -79,8 +85,8 @@ const addRemote = (srcRepoPath, targetRepoPath, remoteName = 'origin') => {
79
85
/**
80
86
* adds a file to a git repository
81
87
*
82
- * @param {String } repoPath
83
- * @param {String } name
88
+ * @param {string } repoPath
89
+ * @param {string } name
84
90
* @param {object } options stage: boolean, commit: boolean
85
91
* @returns {Promise<void> } options stage: boolean, commit: boolean
86
92
*/
@@ -89,7 +95,7 @@ const addFileToRepo = async (repoPath, name, options = { stage: false, commit: f
89
95
if ( options . branch ) {
90
96
await repo . checkout ( options . branch , { '-q' : true , 'b' : true } ) ;
91
97
}
92
- writeFileSync ( join ( repoPath , name ) , '' ) ;
98
+ await fs . promises . writeFile ( join ( repoPath , name ) , '' ) ;
93
99
if ( options . stage ) {
94
100
await repo . add ( name ) ;
95
101
if ( options . commit ) {
@@ -101,7 +107,7 @@ const addFileToRepo = async (repoPath, name, options = { stage: false, commit: f
101
107
/**
102
108
* Check if repo has untracked or uncommitted changes
103
109
*
104
- * @param {String } repoPath to repository
110
+ * @param {string } repoPath to repository
105
111
* @returns {Promise<Boolean> }
106
112
*/
107
113
const isDirty = async ( repoPath ) => {
@@ -114,8 +120,8 @@ const isDirty = async (repoPath) => {
114
120
/**
115
121
* Retrieve current branch
116
122
*
117
- * @param {String } repoPath
118
- * @returns {Promise<String > } branch name
123
+ * @param {string } repoPath
124
+ * @returns {Promise<string > } branch name
119
125
*/
120
126
const currentBranch = async ( repoPath ) => {
121
127
try {
@@ -130,9 +136,9 @@ const currentBranch = async (repoPath) => {
130
136
/**
131
137
* Push local to remote
132
138
*
133
- * @param {String } repoPath
134
- * @param {String } remote
135
- * @param {String } branch
139
+ * @param {string } repoPath
140
+ * @param {string } remote
141
+ * @param {string } branch
136
142
* @returns {Promise<string> } branch
137
143
*/
138
144
const push = ( repoPath , remote = 'origin' , branch = 'all' ) => {
@@ -146,10 +152,10 @@ const push = (repoPath, remote = 'origin', branch = 'all') => {
146
152
/**
147
153
* Pull from remote
148
154
*
149
- * @param {String } repoPath
150
- * @param {String } remote
151
- * @param {String } branch
152
- * @returns {Promise<String > }
155
+ * @param {string } repoPath
156
+ * @param {string } remote
157
+ * @param {string } branch
158
+ * @returns {Promise<string > }
153
159
*/
154
160
const pull = ( repoPath , remote = 'origin' , branch = 'master' ) => {
155
161
return git ( repoPath ) . pull ( remote , branch , { '-q' : true } ) ;
@@ -158,7 +164,7 @@ const pull = (repoPath, remote = 'origin', branch = 'master') => {
158
164
/**
159
165
* Fetch remotes quietly
160
166
*
161
- * @param {String } repoPath
167
+ * @param {string } repoPath
162
168
*/
163
169
const fetchRemotes = ( repoPath ) => {
164
170
return git ( repoPath ) . fetch ( { '--quiet' : true , '--all' : true } ) ;
@@ -167,35 +173,45 @@ const fetchRemotes = (repoPath) => {
167
173
/**
168
174
* Create new local commit
169
175
*
170
- * @param {String } repoPath
171
- * @param {String | null } fileName - optional file name
172
- * @param {String } branch - optional
173
- * @returns {Promise<String > } log subject
176
+ * @param {string } repoPath
177
+ * @param {string | null } fileName - optional file name
178
+ * @param {string } branch - optional
179
+ * @returns {Promise<string > } log subject
174
180
*/
175
181
const addCommit = async ( repoPath , fileName , branch ) => {
176
182
const repo = git ( repoPath ) ;
177
- const _name = fileName || randomize ( 'Aa0' , commitLength ) ;
178
- writeFileSync ( join ( repoPath , _name ) , '' ) ;
183
+
179
184
if ( branch ) {
180
- await repo . checkout ( branch , { '-q' : true , 'b' : true } ) ;
185
+ // create a new branch with simple-git
186
+ await repo . branch ( [ branch , 'HEAD' ] ) ;
187
+ await repo . checkout ( branch , { '-q' : true } ) ;
181
188
}
182
189
183
- return repo . add ( _name ) . commit ( `${ _name } ` )
184
- . log ( ) . latest . message ;
190
+ const _name = fileName || randomize ( 'Aa0' , commitLength ) ;
191
+ await fs . promises . writeFile ( join ( repoPath , _name ) , '' ) ;
192
+
193
+ try {
194
+ const result = await repo . add ( _name ) . commit ( `${ _name } ` )
195
+ . log ( ) ;
196
+ return result ?. latest ?. message ;
197
+ }
198
+ catch ( err ) {
199
+ console . log ( 'wtf' ) ;
200
+ }
185
201
} ;
186
202
187
203
/**
188
204
* Create new local commit with message
189
205
*
190
- * @param {String } repoPath
191
- * @param {String } message
192
- * @param {String } branch - optional
193
- * @returns {Promise<String > } log subject
206
+ * @param {string } repoPath
207
+ * @param {string } message
208
+ * @param {string } branch - optional
209
+ * @returns {Promise<string > } log subject
194
210
*/
195
211
const addCommitWithMessage = async ( repoPath , message , branch ) => {
196
212
const repo = git ( repoPath ) ;
197
213
const _name = randomize ( 'Aa0' , commitLength ) ;
198
- writeFileSync ( join ( repoPath , _name ) , '' ) ;
214
+ await fs . promises . writeFile ( join ( repoPath , _name ) , '' ) ;
199
215
if ( branch ) {
200
216
await repo . checkout ( branch , { '-q' : true , 'b' : true } ) ;
201
217
}
@@ -207,9 +223,9 @@ const addCommitWithMessage = async (repoPath, message, branch) => {
207
223
/**
208
224
* Delete file from repository
209
225
*
210
- * @param {String } repoPath
211
- * @param {String } name
212
- * @returns {Promise<String > } name
226
+ * @param {string } repoPath
227
+ * @param {string } name
228
+ * @returns {Promise<string > } name
213
229
*/
214
230
const deleteFile = ( repoPath , name ) => {
215
231
return git ( repoPath ) . rm ( name )
@@ -220,8 +236,8 @@ const deleteFile = (repoPath, name) => {
220
236
/**
221
237
* Returns branch log
222
238
*
223
- * @param {String } repoPath
224
- * @param {String } branch
239
+ * @param {string } repoPath
240
+ * @param {string } branch
225
241
* @returns {Array }
226
242
*/
227
243
const log = ( repoPath , branch = 'master' ) => {
0 commit comments