Skip to content

Commit

Permalink
refactor(maker): extract author name parsing into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
malept committed Jan 30, 2018
1 parent 52a6408 commit fa80cd3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 47 deletions.
24 changes: 5 additions & 19 deletions src/makers/win32/appx.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'fs';
import path from 'path';
import parseAuthor from 'parse-author';
import resolveCommand from 'cross-spawn/lib/util/resolveCommand';
import windowsStore from 'electron-windows-store';
import { isValidPublisherName, makeCert } from 'electron-windows-store/lib/sign';

import resolveCommand from 'cross-spawn/lib/util/resolveCommand';
import { ensureDirectory } from '../../util/ensure-output';
import configFn from '../../util/config-fn';
import getNameFromAuthor from '../../util/author-name';
import { ensureDirectory } from '../../util/ensure-output';

// electron-windows-store doesn't set its 'os' field even though it only runs on
// win32
Expand Down Expand Up @@ -48,22 +48,8 @@ export async function createDefaultCertificate(publisherName, { certFilePath, ce
return await makeCert(makeCertOptions);
}

export function getDistinguishedNameFromAuthor(author) {
let publisher = author || '';

if (typeof publisher === 'string') {
publisher = parseAuthor(publisher);
}

if (typeof publisher.name === 'string') {
publisher = publisher.name;
}

if (typeof publisher !== 'string') {
publisher = '';
}

return `CN=${publisher}`;
function getDistinguishedNameFromAuthor(author) {
return `CN=${getNameFromAuthor(author)}`;
}

export default async ({ dir, appName, targetArch, forgeConfig, packageJSON }) => {
Expand Down
4 changes: 2 additions & 2 deletions src/makers/win32/wix.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path';

import { ensureDirectory } from '../../util/ensure-output';
import getNameFromAuthor from '../../util/author-name';
import configFn from '../../util/config-fn';
import isInstalled from '../../util/is-installed';
import { getDistinguishedNameFromAuthor } from './appx';

export const isSupportedOnCurrentPlatform = async () => isInstalled('electron-wix-msi');

Expand All @@ -17,7 +17,7 @@ export default async ({ dir, appName, targetArch, forgeConfig, packageJSON }) =>
description: packageJSON.description,
name: appName,
version: packageJSON.version,
manufacturer: getDistinguishedNameFromAuthor(packageJSON.author).substr(3),
manufacturer: getNameFromAuthor(packageJSON.author),
exe: `${appName}.exe`,
}, configFn(forgeConfig.electronWixMSIConfig, targetArch), {
appDirectory: dir,
Expand Down
19 changes: 19 additions & 0 deletions src/util/author-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import parseAuthor from 'parse-author';

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

if (typeof publisher === 'string') {
publisher = parseAuthor(publisher);
}

if (typeof publisher.name === 'string') {
publisher = publisher.name;
}

if (typeof publisher !== 'string') {
publisher = '';
}

return publisher;
}
30 changes: 30 additions & 0 deletions test/fast/author-name_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from 'chai';

import getNameFromAuthor from '../../src/util/author-name';

describe('author-name', () => {
describe('getNameFromAuthor', () => {
[{
author: 'First Last',
expectedReturnValue: 'First Last',
}, {
author: 'First Last <[email protected]>',
expectedReturnValue: 'First Last',
}, {
author: {
name: 'First Last',
},
expectedReturnValue: 'First Last',
}, {
author: undefined,
expectedReturnValue: '',
}, {
author: '',
expectedReturnValue: '',
}].forEach((scenario) => {
it(`${JSON.stringify(scenario.author)} -> "${scenario.expectedReturnValue}"`, () => {
expect(getNameFromAuthor(scenario.author)).to.equal(scenario.expectedReturnValue);
});
});
});
});
27 changes: 1 addition & 26 deletions test/fast/makers/appx_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from 'path';
import fs from 'fs-extra';
import { expect } from 'chai';

import { getDistinguishedNameFromAuthor, createDefaultCertificate } from '../../../src/makers/win32/appx';
import { createDefaultCertificate } from '../../../src/makers/win32/appx';

describe('appx maker', () => {
describe('createDefaultCertificate', () => {
Expand Down Expand Up @@ -35,29 +35,4 @@ describe('appx maker', () => {
});
}
});

describe('getDistinguishedNameFromAuthor', () => {
[{
author: 'First Last',
expectedReturnValue: 'CN=First Last',
}, {
author: 'First Last <[email protected]>',
expectedReturnValue: 'CN=First Last',
}, {
author: {
name: 'First Last',
},
expectedReturnValue: 'CN=First Last',
}, {
author: undefined,
expectedReturnValue: 'CN=',
}, {
author: '',
expectedReturnValue: 'CN=',
}].forEach((scenario) => {
it(`${JSON.stringify(scenario.author)} -> "${scenario.expectedReturnValue}"`, () => {
expect(getDistinguishedNameFromAuthor(scenario.author)).to.equal(scenario.expectedReturnValue);
});
});
});
});

0 comments on commit fa80cd3

Please sign in to comment.