Skip to content
Permalink
Browse files

feat(core): use git config to determine author before username (#920)

  • Loading branch information
pd4d10 authored and malept committed Jul 1, 2019
1 parent eedf549 commit 57e30a47b4955bd0fed4e06ee40c756b6c12d5dc
@@ -2,11 +2,11 @@ import { asyncOra } from '@electron-forge/async-ora';
import debug from 'debug';
import fs from 'fs-extra';
import path from 'path';
import username from 'username';

import { setInitialForgeConfig } from '../../util/forge-config';
import determineAuthor from '../../util/determine-author';
import installDepList, { DepType, DepVersionRestriction } from '../../util/install-dependencies';
import { readRawPackageJson } from '../../util/read-package-json';
import { setInitialForgeConfig } from '../../util/forge-config';

const d = debug('electron-forge:init:npm');
const corePackage = fs.readJsonSync(path.resolve(__dirname, '../../../package.json'));
@@ -30,7 +30,7 @@ export default async (dir: string) => {
const packageJSON = await readRawPackageJson(path.resolve(__dirname, '../../../tmpl'));
// eslint-disable-next-line no-multi-assign
packageJSON.productName = packageJSON.name = path.basename(dir).toLowerCase();
packageJSON.author = await username();
packageJSON.author = await determineAuthor(dir);
setInitialForgeConfig(packageJSON);

packageJSON.scripts.lint = 'echo "No linting configured"';
@@ -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();
@@ -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');
});
});
@@ -1,11 +1,8 @@
import parseAuthor from 'parse-author';
import { PackagePerson } from '@electron-forge/shared-types';

type AuthorType = string | {
name: string;
} | undefined;

export default function getNameFromAuthor(author: AuthorType) {
let publisher: AuthorType = author || '';
export default function getNameFromAuthor(author: PackagePerson) {
let publisher: PackagePerson = author || '';

if (typeof publisher === 'string') {
publisher = parseAuthor(publisher);
@@ -1,11 +1,8 @@
import parseAuthor from 'parse-author';
import { PackagePerson } from '@electron-forge/shared-types';

type AuthorType = string | {
name: string;
} | undefined;

export default function getNameFromAuthor(author: AuthorType) {
let publisher: AuthorType = author || '';
export default function getNameFromAuthor(author: PackagePerson) {
let publisher: PackagePerson = author || '';

if (typeof publisher === 'string') {
publisher = parseAuthor(publisher);
@@ -121,3 +121,9 @@ export interface ForgeTemplate {
devDependencies?: string[];
initializeTemplate?: (dir: string) => void;
}

export type PackagePerson = undefined | string | {
name: string;
email?: string;
url?: string;
};

0 comments on commit 57e30a4

Please sign in to comment.
You can’t perform that action at this time.