@deepdynamic/cli 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +32 -2
- package/THIRD_PARTY_NOTICES.txt +102 -0
- package/dist/agents/content.js +12 -0
- package/dist/agents/edit.js +140 -0
- package/dist/agents/install.js +190 -0
- package/dist/agents/refresh.js +20 -0
- package/dist/agents/setup.js +29 -0
- package/dist/agents/targets.js +91 -0
- package/dist/auth/credentials.js +63 -0
- package/dist/auth/session.js +49 -0
- package/dist/cli.js +198 -0
- package/dist/commands/account.js +96 -0
- package/dist/commands/activity.js +80 -0
- package/dist/commands/blocked.js +89 -0
- package/dist/commands/deploy/agents.js +11 -0
- package/dist/commands/deploy/files.js +62 -0
- package/dist/commands/deploy/index.js +95 -0
- package/dist/commands/deploy/manifest.js +24 -0
- package/dist/commands/deploy/minify.js +27 -0
- package/dist/commands/deploy/progress.js +85 -0
- package/dist/commands/deploy/ship.js +117 -0
- package/dist/commands/deploy/upload.js +125 -0
- package/dist/commands/deployments.js +60 -0
- package/dist/commands/dns.js +777 -0
- package/dist/commands/domains.js +639 -0
- package/dist/commands/emails.js +74 -0
- package/dist/commands/env.js +194 -0
- package/dist/commands/help.js +148 -0
- package/dist/commands/keys.js +263 -0
- package/dist/commands/login/flow.js +155 -0
- package/dist/commands/login/index.js +26 -0
- package/dist/commands/logout.js +71 -0
- package/dist/commands/mail.js +241 -0
- package/dist/commands/notifications.js +133 -0
- package/dist/commands/plan.js +355 -0
- package/dist/commands/projects.js +838 -0
- package/dist/commands/reference.js +2187 -0
- package/dist/commands/search.js +86 -0
- package/dist/commands/server.js +301 -0
- package/dist/commands/sessions.js +114 -0
- package/dist/commands/sites.js +102 -0
- package/dist/commands/sms.js +65 -0
- package/dist/commands/status.js +87 -0
- package/dist/commands/support.js +115 -0
- package/dist/commands/update.js +100 -0
- package/dist/commands/usage.js +51 -0
- package/dist/commands/verify.js +149 -0
- package/dist/commands/version.js +6 -0
- package/dist/guide.js +37 -0
- package/dist/http/call.js +6 -0
- package/dist/http/request.js +54 -0
- package/dist/index.js +11 -0
- package/dist/metadata.js +4 -0
- package/dist/output.js +9 -0
- package/dist/pretty.js +70 -0
- package/dist/refresh-skills.js +5 -0
- package/dist/registry.js +688 -0
- package/dist/runtime/browser.js +15 -0
- package/dist/runtime/card-request.js +55 -0
- package/dist/runtime/confirm.js +21 -0
- package/dist/runtime/development.js +13 -0
- package/dist/runtime/domain-name.js +5 -0
- package/dist/runtime/dry-run.js +28 -0
- package/dist/runtime/env-file.js +55 -0
- package/dist/runtime/error-hints.js +26 -0
- package/dist/runtime/flags.js +48 -0
- package/dist/runtime/login-screen.js +78 -0
- package/dist/runtime/page.js +18 -0
- package/dist/runtime/project-name.js +1 -0
- package/dist/runtime/run-mode.js +7 -0
- package/dist/runtime/server-options.js +28 -0
- package/dist/runtime/spinner.js +17 -0
- package/dist/runtime/suggest.js +22 -0
- package/dist/runtime/support.js +13 -0
- package/dist/runtime/text-file.js +20 -0
- package/dist/runtime/timeout.js +22 -0
- package/dist/runtime/update-check.js +158 -0
- package/dist/runtime/update-install.js +80 -0
- package/dist/runtime/usage-error.js +9 -0
- package/dist/setup-agents.js +15 -0
- package/dist/site.js +1 -0
- package/dist/update-refresh.js +5 -0
- package/package.json +46 -3
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { defaultAuthDeps } from "../auth/session.js";
|
|
2
|
+
import { httpFailure } from "../http/request.js";
|
|
3
|
+
import { agentResult } from "../output.js";
|
|
4
|
+
import { prettyResult } from "../pretty.js";
|
|
5
|
+
import { plannedChange, plannedRequest } from "../runtime/dry-run.js";
|
|
6
|
+
import { parseFlags, unknownArgument } from "../runtime/flags.js";
|
|
7
|
+
import { pageRows, pagination, parsePage } from "../runtime/page.js";
|
|
8
|
+
import { DOMAIN_NOT_FOUND, parseDomain, rehearsed } from "./dns.js";
|
|
9
|
+
export const MAIL_PATH = '/api/v1/mail';
|
|
10
|
+
const POLICIES = {
|
|
11
|
+
none: { label: 'Monitor', effect: 'Spoofed mail is still delivered; you only receive reports.' },
|
|
12
|
+
quarantine: { label: 'Quarantine', effect: 'Mail that fails authentication goes to the spam folder.' },
|
|
13
|
+
reject: { label: 'Reject', effect: 'Mail that fails authentication is rejected outright.' }
|
|
14
|
+
};
|
|
15
|
+
const EMAIL = /^[a-z0-9._+-]+@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/;
|
|
16
|
+
const HOST = /^([a-z0-9_]([a-z0-9_-]*[a-z0-9])?\.)+[a-z]{2,}$/;
|
|
17
|
+
const SELECTOR = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/;
|
|
18
|
+
const PUBLIC_KEY = /^[A-Za-z0-9+/]{64,}={0,2}$/;
|
|
19
|
+
const FLAGS = ['--policy', '--rua', '--spf-include', '--selector', '--public-key'];
|
|
20
|
+
const UNSET = 'Not configured';
|
|
21
|
+
const AUTO = 'Automatic setup applied';
|
|
22
|
+
const CUSTOM = 'Custom settings in use';
|
|
23
|
+
const PENDING_APPLY = 'DNS is being prepared. The saved records will be applied once it is ready.';
|
|
24
|
+
const PROPAGATION = 'Changes usually propagate worldwide within a few minutes; old values can persist for up to the TTL.';
|
|
25
|
+
const EXTERNAL_RUA = 'Reports to an address on another domain arrive only if that domain publishes a record that authorizes them.';
|
|
26
|
+
const MANUAL_WARNING = 'Custom settings can stop mail from being sent or received.';
|
|
27
|
+
const lower = (value) => value.trim().toLowerCase();
|
|
28
|
+
const FIELDS = {
|
|
29
|
+
'--policy': {
|
|
30
|
+
key: 'policy',
|
|
31
|
+
clean: lower,
|
|
32
|
+
valid: (value) => Object.hasOwn(POLICIES, value),
|
|
33
|
+
error: '--policy must be none, quarantine or reject.'
|
|
34
|
+
},
|
|
35
|
+
'--rua': {
|
|
36
|
+
key: 'rua',
|
|
37
|
+
clean: (value) => lower(value).replace(/^mailto:/, ''),
|
|
38
|
+
valid: (value) => EMAIL.test(value),
|
|
39
|
+
error: '--rua must be an email address such as rua@example.com.'
|
|
40
|
+
},
|
|
41
|
+
'--spf-include': {
|
|
42
|
+
key: 'spfInclude',
|
|
43
|
+
clean: (value) => lower(value).replace(/^include:/, '').replace(/\.$/, ''),
|
|
44
|
+
valid: (value) => HOST.test(value),
|
|
45
|
+
error: '--spf-include must be a host name such as _spf.example.com.'
|
|
46
|
+
},
|
|
47
|
+
'--selector': {
|
|
48
|
+
key: 'selector',
|
|
49
|
+
clean: lower,
|
|
50
|
+
valid: (value) => SELECTOR.test(value),
|
|
51
|
+
error: '--selector must be lowercase letters, digits or hyphens that start and end with a letter or digit.'
|
|
52
|
+
},
|
|
53
|
+
'--public-key': {
|
|
54
|
+
key: 'publicKey',
|
|
55
|
+
clean: (value) => value.replace(/\s+/g, ''),
|
|
56
|
+
valid: (value) => PUBLIC_KEY.test(value),
|
|
57
|
+
error: '--public-key must be at least 64 base64 characters.'
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const autoConfig = (domain) => ({ policy: 'quarantine', rua: `rua@${domain}`, spfInclude: '', selector: '', publicKey: '' });
|
|
61
|
+
const externalRua = (config, domain) => config.rua.split('@')[1] !== domain;
|
|
62
|
+
const hasKey = (config) => !!config.selector && !!config.publicKey;
|
|
63
|
+
const mailUrl = (siteUrl, domain, action = '') => new URL(`${MAIL_PATH}/${encodeURIComponent(domain)}${action}`, siteUrl).href;
|
|
64
|
+
const mailOf = (body) => body.mail;
|
|
65
|
+
const stateOf = (zone) => !zone.configured ? UNSET : zone.custom ? CUSTOM : AUTO;
|
|
66
|
+
function parseSet(args, pretty) {
|
|
67
|
+
const domain = parseDomain(args[0], pretty);
|
|
68
|
+
if (typeof domain !== 'string')
|
|
69
|
+
return domain;
|
|
70
|
+
const flags = parseFlags(args.slice(1), FLAGS);
|
|
71
|
+
if ('error' in flags)
|
|
72
|
+
return flags;
|
|
73
|
+
if (!flags.entries.length) {
|
|
74
|
+
return { error: 'Give at least one of --policy, --rua, --spf-include, or --selector with --public-key.' };
|
|
75
|
+
}
|
|
76
|
+
const changes = {};
|
|
77
|
+
for (const [flag, input] of flags.entries) {
|
|
78
|
+
const { key, clean, valid, error } = FIELDS[flag];
|
|
79
|
+
if (key in changes)
|
|
80
|
+
return { error: `Give ${flag} only once.` };
|
|
81
|
+
const value = clean(input);
|
|
82
|
+
if (!valid(value))
|
|
83
|
+
return { error };
|
|
84
|
+
changes[key] = value;
|
|
85
|
+
}
|
|
86
|
+
if (('selector' in changes) !== ('publicKey' in changes))
|
|
87
|
+
return { error: 'Give --selector and --public-key together.' };
|
|
88
|
+
return { domain, changes };
|
|
89
|
+
}
|
|
90
|
+
function mailRecords(config) {
|
|
91
|
+
return [
|
|
92
|
+
{ key: 'spf', type: 'TXT', name: '@', value: `v=spf1${config.spfInclude ? ` include:${config.spfInclude}` : ''} ~all` },
|
|
93
|
+
...(hasKey(config) ? [{ key: 'dkim', type: 'TXT', name: `${config.selector}._domainkey`, value: `v=DKIM1; k=rsa; p=${config.publicKey}` }] : []),
|
|
94
|
+
{ key: 'dmarc', type: 'TXT', name: '_dmarc', value: `v=DMARC1; p=${config.policy}; rua=mailto:${config.rua}` }
|
|
95
|
+
];
|
|
96
|
+
}
|
|
97
|
+
const mailPlan = (config) => ({
|
|
98
|
+
config: { policy: config.policy, rua: config.rua, spfInclude: config.spfInclude || null, selector: hasKey(config) ? config.selector : null },
|
|
99
|
+
records: mailRecords(config)
|
|
100
|
+
});
|
|
101
|
+
function configRows(domain, config) {
|
|
102
|
+
const { label, effect } = POLICIES[config.policy] ?? { label: config.policy, effect: '' };
|
|
103
|
+
return [
|
|
104
|
+
['DMARC policy', `${label} (p=${config.policy})${effect ? ` · ${effect}` : ''}`],
|
|
105
|
+
['Report address', config.rua],
|
|
106
|
+
...(externalRua(config, domain) ? [['', EXTERNAL_RUA]] : []),
|
|
107
|
+
['Sending server', config.spfInclude || 'Not set (v=spf1 ~all)'],
|
|
108
|
+
['DKIM key', hasKey(config) ? `Custom key · ${config.selector}` : 'No key registered'],
|
|
109
|
+
...mailRecords(config).map(({ name, value }, index) => [index ? '' : 'Records', `TXT ${name} ${value}`])
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
function zoneRows(zone) {
|
|
113
|
+
return [
|
|
114
|
+
['Status', stateOf(zone)],
|
|
115
|
+
...(zone.status === 'pending' ? [['Pending', PENDING_APPLY]] : []),
|
|
116
|
+
...configRows(zone.domain, zone.config)
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
function readPlan(command, pretty, depth, steps, url, spinner, failed, notFound, done) {
|
|
120
|
+
return {
|
|
121
|
+
steps,
|
|
122
|
+
result: (siteUrl) => plannedRequest(command, pretty, depth, siteUrl, url(siteUrl)),
|
|
123
|
+
send: { method: 'GET', url, spinner, failed, notFound, done }
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
export async function runMailList(args, pretty, depth, deps = defaultAuthDeps(pretty, depth)) {
|
|
127
|
+
const command = 'mail';
|
|
128
|
+
return rehearsed(command, args, pretty, depth, deps, (rest) => {
|
|
129
|
+
const page = parsePage(rest);
|
|
130
|
+
if (typeof page !== 'number')
|
|
131
|
+
return page;
|
|
132
|
+
return readPlan(command, pretty, depth, [{ stage: 'prepare_request', message: 'Preparing the request for the mail settings of your domains.' }], (siteUrl) => `${new URL(MAIL_PATH, siteUrl).href}?page=${page}`, 'Loading mail settings.', 'Could not load mail settings.', httpFailure(404), (body, siteUrl) => {
|
|
133
|
+
const { domains, ...paged } = body;
|
|
134
|
+
const paging = pagination(paged, command);
|
|
135
|
+
if (!pretty)
|
|
136
|
+
return agentResult(0, { command, siteUrl, ...paging, domains });
|
|
137
|
+
return prettyResult(0, depth, command, domains.length ? `Loaded mail settings for ${domains.length} domain${domains.length === 1 ? '' : 's'}.` : 'You have no managed domains. Buy a domain to manage its mail settings here.', [
|
|
138
|
+
['Server', siteUrl],
|
|
139
|
+
...domains.map((zone) => [zone.domain, `${stateOf(zone)} · policy ${POLICIES[zone.config.policy]?.label ?? zone.config.policy} · reports to ${zone.config.rua}`]),
|
|
140
|
+
...pageRows(paging)
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
export async function runMailShow(args, pretty, depth, deps = defaultAuthDeps(pretty, depth)) {
|
|
146
|
+
const command = 'mail show';
|
|
147
|
+
return rehearsed(command, args, pretty, depth, deps, ([input, ...extra]) => {
|
|
148
|
+
const domain = parseDomain(input, pretty);
|
|
149
|
+
if (typeof domain !== 'string')
|
|
150
|
+
return domain;
|
|
151
|
+
if (extra.length)
|
|
152
|
+
return { error: unknownArgument(extra[0]) };
|
|
153
|
+
return readPlan(command, pretty, depth, [{ stage: 'check_domain', message: 'Checking the domain format.' }, { stage: 'prepare_request', message: 'Preparing the mail settings request.' }], (siteUrl) => mailUrl(siteUrl, domain), 'Loading mail settings.', 'Could not load mail settings.', DOMAIN_NOT_FOUND, (body, siteUrl) => {
|
|
154
|
+
const mail = mailOf(body);
|
|
155
|
+
if (!pretty)
|
|
156
|
+
return agentResult(0, { command, siteUrl, mail });
|
|
157
|
+
return prettyResult(0, depth, command, `Loaded the mail settings for ${mail.domain}.`, [['Server', siteUrl], ['Domain', mail.domain], ...zoneRows(mail)]);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
function applied(command, pretty, depth, message, body, siteUrl) {
|
|
162
|
+
const mail = mailOf(body);
|
|
163
|
+
return pretty
|
|
164
|
+
? prettyResult(0, depth, command, message, [['Server', siteUrl], ['Domain', mail.domain], ...zoneRows(mail), ['Propagation', PROPAGATION]])
|
|
165
|
+
: agentResult(0, { command, siteUrl, applied: true, domain: mail.domain, mail });
|
|
166
|
+
}
|
|
167
|
+
export async function runMailSet(args, pretty, depth, deps = defaultAuthDeps(pretty, depth)) {
|
|
168
|
+
const command = 'mail set';
|
|
169
|
+
return rehearsed(command, args, pretty, depth, deps, (rest) => {
|
|
170
|
+
const parsed = parseSet(rest, pretty);
|
|
171
|
+
if ('error' in parsed)
|
|
172
|
+
return parsed;
|
|
173
|
+
const { domain, changes } = parsed;
|
|
174
|
+
const config = { ...autoConfig(domain), ...changes };
|
|
175
|
+
return {
|
|
176
|
+
confirmArgs: [domain, ...rest.slice(1)],
|
|
177
|
+
steps: [
|
|
178
|
+
{ stage: 'normalize_input', message: 'Removing mailto: and include: prefixes and whitespace from the input.' },
|
|
179
|
+
{ stage: 'validate_config', message: 'Checking the report address, sending server and DKIM key formats.' },
|
|
180
|
+
{ stage: 'build_records', message: 'Building the SPF, DKIM and DMARC records to apply.' },
|
|
181
|
+
{ stage: 'prepare_request', message: 'Preparing the mail settings change request.' }
|
|
182
|
+
],
|
|
183
|
+
result: (siteUrl) => plannedChange(command, pretty, depth, siteUrl, { url: mailUrl(siteUrl, domain), body: changes }, 'Checked the mail settings. Nothing was applied.', [
|
|
184
|
+
['Domain', domain],
|
|
185
|
+
['Basis', 'The automatic setup with the given values applied.'],
|
|
186
|
+
['', 'Fields you did not give keep their current values when applied.'],
|
|
187
|
+
...configRows(domain, config),
|
|
188
|
+
['Warning', MANUAL_WARNING],
|
|
189
|
+
['', `If mail breaks, run deepdynamic mail reset ${domain} to return to the automatic setup.`]
|
|
190
|
+
], { domain, basis: 'automatic', ...mailPlan(config), externalRua: externalRua(config, domain) }),
|
|
191
|
+
send: {
|
|
192
|
+
method: 'POST',
|
|
193
|
+
url: (siteUrl) => mailUrl(siteUrl, domain),
|
|
194
|
+
body: changes,
|
|
195
|
+
spinner: 'Applying the mail settings.',
|
|
196
|
+
failed: 'Could not apply the mail settings.',
|
|
197
|
+
notFound: DOMAIN_NOT_FOUND,
|
|
198
|
+
done: (body, siteUrl) => applied(command, pretty, depth, `Saved the mail settings for ${domain}.`, body, siteUrl)
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
}, {
|
|
202
|
+
message: 'Setting replaces the managed SPF, DKIM and DMARC records of the domain.',
|
|
203
|
+
display: 'Applying replaces the SPF, DKIM and DMARC records of this domain. Confirm with the user, then run it again with --yes.'
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
export async function runMailReset(args, pretty, depth, deps = defaultAuthDeps(pretty, depth)) {
|
|
207
|
+
const command = 'mail reset';
|
|
208
|
+
return rehearsed(command, args, pretty, depth, deps, ([input, ...extra]) => {
|
|
209
|
+
const domain = parseDomain(input, pretty);
|
|
210
|
+
if (typeof domain !== 'string')
|
|
211
|
+
return domain;
|
|
212
|
+
if (extra.length)
|
|
213
|
+
return { error: unknownArgument(extra[0]) };
|
|
214
|
+
const config = autoConfig(domain);
|
|
215
|
+
return {
|
|
216
|
+
confirmArgs: [domain],
|
|
217
|
+
steps: [
|
|
218
|
+
{ stage: 'check_domain', message: 'Checking the domain format.' },
|
|
219
|
+
{ stage: 'build_records', message: 'Building the SPF and DMARC records of the automatic setup.' },
|
|
220
|
+
{ stage: 'prepare_request', message: 'Preparing the reset request.' }
|
|
221
|
+
],
|
|
222
|
+
result: (siteUrl) => plannedChange(command, pretty, depth, siteUrl, { url: mailUrl(siteUrl, domain, '/reset'), body: {} }, 'Checked the mail settings reset. Nothing was reset.', [
|
|
223
|
+
['Domain', domain],
|
|
224
|
+
['Warning', 'Custom values are removed and the automatic setup is restored. A custom DKIM record is removed too.'],
|
|
225
|
+
...configRows(domain, config)
|
|
226
|
+
], { domain, ...mailPlan(config) }),
|
|
227
|
+
send: {
|
|
228
|
+
method: 'POST',
|
|
229
|
+
url: (siteUrl) => mailUrl(siteUrl, domain, '/reset'),
|
|
230
|
+
body: {},
|
|
231
|
+
spinner: 'Resetting the mail settings.',
|
|
232
|
+
failed: 'Could not reset the mail settings.',
|
|
233
|
+
notFound: DOMAIN_NOT_FOUND,
|
|
234
|
+
done: (body, siteUrl) => applied(command, pretty, depth, `Reset the mail settings for ${domain} to the automatic setup.`, body, siteUrl)
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}, {
|
|
238
|
+
message: 'Resetting removes the hand-set policy, report address, SPF include and DKIM key of the domain.',
|
|
239
|
+
display: 'Resetting removes the custom policy, report address, SPF include and DKIM key. Confirm with the user, then run it again with --yes.'
|
|
240
|
+
});
|
|
241
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { defaultAuthDeps } from "../auth/session.js";
|
|
2
|
+
import { httpFailure } from "../http/request.js";
|
|
3
|
+
import { agentResult } from "../output.js";
|
|
4
|
+
import { prettyResult } from "../pretty.js";
|
|
5
|
+
import { plannedChange, plannedRequest } from "../runtime/dry-run.js";
|
|
6
|
+
import { parseFlags, unknownArgument } from "../runtime/flags.js";
|
|
7
|
+
import { rehearsed } from "./dns.js";
|
|
8
|
+
export const NOTIFICATIONS_PATH = '/api/v1/notifications';
|
|
9
|
+
export const NOTIFICATIONS_READ_PATH = '/api/v1/notifications/read';
|
|
10
|
+
export const NOTIFICATION_KINDS = ['billing_failed', 'certificate_failed', 'certificate_expiring', 'domain_expiring', 'autopilot_limit', 'service_notice'];
|
|
11
|
+
export const NOTIFICATION_LIMIT_MAX = 100;
|
|
12
|
+
export const NOTIFICATION_READ_MAX = 100;
|
|
13
|
+
const UNREAD = '--unread';
|
|
14
|
+
const ALL = '--all';
|
|
15
|
+
const FLAGS = ['--limit'];
|
|
16
|
+
const LIMIT_RE = /^[1-9]\d{0,2}$/;
|
|
17
|
+
const NOTIFICATION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
18
|
+
const LIST_STEPS = [
|
|
19
|
+
{ stage: 'validate_filters', message: 'Checking the filters.' },
|
|
20
|
+
{ stage: 'prepare_request', message: 'Preparing the notifications request.' }
|
|
21
|
+
];
|
|
22
|
+
const READ_STEPS = [
|
|
23
|
+
{ stage: 'validate_ids', message: 'Checking the notification ID format.' },
|
|
24
|
+
{ stage: 'prepare_request', message: 'Preparing the mark-as-read request.' }
|
|
25
|
+
];
|
|
26
|
+
const once = (flag) => ({ error: `${flag} can be given only once.` });
|
|
27
|
+
function parseListArgs(args) {
|
|
28
|
+
const unreadCount = args.filter((arg) => arg === UNREAD).length;
|
|
29
|
+
if (unreadCount > 1)
|
|
30
|
+
return once(UNREAD);
|
|
31
|
+
const parsed = parseFlags(args.filter((arg) => arg !== UNREAD), FLAGS);
|
|
32
|
+
if ('error' in parsed)
|
|
33
|
+
return parsed;
|
|
34
|
+
if (parsed.entries.length > 1)
|
|
35
|
+
return once('--limit');
|
|
36
|
+
const limit = parsed.entries[0]?.[1];
|
|
37
|
+
if (limit !== undefined && (!LIMIT_RE.test(limit) || Number(limit) > NOTIFICATION_LIMIT_MAX)) {
|
|
38
|
+
return { error: `--limit requires an integer from 1 to ${NOTIFICATION_LIMIT_MAX}: ${limit}` };
|
|
39
|
+
}
|
|
40
|
+
return { limit: limit === undefined ? null : Number(limit), unread: unreadCount === 1 };
|
|
41
|
+
}
|
|
42
|
+
function parseReadArgs(args) {
|
|
43
|
+
const allCount = args.filter((arg) => arg === ALL).length;
|
|
44
|
+
if (allCount > 1)
|
|
45
|
+
return once(ALL);
|
|
46
|
+
const option = args.find((arg) => arg.startsWith('-') && arg !== ALL);
|
|
47
|
+
if (option)
|
|
48
|
+
return { error: unknownArgument(option) };
|
|
49
|
+
const given = args.filter((arg) => arg !== ALL);
|
|
50
|
+
if (allCount && given.length)
|
|
51
|
+
return { error: 'Give notification IDs or --all, not both.' };
|
|
52
|
+
if (allCount)
|
|
53
|
+
return { all: true };
|
|
54
|
+
if (!given.length)
|
|
55
|
+
return { error: 'Give one or more notification IDs, or --all.' };
|
|
56
|
+
const invalid = given.find((arg) => !NOTIFICATION_ID.test(arg.toLowerCase()));
|
|
57
|
+
if (invalid !== undefined) {
|
|
58
|
+
return { error: `Notification IDs are the UUID id values listed by deepdynamic account notifications: ${invalid}` };
|
|
59
|
+
}
|
|
60
|
+
const ids = [...new Set(given.map((arg) => arg.toLowerCase()))];
|
|
61
|
+
if (ids.length > NOTIFICATION_READ_MAX) {
|
|
62
|
+
return { error: `Give at most ${NOTIFICATION_READ_MAX} notification IDs at once.` };
|
|
63
|
+
}
|
|
64
|
+
return { ids };
|
|
65
|
+
}
|
|
66
|
+
const notificationCount = (count) => `${count} notification${count === 1 ? '' : 's'}`;
|
|
67
|
+
const seoulTime = (iso) => `${new Date(iso).toLocaleString('sv-SE', { timeZone: 'Asia/Seoul' }).slice(0, 16)} KST`;
|
|
68
|
+
function notificationLine(notification) {
|
|
69
|
+
const parts = [notification.kind, notification.title, ...(notification.readAt === null ? ['unread'] : [])];
|
|
70
|
+
return [seoulTime(notification.createdAt), parts.join(' · ')];
|
|
71
|
+
}
|
|
72
|
+
export async function runNotificationsList(args, pretty, depth, deps = defaultAuthDeps(pretty, depth)) {
|
|
73
|
+
const command = 'account notifications';
|
|
74
|
+
return rehearsed(command, args, pretty, depth, deps, (rest) => {
|
|
75
|
+
const options = parseListArgs(rest);
|
|
76
|
+
if ('error' in options)
|
|
77
|
+
return options;
|
|
78
|
+
const url = (siteUrl) => {
|
|
79
|
+
const endpoint = new URL(NOTIFICATIONS_PATH, siteUrl);
|
|
80
|
+
if (options.limit !== null)
|
|
81
|
+
endpoint.searchParams.set('limit', String(options.limit));
|
|
82
|
+
if (options.unread)
|
|
83
|
+
endpoint.searchParams.set('unread', 'true');
|
|
84
|
+
return endpoint.href;
|
|
85
|
+
};
|
|
86
|
+
return {
|
|
87
|
+
steps: LIST_STEPS,
|
|
88
|
+
result: (siteUrl) => plannedRequest(command, pretty, depth, siteUrl, url(siteUrl)),
|
|
89
|
+
send: {
|
|
90
|
+
method: 'GET',
|
|
91
|
+
url,
|
|
92
|
+
spinner: 'Loading notifications.',
|
|
93
|
+
failed: 'Could not load notifications.',
|
|
94
|
+
notFound: httpFailure(404),
|
|
95
|
+
done: (body, siteUrl) => {
|
|
96
|
+
const { notifications, unread } = body;
|
|
97
|
+
if (!pretty) {
|
|
98
|
+
return agentResult(0, { command, siteUrl, unread, notifications: notifications.map((notification) => ({ ...notification, url: new URL(notification.link, siteUrl).href })) });
|
|
99
|
+
}
|
|
100
|
+
return prettyResult(0, depth, command, `Loaded ${notificationCount(notifications.length)}, ${unread} unread.`, [['Server', siteUrl], ...notifications.map(notificationLine)]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export async function runNotificationsRead(args, pretty, depth, deps = defaultAuthDeps(pretty, depth)) {
|
|
107
|
+
const command = 'account notifications read';
|
|
108
|
+
return rehearsed(command, args, pretty, depth, deps, (rest) => {
|
|
109
|
+
const target = parseReadArgs(rest);
|
|
110
|
+
if ('error' in target)
|
|
111
|
+
return target;
|
|
112
|
+
const url = (siteUrl) => new URL(NOTIFICATIONS_READ_PATH, siteUrl).href;
|
|
113
|
+
const scope = 'all' in target ? 'All notifications' : notificationCount(target.ids.length);
|
|
114
|
+
return {
|
|
115
|
+
steps: READ_STEPS,
|
|
116
|
+
result: (siteUrl) => plannedChange(command, pretty, depth, siteUrl, { url: url(siteUrl), body: target }, 'Checked the mark-as-read request. Nothing was changed.', [['Target', scope]], target),
|
|
117
|
+
send: {
|
|
118
|
+
method: 'POST',
|
|
119
|
+
url,
|
|
120
|
+
body: target,
|
|
121
|
+
spinner: 'Marking notifications as read.',
|
|
122
|
+
failed: 'Could not mark notifications as read.',
|
|
123
|
+
notFound: httpFailure(404),
|
|
124
|
+
done: (body, siteUrl) => {
|
|
125
|
+
const { updated, unread } = body;
|
|
126
|
+
return pretty
|
|
127
|
+
? prettyResult(0, depth, command, `Marked ${notificationCount(updated)} as read.`, [['Server', siteUrl], ['Target', scope], ['Unread', String(unread)]])
|
|
128
|
+
: agentResult(0, { command, siteUrl, updated, unread });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
}
|