You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
4.9 KiB
207 lines
4.9 KiB
9 years ago
|
#!/usr/bin/env node
|
||
9 years ago
|
import Progress from 'progress';
|
||
|
import copy from '../lib/copy';
|
||
9 years ago
|
import { resolve } from 'path';
|
||
9 years ago
|
import login from '../lib/login';
|
||
9 years ago
|
import * as cfg from '../lib/cfg';
|
||
|
import { version } from '../../package';
|
||
9 years ago
|
import checkUpdate from '../lib/check-update';
|
||
9 years ago
|
import Logger from '../lib/build-logger';
|
||
9 years ago
|
import bytes from 'bytes';
|
||
9 years ago
|
import chalk from 'chalk';
|
||
9 years ago
|
import minimist from 'minimist';
|
||
9 years ago
|
import Now from '../lib';
|
||
9 years ago
|
import ms from 'ms';
|
||
|
|
||
9 years ago
|
const argv = minimist(process.argv.slice(2));
|
||
|
const help = () => {
|
||
|
console.log(`
|
||
9 years ago
|
𝚫 now [options] <command|path>
|
||
|
|
||
|
Commands:
|
||
|
|
||
|
list output list of instances
|
||
|
ls alias of list
|
||
9 years ago
|
|
||
9 years ago
|
Options:
|
||
9 years ago
|
|
||
9 years ago
|
-h, --help output usage information
|
||
|
-v, --version output the version number
|
||
|
-d, --debug Debug mode [off]
|
||
|
-f, --force Force a new deployment even if nothing has changed
|
||
|
-L, --login Configure login
|
||
|
-C, --no-clipboard Do not attempt to copy URL to clipboard
|
||
9 years ago
|
`);
|
||
|
};
|
||
|
|
||
|
let path = argv._[0];
|
||
9 years ago
|
|
||
|
if (path) {
|
||
|
if ('/' !== path[0]) {
|
||
|
path = resolve(process.cwd(), path);
|
||
|
}
|
||
|
} else {
|
||
9 years ago
|
path = process.cwd();
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// options
|
||
|
const debug = argv.debug || argv.d;
|
||
|
const clipboard = !(argv.noClipboard || argv.C);
|
||
|
const force = argv.f || argv.force;
|
||
|
const forceSync = argv.F || argv.forceSync;
|
||
|
const shouldLogin = argv.L || argv.login;
|
||
|
|
||
|
// auto-update checking
|
||
9 years ago
|
const config = cfg.read();
|
||
|
const update = checkUpdate({ debug });
|
||
9 years ago
|
const exit = (code) => {
|
||
|
update.then(() => process.exit(code));
|
||
|
// don't wait for updates more than a second
|
||
|
// when the process really wants to exit
|
||
|
setTimeout(() => process.exit(code), 1000);
|
||
|
};
|
||
9 years ago
|
|
||
9 years ago
|
if (argv.h || argv.help) {
|
||
|
help();
|
||
|
exit(0);
|
||
|
} else if (argv.v || argv.version) {
|
||
|
console.log(chalk.bold('𝚫 now'), version);
|
||
|
exit(0);
|
||
|
} else if (!config.token || shouldLogin) {
|
||
9 years ago
|
login()
|
||
9 years ago
|
.then((token) => {
|
||
9 years ago
|
if (shouldLogin) {
|
||
9 years ago
|
console.log('> Logged in successfully. Token saved in ~/.now.json');
|
||
9 years ago
|
exit(0);
|
||
9 years ago
|
} else {
|
||
9 years ago
|
sync(token).catch((err) => {
|
||
|
error(`Unknown error: ${err.stack}`);
|
||
9 years ago
|
exit(1);
|
||
9 years ago
|
});
|
||
9 years ago
|
}
|
||
|
})
|
||
|
.catch((e) => {
|
||
|
error(`Authentication error – ${e.message}`);
|
||
9 years ago
|
exit(1);
|
||
9 years ago
|
});
|
||
9 years ago
|
} else {
|
||
9 years ago
|
sync(config.token).catch((err) => {
|
||
|
error(`Unknown error: ${err.stack}`);
|
||
9 years ago
|
exit(1);
|
||
9 years ago
|
});
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
async function sync (token) {
|
||
9 years ago
|
const start = Date.now();
|
||
|
|
||
9 years ago
|
console.log(`> Deploying "${path}"`);
|
||
9 years ago
|
|
||
9 years ago
|
const now = new Now(token, { debug });
|
||
|
|
||
|
try {
|
||
9 years ago
|
await now.create(path, { forceNew: force, forceSync: forceSync });
|
||
9 years ago
|
} catch (err) {
|
||
|
handleError(err);
|
||
9 years ago
|
return;
|
||
9 years ago
|
}
|
||
|
|
||
|
const { url } = now;
|
||
|
const elapsed = ms(new Date() - start);
|
||
|
|
||
|
if (clipboard) {
|
||
|
try {
|
||
|
await copy(url);
|
||
9 years ago
|
console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} (copied to clipboard) [${elapsed}]`);
|
||
9 years ago
|
} catch (err) {
|
||
9 years ago
|
console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} [${elapsed}]`);
|
||
9 years ago
|
}
|
||
9 years ago
|
} else {
|
||
|
console.log(`> ${url} [${elapsed}]`);
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
const start_u = new Date();
|
||
|
const complete = () => {
|
||
|
const elapsed_u = ms(new Date() - start_u);
|
||
|
console.log(`> Sync complete (${bytes(now.syncAmount)}) [${elapsed_u}] `);
|
||
9 years ago
|
|
||
|
// close http2 agent
|
||
9 years ago
|
now.close();
|
||
9 years ago
|
|
||
|
// show build logs
|
||
|
printLogs(now.host);
|
||
9 years ago
|
};
|
||
|
|
||
|
if (now.syncAmount) {
|
||
|
const bar = new Progress('> Upload [:bar] :percent :etas', {
|
||
|
width: 20,
|
||
|
complete: '=',
|
||
|
incomplete: '',
|
||
|
total: now.syncAmount
|
||
|
});
|
||
|
|
||
|
now.upload();
|
||
|
|
||
|
now.on('upload', ({ name, data }) => {
|
||
9 years ago
|
const amount = data.length;
|
||
9 years ago
|
if (debug) {
|
||
9 years ago
|
console.log(`> [debug] Uploaded: ${name} (${bytes(data.length)})`);
|
||
9 years ago
|
}
|
||
|
bar.tick(amount);
|
||
|
});
|
||
|
|
||
9 years ago
|
now.on('complete', complete);
|
||
9 years ago
|
|
||
|
now.on('error', (err) => {
|
||
|
error('Upload failed');
|
||
|
handleError(err);
|
||
9 years ago
|
exit(1);
|
||
9 years ago
|
});
|
||
|
} else {
|
||
|
console.log('> Sync complete (cached)');
|
||
9 years ago
|
|
||
9 years ago
|
// close http2 agent
|
||
9 years ago
|
now.close();
|
||
|
|
||
9 years ago
|
// show build logs
|
||
|
printLogs(now.host);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
function printLogs (host) {
|
||
|
// log build
|
||
|
const logger = new Logger(host);
|
||
|
logger.on('error', () => {
|
||
|
console.log('> Connection error.');
|
||
|
exit(1);
|
||
|
});
|
||
|
logger.on('close', () => {
|
||
9 years ago
|
console.log(`${chalk.cyan('> Deployment complete!')}`);
|
||
9 years ago
|
exit(0);
|
||
|
});
|
||
|
}
|
||
|
|
||
9 years ago
|
function handleError (err) {
|
||
|
if (403 === err.status) {
|
||
9 years ago
|
error('Authentication error. Run `now -L` or `now --login` to log-in again.');
|
||
9 years ago
|
} else if (429 === err.status) {
|
||
|
if (null != err.retryAfter) {
|
||
|
error('Rate limit exceeded error. Try again in ' +
|
||
|
ms(err.retryAfter * 1000, { long: true }) +
|
||
|
', or upgrade your account: https://zeit.co/now#pricing');
|
||
|
} else {
|
||
|
error('Rate limit exceeded error. Please try later.');
|
||
|
}
|
||
9 years ago
|
} else if (err.userError) {
|
||
|
error(err.message);
|
||
9 years ago
|
} else if (500 === err.status) {
|
||
|
error('Unexpected server error. Please retry.');
|
||
|
} else {
|
||
|
error(`Unexpected error. Please try later. (${err.message})`);
|
||
|
}
|
||
9 years ago
|
exit(1);
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
function error (err) {
|
||
9 years ago
|
console.error(`> \u001b[31mError!\u001b[39m ${err}`);
|
||
9 years ago
|
}
|