@ds-sfdc/sfparty 1.4.16 → 1.4.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/lib/gitUtils.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
import * as os from 'node:os'
|
|
3
|
+
import { execSync } from 'child_process'
|
|
3
4
|
|
|
4
5
|
const defaultDefinition = {
|
|
5
6
|
git: {
|
|
@@ -159,21 +160,42 @@ export function lastCommit({
|
|
|
159
160
|
try {
|
|
160
161
|
const folder = path.resolve(dir, '.sfdx', 'sfparty')
|
|
161
162
|
const filePath = path.resolve(folder, fileName)
|
|
162
|
-
let
|
|
163
|
+
let branchSpecificLastCommit
|
|
163
164
|
|
|
165
|
+
// Ensure the folder exists
|
|
164
166
|
fileUtils.createDirectory(folder)
|
|
167
|
+
|
|
165
168
|
if (existsSync(filePath)) {
|
|
166
169
|
const data = fileUtils.readFile(filePath)
|
|
167
|
-
|
|
168
|
-
|
|
170
|
+
|
|
171
|
+
// Determine the current branch name
|
|
172
|
+
const currentBranch = execSync(
|
|
173
|
+
`git rev-parse --abbrev-ref HEAD`,
|
|
174
|
+
{
|
|
175
|
+
cwd: dir,
|
|
176
|
+
encoding: 'utf-8',
|
|
177
|
+
},
|
|
178
|
+
).trim()
|
|
179
|
+
|
|
180
|
+
// Check if branch-specific last commit exists
|
|
181
|
+
if (
|
|
182
|
+
data.git.branches &&
|
|
183
|
+
data.git.branches[currentBranch] !== undefined
|
|
184
|
+
) {
|
|
185
|
+
branchSpecificLastCommit = data.git.branches[currentBranch]
|
|
186
|
+
} else {
|
|
187
|
+
// Fallback to top-level lastCommit if branch-specific commit doesn't exist
|
|
188
|
+
branchSpecificLastCommit = data.git.lastCommit
|
|
169
189
|
}
|
|
170
190
|
}
|
|
191
|
+
|
|
171
192
|
const latestCommit = execSync(`git log --format=format:%H -1`, {
|
|
172
193
|
cwd: dir,
|
|
173
194
|
encoding: 'utf-8',
|
|
174
195
|
})
|
|
196
|
+
|
|
175
197
|
resolve({
|
|
176
|
-
lastCommit:
|
|
198
|
+
lastCommit: branchSpecificLastCommit,
|
|
177
199
|
latestCommit: latestCommit,
|
|
178
200
|
})
|
|
179
201
|
} catch (error) {
|
|
@@ -187,10 +209,12 @@ export function updateLastCommit({ dir, latest, fileUtils, fs }) {
|
|
|
187
209
|
throw new Error(
|
|
188
210
|
`updateLastCommit received a ${typeof latest} instead of string`,
|
|
189
211
|
)
|
|
212
|
+
|
|
190
213
|
if (latest !== undefined) {
|
|
191
214
|
const folder = path.join(dir, '.sfdx', 'sfparty')
|
|
192
215
|
const fileName = path.join(folder, 'index.yaml')
|
|
193
216
|
let data = undefined
|
|
217
|
+
|
|
194
218
|
if (fileUtils.fileExists({ filePath: fileName, fs })) {
|
|
195
219
|
data = fileUtils.readFile(fileName)
|
|
196
220
|
}
|
|
@@ -199,7 +223,20 @@ export function updateLastCommit({ dir, latest, fileUtils, fs }) {
|
|
|
199
223
|
data = defaultDefinition
|
|
200
224
|
}
|
|
201
225
|
|
|
202
|
-
|
|
226
|
+
// Determine the current branch name
|
|
227
|
+
const currentBranch = execSync(`git rev-parse --abbrev-ref HEAD`, {
|
|
228
|
+
cwd: dir,
|
|
229
|
+
encoding: 'utf-8',
|
|
230
|
+
}).trim()
|
|
231
|
+
|
|
232
|
+
// Initialize branches object if not exist
|
|
233
|
+
if (!data.git.branches) {
|
|
234
|
+
data.git.branches = {}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Update the last commit for the current branch
|
|
238
|
+
data.git.branches[currentBranch] = latest
|
|
239
|
+
|
|
203
240
|
fileUtils.saveFile(data, fileName)
|
|
204
241
|
}
|
|
205
242
|
}
|
|
@@ -134,3 +134,30 @@ test('should throw an error when execSync returns an error', async () => {
|
|
|
134
134
|
expect(e.message).toBe('execSync error')
|
|
135
135
|
}
|
|
136
136
|
})
|
|
137
|
+
|
|
138
|
+
// Mock execSync to simulate branch detection
|
|
139
|
+
jest.mock('child_process', () => ({
|
|
140
|
+
...jest.requireActual('child_process'),
|
|
141
|
+
execSync: jest.fn((command) => {
|
|
142
|
+
if (command === 'git rev-parse --abbrev-ref HEAD') {
|
|
143
|
+
return 'currentBranch' // Simulate current branch name
|
|
144
|
+
}
|
|
145
|
+
return 'testCommit' // Simulate latest commit hash
|
|
146
|
+
}),
|
|
147
|
+
}))
|
|
148
|
+
|
|
149
|
+
test('should throw an error when execSync returns an error', async () => {
|
|
150
|
+
jest.spyOn(child_process, 'execSync').mockImplementationOnce(() => {
|
|
151
|
+
throw new Error('execSync error')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
await expect(
|
|
155
|
+
lastCommit({
|
|
156
|
+
dir: '/test',
|
|
157
|
+
fileUtils,
|
|
158
|
+
fs,
|
|
159
|
+
existsSync: fs.existsSync,
|
|
160
|
+
execSync: child_process.execSync,
|
|
161
|
+
}),
|
|
162
|
+
).rejects.toThrow('execSync error')
|
|
163
|
+
})
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
1
|
import * as fileUtils from '../../../src/lib/fileUtils.js'
|
|
3
2
|
import { updateLastCommit } from '../../../src/lib/gitUtils.js'
|
|
3
|
+
import { execSync } from 'child_process'
|
|
4
4
|
|
|
5
5
|
let dir, latest
|
|
6
6
|
|
|
@@ -33,41 +33,37 @@ it('should not update lastCommit property if latest is undefined', () => {
|
|
|
33
33
|
expect(fileUtils.saveFile).not.toHaveBeenCalled()
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
//
|
|
36
|
+
jest.mock('child_process', () => ({
|
|
37
|
+
...jest.requireActual('child_process'), // This line ensures that other child_process methods are still available as normal
|
|
38
|
+
execSync: jest.fn().mockReturnValue('mock-branch'),
|
|
39
|
+
}))
|
|
40
|
+
|
|
41
|
+
// Then in your test:
|
|
42
|
+
it('should update lastCommit property in index.yaml for the current branch', () => {
|
|
38
43
|
fileUtils.fileExists = jest.fn(() => true)
|
|
39
|
-
// mock the readFile method of fileUtils
|
|
40
44
|
fileUtils.readFile = jest.fn(() => ({
|
|
41
|
-
git: {
|
|
45
|
+
git: { branches: { 'mock-branch': '1111111111abcdef' } },
|
|
42
46
|
}))
|
|
43
|
-
// mock the saveFile method of fileUtils
|
|
44
47
|
fileUtils.saveFile = jest.fn()
|
|
48
|
+
|
|
45
49
|
updateLastCommit({ dir, latest, fileUtils })
|
|
46
|
-
|
|
50
|
+
|
|
47
51
|
expect(fileUtils.saveFile).toHaveBeenCalledWith(
|
|
48
|
-
{ git: {
|
|
52
|
+
{ git: { branches: { 'mock-branch': latest } } },
|
|
49
53
|
'/test/directory/.sfdx/sfparty/index.yaml',
|
|
50
54
|
)
|
|
51
55
|
})
|
|
52
56
|
|
|
53
|
-
it('should
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
updateLastCommit({ dir, latest, fileUtils })
|
|
58
|
-
expect(fileUtils.fileExists).toHaveBeenCalled()
|
|
59
|
-
expect(fileUtils.readFile).not.toHaveBeenCalled()
|
|
60
|
-
expect(fileUtils.saveFile).toHaveBeenCalled()
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
it('should save the default definition if file does not exist', () => {
|
|
57
|
+
it('should save the default definition with branches object if file does not exist', () => {
|
|
58
|
+
jest.mock('child_process', () => ({
|
|
59
|
+
execSync: jest.fn().mockReturnValue('mock-branch'),
|
|
60
|
+
}))
|
|
64
61
|
fileUtils.fileExists = jest.fn(() => false)
|
|
65
|
-
fileUtils.readFile = jest.fn()
|
|
66
62
|
fileUtils.saveFile = jest.fn()
|
|
67
63
|
|
|
68
|
-
const
|
|
64
|
+
const defaultDefinitionWithBranches = {
|
|
69
65
|
git: {
|
|
70
|
-
|
|
66
|
+
branches: { 'mock-branch': latest },
|
|
71
67
|
},
|
|
72
68
|
local: {
|
|
73
69
|
lastDate: undefined,
|
|
@@ -75,10 +71,9 @@ it('should save the default definition if file does not exist', () => {
|
|
|
75
71
|
}
|
|
76
72
|
|
|
77
73
|
updateLastCommit({ dir, latest, fileUtils })
|
|
78
|
-
|
|
79
|
-
expect(fileUtils.readFile).not.toHaveBeenCalled()
|
|
74
|
+
|
|
80
75
|
expect(fileUtils.saveFile).toHaveBeenCalledWith(
|
|
81
|
-
|
|
76
|
+
defaultDefinitionWithBranches,
|
|
82
77
|
'/test/directory/.sfdx/sfparty/index.yaml',
|
|
83
78
|
)
|
|
84
79
|
})
|