Skip to content

Commit

Permalink
refactor(template-base): replace exec/promisify with @malept/cross-sp…
Browse files Browse the repository at this point in the history
…awn-promise (#2391)
  • Loading branch information
malept authored Jul 20, 2021
1 parent 9b7a185 commit 61deea9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
4 changes: 3 additions & 1 deletion packages/template/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
"main": "dist/BaseTemplate.js",
"typings": "dist/BaseTemplate.d.ts",
"scripts": {
"test": "echo No Tests"
"test": "mocha --config ../../../.mocharc.js test/**/*_spec.ts"
},
"engines": {
"node": ">= 12.13.0"
},
"dependencies": {
"@electron-forge/async-ora": "6.0.0-beta.58",
"@electron-forge/shared-types": "6.0.0-beta.58",
"@malept/cross-spawn-promise": "^2.0.0",
"debug": "^4.3.1",
"fs-extra": "^10.0.0",
"username": "^5.1.0"
},
"devDependencies": {
"@electron-forge/test-utils": "6.0.0-beta.58",
"chai": "^4.3.3",
"mocha": "^9.0.1",
"proxyquire": "^2.1.3",
"sinon": "^11.1.1"
}
Expand Down
16 changes: 7 additions & 9 deletions packages/template/base/src/determine-author.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import childProcess from 'child_process';
import debug from 'debug';
import { PackagePerson } from '@electron-forge/shared-types';
import { promisify } from 'util';
import { spawn } from '@malept/cross-spawn-promise';
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();
};
async function getGitConfig(name: string, cwd: string): Promise<string> {
const value = await spawn('git', ['config', '--get', name], { cwd });
return value.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);
const name = await getGitConfig('user.name', dir);
const email = await getGitConfig('user.email', dir);
return { name, email };
} catch (err) {
d('Error when getting git config:', err);
Expand Down
24 changes: 12 additions & 12 deletions packages/template/base/test/determine-author_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ describe('determineAuthor', () => {
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')) {
const fakeSpawn = async (cmd: string, args: string[], _options: { cwd: string }): Promise<string> => {
if (args.includes('user.name')) {
if (returnGitUsername) {
callback(null, { stdout: 'Foo Bar\n' });
} else {
callback(new Error('Not returning username'));
return Promise.resolve('Foo Bar\n');
}
} else if (cmd.includes('user.email')) {

throw new Error('Not returning username');
} else if (args.includes('user.email')) {
if (returnGitEmail) {
callback(null, { stdout: 'foo@example.com\n' });
} else {
callback(new Error('Not returning email'));
return Promise.resolve('foo@example.com\n');
}
} else {
callback(new Error('Unknown cmd'));

throw new Error('Not returning email');
}

throw new Error(`Unknown command: ${cmd} ${args.join(' ')}`);
};

beforeEach(() => {
determineAuthor = proxyquire.noCallThru().load('../src/determine-author', {
child_process: { exec: sinon.stub().callsFake(fakeExec) },
'@malept/cross-spawn-promise': { spawn: sinon.stub().callsFake(fakeSpawn) },
username: sinon.stub().resolves('fromUsername'),
}).default;
});
Expand Down

0 comments on commit 61deea9

Please sign in to comment.