|
|
@ -2,11 +2,15 @@ |
|
|
|
import program from 'commander'; |
|
|
|
import { resolve } from 'path'; |
|
|
|
import { copy } from 'copy-paste'; |
|
|
|
import login from '../lib/login'; |
|
|
|
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('-L, --login', 'Configure login') |
|
|
|
.option('-C, --no-clipboard', 'Do not attempt to copy URL to clipboard') |
|
|
|
.parse(process.argv); |
|
|
|
|
|
|
@ -20,25 +24,53 @@ if (path) { |
|
|
|
path = process.cwd(); |
|
|
|
} |
|
|
|
|
|
|
|
const debug = !!program.debug; |
|
|
|
const clipboard = !program.noClipboard; |
|
|
|
const start = Date.now(); |
|
|
|
let config; |
|
|
|
|
|
|
|
console.log(`> Deploying ${path}`); |
|
|
|
try { |
|
|
|
config = fs.readFileSync(resolve(os.homedir(), '.now.json'), 'utf8'); |
|
|
|
config = JSON.parse(config); |
|
|
|
} catch (err) { |
|
|
|
login() |
|
|
|
.then(() => { |
|
|
|
if (program.L) { |
|
|
|
console.log('> Logged in successfully. Token saved in ~/.now.json'); |
|
|
|
process.exit(0); |
|
|
|
} else { |
|
|
|
sync(); |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch((e) => { |
|
|
|
error(`Authentication error – ${e.message}`); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
now(path, { 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) => { |
|
|
|
error(err.message); |
|
|
|
process.exit(1); |
|
|
|
}); |
|
|
|
const sync = () => { |
|
|
|
const debug = !!program.debug; |
|
|
|
const clipboard = !program.noClipboard; |
|
|
|
const start = Date.now(); |
|
|
|
|
|
|
|
console.log(`> Deploying ${path}`); |
|
|
|
|
|
|
|
now(path, { 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); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
function error (err) { |
|
|
|
console.error(`> \u001b[31mError!\u001b[39m ${err}.`); |
|
|
|