Browse Source

Merge pull request #225 from electron-userland/write-file-after-uncompressing

Write path.txt file after uncompressing
Kevin Sawicki 8 years ago
parent
commit
39caff88c9
4 changed files with 89 additions and 18 deletions
  1. 1 0
      npm/.gitignore
  2. 1 0
      npm/.npmignore
  3. 25 18
      npm/install.js
  4. 62 0
      npm/test/errors.js

+ 1 - 0
npm/.gitignore

@@ -2,3 +2,4 @@ node_modules
 dist
 path.txt
 .DS_Store
+npm-debug.log

+ 1 - 0
npm/.npmignore

@@ -6,3 +6,4 @@ appveyor.yml
 CONTRIBUTING.md
 issue_template.md
 test/
+npm-debug.log

+ 25 - 18
npm/install.js

@@ -12,26 +12,13 @@ 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
 }
 
-var platform = process.env.npm_config_platform || os.platform()
+var platformPath = getPlatformPath()
 
-function onerror (err) {
-  throw err
-}
-
-var paths = {
-  darwin: 'dist/Electron.app/Contents/MacOS/Electron',
-  freebsd: 'dist/electron',
-  linux: 'dist/electron',
-  win32: 'dist/electron.exe'
-}
-
-if (!paths[platform]) throw new Error('Electron builds are not available on platform: ' + platform)
-
-if (installedVersion === version && fs.existsSync(path.join(__dirname, paths[platform]))) {
+if (installedVersion === version && fs.existsSync(path.join(__dirname, platformPath))) {
   process.exit(0)
 }
 
@@ -47,10 +34,30 @@ 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'), platformPath, function (err) {
       if (err) return onerror(err)
     })
   })
 }
+
+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)
+  }
+}

+ 62 - 0
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.notEqual(code, 0, 'exited with error')
+    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.notEqual(code, 0, 'exited with error')
+    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
+  }
+}