nkzawa
9 years ago
6 changed files with 113 additions and 46 deletions
@ -1,3 +1,44 @@ |
|||
#!/usr/bin/env node |
|||
|
|||
console.log('!list', process.argv); |
|||
import minimist from 'minimist'; |
|||
import Now from '../lib'; |
|||
import login from '../lib/login'; |
|||
import * as cfg from '../lib/cfg'; |
|||
import { handleError, error } from '../lib/error'; |
|||
|
|||
const argv = minimist(process.argv.slice(2)); |
|||
const app = argv._[0]; |
|||
|
|||
// options |
|||
const debug = argv.debug || argv.d; |
|||
const apiUrl = argv.url || 'https://api.now.sh'; |
|||
|
|||
const config = cfg.read(); |
|||
|
|||
Promise.resolve(config.token || login(apiUrl)) |
|||
.then(async (token) => { |
|||
try { |
|||
await list(token); |
|||
} catch (err) { |
|||
error(`Unknown error: ${err.stack}`); |
|||
process.exit(1); |
|||
} |
|||
}) |
|||
.catch((e) => { |
|||
error(`Authentication error – ${e.message}`); |
|||
process.exit(1); |
|||
}); |
|||
|
|||
async function list (token) { |
|||
const now = new Now(apiUrl, token, { debug }); |
|||
|
|||
let deployments; |
|||
try { |
|||
deployments = await now.list(app); |
|||
} catch (err) { |
|||
handleError(err); |
|||
process.exit(1); |
|||
} |
|||
|
|||
console.log(deployments); |
|||
} |
|||
|
@ -0,0 +1,25 @@ |
|||
import ms from 'ms'; |
|||
|
|||
export function handleError (err) { |
|||
if (403 === err.status) { |
|||
error('Authentication error. Run `now -L` or `now --login` to log-in again.'); |
|||
} 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.'); |
|||
} |
|||
} else if (err.userError) { |
|||
error(err.message); |
|||
} else if (500 === err.status) { |
|||
error('Unexpected server error. Please retry.'); |
|||
} else { |
|||
error(`Unexpected error. Please try later. (${err.message})`); |
|||
} |
|||
} |
|||
|
|||
export function error (err) { |
|||
console.error(`> \u001b[31mError!\u001b[39m ${err}`); |
|||
} |
Loading…
Reference in new issue