Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(readMetadata): add support for macOS target apps #67

Merged
merged 2 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/readmetadata.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
'use strict'

const { promisify } = require('util')

const asar = require('asar')
const fs = require('fs-extra')
const glob = promisify(require('glob'))
const path = require('path')
const { wrapError } = require('./error')

async function readPackageJSONFromUnpackedApp (options) {
const appPackageJSONPath = path.join(options.src, 'resources', 'app', 'package.json')
async function determineResourcesDir (src) {
if (await fs.pathExists(path.join(src, 'resources'))) {
return 'resources'
}

return (await glob('*.app/Contents/Resources', { cwd: src }))[0]
}

async function readPackageJSONFromUnpackedApp (resourcesDir, options) {
const appPackageJSONPath = path.join(options.src, resourcesDir, 'app', 'package.json')
options.logger(`Reading package metadata from ${appPackageJSONPath}`)

return fs.readJson(appPackageJSONPath)
Expand All @@ -16,8 +27,9 @@ async function readPackageJSONFromUnpackedApp (options) {
}

/**
* Read `package.json` either from `resources/app.asar` (if the app is packaged)
* or from `resources/app/package.json` (if it is not).
* Read `package.json` either from `$RESOURCES_DIR/app.asar` (if the app is packaged)
* or from `$RESOURCES_DIR/app/package.json` (if it is not). `$RESOURCES_DIR` is either
* `AppName.app/Contents/Resources` on macOS, or `resources` on other platforms.
*
* Options used:
*
Expand All @@ -26,14 +38,18 @@ async function readPackageJSONFromUnpackedApp (options) {
* `debug('electron-installer-something:some-module')`
*/
module.exports = async function readMetadata (options) {
const appAsarPath = path.join(options.src, 'resources/app.asar')

return wrapError('reading package metadata', async () => {
const resourcesDir = await determineResourcesDir(options.src)
if (!resourcesDir) {
throw new Error('Could not determine resources directory in Electron app')
}
const appAsarPath = path.join(options.src, resourcesDir, 'app.asar')

if (await fs.pathExists(appAsarPath)) {
options.logger(`Reading package metadata from ${appAsarPath}`)
return JSON.parse(asar.extractFile(appAsarPath, 'package.json'))
} else {
return readPackageJSONFromUnpackedApp(options)
return readPackageJSONFromUnpackedApp(resourcesDir, options)
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "bartest",
"description": "Bar test (without asar).",
"version": "0.0.1",
"license": "MIT",

"private": true,
"main": "index.js",

"copyright": "Copyright 2015 Daniel Perez Alvarez",
"projectId": "org.electron-userland.Bar",
"projectName": "Bar",
"productName": "Bar",
"productUrl": "https://github.com/electron-userland/",
"companyName": "Daniel Perez Alvarez",
"companyUrl": "https://github.com/electron-userland/",

"author": "Daniel Perez Alvarez <[email protected]> (https://unindented.org/)"
}
18 changes: 16 additions & 2 deletions test/readmetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ test('readMetadata for app without asar', async t => {
t.regex(packageJSON.description, /without asar/)
})

test('readMetadata for invalid app', t => {
return t.throwsAsync(readMetadata({ src: path.join(__dirname, 'fixtures'), logger: log => log }), { message: /Could not find, read, or parse/ })
test('readMetadata for macOS app with asar', async t => {
const packageJSON = await readMetadata({ src: path.join(__dirname, 'fixtures', 'macOS-app-with-asar'), logger: log => log })
t.regex(packageJSON.description, /with asar/)
})

test('readMetadata for macOS app without asar', async t => {
const packageJSON = await readMetadata({ src: path.join(__dirname, 'fixtures', 'macOS-app-without-asar'), logger: log => log })
t.regex(packageJSON.description, /without asar/)
})

test('readMetadata for app without a resources directory', t => {
return t.throwsAsync(readMetadata({ src: path.join(__dirname, 'fixtures'), logger: log => log }), { message: /Could not determine resources directory/ })
})

test('readMetadata for app with a bad package.json', t => {
return t.throwsAsync(readMetadata({ src: path.join(__dirname, 'fixtures', 'app-with-bad-package-json'), logger: log => log }), { message: /Could not find, read, or parse/ })
})