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.

83 lines
1.8 KiB

9 years ago
#!/usr/bin/env node
import program from 'commander';
import { resolve } from 'path';
import { copy } from 'copy-paste';
import login from '../lib/login';
9 years ago
import now from '../lib';
import fs from 'fs';
9 years ago
import ms from 'ms';
import os from 'os';
9 years ago
program
.option('-d, --debug', 'Debug mode [off]', false)
.option('-L, --login', 'Configure login')
9 years ago
.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;
9 years ago
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((e) => {
error(`Authentication error – ${e.message}`);
process.exit(1);
});
} else {
sync(config.token);
}
9 years ago
const sync = (token) => {
const debug = !!program.debug;
const clipboard = !program.noClipboard;
const start = Date.now();
console.log(`> Deploying ${path}`);
9 years ago
now(path, token, { debug })
.then((url) => {
const elapsed = ms(new Date() - start);
if (clipboard) {
copy(url, (err) => {
console.log(`> ${url} ${!err ? '(copied to clipboard)' : ''} [${elapsed}]`);
});
} else {
console.log(`> ${url} [${elapsed}]`);
}
}, (err) => {
if (403 === err.statusCode) {
error('Authorization error. Run `now -L` to log-in again.');
} else {
error(err.message);
}
process.exit(1);
});
};
9 years ago
function error (err) {
console.error(`> \u001b[31mError!\u001b[39m ${err}.`);
}