Browse Source

Add now cli proxy (#569)

* unite now-cli and now-cli-proxy into single repo

* fix xo

* package.json.name is `now`
master
Leo Lamprecht 8 years ago
committed by GitHub
parent
commit
bbee653c29
  1. 1
      .gitignore
  2. 9
      download/chmod.js
  3. 104
      download/index.js
  4. 38
      download/log.js
  5. 41
      download/webpack.js
  6. 43
      package.json
  7. 14
      placeholder.js
  8. 22
      postinstall.js

1
.gitignore

@ -1,4 +1,5 @@
# build output
dist
packed
# dependencies

9
download/chmod.js

@ -0,0 +1,9 @@
import fs from 'fs'
export default function (file) {
const s = fs.statSync(file)
const newMode = s.mode | 64 | 8 | 1
if (s.mode === newMode) return
const base8 = newMode.toString(8).slice(-3)
fs.chmodSync(file, base8)
}

104
download/index.js

@ -0,0 +1,104 @@
// Native
import fs from 'fs'
import path from 'path'
// Packages
import fetch from 'node-fetch'
// Utilities
import plusxSync from './chmod'
import { disableProgress, enableProgress, info, showProgress } from './log'
const now = path.join(__dirname, 'now')
const targetWin32 = path.join(__dirname, 'now.exe')
const target = process.platform === 'win32' ? targetWin32 : now
const partial = target + '.partial'
const platformToName = {
darwin: 'now-macos',
linux: 'now-linux',
win32: 'now-win.exe'
}
const getLatest = async () => {
const res = await fetch('https://now-cli-latest.zeit.sh')
if (res.status !== 200) {
throw new Error(res.statusText)
}
return res.json()
}
async function main() {
info('Retrieving the latest CLI version...')
const latest = await getLatest()
const name = platformToName[process.platform]
const asset = latest.assets.filter(a => a.name === name)[0]
info('For the sources, check out: https://github.com/zeit/now-cli')
// Print an empty line
console.log('')
enableProgress('Downloading now CLI ' + latest.tag)
showProgress(0)
const { url } = asset
const resp = await fetch(url)
if (resp.status !== 200) {
disableProgress()
throw new Error(resp.statusText + ' ' + url)
}
const size = resp.headers.get('content-length')
const ws = fs.createWriteStream(partial)
await new Promise((resolve, reject) => {
let bytesRead = 0
resp.body.on('data', chunk => {
bytesRead += chunk.length
showProgress(100 * bytesRead / size)
})
resp.body.pipe(ws)
ws
.on('close', () => {
showProgress(100)
disableProgress()
resolve()
})
.on('error', error => {
disableProgress()
reject(error)
})
})
fs.renameSync(partial, target)
if (process.platform === 'win32') {
fs.writeFileSync(
now,
'#!/usr/bin/env node\n' +
'var chip = require("child_process")\n' +
'var args = process.argv.slice(2)\n' +
'var opts = { stdio: "inherit" }\n' +
'var r = chip.spawnSync(__dirname + "/now.exe", args, opts)\n' +
'if (r.error) throw r.error\n' +
'process.exit(r.status)\n'
)
} else {
plusxSync(now)
}
}
main().catch(err => {
console.error(err)
// eslint-disable-next-line unicorn/no-process-exit
process.exit(2)
})

38
download/log.js

@ -0,0 +1,38 @@
// Packages
import assert from 'assert'
import Progress from 'progress'
let bar
export function enableProgress(text) {
assert(!bar)
// OLD: text += ' '.repeat(28 - text.length);
bar = new Progress(`> ${text} [:bar] :percent`, {
stream: process.stdout,
width: 20,
complete: '=',
incomplete: ' ',
total: 100
})
}
export function info(text) {
console.log(`> ${text}`)
}
export function showProgress(percentage) {
assert(bar)
bar.update(percentage / 100)
}
export function disableProgress() {
assert(bar)
// It is auto-completed once it updates to 100
// otherwise it outputs a blank line
if (!bar.complete) {
bar.update(1)
}
bar = undefined
}

41
download/webpack.js

@ -0,0 +1,41 @@
const path = require('path')
const webpack = require('webpack')
module.exports = {
target: 'node',
node: {
__dirname: false,
__filename: false
},
entry: [
'./index.js'
],
output: {
path: path.join(__dirname, '../dist'),
filename: 'download.js'
},
module: {
loaders: [ {
test: /.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
plugins: [
'transform-async-to-generator',
'transform-runtime'
],
presets: [
'es2015'
]
}
} ]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.UglifyJsPlugin()
]
}

43
package.json

@ -1,12 +1,15 @@
{
"name": "now-cli",
"name": "now",
"version": "6.0.0",
"private": true,
"scripts": {
"postinstall": "node postinstall.js",
"precommit": "xo --quiet && lint-staged",
"prepublish": "in-install || (npm run webpack && node placeholder.js)",
"lint": "xo --quiet",
"test": "npm run lint && ava",
"pack": "pkg bin/now.js -c package.json -o packed/now"
"pack": "pkg bin/now.js -c package.json -o packed/now",
"webpack": "webpack --context download --config download/webpack.js"
},
"pkg": {
"scripts": [
@ -21,8 +24,12 @@
]
},
"bin": {
"now": "./bin/now.js"
"now": "dist/now"
},
"files": [
"dist",
"postinstall.js"
],
"ava": {
"failFast": true,
"files": [
@ -42,13 +49,21 @@
"git add"
]
},
"dependencies": {
"dependencies": {},
"devDependencies": {
"@google/maps": "0.3.1",
"alpha-sort": "2.0.1",
"ansi-escapes": "2.0.0",
"ansi-regex": "2.1.1",
"arr-flatten": "1.0.3",
"array-unique": "0.3.2",
"async-retry": "0.3.0",
"ava": "0.19.1",
"babel-core": "6.24.1",
"babel-loader": "7.0.0",
"babel-plugin-transform-async-to-generator": "6.24.1",
"babel-plugin-transform-runtime": "6.23.0",
"babel-preset-es2015": "6.24.1",
"bytes": "2.5.0",
"chalk": "1.1.3",
"clipboardy": "1.1.2",
@ -61,22 +76,29 @@
"email-prompt": "0.3.0",
"email-validator": "1.0.7",
"epipebomb": "1.0.0",
"eslint-config-prettier": "2.1.0",
"fs-extra": "3.0.1",
"glob": "7.1.1",
"ignore": "3.3.1",
"in-publish": "2.0.0",
"ini": "1.3.4",
"inquirer": "3.0.6",
"is-url": "1.2.2",
"lint-staged": "3.4.2",
"lodash.range": "3.2.0",
"minimist": "1.2.0",
"ms": "2.0.0",
"node-fetch": "1.6.3",
"ora": "1.2.0",
"pkg": "3.0.4",
"prettier": "1.3.1",
"printf": "0.2.5",
"progress": "2.0.0",
"psl": "1.1.18",
"resumer": "0.0.0",
"serve": "5.1.5",
"single-line-log": "1.1.2",
"slackup": "2.0.1",
"socket.io-client": "2.0.1",
"split-array": "1.0.1",
"strip-ansi": "3.0.1",
@ -84,17 +106,8 @@
"supports-color": "3.2.3",
"text-table": "0.2.0",
"tmp-promise": "1.0.3",
"update-notifier": "2.1.0"
},
"devDependencies": {
"alpha-sort": "2.0.1",
"ava": "0.19.1",
"eslint-config-prettier": "2.1.0",
"lint-staged": "3.4.2",
"pkg": "3.0.4",
"prettier": "1.3.1",
"serve": "5.1.5",
"slackup": "2.0.1",
"update-notifier": "2.1.0",
"webpack": "2.5.1",
"xo": "0.18.2"
}
}

14
placeholder.js

@ -0,0 +1,14 @@
/* eslint-disable no-var */
// Native
var path = require('path')
var fs = require('fs')
var dist = path.join(__dirname, 'dist')
var now = path.join(dist, 'now')
fs.writeFileSync(
now,
'#!/usr/bin/env node\n' +
'console.log("\'Now\' binary downloading was interrupted. Please reinstall!")\n'
)

22
postinstall.js

@ -0,0 +1,22 @@
/* eslint-disable no-var */
// Native
var path = require('path')
var fs = require('fs')
var dist = path.join(__dirname, 'dist')
var download = path.join(dist, 'download.js')
try {
fs.mkdirSync(dist)
} catch (err) {
if (err.code !== 'EEXIST') {
throw err
}
}
fs.closeSync(
fs.openSync(download, 'a')
)
require(download)
Loading…
Cancel
Save