Skip to content

Commit 18ca368

Browse files
committed
move and repair git spec/tools
1 parent fcb126e commit 18ca368

File tree

3 files changed

+123
-106
lines changed

3 files changed

+123
-106
lines changed

src/common/git-tools.js

+74-58
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* @module Git-Tools
33
*/
44
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');
77
const randomize = require('randomatic');
88
const { createTempFolder } = require('./temp');
99
const git = require('simple-git');
@@ -14,7 +14,7 @@ const commitLength = 20;
1414
* initializes and empty git repository
1515
* into a temp folder within the base temp folder
1616
*
17-
* @param {String} nameOfFileToCommit - name
17+
* @param {string} nameOfFileToCommit - name
1818
*/
1919
const createRepo = async (nameOfFileToCommit = '') => {
2020
const path = createTempFolder();
@@ -30,46 +30,52 @@ const createRepo = async (nameOfFileToCommit = '') => {
3030
};
3131
const createBareRepo = createRepo;
3232

33-
// mkdirSync, readdirSync, lstatSync, copyFileSync
3433
/**
3534
* Copy folder recursively
3635
*
3736
* @private
38-
* @param {String} src
39-
* @param {String} dest
37+
* @param {string} src
38+
* @param {string} dest
39+
* @returns {Promise}
4040
*/
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);
5154
}
52-
});
53-
};
55+
}
56+
catch (error) {
57+
console.error('An error occurred:', error);
58+
}
59+
}
5460

5561
/**
5662
* Replicates a repository
5763
*
58-
* @param {String} repoPath
59-
* @returns {String} path to new repo
64+
* @param {string} repoPath
65+
* @returns {Promise<string>} path to new repo
6066
*/
61-
const duplicateRepo = (repoPath) => {
67+
const duplicateRepo = async (repoPath) => {
6268
const newPath = createTempFolder();
63-
copyFolder(repoPath, newPath);
69+
await copyFolder(repoPath, newPath);
6470
return newPath;
6571
};
6672

6773
/**
6874
* Adds remote from src to target repo
6975
*
70-
* @param {String} srcRepoPath
71-
* @param {String} targetRepoPath
72-
* @param {String} remoteName
76+
* @param {string} srcRepoPath
77+
* @param {string} targetRepoPath
78+
* @param {string} remoteName
7379
* @return {Promise}
7480
*/
7581
const addRemote = (srcRepoPath, targetRepoPath, remoteName = 'origin') => {
@@ -79,8 +85,8 @@ const addRemote = (srcRepoPath, targetRepoPath, remoteName = 'origin') => {
7985
/**
8086
* adds a file to a git repository
8187
*
82-
* @param {String} repoPath
83-
* @param {String} name
88+
* @param {string} repoPath
89+
* @param {string} name
8490
* @param {object} options stage: boolean, commit: boolean
8591
* @returns {Promise<void>} options stage: boolean, commit: boolean
8692
*/
@@ -89,7 +95,7 @@ const addFileToRepo = async (repoPath, name, options = { stage: false, commit: f
8995
if(options.branch) {
9096
await repo.checkout(options.branch, { '-q': true, 'b': true });
9197
}
92-
writeFileSync(join(repoPath, name), '');
98+
await fs.promises.writeFile(join(repoPath, name), '');
9399
if(options.stage) {
94100
await repo.add(name);
95101
if(options.commit) {
@@ -101,7 +107,7 @@ const addFileToRepo = async (repoPath, name, options = { stage: false, commit: f
101107
/**
102108
* Check if repo has untracked or uncommitted changes
103109
*
104-
* @param {String} repoPath to repository
110+
* @param {string} repoPath to repository
105111
* @returns {Promise<Boolean>}
106112
*/
107113
const isDirty = async (repoPath) => {
@@ -114,8 +120,8 @@ const isDirty = async (repoPath) => {
114120
/**
115121
* Retrieve current branch
116122
*
117-
* @param {String} repoPath
118-
* @returns {Promise<String>} branch name
123+
* @param {string} repoPath
124+
* @returns {Promise<string>} branch name
119125
*/
120126
const currentBranch = async (repoPath) => {
121127
try {
@@ -130,9 +136,9 @@ const currentBranch = async (repoPath) => {
130136
/**
131137
* Push local to remote
132138
*
133-
* @param {String} repoPath
134-
* @param {String} remote
135-
* @param {String} branch
139+
* @param {string} repoPath
140+
* @param {string} remote
141+
* @param {string} branch
136142
* @returns {Promise<string>} branch
137143
*/
138144
const push = (repoPath, remote = 'origin', branch = 'all') => {
@@ -146,10 +152,10 @@ const push = (repoPath, remote = 'origin', branch = 'all') => {
146152
/**
147153
* Pull from remote
148154
*
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>}
153159
*/
154160
const pull = (repoPath, remote = 'origin', branch = 'master') => {
155161
return git(repoPath).pull(remote, branch, { '-q': true });
@@ -158,7 +164,7 @@ const pull = (repoPath, remote = 'origin', branch = 'master') => {
158164
/**
159165
* Fetch remotes quietly
160166
*
161-
* @param {String} repoPath
167+
* @param {string} repoPath
162168
*/
163169
const fetchRemotes = (repoPath) => {
164170
return git(repoPath).fetch({ '--quiet': true, '--all': true });
@@ -167,35 +173,45 @@ const fetchRemotes = (repoPath) => {
167173
/**
168174
* Create new local commit
169175
*
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
174180
*/
175181
const addCommit = async (repoPath, fileName, branch) => {
176182
const repo = git(repoPath);
177-
const _name = fileName || randomize('Aa0', commitLength);
178-
writeFileSync(join(repoPath, _name), '');
183+
179184
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 });
181188
}
182189

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+
}
185201
};
186202

187203
/**
188204
* Create new local commit with message
189205
*
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
194210
*/
195211
const addCommitWithMessage = async (repoPath, message, branch) => {
196212
const repo = git(repoPath);
197213
const _name = randomize('Aa0', commitLength);
198-
writeFileSync(join(repoPath, _name), '');
214+
await fs.promises.writeFile(join(repoPath, _name), '');
199215
if(branch) {
200216
await repo.checkout(branch, { '-q': true, 'b': true });
201217
}
@@ -207,9 +223,9 @@ const addCommitWithMessage = async (repoPath, message, branch) => {
207223
/**
208224
* Delete file from repository
209225
*
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
213229
*/
214230
const deleteFile = (repoPath, name) => {
215231
return git(repoPath).rm(name)
@@ -220,8 +236,8 @@ const deleteFile = (repoPath, name) => {
220236
/**
221237
* Returns branch log
222238
*
223-
* @param {String} repoPath
224-
* @param {String} branch
239+
* @param {string} repoPath
240+
* @param {string} branch
225241
* @returns {Array}
226242
*/
227243
const log = (repoPath, branch = 'master') => {

src/common/git.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const git = require('simple-git');
33
/**
44
* Determines if repo has any commits on current branch
55
*
6-
* @param {String} repo - path to repository
7-
* @returns {Object} as Promise
6+
* @param {string} repo - path to repository
7+
* @returns {Promise<object>} as Promise
88
* @returns o.repo - same as repo
99
* @returns o.error - truthy if no commits
1010
*/
@@ -23,8 +23,8 @@ const hasCommits = async (repo) => {
2323
/**
2424
* Retrieves branch name from local repo
2525
*
26-
* @param {String} pathToProject
27-
* @returns {Promise} branch name
26+
* @param {string} pathToProject
27+
* @returns {Promise<string>} branch name
2828
*/
2929
const currentBranch = async (pathToProject) => {
3030
try {
@@ -40,8 +40,8 @@ const currentBranch = async (pathToProject) => {
4040
/**
4141
* Retrieves commit hash of local head
4242
*
43-
* @param {String} pathToProject [description]
44-
* @returns {String}
43+
* @param {string} pathToProject [description]
44+
* @returns {Promise<string>}
4545
*/
4646
const currentHash = (pathToProject) => {
4747
return git(pathToProject).revparse({ 'HEAD': true });
@@ -50,7 +50,7 @@ const currentHash = (pathToProject) => {
5050
/**
5151
* Updates local refs by performing a fetch
5252
*
53-
* @param {String} repoPath
53+
* @param {string} repoPath
5454
* @return {Promise}
5555
*/
5656
const gitFetch = (repoPath) => {
@@ -60,9 +60,9 @@ const gitFetch = (repoPath) => {
6060
/**
6161
* Counts number of commits a branch has which is not on master
6262
*
63-
* @param {String} repoPath
64-
* @param {String} branch
65-
* @param {Boolean} isTotal
63+
* @param {string} repoPath
64+
* @param {string} branch
65+
* @param {boolean} isTotal
6666
* @returns {Promise} number of commits
6767
*/
6868
const commitsDiff = async (repoPath, branch = 'master', isTotal = false) => {
@@ -83,8 +83,8 @@ const commitsDiff = async (repoPath, branch = 'master', isTotal = false) => {
8383
* Calculate ahead and behind commits counts
8484
*
8585
* @private
86-
* @param {Array} results
87-
* @returns {Object} { ahead: Integer, behind: Integer }
86+
* @param {array} results
87+
* @returns {{ahead:number, behind:number}} { ahead: Integer, behind: Integer }
8888
*/
8989
const diffResult = (results) => {
9090
const ahead = results[1];
@@ -95,7 +95,7 @@ const diffResult = (results) => {
9595
/**
9696
* Count commits which differ between origin and local
9797
*
98-
* @param {String} repoPath
98+
* @param {string} repoPath
9999
* @returns {Promise} { ahead: Integer, behind: Integer }
100100
*/
101101
const commitDiffCounts = (repoPath) => {
@@ -118,8 +118,8 @@ const commitDiffCounts = (repoPath) => {
118118
/**
119119
* Check status for un-committed changes including untracked files
120120
*
121-
* @param {String} repoPath - resolved path to repository
122-
* @return {Promise<Boolean>} true is dirty
121+
* @param {string} repoPath - resolved path to repository
122+
* @return {Promise<boolean>} true is dirty
123123
*/
124124
const isDirty = async (repoPath) => {
125125
const status = await git(repoPath).status();
@@ -131,8 +131,8 @@ const isDirty = async (repoPath) => {
131131
/**
132132
* Obtain log message of HEAD
133133
*
134-
* @param {String} repoPath
135-
* @param {Object} logger
134+
* @param {string} repoPath
135+
* @param {object} logger
136136
* @returns {Promise} standard out
137137
*/
138138
const headLog = async (repoPath, logger) => {

0 commit comments

Comments
 (0)