From d2df641b0d23fd1f709b71e28026b2dedb39f672 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 27 Dec 2016 11:03:51 -0800 Subject: [PATCH 1/7] Write path.txt after extraction --- npm/install.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/npm/install.js b/npm/install.js index 07f7393e6c5c..afcff4031c27 100755 --- a/npm/install.js +++ b/npm/install.js @@ -47,9 +47,9 @@ download({ // unzips and makes path.txt point at the correct executable function extractFile (err, zipPath) { if (err) return onerror(err) - fs.writeFile(path.join(__dirname, 'path.txt'), paths[platform], function (err) { + extract(zipPath, {dir: path.join(__dirname, 'dist')}, function (err) { if (err) return onerror(err) - extract(zipPath, {dir: path.join(__dirname, 'dist')}, function (err) { + fs.writeFile(path.join(__dirname, 'path.txt'), paths[platform], function (err) { if (err) return onerror(err) }) }) From d5bb1d87ccdf3cc19de80f0c700891a71c15ced0 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 27 Dec 2016 11:13:15 -0800 Subject: [PATCH 2/7] Add getPath helper --- npm/install.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/npm/install.js b/npm/install.js index afcff4031c27..ea87df88a13b 100755 --- a/npm/install.js +++ b/npm/install.js @@ -22,16 +22,23 @@ function onerror (err) { throw err } -var paths = { - darwin: 'dist/Electron.app/Contents/MacOS/Electron', - freebsd: 'dist/electron', - linux: 'dist/electron', - win32: 'dist/electron.exe' +function getPath (platform) { + switch (platform) { + case 'darwin': + return 'dist/Electron.app/Contents/MacOS/Electron' + case 'freebsd': + case 'linux': + return 'dist/electron' + case 'win32': + return 'dist/electron.exe' + default: + throw new Error('Electron builds are not available on platform: ' + platform) + } } -if (!paths[platform]) throw new Error('Electron builds are not available on platform: ' + platform) +var platformPath = getPath(platform) -if (installedVersion === version && fs.existsSync(path.join(__dirname, paths[platform]))) { +if (installedVersion === version && fs.existsSync(path.join(__dirname, platformPath))) { process.exit(0) } @@ -49,7 +56,7 @@ function extractFile (err, zipPath) { if (err) return onerror(err) extract(zipPath, {dir: path.join(__dirname, 'dist')}, function (err) { if (err) return onerror(err) - fs.writeFile(path.join(__dirname, 'path.txt'), paths[platform], function (err) { + fs.writeFile(path.join(__dirname, 'path.txt'), platformPath, function (err) { if (err) return onerror(err) }) }) From af2ca221b4f8e60e445d5085c09a95a33f3c560e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 27 Dec 2016 11:21:18 -0800 Subject: [PATCH 3/7] Ignore npm-debug.log --- npm/.gitignore | 1 + npm/.npmignore | 1 + 2 files changed, 2 insertions(+) diff --git a/npm/.gitignore b/npm/.gitignore index 9ec0c37f1d7a..6424611f74c1 100644 --- a/npm/.gitignore +++ b/npm/.gitignore @@ -2,3 +2,4 @@ node_modules dist path.txt .DS_Store +npm-debug.log diff --git a/npm/.npmignore b/npm/.npmignore index de77ba146f1b..9d10d49cbab1 100644 --- a/npm/.npmignore +++ b/npm/.npmignore @@ -6,3 +6,4 @@ appveyor.yml CONTRIBUTING.md issue_template.md test/ +npm-debug.log From 835c64b7b2e5002ca27e5f96831d38d6bd92eb23 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 27 Dec 2016 11:37:01 -0800 Subject: [PATCH 4/7] Add specs for error cases --- npm/test/errors.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 npm/test/errors.js diff --git a/npm/test/errors.js b/npm/test/errors.js new file mode 100644 index 000000000000..1c86a44bec5e --- /dev/null +++ b/npm/test/errors.js @@ -0,0 +1,62 @@ +var fs = require('fs') +var tape = require('tape') +var path = require('path') +var childProcess = require('child_process') + +tape('fails for unsupported platforms', function (t) { + install({npm_config_platform: 'android'}, function (code, stderr) { + t.notEqual(stderr.indexOf('Electron builds are not available on platform: android'), -1, 'has error message') + t.equal(code, 1, 'exited with 1') + t.end() + }) +}) + +tape('fails for unknown architectures', function (t) { + install({ + npm_config_arch: 'midcentury', + npm_config_platform: 'win32', + HOME: process.env.HOME, + USERPROFILE: process.env.USERPROFILE + }, function (code, stderr) { + t.notEqual(stderr.indexOf('Failed to find Electron'), -1, 'has error message') + t.notEqual(stderr.indexOf('win32-midcentury'), -1, 'has error message') + t.equal(code, 1, 'exited with 1') + t.end() + }) +}) + +var install = function (env, callback) { + removeVersionFile() + + var installPath = path.join(__dirname, '..', 'install.js') + var installProcess = childProcess.fork(installPath, { + silent: true, + env: env + }) + + var stderr = '' + installProcess.stderr.on('data', function (data) { + stderr += data + }) + + installProcess.on('close', function (code) { + restoreVersionFile() + callback(code, stderr) + }) +} + +var versionPath = path.join(__dirname, '..', 'dist', 'version') +var versionContents = null +function removeVersionFile () { + if (fs.existsSync(versionPath)) { + versionContents = fs.readFileSync(versionPath) + fs.unlinkSync(versionPath) + } +} + +function restoreVersionFile () { + if (versionContents != null) { + fs.writeFileSync(versionPath, versionContents) + versionContents = null + } +} From ed8260e4d9b1ce3be9bee8419a3e733ccbbb5249 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 27 Dec 2016 11:48:14 -0800 Subject: [PATCH 5/7] Inline platform variable --- npm/install.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/npm/install.js b/npm/install.js index ea87df88a13b..a0c1d3f0ccd3 100755 --- a/npm/install.js +++ b/npm/install.js @@ -16,27 +16,7 @@ try { // do nothing } -var platform = process.env.npm_config_platform || os.platform() - -function onerror (err) { - throw err -} - -function getPath (platform) { - switch (platform) { - case 'darwin': - return 'dist/Electron.app/Contents/MacOS/Electron' - case 'freebsd': - case 'linux': - return 'dist/electron' - case 'win32': - return 'dist/electron.exe' - default: - throw new Error('Electron builds are not available on platform: ' + platform) - } -} - -var platformPath = getPath(platform) +var platformPath = getPlatformPath() if (installedVersion === version && fs.existsSync(path.join(__dirname, platformPath))) { process.exit(0) @@ -61,3 +41,23 @@ function extractFile (err, zipPath) { }) }) } + +function onerror (err) { + throw err +} + +function getPlatformPath () { + var platform = process.env.npm_config_platform || os.platform() + + switch (platform) { + case 'darwin': + return 'dist/Electron.app/Contents/MacOS/Electron' + case 'freebsd': + case 'linux': + return 'dist/electron' + case 'win32': + return 'dist/electron.exe' + default: + throw new Error('Electron builds are not available on platform: ' + platform) + } +} From 8d4660158ac46080a3253da20c19475a378ff252 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 27 Dec 2016 11:49:44 -0800 Subject: [PATCH 6/7] err -> ignored --- npm/install.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/npm/install.js b/npm/install.js index a0c1d3f0ccd3..7a41a4e83316 100755 --- a/npm/install.js +++ b/npm/install.js @@ -12,7 +12,7 @@ var download = require('electron-download') var installedVersion = null try { installedVersion = fs.readFileSync(path.join(__dirname, 'dist', 'version'), 'utf-8').replace(/^v/, '') -} catch (err) { +} catch (ignored) { // do nothing } From e165294ff1643537a118ee0776351d69e58b94a2 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 27 Dec 2016 13:20:51 -0800 Subject: [PATCH 7/7] Check that exit code was non-zero --- npm/test/errors.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/npm/test/errors.js b/npm/test/errors.js index 1c86a44bec5e..abc5d15b6c2d 100644 --- a/npm/test/errors.js +++ b/npm/test/errors.js @@ -6,7 +6,7 @@ var childProcess = require('child_process') tape('fails for unsupported platforms', function (t) { install({npm_config_platform: 'android'}, function (code, stderr) { t.notEqual(stderr.indexOf('Electron builds are not available on platform: android'), -1, 'has error message') - t.equal(code, 1, 'exited with 1') + t.notEqual(code, 0, 'exited with error') t.end() }) }) @@ -20,7 +20,7 @@ tape('fails for unknown architectures', function (t) { }, function (code, stderr) { t.notEqual(stderr.indexOf('Failed to find Electron'), -1, 'has error message') t.notEqual(stderr.indexOf('win32-midcentury'), -1, 'has error message') - t.equal(code, 1, 'exited with 1') + t.notEqual(code, 0, 'exited with error') t.end() }) })