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.
 

142 lines
3.3 KiB

#!/usr/bin/env node
import program from 'commander';
import Progress from 'progress';
import copy from '../lib/copy';
import { resolve } from 'path';
import login from '../lib/login';
import bytes from 'bytes';
import Now from '../lib';
import fs from 'fs';
import ms from 'ms';
import os from 'os';
program
.option('-d, --debug', 'Debug mode [off]', false)
.option('-f, --force', 'Force a new deployment even if nothing has changed', false)
.option('-L, --login', 'Configure login')
.option('-C, --no-clipboard', 'Do not attempt to copy URL to clipboard')
.parse(process.argv);
let path = program.args[program.args.length - 1];
if (path) {
if ('/' !== path[0]) {
path = resolve(process.cwd(), path);
}
} else {
path = process.cwd();
}
let config;
try {
config = fs.readFileSync(resolve(os.homedir(), '.now.json'), 'utf8');
config = JSON.parse(config);
} catch (err) {}
if (!config || !config.token) {
login()
.then((token) => {
if (program.L) {
console.log('> Logged in successfully. Token saved in ~/.now.json');
process.exit(0);
} else {
sync(token).catch((err) => {
error(`Unknown error: ${err.stack}`);
});
}
})
.catch((e) => {
error(`Authentication error – ${e.message}`);
process.exit(1);
});
} else {
sync(config.token).catch((err) => {
error(`Unknown error: ${err.stack}`);
});
}
async function sync (token) {
const debug = !!program.debug;
const clipboard = !program.noClipboard;
const start = Date.now();
console.log(`> Deploying ${path}`);
const now = new Now(token, { debug });
try {
await now.create(path, { forceNew: program.force });
} catch (err) {
handleError(err);
}
const { url } = now;
const elapsed = ms(new Date() - start);
if (clipboard) {
try {
await copy(url);
console.log(`> ${url} (copied to clipboard) [${elapsed}]`);
} catch (err) {
console.log(`> ${url} [${elapsed}]`);
}
} else {
console.log(`> ${url} [${elapsed}]`);
}
const start_u = new Date();
const complete = () => {
const elapsed_u = ms(new Date() - start_u);
console.log(`> Sync complete (${bytes(now.syncAmount)}) [${elapsed_u}] `);
now.close();
process.exit(0);
};
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 }) => {
const amount = Buffer.byteLength(data);
if (debug) {
console.log(`> [debug] Uploaded: ${name} (${bytes(Buffer.byteLength(data))})`);
}
bar.tick(amount);
});
now.on('complete', () => {
complete();
});
now.on('error', (err) => {
error('Upload failed');
handleError(err);
process.exit(1);
});
} else {
console.log('> Sync complete (cached)');
now.close();
}
}
function handleError (err) {
if (403 === err.status) {
error('Authentication error. Run `now -L` to log-in again.');
} else if (500 === err.status) {
error('Unexpected server error. Please retry.');
} else {
error(`Unexpected error. Please try later. (${err.message})`);
}
process.exit(1);
}
function error (err) {
console.error(`> \u001b[31mError!\u001b[39m ${err}.`);
}