@basaltkit/deploy-ui 0.3.0 → 0.4.0

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/README.md CHANGED
@@ -64,6 +64,7 @@ repository must not be able to repoint it. The file holds no secret.
64
64
  {
65
65
  "version": 1,
66
66
  "actor": "alice",
67
+ "locale": "pt",
67
68
  "targets": [
68
69
  { "name": "staging", "ssh": "deploy@203.0.113.10", "dir": "/srv/myapp" },
69
70
  { "name": "production", "ssh": "myapp-prod", "port": 2222, "dir": "/srv/myapp", "node": "/usr/local/bin/node", "protected": true }
@@ -81,6 +82,7 @@ repository must not be able to repoint it. The file holds no secret.
81
82
  | `config` | optional `--config` path, relative to the checkout |
82
83
  | `protected` | deploy and rollback need the target's name typed |
83
84
  | `actor` | recorded with each release (`--actor`); default: your OS user name |
85
+ | `locale` | the page's language, `en` or `pt`, as you last picked it; default: your browser's language |
84
86
 
85
87
  Every field has a closed grammar (no leading `-`, no spaces, no `..`); an
86
88
  unknown field is refused, not ignored. The UI refuses a targets file that is a
@@ -107,6 +109,10 @@ with its subject and, one click away, the commits it brought in), with the
107
109
  commits that would leave production listed before you confirm; and
108
110
  "Write .env only" for a first setup.
109
111
 
112
+ The page speaks **English and Portuguese**: your browser's language until you
113
+ pick one in the sidebar, then the one kept in the targets file. Only its own
114
+ words are translated; what the server's deploy prints is shown as it came.
115
+
110
116
  Each action is one `ssh` invocation of the app's own, lockfile-pinned
111
117
  `node node_modules/@basaltkit/deploy/dist/bin.js <command> --json=1`, with
112
118
  `--via=ui` so the release record says how it was made. `install`, `bootstrap`
@@ -1,3 +1,4 @@
1
+ import { t } from './i18n.js';
1
2
  /**
2
3
  * The page's calls to its local server: POST, JSON, the session token as a
3
4
  * bearer header (never a cookie: cookies are not port-scoped). Streams are
@@ -112,7 +113,7 @@ export class Api {
112
113
  const crypto = this.options.crypto;
113
114
  const serverKey = this.options.serverKey;
114
115
  if (!serverKey || !crypto?.subtle)
115
- throw this.stop('unverified', 'this page cannot check its local server (no server key, or no WebCrypto): nothing is sent');
116
+ throw this.stop('unverified', t('api.cannotCheck'));
116
117
  const nonce = toBase64Url(crypto.getRandomValues(new Uint8Array(32)));
117
118
  let proof;
118
119
  try {
@@ -121,7 +122,7 @@ export class Api {
121
122
  catch (error) {
122
123
  if (error instanceof ServerGone)
123
124
  throw error;
124
- throw this.stop('unverified', 'the local server refused to prove it is this UI: it is not the one this page was opened from');
125
+ throw this.stop('unverified', t('api.refusedProof'));
125
126
  }
126
127
  let valid = false;
127
128
  if (typeof proof === 'string' && /^[A-Za-z0-9_-]{43}$/.test(proof)) {
@@ -129,7 +130,7 @@ export class Api {
129
130
  valid = await crypto.subtle.verify('HMAC', key, fromBase64Url(proof), new TextEncoder().encode(`${PROOF_CONTEXT}${nonce}`));
130
131
  }
131
132
  if (!valid)
132
- throw this.stop('unverified', 'the local server could not prove it is this UI: another program may be listening on its port');
133
+ throw this.stop('unverified', t('api.wrongProof'));
133
134
  }
134
135
  /** A call; one that carries answers checks the server first. */
135
136
  async post(route, body = {}) {
@@ -171,7 +172,7 @@ export class Api {
171
172
  // The page's own bug is not the server going away.
172
173
  if (handlerFailed || signal?.aborted)
173
174
  throw error;
174
- throw this.stop('unreachable', 'the deploy UI stopped while this stream was open');
175
+ throw this.stop('unreachable', t('api.streamStopped'));
175
176
  }
176
177
  }
177
178
  async send(route, body) {
@@ -190,7 +191,7 @@ export class Api {
190
191
  catch (error) {
191
192
  if (signal?.aborted)
192
193
  throw error;
193
- throw this.stop('unreachable', 'the deploy UI is not running any more (it stopped after its idle time, from Quit, or with Ctrl-C)');
194
+ throw this.stop('unreachable', t('api.notRunning'));
194
195
  }
195
196
  }
196
197
  stop(why, message) {
@@ -1,6 +1,7 @@
1
1
  import { ApiError } from './api.js';
2
2
  import { confirmDialog } from './dialog.js';
3
3
  import { button, el, emptyState, quiet, replace } from './dom.js';
4
+ import { getLocale, isLocale, LOCALES, setLocale, t } from './i18n.js';
4
5
  import { icon } from './icons.js';
5
6
  import { describe } from './reply.js';
6
7
  import { renderHistory } from './view-history.js';
@@ -21,7 +22,20 @@ export function forgetSession(storage) {
21
22
  // No storage: nothing kept.
22
23
  }
23
24
  }
24
- const SCREENS = [['status', 'Status', 'status'], ['plan', 'Plan & deploy', 'plan'], ['history', 'History', 'history'], ['run', 'Run', 'run'], ['targets', 'Targets', 'targets']];
25
+ const SCREENS = [['status', 'screen.status', 'status'], ['plan', 'screen.plan', 'plan'], ['history', 'screen.history', 'history'], ['run', 'screen.run', 'run'], ['targets', 'screen.targets', 'targets']];
26
+ /**
27
+ * The page's fixed shell (page.ts) in the current language: the document's
28
+ * `lang`, the screens' landmark and the local-only note.
29
+ */
30
+ export function applyShell(doc) {
31
+ doc.documentElement.lang = getLocale();
32
+ doc.getElementById('nav')?.setAttribute('aria-label', t('shell.screens'));
33
+ const note = doc.querySelector('.local-note');
34
+ note?.setAttribute('title', t('shell.localOnlyTitle'));
35
+ const label = doc.querySelector('.local-label');
36
+ if (label)
37
+ replace(label, t('shell.localOnly'));
38
+ }
25
39
  /** Per-target state the screens render; every server string in it is shown as text only. */
26
40
  export class App {
27
41
  api;
@@ -58,7 +72,7 @@ export class App {
58
72
  this.stopped = true;
59
73
  this.run?.stop();
60
74
  forgetSession(this.storage);
61
- replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', 'Disconnected', 'This page sends nothing from here on.'), el('div', { class: 'banner err', role: 'alert' }, icon('error', 'icon banner-icon'), el('div', { class: 'banner-body' }, el('strong', { class: 'banner-title' }, error.why === 'unverified' ? 'This page no longer talks to your deploy UI.' : 'The deploy UI has stopped.'), el('p', {}, error.message), el('p', {}, 'Nothing was sent. Detached runs go on on the server. Start basalt-deploy-ui again and open the URL it prints.')))));
75
+ replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', t('gone.title'), t('gone.sendsNothing')), el('div', { class: 'banner err', role: 'alert' }, icon('error', 'icon banner-icon'), el('div', { class: 'banner-body' }, el('strong', { class: 'banner-title' }, error.why === 'unverified' ? t('gone.unverified') : t('gone.stopped')), el('p', {}, error.message), el('p', {}, t('gone.help'))))));
62
76
  replace(this.shell.nav);
63
77
  this.clearBar();
64
78
  replace(this.shell.session);
@@ -70,8 +84,10 @@ export class App {
70
84
  return this.planReply?.result;
71
85
  }
72
86
  async start() {
73
- replace(this.shell.session, el('button', { type: 'button', class: 'ghost danger', on: { click: () => { void this.quit(); } } }, icon('power'), 'Quit'));
74
87
  await this.loadTargets();
88
+ if (this.targets.locale && this.targets.locale !== getLocale())
89
+ this.useLocale(this.targets.locale);
90
+ this.renderSession();
75
91
  const first = this.targets.targets[0];
76
92
  if (first) {
77
93
  this.select(first.name);
@@ -81,6 +97,37 @@ export class App {
81
97
  else
82
98
  this.show('targets');
83
99
  }
100
+ /** The switcher and Quit, at the foot of the sidebar. */
101
+ renderSession() {
102
+ const select = el('select', { id: 'locale-select', aria: { label: t('language') }, on: { change: (event) => {
103
+ const value = event.target.value;
104
+ if (isLocale(value))
105
+ void this.chooseLocale(value);
106
+ } } }, ...LOCALES.map((locale) => {
107
+ const option = el('option', { value: locale }, t(`language.${locale}`));
108
+ option.lang = locale;
109
+ option.selected = locale === getLocale();
110
+ return option;
111
+ }));
112
+ replace(this.shell.session, el('div', { class: 'locale' }, icon('globe'), select), el('button', { type: 'button', class: 'ghost danger', on: { click: () => { void this.quit(); } } }, icon('power'), t('quit')));
113
+ }
114
+ /** Every screen again in another language; nothing is asked of the server. */
115
+ useLocale(locale) {
116
+ setLocale(locale);
117
+ applyShell(this.shell.main.ownerDocument);
118
+ }
119
+ /** The operator's choice: the page switches at once, and the targets file keeps it for the next start (best effort). */
120
+ async chooseLocale(locale) {
121
+ this.useLocale(locale);
122
+ this.renderSession();
123
+ this.refresh();
124
+ try {
125
+ this.targets = await this.api.post('targets/save', { locale });
126
+ }
127
+ catch {
128
+ // Not saved: this tab still speaks it.
129
+ }
130
+ }
84
131
  async loadTargets() {
85
132
  this.targets = await this.api.post('targets');
86
133
  if (this.current && !this.targets.targets.some((t) => t.name === this.current?.name))
@@ -118,30 +165,30 @@ export class App {
118
165
  if (screen === 'targets')
119
166
  return replace(main, renderTargets(this));
120
167
  if (!this.current)
121
- return replace(main, emptyState('targets', 'No target selected', 'Add a target first.', el('span', { class: 'row' }, button('Targets', () => this.show('targets'), { class: 'primary' }))));
168
+ return replace(main, emptyState('targets', t('app.noTarget'), t('app.addTargetFirst'), el('span', { class: 'row' }, button(t('screen.targets'), () => this.show('targets'), { class: 'primary' }))));
122
169
  if (screen === 'status')
123
170
  return replace(main, renderStatus(this));
124
171
  if (screen === 'plan')
125
172
  return replace(main, renderPlan(this));
126
173
  if (screen === 'history')
127
174
  return replace(main, renderHistory(this));
128
- replace(main, this.run ? this.run.element : el('section', { aria: { label: 'Run' } }, emptyState('run', 'Nothing running here', 'No run in this tab yet: start one from Plan & deploy, or attach from Status.')));
175
+ replace(main, this.run ? this.run.element : el('section', { aria: { label: t('screen.run') } }, emptyState('run', t('app.nothingRunning'), t('app.nothingRunningText'))));
129
176
  }
130
177
  /** Re-renders the current screen (after a state change). */
131
178
  refresh() {
132
179
  this.show(this.screen);
133
180
  }
134
181
  renderNav() {
135
- const select = el('select', { id: 'target-select', aria: { label: 'Target' }, on: { change: (event) => { this.select(event.target.value); this.refresh(); if (this.current)
182
+ const select = el('select', { id: 'target-select', aria: { label: t('target') }, on: { change: (event) => { this.select(event.target.value); this.refresh(); if (this.current)
136
183
  void this.refreshStatus(); } } }, ...this.targets.targets.map((t) => {
137
184
  const option = el('option', { value: t.name }, t.name);
138
185
  option.selected = t.name === this.current?.name;
139
186
  return option;
140
187
  }));
141
- replace(this.shell.nav, this.targets.targets.length > 0 ? el('div', { class: 'nav-target' }, el('label', { class: 'section-label', for: 'target-select' }, 'Target'), select) : null, el('div', { class: 'nav-list' }, ...SCREENS.map(([screen, label, name]) => el('button', {
188
+ replace(this.shell.nav, this.targets.targets.length > 0 ? el('div', { class: 'nav-target' }, el('label', { class: 'section-label', for: 'target-select' }, t('target')), select) : null, el('div', { class: 'nav-list' }, ...SCREENS.map(([screen, label, name]) => el('button', {
142
189
  type: 'button', class: 'nav-item', ...(screen === this.screen ? { aria: { current: 'page' } } : {}),
143
190
  on: { click: () => this.show(screen) },
144
- }, icon(name), label))));
191
+ }, icon(name), t(label)))));
145
192
  }
146
193
  /**
147
194
  * "Am I about to deploy production?": name, destination, the server's host
@@ -149,29 +196,29 @@ export class App {
149
196
  * screen readers and copy, drawn as rules.
150
197
  */
151
198
  renderBar() {
152
- const t = this.current;
153
- if (!t)
199
+ const target = this.current;
200
+ if (!target)
154
201
  return this.clearBar();
155
202
  const status = this.status;
156
203
  const sep = () => el('span', { class: 'ctx-sep', aria: { hidden: 'true' } }, ' · ');
157
204
  const item = (name, value, label) => [sep(),
158
205
  el('span', { class: 'ctx-item' }, icon(name), label ? el('span', { class: 'ctx-label' }, label) : null, label ? ' ' : null, el('span', { class: 'ctx-val' }, value))];
159
- this.shell.bar.classList.toggle('is-protected', t.protected === true);
160
- replace(this.shell.bar, el('span', { class: 'ctx-target' }, el('strong', { class: 'ctx-name' }, t.name), t.protected ? el('span', { class: 'badge badge-protected', title: 'Deploy and rollback ask for the name to be typed' }, icon('shield'), quiet(' '), 'PROTECTED') : null), ...item('terminal', `${t.ssh}${t.port ? `:${t.port}` : ''}`), ...item('folder', t.dir), ...(this.hello ? [...item('server', this.hello.host, 'host'), ...item('tag', this.hello.version, 'deploy')] : []), ...(status ? item('app', status.app, 'app') : []));
206
+ this.shell.bar.classList.toggle('is-protected', target.protected === true);
207
+ replace(this.shell.bar, el('span', { class: 'ctx-target' }, el('strong', { class: 'ctx-name' }, target.name), target.protected ? el('span', { class: 'badge badge-protected', title: t('protected.title') }, icon('shield'), quiet(' '), t('protected.badge')) : null), ...item('terminal', `${target.ssh}${target.port ? `:${target.port}` : ''}`), ...item('folder', target.dir), ...(this.hello ? [...item('server', this.hello.host, t('ctx.host')), ...item('tag', this.hello.version, t('ctx.deploy'))] : []), ...(status ? item('app', status.app, t('ctx.app')) : []));
161
208
  }
162
209
  clearBar() {
163
210
  this.shell.bar.classList.remove('is-protected');
164
211
  replace(this.shell.bar);
165
212
  }
166
213
  async refreshStatus() {
167
- const t = this.current;
168
- if (!t)
214
+ const target = this.current;
215
+ if (!target)
169
216
  return;
170
217
  this.statusBusy = true;
171
218
  if (this.screen === 'status')
172
219
  this.refresh();
173
220
  try {
174
- this.statusReply = await this.api.post('status', { target: t.name });
221
+ this.statusReply = await this.api.post('status', { target: target.name });
175
222
  if (this.statusReply.hello)
176
223
  this.hello = this.statusReply.hello;
177
224
  }
@@ -181,18 +228,18 @@ export class App {
181
228
  finally {
182
229
  this.statusBusy = false;
183
230
  }
184
- if (this.current?.name === t.name)
231
+ if (this.current?.name === target.name)
185
232
  this.refresh();
186
233
  }
187
234
  async runPlan() {
188
- const t = this.current;
189
- if (!t)
235
+ const target = this.current;
236
+ if (!target)
190
237
  return;
191
238
  this.planBusy = true;
192
239
  this.planReply = undefined;
193
240
  this.refresh();
194
241
  try {
195
- this.planReply = await this.api.post('plan', { target: t.name });
242
+ this.planReply = await this.api.post('plan', { target: target.name });
196
243
  if (this.planReply.hello)
197
244
  this.hello = this.planReply.hello;
198
245
  }
@@ -202,16 +249,16 @@ export class App {
202
249
  finally {
203
250
  this.planBusy = false;
204
251
  }
205
- if (this.current?.name === t.name)
252
+ if (this.current?.name === target.name)
206
253
  this.refresh();
207
254
  }
208
255
  /** The commits between two releases of the current target (read-only on the server). */
209
256
  commits(from, to) {
210
- const t = this.current;
211
- const key = `${t.name}\u0000${from}\u0000${to}`;
257
+ const target = this.current;
258
+ const key = `${target.name}\u0000${from}\u0000${to}`;
212
259
  let reply = this.ranges.get(key);
213
260
  if (!reply) {
214
- reply = this.api.post('commits', { target: t.name, from, to });
261
+ reply = this.api.post('commits', { target: target.name, from, to });
215
262
  // A failure is not remembered: the next click asks again.
216
263
  reply.then((r) => { if (!r.ok)
217
264
  this.ranges.delete(key); }, () => this.ranges.delete(key));
@@ -221,8 +268,8 @@ export class App {
221
268
  }
222
269
  /** What a rollback would take out of production, now (never cached: the releases move). */
223
270
  rollbackPreview(to) {
224
- const t = this.current;
225
- return this.api.post('commits', { target: t.name, rollback: true, ...(to ? { to } : {}) });
271
+ const target = this.current;
272
+ return this.api.post('commits', { target: target.name, rollback: true, ...(to ? { to } : {}) });
226
273
  }
227
274
  /** A run to watch: a new view, attached from the seq this tab last saw (or 0). */
228
275
  watch(runId, title, fromStart = false) {
@@ -231,8 +278,7 @@ export class App {
231
278
  return this.run;
232
279
  }
233
280
  this.run?.stop();
234
- const t = this.current;
235
- this.run = new RunView(this, t, title, runId);
281
+ this.run = new RunView(this, this.current, title, runId);
236
282
  this.show('run');
237
283
  void this.run.attach(fromStart ? 0 : this.run.resumeSeq());
238
284
  return this.run;
@@ -240,8 +286,7 @@ export class App {
240
286
  /** An attended stream (env, an attended run): it stops if this page closes. */
241
287
  attended(title, route, body) {
242
288
  this.run?.stop();
243
- const t = this.current;
244
- this.run = new RunView(this, t, title, undefined);
289
+ this.run = new RunView(this, this.current, title, undefined);
245
290
  this.show('run');
246
291
  void this.run.streamAttended(route, body);
247
292
  return this.run;
@@ -249,12 +294,9 @@ export class App {
249
294
  async quit() {
250
295
  const watching = this.run?.following === true;
251
296
  const ok = await confirmDialog({
252
- title: 'Quit the deploy UI?',
253
- lines: [
254
- 'The local server stops and this page stops working.',
255
- watching ? 'A run is being watched: detached runs go on on the server (re-attach later); an attended one stops at its next step.' : 'Detached runs go on on the server.',
256
- ],
257
- confirmLabel: 'Quit',
297
+ title: t('quit.title'),
298
+ lines: [t('quit.line'), watching ? t('quit.watching') : t('quit.detached')],
299
+ confirmLabel: t('quit'),
258
300
  });
259
301
  if (!ok)
260
302
  return;
@@ -267,7 +309,7 @@ export class App {
267
309
  }
268
310
  this.stopped = true;
269
311
  forgetSession(this.storage);
270
- replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', 'Stopped', 'The deploy UI has stopped. You can close this tab.', 'Detached runs go on on the server; start basalt-deploy-ui again to re-attach.')));
312
+ replace(this.shell.main, el('div', { class: 'stopped' }, emptyState('power', t('quit.stopped'), t('quit.stoppedText'), t('quit.stoppedHelp'))));
271
313
  replace(this.shell.nav);
272
314
  this.clearBar();
273
315
  replace(this.shell.session);
@@ -1,4 +1,5 @@
1
1
  import { append, button, el } from './dom.js';
2
+ import { t } from './i18n.js';
2
3
  import { icon } from './icons.js';
3
4
  /**
4
5
  * A native modal `<dialog>` (focus stays inside, Escape cancels). Resolves
@@ -18,7 +19,7 @@ export function confirmDialog(options, host = document.body) {
18
19
  resolve(answer);
19
20
  };
20
21
  const confirm = button(options.confirmLabel, () => finish(true), { class: options.danger ? 'danger' : 'primary' });
21
- const dismiss = button('Cancel', () => finish(false));
22
+ const dismiss = button(t('cancel'), () => finish(false));
22
23
  let typed;
23
24
  if (options.typeToConfirm !== undefined) {
24
25
  const expected = options.typeToConfirm;
@@ -28,7 +29,7 @@ export function confirmDialog(options, host = document.body) {
28
29
  on: { input: () => { confirm.disabled = typed.value !== expected; } },
29
30
  });
30
31
  }
31
- append(dialog, el('div', { class: 'dialog-head' }, el('span', { class: 'dialog-icon' }, icon(options.danger ? 'alert' : 'info')), el('h2', { id: 'confirm-title' }, options.title)), el('div', { class: 'dialog-body' }, options.context ? el('p', { class: 'dialog-context' }, options.context) : null, options.typeToConfirm !== undefined ? el('p', { class: 'dialog-protected' }, el('span', { class: 'badge badge-protected' }, icon('shield'), 'PROTECTED'), ' This target is protected.') : null, ...options.lines.filter((line) => line !== null && line !== undefined && line !== false && line !== '').map((line) => el('p', {}, line)), typed ? el('div', { class: 'field' }, el('label', { for: 'confirm-name' }, 'Type ', el('code', {}, options.typeToConfirm), ' to confirm'), typed) : null), el('div', { class: 'dialog-actions' }, dismiss, confirm));
32
+ append(dialog, el('div', { class: 'dialog-head' }, el('span', { class: 'dialog-icon' }, icon(options.danger ? 'alert' : 'info')), el('h2', { id: 'confirm-title' }, options.title)), el('div', { class: 'dialog-body' }, options.context ? el('p', { class: 'dialog-context' }, options.context) : null, options.typeToConfirm !== undefined ? el('p', { class: 'dialog-protected' }, el('span', { class: 'badge badge-protected' }, icon('shield'), t('protected.badge')), t('dialog.protected')) : null, ...options.lines.filter((line) => line !== null && line !== undefined && line !== false && line !== '').map((line) => el('p', {}, line)), typed ? el('div', { class: 'field' }, el('label', { for: 'confirm-name' }, t('dialog.typeTo'), el('code', {}, options.typeToConfirm), t('dialog.toConfirm')), typed) : null), el('div', { class: 'dialog-actions' }, dismiss, confirm));
32
33
  dialog.addEventListener('cancel', (event) => { event.preventDefault(); finish(false); });
33
34
  dialog.addEventListener('keydown', (event) => { if (event.key === 'Escape')
34
35
  finish(false); });
@@ -0,0 +1,14 @@
1
+ /** Identity helper that pins a catalog's key/value types for `t()`. */
2
+ export function defineMessages(messages) {
3
+ return messages;
4
+ }
5
+ const interpolate = (template, params) => template.replace(/\{(\w+)\}/g, (_match, key) => params[key] === undefined ? `{${key}}` : String(params[key]));
6
+ /** Resolves a message value to a string for a locale, applying plurals + params. */
7
+ export function render(value, locale, params) {
8
+ if (typeof value === 'string')
9
+ return interpolate(value, params);
10
+ const count = Number(params['count'] ?? 0);
11
+ const category = new Intl.PluralRules(locale).select(count);
12
+ const template = value[category] ?? value.other ?? '';
13
+ return interpolate(template, params);
14
+ }