From 61deea9c25e458be4ef2b2537dec4f37d307d906 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Mon, 19 Jul 2021 19:59:29 -0700 Subject: [PATCH] refactor(template-base): replace exec/promisify with @malept/cross-spawn-promise (#2391) --- packages/template/base/package.json | 4 +++- .../template/base/src/determine-author.ts | 16 ++++++------- .../base/test/determine-author_spec.ts | 24 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/template/base/package.json b/packages/template/base/package.json index 7ef84574bf..3c8c756ac1 100644 --- a/packages/template/base/package.json +++ b/packages/template/base/package.json @@ -8,7 +8,7 @@ "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" @@ -16,6 +16,7 @@ "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" @@ -23,6 +24,7 @@ "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" } diff --git a/packages/template/base/src/determine-author.ts b/packages/template/base/src/determine-author.ts index d2e755043c..bb8c24d3f9 100644 --- a/packages/template/base/src/determine-author.ts +++ b/packages/template/base/src/determine-author.ts @@ -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 { + const value = await spawn('git', ['config', '--get', name], { cwd }); + return value.trim(); +} const getAuthorFromGitConfig = async (dir: string): Promise => { 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); diff --git a/packages/template/base/test/determine-author_spec.ts b/packages/template/base/test/determine-author_spec.ts index d9c7c90393..dd81fff70a 100644 --- a/packages/template/base/test/determine-author_spec.ts +++ b/packages/template/base/test/determine-author_spec.ts @@ -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 => { + 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; });