Skip to content

Commit

Permalink
Infer win32metadata from package.json/top-level options
Browse files Browse the repository at this point in the history
Closes #297.
Fixes #611.
  • Loading branch information
malept committed Jun 6, 2017
1 parent 46d1ed3 commit cf83b91
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 18 deletions.
12 changes: 6 additions & 6 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,15 @@ option. Maps to the `CFBundleURLName` metadata property.
*Object*

Object (also known as a "hash") of application metadata to embed into the executable:
- `CompanyName`
- `FileDescription`
- `OriginalFilename`
- `ProductName`
- `InternalName`
- `CompanyName` (defaults to `author` name from the nearest `package.json`)
- `FileDescription` (defaults to `description` from the nearest `package.json`)
- `OriginalFilename` (defaults to renamed `.exe` file)
- `ProductName` (defaults to either `productName` or `name` from the nearest `package.json`)
- `InternalName` (defaults to either `productName` or `name` from the nearest `package.json`)
- `requested-execution-level`
- `application-manifest`

For more information, see the [node-rcedit module](https://github.com/electron/node-rcedit).
For more information, see the [`node-rcedit` module](https://github.com/electron/node-rcedit).

## callback

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ module.exports = pify(function packager (opts, cb) {

common.camelCase(opts, true)

getMetadataFromPackageJSON(opts, path.resolve(process.cwd(), opts.dir) || process.cwd(), function (err) {
getMetadataFromPackageJSON(platforms, opts, path.resolve(process.cwd(), opts.dir) || process.cwd(), function (err) {
if (err) return cb(err)

if (/ Helper$/.test(opts.name)) {
Expand Down
27 changes: 26 additions & 1 deletion infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const debug = require('debug')('electron-packager')
const getPackageInfo = require('get-package-info')
const parseAuthor = require('parse-author')
const path = require('path')
const resolve = require('resolve')

Expand Down Expand Up @@ -61,7 +62,7 @@ function getVersion (opts, electronProp, cb) {
}
}

module.exports = function getMetadataFromPackageJSON (opts, dir, cb) {
module.exports = function getMetadataFromPackageJSON (platforms, opts, dir, cb) {
let props = []
if (!opts.name) props.push(['productName', 'name'])
if (!opts.appVersion) props.push('version')
Expand All @@ -76,6 +77,16 @@ module.exports = function getMetadataFromPackageJSON (opts, dir, cb) {
])
}

if (platforms.indexOf('win32') !== -1) {
if (!(opts.win32metadata && opts.win32metadata.FileDescription)) {
props.push('description')
}

if (!(opts.win32metadata && opts.win32metadata.CompanyName)) {
props.push('author')
}
}

// Name and version provided, no need to infer
if (props.length === 0) return cb(null)

Expand Down Expand Up @@ -110,6 +121,20 @@ module.exports = function getMetadataFromPackageJSON (opts, dir, cb) {
opts.appVersion = result.values.version
}

if ((result.values.description || result.values.author) && !opts.win32metadata) {
opts.win32metadata = {}
}

if (result.values.description) {
debug(`Inferring win32metadata.FileDescription from description in ${result.source.description.src}`)
opts.win32metadata.FileDescription = result.values.description
}

if (result.values.author) {
debug(`Inferring win32metadata.CompanyName from description in ${result.source.author.src}`)
opts.win32metadata.CompanyName = parseAuthor(result.values.author).name
}

if (result.values['dependencies.electron']) {
return getVersion(opts, result.source['dependencies.electron'], cb)
} else {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"fs-extra": "^3.0.0",
"get-package-info": "^1.0.0",
"minimist": "^1.1.1",
"parse-author": "^2.0.0",
"pify": "^3.0.0",
"plist": "^2.0.0",
"rcedit": "^0.9.0",
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/infer-win32metadata/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"main": "main.js",
"productName": "MainJS",
"author": "Foo Bar <[email protected]>",
"description": "Some description",
"devDependencies": {
"electron-prebuilt-compile": "1.4.15"
}
}
38 changes: 36 additions & 2 deletions test/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function createInferElectronVersionTest (fixture, packageName) {
delete opts.electronVersion
opts.dir = path.join(__dirname, 'fixtures', fixture)

pify(getMetadataFromPackageJSON)(opts, opts.dir)
pify(getMetadataFromPackageJSON)([], opts, opts.dir)
.then((pkg) => {
const packageJSON = require(path.join(opts.dir, 'package.json'))
t.equal(opts.electronVersion, packageJSON.devDependencies[packageName], `The version should be inferred from installed ${packageName} version`)
Expand Down Expand Up @@ -71,7 +71,7 @@ function createInferMissingVersionTest (opts) {
delete opts.electronVersion
opts.dir = dir

return pify(getMetadataFromPackageJSON)(opts, dir)
return pify(getMetadataFromPackageJSON)([], opts, dir)
}).then(() => {
const packageJSON = require(path.join(opts.dir, 'package.json'))
t.equal(opts.electronVersion, packageJSON.devDependencies['electron'], 'The version should be inferred from installed electron module version')
Expand All @@ -80,6 +80,19 @@ function createInferMissingVersionTest (opts) {
}
}

function testInferWin32metadata (t, opts, expected, assertionMessage) {
t.timeoutAfter(config.timeout)
copyFixtureToTempDir('infer-win32metadata')
.then((dir) => {
opts.dir = dir

return pify(getMetadataFromPackageJSON)(['win32'], opts, dir)
}).then(() => {
t.deepEqual(opts.win32metadata, expected, assertionMessage)
return t.end()
}).catch(t.end)
}

function createInferMissingFieldsTest (opts) {
return createInferFailureTest(opts, 'infer-missing-fields')
}
Expand All @@ -100,6 +113,27 @@ util.testSinglePlatform('infer using `electron-prebuilt` package', createInferEl
util.testSinglePlatform('infer using `electron-prebuilt-compile` package', createInferElectronVersionTest('infer-electron-prebuilt-compile', 'electron-prebuilt-compile'))
util.testSinglePlatform('infer using `electron` package only', createInferMissingVersionTest)
util.testSinglePlatform('infer where `electron` version is preferred over `electron-prebuilt`', createInferElectronVersionTest('basic-renamed-to-electron', 'electron'))
util.testSinglePlatform('infer win32metadata', (opts) => {
return (t) => {
const expected = {
CompanyName: 'Foo Bar',
FileDescription: 'Some description'
}

testInferWin32metadata(t, opts, expected, 'win32metadata matches package.json values')
}
})
util.testSinglePlatform('do not infer win32metadata if it already exists', (opts) => {
return (t) => {
opts.win32metadata = {
CompanyName: 'Existing',
FileDescription: 'Existing description'
}
const expected = Object.assign({}, opts.win32metadata)

testInferWin32metadata(t, opts, expected, 'win32metadata did not update with package.json values')
}
})
util.testSinglePlatform('infer missing fields test', createInferMissingFieldsTest)
util.testSinglePlatform('infer with bad fields test', createInferWithBadFieldsTest)
util.testSinglePlatform('infer with malformed JSON test', createInferWithMalformedJSONTest)
Expand Down
12 changes: 12 additions & 0 deletions test/win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ test('error message unchanged when error not about wine', (t) => {
t.end()
})

test('win32metadata defaults', (t) => {
const opts = {
name: 'Win32 App'
}
const rcOpts = win32.generateRceditOptionsSansIcon(opts, 'Win32 App.exe')

t.equal(rcOpts['version-string'].InternalName, opts.name)
t.equal(rcOpts['version-string'].OriginalFilename, 'Win32 App.exe')
t.equal(rcOpts['version-string'].ProductName, opts.name)
t.end()
})

util.packagerTest('win32 executable name is based on sanitized app name', (t) => {
const opts = Object.assign({}, baseOpts, {name: '@username/package-name'})

Expand Down
10 changes: 5 additions & 5 deletions usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ win32metadata a list of sub-properties used to set the application metadata
e.g. --win32metadata.CompanyName="Company Inc."
or --win32metadata.ProductName="Product"
Properties supported:
- CompanyName
- FileDescription
- OriginalFilename
- ProductName
- InternalName
- CompanyName (default: author name from nearest package.json)
- FileDescription (default: description from nearest package.json)
- OriginalFilename (default: renamed exe)
- ProductName (default: appname)
- InternalName (default: appname)
- requested-execution-level (user, asInvoker, or requireAdministrator)
- application-manifest
10 changes: 7 additions & 3 deletions win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ const debug = require('debug')('electron-packager')
const path = require('path')
const series = require('run-series')

function generateRceditOptionsSansIcon (opts) {
const win32metadata = Object.assign({}, opts['version-string'], opts.win32metadata)
function generateRceditOptionsSansIcon (opts, newExeName) {
const win32metadata = Object.assign({
InternalName: opts.name,
OriginalFilename: newExeName,
ProductName: opts.name
}, opts['version-string'], opts.win32metadata)

let rcOpts = {'version-string': win32metadata}

Expand Down Expand Up @@ -56,7 +60,7 @@ module.exports = {
}
]

const rcOpts = generateRceditOptionsSansIcon(opts)
const rcOpts = generateRceditOptionsSansIcon(opts, newExeName)

if (opts.icon || opts.win32metadata || opts['version-string'] || opts.appCopyright || opts.appVersion || opts.buildVersion) {
operations.push(function (cb) {
Expand Down

0 comments on commit cf83b91

Please sign in to comment.