Merge pull request #224 from electron-userland/check-if-path-file-exists

Check that the path file exists before spawning
This commit is contained in:
Kevin Sawicki 2016-12-28 10:24:07 -08:00 committed by GitHub
commit 7e72581469
2 changed files with 47 additions and 17 deletions

View file

@ -1,4 +1,10 @@
var fs = require('fs') var fs = require('fs')
var path = require('path') var path = require('path')
module.exports = path.join(__dirname, fs.readFileSync(path.join(__dirname, 'path.txt'), 'utf-8')) var pathFile = path.join(__dirname, 'path.txt')
if (fs.existsSync(pathFile)) {
module.exports = path.join(__dirname, fs.readFileSync(pathFile, 'utf-8'))
} else {
throw new Error('Electron failed to install correctly, please delete node_modules/' + path.basename(__dirname) + ' and try installing again')
}

View file

@ -3,7 +3,7 @@ var tape = require('tape')
var path = require('path') var path = require('path')
var childProcess = require('child_process') var childProcess = require('child_process')
tape('fails for unsupported platforms', function (t) { tape('install fails for unsupported platforms', function (t) {
install({npm_config_platform: 'android'}, function (code, stderr) { 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.notEqual(stderr.indexOf('Electron builds are not available on platform: android'), -1, 'has error message')
t.notEqual(code, 0, 'exited with error') t.notEqual(code, 0, 'exited with error')
@ -11,7 +11,7 @@ tape('fails for unsupported platforms', function (t) {
}) })
}) })
tape('fails for unknown architectures', function (t) { tape('install fails for unsupported architectures', function (t) {
install({ install({
npm_config_arch: 'midcentury', npm_config_arch: 'midcentury',
npm_config_platform: 'win32', npm_config_platform: 'win32',
@ -25,8 +25,16 @@ tape('fails for unknown architectures', function (t) {
}) })
}) })
var install = function (env, callback) { tape('require fails for corrupted installs', function (t) {
removeVersionFile() cli(function (code, stderr) {
t.notEqual(stderr.indexOf('Electron failed to install correctly'), -1, 'has error message')
t.notEqual(code, 0, 'exited with error')
t.end()
})
})
function install (env, callback) {
var restoreFile = removeFile(path.join(__dirname, '..', 'dist', 'version'))
var installPath = path.join(__dirname, '..', 'install.js') var installPath = path.join(__dirname, '..', 'install.js')
var installProcess = childProcess.fork(installPath, { var installProcess = childProcess.fork(installPath, {
@ -40,23 +48,39 @@ var install = function (env, callback) {
}) })
installProcess.on('close', function (code) { installProcess.on('close', function (code) {
restoreVersionFile() restoreFile()
callback(code, stderr) callback(code, stderr)
}) })
} }
var versionPath = path.join(__dirname, '..', 'dist', 'version') function cli (callback) {
var versionContents = null var restoreFile = removeFile(path.join(__dirname, '..', 'path.txt'))
function removeVersionFile () {
if (fs.existsSync(versionPath)) { var cliPath = path.join(__dirname, '..', 'cli.js')
versionContents = fs.readFileSync(versionPath) var cliProcess = childProcess.fork(cliPath, {
fs.unlinkSync(versionPath) silent: true
} })
var stderr = ''
cliProcess.stderr.on('data', function (data) {
stderr += data
})
cliProcess.on('close', function (code) {
restoreFile()
callback(code, stderr)
})
} }
function restoreVersionFile () { function removeFile (filePath) {
if (versionContents != null) { var contents = null
fs.writeFileSync(versionPath, versionContents) if (fs.existsSync(filePath)) {
versionContents = null contents = fs.readFileSync(filePath)
fs.unlinkSync(filePath)
}
return function restoreFile () {
if (contents != null) {
fs.writeFileSync(filePath, contents)
}
} }
} }