Browse Source
* 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
8 changed files with 257 additions and 15 deletions
@ -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) |
|||
} |
@ -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) |
|||
}) |
@ -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 |
|||
} |
@ -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() |
|||
] |
|||
} |
@ -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' |
|||
) |
@ -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…
Reference in new issue