-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): use git config to determine author before username (#920)
- Loading branch information
Showing
6 changed files
with
95 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import childProcess from 'child_process'; | ||
import debug from 'debug'; | ||
import { PackagePerson } from '@electron-forge/shared-types'; | ||
import { promisify } from 'util'; | ||
import username from 'username'; | ||
|
||
const d = debug('electron-forge:determine-author'); | ||
const exec = promisify(childProcess.exec); | ||
|
||
const execAndTrimResult = async (command: string, dir: string) => { | ||
const { stdout } = await exec(command, { cwd: dir }); | ||
return stdout.trim(); | ||
}; | ||
|
||
const getAuthorFromGitConfig = async (dir: string): Promise<PackagePerson> => { | ||
try { | ||
const name = await execAndTrimResult('git config --get user.name', dir); | ||
const email = await execAndTrimResult('git config --get user.email', dir); | ||
return { name, email }; | ||
} catch (err) { | ||
d('Error when getting git config:', err); | ||
return undefined; | ||
} | ||
}; | ||
|
||
export default async (dir: string) => (await getAuthorFromGitConfig(dir)) || username(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { expect } from 'chai'; | ||
import proxyquire from 'proxyquire'; | ||
import sinon from 'sinon'; | ||
|
||
import { PackagePerson } from '@electron-forge/shared-types'; | ||
|
||
describe('determineAuthor', () => { | ||
let determineAuthor: (dir: string) => Promise<PackagePerson>; | ||
let returnGitUsername = true; | ||
let returnGitEmail = true; | ||
// eslint-disable-next-line max-len | ||
const fakeExec = (cmd: string, options: { cwd: string }, callback: (err: Error | null, result?: { stdout?: string, stderr?: string }) => void) => { | ||
if (cmd.includes('user.name')) { | ||
if (returnGitUsername) { | ||
callback(null, { stdout: 'Foo Bar\n' }); | ||
} else { | ||
callback(new Error('Not returning username')); | ||
} | ||
} else if (cmd.includes('user.email')) { | ||
if (returnGitEmail) { | ||
callback(null, { stdout: 'foo@example.com\n' }); | ||
} else { | ||
callback(new Error('Not returning email')); | ||
} | ||
} else { | ||
callback(new Error('Unknown cmd')); | ||
} | ||
}; | ||
|
||
beforeEach(() => { | ||
determineAuthor = proxyquire.noCallThru().load('../../src/util/determine-author', { | ||
child_process: { exec: sinon.stub().callsFake(fakeExec) }, | ||
username: sinon.stub().resolves('fromUsername'), | ||
}).default; | ||
}); | ||
|
||
it('returns git config if both name and email are set', async () => { | ||
returnGitUsername = true; | ||
returnGitEmail = true; | ||
expect(await determineAuthor('foo')).to.deep.equal({ name: 'Foo Bar', email: 'foo@example.com' }); | ||
}); | ||
|
||
it('returns username if only name is set', async () => { | ||
returnGitUsername = true; | ||
returnGitEmail = false; | ||
expect(await determineAuthor('foo')).to.equal('fromUsername'); | ||
}); | ||
|
||
it('returns username if only name is set', async () => { | ||
returnGitUsername = false; | ||
returnGitEmail = true; | ||
expect(await determineAuthor('foo')).to.equal('fromUsername'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters