-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ls.js
64 lines (56 loc) · 2.05 KB
/
ls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// @flow
import chalk from 'chalk'
import ms from 'ms'
import plural from 'pluralize'
import table from 'text-table'
import { CLIContext, Output } from '../../util/types'
import getContextName from '../../util/get-context-name'
import getDomains from '../../util/domains/get-domains'
import isDomainExternal from '../../util/domains/is-domain-external'
import Now from '../../util'
import stamp from '../../../../util/output/stamp'
import strlen from '../../util/strlen'
import type { CLIDomainsOptions, Domain } from '../../util/types'
async function ls(ctx: CLIContext, opts: CLIDomainsOptions, args: string[], output: Output): Promise<number> {
const {authConfig: { credentials }, config: { sh }} = ctx
const { currentTeam } = sh;
const contextName = getContextName(sh);
const { apiUrl } = ctx;
// $FlowFixMe
const {token} = credentials.find(item => item.provider === 'sh')
const now = new Now({ apiUrl, token, debug: opts['--debug'], currentTeam })
const lsStamp = stamp()
if (args.length !== 0) {
output.error(`Invalid number of arguments. Usage: ${chalk.cyan('`now domains ls`')}`)
return 1;
}
const domains = await getDomains(output, now, contextName);
output.log(`${plural('domain', domains.length, true)} found under ${chalk.bold(contextName)} ${chalk.gray(lsStamp())}\n`)
if (domains.length > 0) {
console.log(formatDomainsTable(domains))
}
return 0;
}
function formatDomainsTable(domains: Domain[]) {
const current = new Date();
return table(
[
['', 'domain', 'dns', 'verified', 'cdn', 'age'].map(s => chalk.dim(s)),
...domains.map(domain => {
const cdnEnabled = domain.cdnEnabled || false
const ns = isDomainExternal(domain) ? 'external' : 'zeit.world'
const url = chalk.bold(domain.name)
const time = chalk.gray(
ms(current - new Date(domain.created))
)
return ['', url, ns, domain.verified, cdnEnabled, time]
})
],
{
align: ['l', 'l', 'l', 'l', 'l'],
hsep: ' '.repeat(2),
stringLength: strlen
}
)
}
export default ls;