Skip to content
Permalink
Browse files

refactor(maker): extract author name parsing into its own function

  • Loading branch information
malept committed Dec 1, 2017
1 parent 52a6408 commit fa80cd3dc8fb84e8f3a5833d33f2068076179f70
Showing with 57 additions and 47 deletions.
  1. +5 −19 src/makers/win32/appx.js
  2. +2 −2 src/makers/win32/wix.js
  3. +19 −0 src/util/author-name.js
  4. +30 −0 test/fast/author-name_spec.js
  5. +1 −26 test/fast/makers/appx_spec.js
@@ -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
@@ -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 }) => {
@@ -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');

@@ -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,
@@ -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;
}
@@ -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 <first.last@example.com>',
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);
});
});
});
});
@@ -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', () => {
@@ -35,29 +35,4 @@ describe('appx maker', () => {
});
}
});

describe('getDistinguishedNameFromAuthor', () => {
[{
author: 'First Last',
expectedReturnValue: 'CN=First Last',
}, {
author: 'First Last <first.last@example.com>',
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.
You can’t perform that action at this time.