Browse Source

Fix for #729 (#745)

* use 'which' to detect the path of global cmd

* fix downloading apline binaries
master
Igor Klopov 8 years ago
committed by Matheus Fernandes
parent
commit
7336c8cb0e
  1. 59
      download/src/index.js
  2. 14
      package-lock.json
  3. 1
      package.json

59
download/src/index.js

@ -3,12 +3,14 @@
// Native // Native
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { spawnSync } from 'child_process'
import zlib from 'zlib' import zlib from 'zlib'
// Packages // Packages
import onDeath from 'death' import onDeath from 'death'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import retry from 'async-retry' import retry from 'async-retry'
import which from 'which'
// Utilities // Utilities
import plusxSync from './chmod' import plusxSync from './chmod'
@ -22,21 +24,42 @@ import {
fetch.Promise = Promise fetch.Promise = Promise
global.Promise = Promise global.Promise = Promise
let { platform } = process
if (detectAlpine()) platform = 'alpine'
const packagePath = path.join(__dirname, '../../package.json')
const packageJSON = JSON.parse(fs.readFileSync(packagePath, 'utf8'))
const now = path.join(__dirname, 'now') const now = path.join(__dirname, 'now')
const targetWin32 = path.join(__dirname, 'now.exe') const targetWin32 = path.join(__dirname, 'now.exe')
const target = process.platform === 'win32' ? targetWin32 : now const target = platform === 'win32' ? targetWin32 : now
const partial = target + '.partial' const partial = target + '.partial'
const backup = target + '.' + packageJSON.version + '.backup'
const packagePath = path.join(__dirname, '../../package.json') function whichPromise (name) {
const packageJSON = JSON.parse(fs.readFileSync(packagePath, 'utf8')) return new Promise((resolve, reject) => {
which(name, (error, result) => {
if (error) return reject(error)
resolve(result)
})
})
}
const platformToName = { const platformToName = {
alpine: 'now-alpine',
darwin: 'now-macos', darwin: 'now-macos',
linux: 'now-linux', linux: 'now-linux',
win32: 'now-win.exe' win32: 'now-win.exe'
} }
async function main() { function detectAlpine () {
if (platform !== 'linux') return false
// https://github.com/sass/node-sass/issues/1589#issuecomment-265292579
const ldd = spawnSync('ldd', [ process.execPath ]).stdout.toString()
return /\bmusl\b/.test(ldd)
}
async function download() {
try { try {
fs.writeFileSync( fs.writeFileSync(
now, now,
@ -76,7 +99,7 @@ async function main() {
showProgress(0) showProgress(0)
try { try {
const name = platformToName[process.platform] const name = platformToName[platform]
const url = `https://cdn.zeit.co/releases/now-cli/${packageJSON.version}/${name}` const url = `https://cdn.zeit.co/releases/now-cli/${packageJSON.version}/${name}`
const resp = await fetch(url, { compress: false }) const resp = await fetch(url, { compress: false })
@ -85,6 +108,11 @@ async function main() {
} }
const size = resp.headers.get('content-length') const size = resp.headers.get('content-length')
if (!size) {
throw new Error('Not found (content-length is absent)')
}
const ws = fs.createWriteStream(partial) const ws = fs.createWriteStream(partial)
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
@ -120,12 +148,23 @@ async function main() {
}) })
fs.renameSync(partial, target) fs.renameSync(partial, target)
fs.writeFileSync(backup, fs.readFileSync(target))
}
async function main() {
if (fs.existsSync(backup)) {
fs.writeFileSync(target, fs.readFileSync(backup))
} else {
await download()
}
if (process.platform === 'win32') { if (platform === 'win32') {
// Now.exe is executed only // Now.exe is executed only
fs.unlinkSync(now) fs.unlinkSync(now)
try {
// Workaround for https://github.com/npm/cmd-shim/pull/25 // Workaround for https://github.com/npm/cmd-shim/pull/25
const gitBashFile = path.join(process.env.APPDATA, 'npm/now') const globalPath = path.dirname(await whichPromise('npm'))
const gitBashFile = path.join(globalPath, 'now')
fs.writeFileSync( fs.writeFileSync(
gitBashFile, gitBashFile,
'#!/bin/sh\n' + '#!/bin/sh\n' +
@ -137,6 +176,12 @@ async function main() {
'\n' + '\n' +
fs.readFileSync(gitBashFile, 'utf8') fs.readFileSync(gitBashFile, 'utf8')
) )
} catch (err) {
if (err.code !== 'ENOENT') {
// Not a problem. only git cmd will not work
console.error(err)
}
}
} else { } else {
plusxSync(now) plusxSync(now)
} }

14
package-lock.json

@ -2005,7 +2005,7 @@
"requires": { "requires": {
"lru-cache": "4.1.1", "lru-cache": "4.1.1",
"shebang-command": "1.2.0", "shebang-command": "1.2.0",
"which": "1.2.14" "which": "1.3.0"
} }
}, },
"cryptiles": { "cryptiles": {
@ -6636,7 +6636,7 @@
"integrity": "sha1-Fc/04ciaONp39W9gVbJPl137K74=", "integrity": "sha1-Fc/04ciaONp39W9gVbJPl137K74=",
"dev": true, "dev": true,
"requires": { "requires": {
"which": "1.2.14" "which": "1.3.0"
} }
}, },
"npm-run-path": { "npm-run-path": {
@ -6656,7 +6656,7 @@
"requires": { "requires": {
"commander": "2.11.0", "commander": "2.11.0",
"npm-path": "2.0.3", "npm-path": "2.0.3",
"which": "1.2.14" "which": "1.3.0"
}, },
"dependencies": { "dependencies": {
"commander": { "commander": {
@ -9072,9 +9072,9 @@
"dev": true "dev": true
}, },
"which": { "which": {
"version": "1.2.14", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/which/-/which-1.2.14.tgz", "resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz",
"integrity": "sha1-mofEN48D6CfOyvGs31bHNsAcFOU=", "integrity": "sha512-xcJpopdamTuY5duC/KnTTNBraPK54YwpenP4lzxU8H91GudWpFv38u0CKjclE1Wi2EH2EDz5LRcHcKbCIzqGyg==",
"dev": true, "dev": true,
"requires": { "requires": {
"isexe": "2.0.0" "isexe": "2.0.0"
@ -9364,7 +9364,7 @@
"dev": true, "dev": true,
"requires": { "requires": {
"lru-cache": "4.1.1", "lru-cache": "4.1.1",
"which": "1.2.14" "which": "1.3.0"
} }
}, },
"execa": { "execa": {

1
package.json

@ -118,6 +118,7 @@
"tmp-promise": "1.0.3", "tmp-promise": "1.0.3",
"update-notifier": "2.2.0", "update-notifier": "2.2.0",
"webpack": "3.4.1", "webpack": "3.4.1",
"which": "1.3.0",
"xo": "0.18.2" "xo": "0.18.2"
} }
} }

Loading…
Cancel
Save