Skip to content

Commit

Permalink
Merge pull request #658 from electron-userland/promisify
Browse files Browse the repository at this point in the history
Promisify tests and packager function
  • Loading branch information
malept authored Jun 5, 2017
2 parents 2e48d1d + 9bb1efb commit 3c8848c
Show file tree
Hide file tree
Showing 15 changed files with 615 additions and 901 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Changed

* Promise support for `packager` - function returns a Promise instead of the return value of the
callback (#658)

## [8.7.0] - 2017-05-01

### Added
Expand Down
10 changes: 9 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# electron-packager API

Short example:
Short [callback](#callback) example:

```javascript
var packager = require('electron-packager')
packager(options, function done_callback (err, appPaths) { /**/ })
```

Short Promise example:

```javascript
const packager = require('electron-packager')
packager(options)
.then((appPaths) => { /**/ })
```

## `options`

### Required
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const getMetadataFromPackageJSON = require('./infer')
const ignore = require('./ignore')
const metadata = require('./package.json')
const path = require('path')
const pify = require('pify')
const series = require('run-series')
const targets = require('./targets')

Expand Down Expand Up @@ -102,7 +103,9 @@ function createSeries (opts, archs, platforms) {

function checkOverwrite () {
var finalPath = common.generateFinalPath(comboOpts)
fs.exists(finalPath, function (exists) {
fs.pathExists(finalPath, function (err, exists) {
if (err) return callback(err)

if (exists) {
if (opts.overwrite) {
fs.remove(finalPath, function () {
Expand Down Expand Up @@ -133,7 +136,7 @@ function createSeries (opts, archs, platforms) {
}))
}

module.exports = function packager (opts, cb) {
module.exports = pify(function packager (opts, cb) {
debugHostInfo()
if (debug.enabled) debug(`Packager Options: ${JSON.stringify(opts)}`)

Expand Down Expand Up @@ -170,4 +173,4 @@ module.exports = function packager (opts, cb) {
}))
})
})
}
})
7 changes: 5 additions & 2 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",
"pify": "^3.0.0",
"plist": "^2.0.0",
"rcedit": "^0.9.0",
"resolve": "^1.1.6",
Expand All @@ -39,13 +40,13 @@
"eslint-config-standard": "^10.0.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^4.2.2",
"eslint-plugin-promise": "^3.0.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.0",
"eslint-plugin-tape": "^1.1.0",
"mz": "^2.6.0",
"nyc": "^10.0.0",
"pkg-up": "^2.0.0",
"rimraf": "^2.3.2",
"run-waterfall": "^1.1.1",
"tape": "^4.0.0",
"which": "^1.2.14"
},
Expand All @@ -64,13 +65,15 @@
"keywords": [],
"eslintConfig": {
"extends": [
"plugin:promise/recommended",
"plugin:tape/recommended",
"standard"
],
"parserOptions": {
"sourceType": "script"
},
"plugins": [
"promise",
"tape"
],
"rules": {
Expand Down
89 changes: 34 additions & 55 deletions test/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ const packager = require('..')
const path = require('path')
const test = require('tape')
const util = require('./util')
const waterfall = require('run-waterfall')

function createDefaultAppAsarTest (opts) {
return function (t) {
return (t) => {
t.timeoutAfter(config.timeout)

opts.name = 'el0374Test'
Expand All @@ -19,24 +18,19 @@ function createDefaultAppAsarTest (opts) {

var resourcesPath

waterfall([
function (cb) {
packager(opts, cb)
}, function (paths, cb) {
packager(opts)
.then(paths => {
resourcesPath = path.join(paths[0], util.generateResourcesPath(opts))
fs.exists(path.join(resourcesPath, 'default_app.asar'), function (exists) {
t.false(exists, 'The output directory should not contain the Electron default_app.asar file')
cb()
})
}
], function (err) {
t.end(err)
})
return fs.pathExists(path.join(resourcesPath, 'default_app.asar'))
}).then(exists => {
t.false(exists, 'The output directory should not contain the Electron default_app.asar file')
return t.end()
}).catch(t.end)
}
}

function createAsarTest (opts) {
return function (t) {
return (t) => {
t.timeoutAfter(config.timeout)

opts.name = 'basicTest'
Expand All @@ -45,65 +39,50 @@ function createAsarTest (opts) {
'unpack': '*.pac',
'unpackDir': 'dir_to_unpack'
}
var finalPath
var resourcesPath
let finalPath
let resourcesPath

waterfall([
function (cb) {
packager(opts, cb)
}, function (paths, cb) {
packager(opts)
.then(paths => {
finalPath = paths[0]
fs.stat(finalPath, cb)
}, function (stats, cb) {
return fs.stat(finalPath)
}).then(stats => {
t.true(stats.isDirectory(), 'The expected output directory should exist')
resourcesPath = path.join(finalPath, util.generateResourcesPath(opts))
fs.stat(resourcesPath, cb)
}, function (stats, cb) {
return fs.stat(resourcesPath)
}).then(stats => {
t.true(stats.isDirectory(), 'The output directory should contain the expected resources subdirectory')
fs.stat(path.join(resourcesPath, 'app.asar'), cb)
}, function (stats, cb) {
return fs.stat(path.join(resourcesPath, 'app.asar'))
}).then(stats => {
t.true(stats.isFile(), 'app.asar should exist under the resources subdirectory when opts.asar is true')
fs.exists(path.join(resourcesPath, 'app'), function (exists) {
t.false(exists, 'app subdirectory should NOT exist when app.asar is built')
})
fs.stat(path.join(resourcesPath, 'app.asar.unpacked'), cb)
}, function (stats, cb) {
return fs.pathExists(path.join(resourcesPath, 'app'))
}).then(exists => {
t.false(exists, 'app subdirectory should NOT exist when app.asar is built')
return fs.stat(path.join(resourcesPath, 'app.asar.unpacked'))
}).then(stats => {
t.true(stats.isDirectory(), 'app.asar.unpacked should exist under the resources subdirectory when opts.asar_unpack is set some expression')
fs.stat(path.join(resourcesPath, 'app.asar.unpacked', 'dir_to_unpack'), cb)
}, function (stats, cb) {
return fs.stat(path.join(resourcesPath, 'app.asar.unpacked', 'dir_to_unpack'))
}).then(stats => {
t.true(stats.isDirectory(), 'dir_to_unpack should exist under app.asar.unpacked subdirectory when opts.asar-unpack-dir is set dir_to_unpack')
cb()
}
], function (err) {
t.end(err)
})
return t.end()
}).catch(t.end)
}
}

test('asar argument test: asar is not set', function (t) {
var opts = {}

var asarOpts = common.createAsarOpts(opts)
test('asar argument test: asar is not set', (t) => {
const asarOpts = common.createAsarOpts({})
t.false(asarOpts, 'createAsarOpts returns false')
t.end()
})

test('asar argument test: asar is true', function (t) {
var opts = {
asar: true
}

var asarOpts = common.createAsarOpts(opts)
test('asar argument test: asar is true', (t) => {
const asarOpts = common.createAsarOpts({asar: true})
t.same(asarOpts, {})
t.end()
})

test('asar argument test: asar is not an Object or a bool', function (t) {
var opts = {
asar: 'string'
}

var asarOpts = common.createAsarOpts(opts)
test('asar argument test: asar is not an Object or a bool', (t) => {
const asarOpts = common.createAsarOpts({asar: 'string'})
t.false(asarOpts, 'createAsarOpts returns false')
t.end()
})
Expand Down
Loading

0 comments on commit 3c8848c

Please sign in to comment.