Skip to content

Commit

Permalink
feat(core): use git config to determine author before username (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 authored and malept committed Jul 1, 2019
1 parent eedf549 commit 57e30a4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/api/core/src/api/init-scripts/init-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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"';
Expand Down
26 changes: 26 additions & 0 deletions packages/api/core/src/util/determine-author.ts
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();
54 changes: 54 additions & 0 deletions packages/api/core/test/fast/determine-author_spec.ts
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');
});
});
9 changes: 3 additions & 6 deletions packages/maker/appx/src/util/author-name.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
9 changes: 3 additions & 6 deletions packages/maker/wix/src/util/author-name.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
6 changes: 6 additions & 0 deletions packages/utils/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.