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: use Node 8 syntax #28

Merged
merged 9 commits into from
May 29, 2019
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js: '6'
node_js: '8'
os: linux
dist: xenial
services:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Builds Snap files for Electron applications that have already been bundled and c

## Requirements

Requires Node 6 or greater, and [`snapcraft`](https://snapcraft.io).
Requires Node 8 or greater, and [`snapcraft`](https://snapcraft.io).

## Quick Start

Expand Down
2 changes: 1 addition & 1 deletion ci/script.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sudo docker run --interactive --tty --volume $(pwd):/code malept/electron-forge-container:latest /code/ci/docker.sh
sudo docker run --interactive --tty --volume $(pwd):/code malept/electron-forge-container:node-8 /code/ci/docker.sh
13 changes: 7 additions & 6 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Minimal example:
```javascript
const snap = require('electron-installer-snap')

snap(options)
.then(snapPath => console.log(`Created snap at ${snapPath}!`))
const snapPath = await snap(options)
console.log(`Created snap at ${snapPath}!`)
```

Full example with [Electron Packager](https://npm.im/electron-packager):
Expand All @@ -21,12 +21,13 @@ const snap = require('electron-installer-snap')

const arch = 'x64'

packager({dir: '/path/to/app', platform: 'linux', arch: arch})
.then(paths => snap({src: paths[0], arch: arch}))
.then(snapPath => console.log(`Created snap at ${snapPath}!`))
const paths = await packager({dir: '/path/to/app', platform: 'linux', arch: arch})
const snapPath = await snap({src: paths[0], arch: arch})
console.log(`Created snap at ${snapPath}!`)
```

If you need to use the callback pattern instead of the Promise pattern, look into the [`nodeify` module](https://npm.im/nodeify).
If you need to use the callback pattern instead of the `async`/`await` pattern, look into the
[`util.callbackify` function](https://nodejs.org/api/util.html#util_util_callbackify_original).

## `options`

Expand Down
15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,28 @@
"eslint": "^5.11.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-ava": "^6.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"nyc": "^14.0.0"
},
"dependencies": {
"cross-spawn-promise": "^0.10.1",
"debug": "^4.1.1",
"electron-installer-common": "^0.6.3",
"electron-installer-common": "^0.7.0",
"fs-extra": "^8.0.1",
"js-yaml": "^3.10.0",
"lodash.filter": "^4.6.0",
"lodash.merge": "^4.6.0",
"lodash.pull": "^4.1.0",
"nodeify": "^1.0.1",
"pify": "^4.0.1",
"semver": "^6.0.0",
"tmp-promise": "^1.0.3",
"semver": "^6.1.0",
"tmp-promise": "^2.0.1",
"which": "^1.3.0",
"yargs": "^13.2.2"
},
"engines": {
"node": ">= 6.0"
"node": ">= 8.0"
},
"eslintConfig": {
"extends": [
Expand All @@ -77,7 +75,6 @@
"ava"
],
"rules": {
"ava/prefer-async-await": 0,
"indent": [
"error",
2,
Expand Down
3 changes: 1 addition & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ function parseArgs () {

const filteredArgs = {}
const YARGS_KEYS = ['_', '$0', 'help']
for (const key in args) {
const value = args[key]
for (const [key, value] in Object.entries(args)) {
if (value !== undefined && !key.includes('-') && !YARGS_KEYS.includes(key)) {
filteredArgs[key] = value
}
Expand Down
6 changes: 3 additions & 3 deletions src/default_args.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function defaultArgsFromPackageJSON (packageJSON) {
}
}

module.exports = function defaultArgsFromApp (packageDir) {
return readMetadata({ src: packageDir, logger: debug })
.then(defaultArgsFromPackageJSON)
module.exports = async function defaultArgsFromApp (packageDir) {
const packageJSON = await readMetadata({ src: packageDir, logger: debug })
return defaultArgsFromPackageJSON(packageJSON)
}
2 changes: 1 addition & 1 deletion src/desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ function templateScope (userSupplied) {
return Object.assign(defaults, userSupplied)
}

module.exports = function (snapGuiDir, userSupplied) {
module.exports = async function (snapGuiDir, userSupplied) {
return createDesktopFile(getDesktopTemplatePath(userSupplied), snapGuiDir, userSupplied.name, templateScope(userSupplied))
}
26 changes: 11 additions & 15 deletions src/hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
/*
Copyright 2018 Mark Lee and contributors
Copyright 2018, 2019 Mark Lee and contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -18,28 +18,24 @@ limitations under the License.
const fs = require('fs-extra')
const path = require('path')

function copyHook (snapMetaDir, hookName, hookPath) {
async function copyHook (snapMetaDir, hookName, hookPath) {
const snapHookPath = path.join(snapMetaDir, hookName)
return fs.pathExists(hookPath)
.then(exists => {
if (!exists) {
throw new Error(`Hook ${hookName} at ${hookPath} does not exist!`)
}

return fs.copy(hookPath, snapHookPath)
}).then(() => fs.chmod(snapHookPath, 0o755))
if (!(await fs.pathExists(hookPath))) {
throw new Error(`Hook ${hookName} at ${hookPath} does not exist!`)
}

await fs.copy(hookPath, snapHookPath)
await fs.chmod(snapHookPath, 0o755)
}

function copyHooks (snapMetaDir, config) {
module.exports = async function copyHooks (snapMetaDir, config) {
if (typeof config.hookScripts !== 'object') {
return
}

const hooks = config.hookScripts
delete config.hookScripts

return fs.mkdirs(snapMetaDir)
.then(() => Promise.all(Object.keys(hooks).map(key => copyHook(snapMetaDir, key, hooks[key]))))
await fs.mkdirs(snapMetaDir)
await Promise.all(Object.keys(hooks).map(key => copyHook(snapMetaDir, key, hooks[key])))
}

module.exports = copyHooks
4 changes: 1 addition & 3 deletions src/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ function getSourceIconPath (userSupplied) {
}
}

function copyIcon (snapGuiDir, userSupplied) {
module.exports = async function copyIcon (snapGuiDir, userSupplied) {
const srcIconPath = getSourceIconPath(userSupplied)
const destIconPath = path.resolve(snapGuiDir, `icon${path.extname(srcIconPath)}`)
return fs.copy(srcIconPath, destIconPath)
}

module.exports = copyIcon
63 changes: 30 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
const common = require('electron-installer-common')
const debug = require('debug')('electron-installer-snap:index')
const fs = require('fs-extra')
const nodeify = require('nodeify')
const path = require('path')
const tmp = require('tmp-promise')

Expand All @@ -32,12 +31,12 @@ const defaultArgsFromApp = require('./default_args')
const { updateSandboxHelperPermissions } = require('electron-installer-common')

class SnapCreator {
prepareOptions (userSupplied) {
async prepareOptions (userSupplied) {
this.packageDir = path.resolve(userSupplied.src || process.cwd())
delete userSupplied.src

return defaultArgsFromApp(this.packageDir)
.then(defaultArgs => this.setOptions(defaultArgs, userSupplied))
const defaultArgs = await defaultArgsFromApp(this.packageDir)
return this.setOptions(defaultArgs, userSupplied)
}

setOptions (defaultArgs, userSupplied) {
Expand Down Expand Up @@ -71,49 +70,47 @@ class SnapCreator {
return sanitized
}

runInTempSnapDir () {
return tmp.dir({ prefix: 'electron-snap-', unsafeCleanup: !debug.enabled })
.then(tmpdir => {
this.tmpdir = tmpdir
return this.prepareAndBuildSnap(tmpdir.path)
}).catch(err => {
/* istanbul ignore if */
if (!debug.enabled) {
this.tmpdir.cleanup()
}
throw err
})
async runInTempSnapDir () {
this.tmpdir = await tmp.dir({ prefix: 'electron-snap-', unsafeCleanup: !debug.enabled })
try {
return this.prepareAndBuildSnap(this.tmpdir.path)
} catch (err) {
/* istanbul ignore if */
if (!debug.enabled) {
this.tmpdir.cleanup()
}
throw err
}
}

prepareAndBuildSnap (snapDir) {
async prepareAndBuildSnap (snapDir) {
const snapMetaDir = path.join(snapDir, 'snap')
const snapGuiDir = path.join(snapMetaDir, 'gui')
return fs.ensureDir(snapGuiDir)
.then(() => updateSandboxHelperPermissions(this.packageDir))
.then(() => createDesktopFile(snapGuiDir, this.config))
.then(() => copyIcon(snapGuiDir, this.config))
.then(() => copyLauncher(snapDir, this.config))
.then(() => createYamlFromTemplate(snapDir, this.packageDir, this.config))
.then(() => copyHooks(snapMetaDir, this.config))
.then(() => this.snapcraft.run(snapDir, 'snap', this.snapcraftOptions))
.then(() => this.snapDestPath)
await fs.ensureDir(snapGuiDir)
await updateSandboxHelperPermissions(this.packageDir)
await createDesktopFile(snapGuiDir, this.config)
await copyIcon(snapGuiDir, this.config)
await copyLauncher(snapDir, this.config)
await createYamlFromTemplate(snapDir, this.packageDir, this.config)
await copyHooks(snapMetaDir, this.config)
await this.snapcraft.run(snapDir, 'snap', this.snapcraftOptions)
return this.snapDestPath
}

create () {
return this.snapcraft.ensureInstalled(this.config.snapcraft)
.then(() => this.runInTempSnapDir())
async create () {
await this.snapcraft.ensureInstalled(this.config.snapcraft)
return this.runInTempSnapDir()
}
}

function createSnap (userSupplied) {
module.exports = async function createSnap (userSupplied) {
if (!userSupplied) {
throw new Error('Missing configuration')
}

const creator = new SnapCreator()
return creator.prepareOptions(userSupplied)
.then(() => creator.create())
await creator.prepareOptions(userSupplied)
return creator.create()
}

module.exports = nodeify(createSnap)
module.exports.SnapCreator = SnapCreator
10 changes: 4 additions & 6 deletions src/launcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
/*
Copyright 2018 Mark Lee and contributors
Copyright 2018, 2019 Mark Lee and contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -18,15 +18,13 @@ limitations under the License.
const fs = require('fs-extra')
const path = require('path')

function copyLauncher (snapDir, config) {
async function copyLauncher (snapDir, config) {
if (config.confinement === 'classic') {
const binDir = path.join(snapDir, 'bin')
const launcherPath = path.resolve(__dirname, '..', 'resources', 'classic-launcher.sh')
return fs.mkdirs(binDir)
.then(() => fs.copy(launcherPath, path.join(binDir, 'electron-launch')))
await fs.mkdirs(binDir)
await fs.copy(launcherPath, path.join(binDir, 'electron-launch'))
}

return Promise.resolve()
}

function createDesktopLaunchCommand (data) {
Expand Down
46 changes: 23 additions & 23 deletions src/snapcraft.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
/*
Copyright 2017 Mark Lee and contributors
Copyright 2017, 2019 Mark Lee and contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,23 +16,22 @@ limitations under the License.
*/

const debug = require('debug')('electron-installer-snap:snapcraft')
const pify = require('pify')
const { promisify } = require('util')
const spawn = require('cross-spawn-promise')
const which = require('which')
const which = promisify(require('which'))

class Snapcraft {
ensureInstalled (snapcraftPath) {
async ensureInstalled (snapcraftPath) {
const cmd = snapcraftPath || 'snapcraft'
return pify(which)(cmd)
.then(cmdPath => {
this.snapcraftPath = cmdPath
return true
}).catch(err => {
throw new Error(
`Cannot locate ${cmd} in your system. Either install snapcraft, or specify the ` +
`absolute path to snapcraft in the options. Details:\n${err}`
)
})
try {
this.snapcraftPath = await which(cmd)
return true
} catch (err) {
throw new Error(
`Cannot locate ${cmd} in your system. Either install snapcraft, or specify the ` +
`absolute path to snapcraft in the options. Details:\n${err}`
)
}
}

/**
Expand Down Expand Up @@ -89,17 +88,18 @@ class Snapcraft {
return spawnOptions
}

run (packageDir, command, options) {
async run (packageDir, command, options) {
const args = this.generateArgs(command, options)
debug(`Running '${this.snapcraftPath} ${args.join(' ')}' in ${packageDir}`)
return spawn(this.snapcraftPath, args, this.generateSpawnOptions(packageDir))
.catch(/* istanbul ignore next */ error => {
console.error(`Snapcraft failed (${error.exitStatus})`)
if (!debug.enabled) {
console.error('Re-run with the environment variable DEBUG=electron-installer-snap:snapcraft for details.')
}
throw error
})
try {
return spawn(this.snapcraftPath, args, this.generateSpawnOptions(packageDir))
} /* istanbul ignore next */ catch (error) {
console.error(`Snapcraft failed (${error.exitStatus})`)
if (!debug.enabled) {
console.error('Re-run with the environment variable DEBUG=electron-installer-snap:snapcraft for details.')
}
throw error
}
}
}

Expand Down
Loading