@kiosko/sdk 0.0.0-stage → 0.2.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/dist/context.d.ts +20 -0
- package/dist/context.js +24 -0
- package/dist/filter.d.ts +26 -0
- package/dist/filter.js +53 -0
- package/dist/format.d.ts +59 -0
- package/dist/format.js +87 -0
- package/dist/i18n.d.ts +9 -0
- package/dist/i18n.js +8 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/list.d.ts +133 -0
- package/dist/list.js +126 -0
- package/dist/location.d.ts +26 -0
- package/dist/location.js +33 -0
- package/dist/plugin.d.ts +419 -0
- package/dist/plugin.js +24 -0
- package/dist/schema.d.ts +155 -0
- package/dist/schema.js +169 -0
- package/dist/system.d.ts +35 -0
- package/dist/system.js +80 -0
- package/dist/theme.d.ts +47 -0
- package/dist/theme.js +49 -0
- package/dist/workday.d.ts +48 -0
- package/dist/workday.js +40 -0
- package/package.json +47 -4
- package/src/context.ts +26 -0
- package/src/filter.ts +56 -0
- package/src/format.ts +123 -0
- package/src/i18n.ts +13 -0
- package/src/index.ts +11 -0
- package/src/list.ts +217 -0
- package/src/location.ts +41 -0
- package/src/plugin.ts +429 -0
- package/src/schema.ts +264 -0
- package/src/system.ts +87 -0
- package/src/theme.ts +98 -0
- package/src/workday.ts +79 -0
- package/README.md +0 -3
package/dist/schema.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tiny, typed schema builder for plugin options.
|
|
3
|
+
*
|
|
4
|
+
* One declaration gives three things at once:
|
|
5
|
+
* - the TypeScript type of the options (`Infer<typeof schema>`),
|
|
6
|
+
* - the defaults and the validation the runtime applies to user config,
|
|
7
|
+
* - the form the editor renders (labels, hints, sliders, colour pickers…).
|
|
8
|
+
*
|
|
9
|
+
* `toJsonSchema` exports a standard JSON Schema (draft 2020-12) with `x-ui`
|
|
10
|
+
* hints, so tools that don't speak TypeScript can still validate a config.
|
|
11
|
+
*/
|
|
12
|
+
import { localize } from "./i18n.js";
|
|
13
|
+
export const t = {
|
|
14
|
+
string: (o = {}) => ({ kind: 'string', default: '', ...o }),
|
|
15
|
+
number: (o = {}) => ({ kind: 'number', default: o.min ?? 0, ...o }),
|
|
16
|
+
integer: (o = {}) => ({ kind: 'number', integer: true, default: o.min ?? 0, ...o }),
|
|
17
|
+
boolean: (o = {}) => ({ kind: 'boolean', default: false, ...o }),
|
|
18
|
+
enum: (values, o = {}) => ({ kind: 'enum', values, default: values[0], ...o }),
|
|
19
|
+
color: (o = {}) => ({ kind: 'color', default: '#ffffff', ...o }),
|
|
20
|
+
list: (item, o = {}) => ({ kind: 'list', item, default: [], ...o }),
|
|
21
|
+
object: (props, o = {}) => ({ kind: 'object', props, ...o, default: Object.fromEntries(Object.entries(props).map(([k, f]) => [k, clone(f.default)])) }),
|
|
22
|
+
map: (o = {}) => ({ kind: 'map', default: {}, ...o }),
|
|
23
|
+
/** Duration in milliseconds, edited in seconds or minutes by the editor. */
|
|
24
|
+
duration: (o = {}) => ({ kind: 'number', integer: true, unit: 'ms', min: 0, default: 60_000, ...o }),
|
|
25
|
+
/**
|
|
26
|
+
* Which items to show, as patterns for `matchFilter`: `["web", "acme/*",
|
|
27
|
+
* "!*-old"]`. Empty = all. Name it after what it filters (`projects`,
|
|
28
|
+
* `repos`, `calendars`, `people`) and give it a `label`; the `help`
|
|
29
|
+
* explaining the patterns comes with it.
|
|
30
|
+
*/
|
|
31
|
+
filter: (o = {}) => ({
|
|
32
|
+
kind: 'list',
|
|
33
|
+
item: { kind: 'string', default: '', placeholder: 'name, group/*, !old-*' },
|
|
34
|
+
default: [],
|
|
35
|
+
filter: true,
|
|
36
|
+
help: FILTER_HELP,
|
|
37
|
+
...o,
|
|
38
|
+
}),
|
|
39
|
+
};
|
|
40
|
+
/** The help every `t.filter()` field carries unless it brings its own. */
|
|
41
|
+
export const FILTER_HELP = {
|
|
42
|
+
en: 'Empty: all. Names or patterns, comma-separated: * matches anything, a leading ! leaves out (web, acme/*, !*-old).',
|
|
43
|
+
es: 'Vacío: todos. Nombres o patrones separados por comas: * vale por cualquier cosa y un ! delante excluye (web, acme/*, !*-old).',
|
|
44
|
+
};
|
|
45
|
+
function clone(v) {
|
|
46
|
+
return v === undefined ? v : JSON.parse(JSON.stringify(v));
|
|
47
|
+
}
|
|
48
|
+
/** The defaults of a field, deep-copied (safe to mutate). */
|
|
49
|
+
export function defaultsOf(f) {
|
|
50
|
+
return clone(f.default);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Coerces `value` to the shape of `f`: unknown keys are kept (so a newer
|
|
54
|
+
* config survives an older plugin), wrong types fall back to the default
|
|
55
|
+
* and are reported. Never throws.
|
|
56
|
+
*/
|
|
57
|
+
export function validate(f, value, path = '') {
|
|
58
|
+
const issues = [];
|
|
59
|
+
const out = coerce(f, value, path, issues);
|
|
60
|
+
return { value: out, issues };
|
|
61
|
+
}
|
|
62
|
+
function coerce(f, v, path, issues) {
|
|
63
|
+
const bad = (msg) => {
|
|
64
|
+
issues.push({ path: path || '(root)', message: msg });
|
|
65
|
+
return clone(f.default);
|
|
66
|
+
};
|
|
67
|
+
if (v === undefined || v === null)
|
|
68
|
+
return clone(f.default);
|
|
69
|
+
switch (f.kind) {
|
|
70
|
+
case 'string':
|
|
71
|
+
if (typeof v === 'number')
|
|
72
|
+
return String(v);
|
|
73
|
+
if (typeof v !== 'string')
|
|
74
|
+
return bad('expected text');
|
|
75
|
+
return f.maxLength && v.length > f.maxLength ? v.slice(0, f.maxLength) : v;
|
|
76
|
+
case 'color':
|
|
77
|
+
if (typeof v !== 'string')
|
|
78
|
+
return bad('expected a colour');
|
|
79
|
+
return v;
|
|
80
|
+
case 'number': {
|
|
81
|
+
const n = typeof v === 'string' && v.trim() !== '' ? Number(v) : v;
|
|
82
|
+
if (typeof n !== 'number' || !Number.isFinite(n))
|
|
83
|
+
return bad('expected a number');
|
|
84
|
+
let x = f.integer ? Math.round(n) : n;
|
|
85
|
+
if (f.min !== undefined && x < f.min)
|
|
86
|
+
x = f.min;
|
|
87
|
+
if (f.max !== undefined && x > f.max)
|
|
88
|
+
x = f.max;
|
|
89
|
+
return x;
|
|
90
|
+
}
|
|
91
|
+
case 'boolean':
|
|
92
|
+
if (typeof v === 'boolean')
|
|
93
|
+
return v;
|
|
94
|
+
if (v === 'true' || v === 'false')
|
|
95
|
+
return v === 'true';
|
|
96
|
+
return bad('expected true/false');
|
|
97
|
+
case 'enum':
|
|
98
|
+
if (typeof v === 'string' && f.values.includes(v))
|
|
99
|
+
return v;
|
|
100
|
+
return bad(`expected one of ${f.values.join(', ')}`);
|
|
101
|
+
case 'list': {
|
|
102
|
+
if (!Array.isArray(v))
|
|
103
|
+
return bad('expected a list');
|
|
104
|
+
const items = v.map((item, i) => coerce(f.item, item, path ? `${path}.${i}` : String(i), issues));
|
|
105
|
+
return f.max !== undefined ? items.slice(0, f.max) : items;
|
|
106
|
+
}
|
|
107
|
+
case 'map': {
|
|
108
|
+
if (typeof v !== 'object' || Array.isArray(v))
|
|
109
|
+
return bad('expected key/value pairs');
|
|
110
|
+
return Object.fromEntries(Object.entries(v).filter(([, x]) => typeof x === 'string' || typeof x === 'number').map(([k, x]) => [k, String(x)]));
|
|
111
|
+
}
|
|
112
|
+
case 'object': {
|
|
113
|
+
if (typeof v !== 'object' || Array.isArray(v))
|
|
114
|
+
return bad('expected an object');
|
|
115
|
+
const src = v;
|
|
116
|
+
const out = { ...src };
|
|
117
|
+
for (const [k, sub] of Object.entries(f.props))
|
|
118
|
+
out[k] = coerce(sub, src[k], path ? `${path}.${k}` : k, issues);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Appended to the description of secret fields in the JSON Schema. */
|
|
124
|
+
export const SECRET_NOTE = 'Secret: leave it empty (or a placeholder such as "<token>"). Kiosko keeps the value it already has, or asks for it in the editor.';
|
|
125
|
+
/**
|
|
126
|
+
* Standard JSON Schema (draft 2020-12) with `x-ui` hints, for tools outside
|
|
127
|
+
* TypeScript: `title`/`description` in English, `x-secret` on secrets and
|
|
128
|
+
* `x-risk` (see `Risk`) on values that run programs, contact hosts or read
|
|
129
|
+
* paths. URLs carry no `format` keyword: an empty value or a placeholder is
|
|
130
|
+
* valid (a secret URL is filled in later), and editors would flag both.
|
|
131
|
+
*/
|
|
132
|
+
export function toJsonSchema(f) {
|
|
133
|
+
const ui = { label: f.label, help: f.help, advanced: f.advanced };
|
|
134
|
+
const secret = f.kind === 'string' && !!f.secret;
|
|
135
|
+
const help = f.help === undefined ? undefined : localize(f.help, 'en');
|
|
136
|
+
const description = secret ? [help, SECRET_NOTE].filter(Boolean).join(' ') : help;
|
|
137
|
+
const base = {
|
|
138
|
+
...(f.label === undefined ? {} : { title: localize(f.label, 'en') }),
|
|
139
|
+
...(description ? { description } : {}),
|
|
140
|
+
default: f.default,
|
|
141
|
+
'x-ui': ui,
|
|
142
|
+
...(secret ? { 'x-secret': true } : {}),
|
|
143
|
+
...(f.risk === undefined ? {} : { 'x-risk': typeof f.risk === 'string' ? f.risk : [...f.risk] }),
|
|
144
|
+
};
|
|
145
|
+
switch (f.kind) {
|
|
146
|
+
case 'string':
|
|
147
|
+
return { ...base, type: 'string', ...(f.format === 'date' ? { format: 'date' } : {}), ...(f.maxLength ? { maxLength: f.maxLength } : {}), 'x-ui': { ...ui, secret: f.secret, multiline: f.multiline, placeholder: f.placeholder, format: f.format } };
|
|
148
|
+
case 'color':
|
|
149
|
+
return { ...base, type: 'string', 'x-ui': { ...ui, widget: 'color' } };
|
|
150
|
+
case 'number':
|
|
151
|
+
return { ...base, type: f.integer ? 'integer' : 'number', minimum: f.min, maximum: f.max, 'x-ui': { ...ui, unit: f.unit, step: f.step, slider: f.slider } };
|
|
152
|
+
case 'boolean':
|
|
153
|
+
return { ...base, type: 'boolean' };
|
|
154
|
+
case 'enum':
|
|
155
|
+
return { ...base, type: 'string', enum: [...f.values], 'x-ui': { ...ui, labels: f.labels } };
|
|
156
|
+
case 'list':
|
|
157
|
+
return { ...base, type: 'array', items: toJsonSchema(f.item), maxItems: f.max, ...(f.filter ? { 'x-ui': { ...ui, widget: 'filter' }, 'x-filter': true } : {}) };
|
|
158
|
+
case 'map':
|
|
159
|
+
return { ...base, type: 'object', additionalProperties: { type: 'string' } };
|
|
160
|
+
case 'object':
|
|
161
|
+
return { ...base, type: 'object', properties: Object.fromEntries(Object.entries(f.props).map(([k, sub]) => [k, toJsonSchema(sub)])) };
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/** The risks of a field, as a list (empty when it has none). */
|
|
165
|
+
export function risksOf(f) {
|
|
166
|
+
const r = 'risk' in f ? f.risk : f['x-risk'];
|
|
167
|
+
const list = typeof r === 'string' ? [r] : Array.isArray(r) ? r : [];
|
|
168
|
+
return list.filter((x) => x === 'exec' || x === 'network' || x === 'path');
|
|
169
|
+
}
|
package/dist/system.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `~`, `~/x` (and `~\x`), `$VAR`, `${VAR}` and, on Windows, `%VAR%` expanded.
|
|
3
|
+
* A variable that isn't set stays as written. `os` and `env` are for tests.
|
|
4
|
+
*/
|
|
5
|
+
export declare function expandPath(p: string, o?: {
|
|
6
|
+
os?: NodeJS.Platform;
|
|
7
|
+
env?: Record<string, string | undefined>;
|
|
8
|
+
home?: string;
|
|
9
|
+
}): string;
|
|
10
|
+
/** What to hand to `execFile` to run `cmd` with `args`. */
|
|
11
|
+
export interface Command {
|
|
12
|
+
file: string;
|
|
13
|
+
args: string[];
|
|
14
|
+
/** Pass as `windowsVerbatimArguments` to `execFile`/`spawn`. */
|
|
15
|
+
windowsVerbatimArguments?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* On Windows, tools installed with npm (`ccusage`, `npx`, `claude`…) are
|
|
19
|
+
* `.cmd` shims, and `execFile('ccusage')` neither finds them (no `PATHEXT`
|
|
20
|
+
* search) nor, since Node 18.20 / 20.12, runs a `.cmd` without a shell.
|
|
21
|
+
* `commandFor` returns the pair to pass to `execFile`: for a `.cmd`/`.bat`
|
|
22
|
+
* (looked up on `PATH`), `cmd.exe /d /s /c "…"` with each argument quoted; for
|
|
23
|
+
* anything else, the command as it was. Elsewhere it changes nothing.
|
|
24
|
+
*
|
|
25
|
+
* const c = commandFor('ccusage', ['daily', '--json']);
|
|
26
|
+
* execFile(c.file, c.args, { windowsHide: true, windowsVerbatimArguments: c.windowsVerbatimArguments }, cb);
|
|
27
|
+
*
|
|
28
|
+
* Throws if an argument holds a quote, a `%` or a line break and the command needs
|
|
29
|
+
* cmd.exe: there is no safe way to pass those through a batch file.
|
|
30
|
+
*/
|
|
31
|
+
export declare function commandFor(cmd: string, args: string[], o?: {
|
|
32
|
+
os?: NodeJS.Platform;
|
|
33
|
+
env?: Record<string, string | undefined>;
|
|
34
|
+
exists?: (path: string) => boolean;
|
|
35
|
+
}): Command;
|
package/dist/system.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small helpers for what differs between operating systems, shared by Kiosko
|
|
3
|
+
* and by plugins (`import { expandPath, commandFor } from '@kiosko/sdk'`).
|
|
4
|
+
*/
|
|
5
|
+
import { statSync } from 'node:fs';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { join, win32 } from 'node:path';
|
|
8
|
+
/**
|
|
9
|
+
* `~`, `~/x` (and `~\x`), `$VAR`, `${VAR}` and, on Windows, `%VAR%` expanded.
|
|
10
|
+
* A variable that isn't set stays as written. `os` and `env` are for tests.
|
|
11
|
+
*/
|
|
12
|
+
export function expandPath(p, o = {}) {
|
|
13
|
+
const env = o.env ?? process.env;
|
|
14
|
+
let v = p.replace(/\$(?:\{(\w+)\}|(\w+))/g, (whole, a, b) => env[a ?? b ?? ''] ?? whole);
|
|
15
|
+
// `%VAR%` is a Windows thing; on other systems a `%` is just a character.
|
|
16
|
+
if ((o.os ?? process.platform) === 'win32')
|
|
17
|
+
v = v.replace(/%([^%\r\n]+)%/g, (whole, name) => env[name] ?? whole);
|
|
18
|
+
const home = o.home ?? homedir();
|
|
19
|
+
if (v === '~')
|
|
20
|
+
return home;
|
|
21
|
+
return v.startsWith('~/') || v.startsWith('~\\') ? join(home, v.slice(2)) : v;
|
|
22
|
+
}
|
|
23
|
+
/** Characters cmd.exe would interpret inside a batch shim's arguments. */
|
|
24
|
+
const CMD_UNSAFE = /["%\r\n\0]/;
|
|
25
|
+
/**
|
|
26
|
+
* On Windows, tools installed with npm (`ccusage`, `npx`, `claude`…) are
|
|
27
|
+
* `.cmd` shims, and `execFile('ccusage')` neither finds them (no `PATHEXT`
|
|
28
|
+
* search) nor, since Node 18.20 / 20.12, runs a `.cmd` without a shell.
|
|
29
|
+
* `commandFor` returns the pair to pass to `execFile`: for a `.cmd`/`.bat`
|
|
30
|
+
* (looked up on `PATH`), `cmd.exe /d /s /c "…"` with each argument quoted; for
|
|
31
|
+
* anything else, the command as it was. Elsewhere it changes nothing.
|
|
32
|
+
*
|
|
33
|
+
* const c = commandFor('ccusage', ['daily', '--json']);
|
|
34
|
+
* execFile(c.file, c.args, { windowsHide: true, windowsVerbatimArguments: c.windowsVerbatimArguments }, cb);
|
|
35
|
+
*
|
|
36
|
+
* Throws if an argument holds a quote, a `%` or a line break and the command needs
|
|
37
|
+
* cmd.exe: there is no safe way to pass those through a batch file.
|
|
38
|
+
*/
|
|
39
|
+
export function commandFor(cmd, args, o = {}) {
|
|
40
|
+
if ((o.os ?? process.platform) !== 'win32')
|
|
41
|
+
return { file: cmd, args };
|
|
42
|
+
const env = o.env ?? process.env;
|
|
43
|
+
const exists = o.exists ?? defaultExists;
|
|
44
|
+
const shim = findShim(cmd, env, exists);
|
|
45
|
+
if (!shim)
|
|
46
|
+
return { file: cmd, args };
|
|
47
|
+
for (const a of args)
|
|
48
|
+
if (CMD_UNSAFE.test(a))
|
|
49
|
+
throw new Error(`argument not allowed for ${cmd}: ${JSON.stringify(a)}`);
|
|
50
|
+
const quote = (s) => (/^[\w@+=:,./\\-]+$/.test(s) ? s : `"${s}"`);
|
|
51
|
+
return { file: env.ComSpec || 'cmd.exe', args: ['/d', '/s', '/c', `"${[shim, ...args].map(quote).join(' ')}"`], windowsVerbatimArguments: true };
|
|
52
|
+
}
|
|
53
|
+
function defaultExists(path) {
|
|
54
|
+
try {
|
|
55
|
+
// Sync and tiny: a handful of PATH entries, once per call.
|
|
56
|
+
return statSync(path).isFile();
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** The `.cmd`/`.bat` that `cmd` names on Windows, or null if it is (or may be) an executable. */
|
|
63
|
+
function findShim(cmd, env, exists) {
|
|
64
|
+
if (/\.(exe|com)$/i.test(cmd))
|
|
65
|
+
return null;
|
|
66
|
+
if (/\.(cmd|bat)$/i.test(cmd) && /[\\/]/.test(cmd))
|
|
67
|
+
return exists(cmd) ? cmd : null;
|
|
68
|
+
const dirs = /[\\/]/.test(cmd) ? [''] : (env.PATH ?? env.Path ?? '').split(';').filter(Boolean);
|
|
69
|
+
const exts = (env.PATHEXT ?? '.COM;.EXE;.BAT;.CMD').split(';').filter(Boolean);
|
|
70
|
+
for (const dir of dirs) {
|
|
71
|
+
const base = dir ? win32.join(dir, cmd) : cmd;
|
|
72
|
+
for (const ext of /\.\w+$/.test(cmd) ? [''] : exts) {
|
|
73
|
+
const p = base + ext.toLowerCase();
|
|
74
|
+
if (!exists(p))
|
|
75
|
+
continue;
|
|
76
|
+
return /\.(cmd|bat)$/i.test(p) ? p : null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
package/dist/theme.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Themes are design tokens, not CSS: every widget paints with `ctx.theme`, so
|
|
3
|
+
* switching the theme restyles the whole panel without touching a plugin.
|
|
4
|
+
*
|
|
5
|
+
* The default (`midnight`) is the art direction of the original aimonitor:
|
|
6
|
+
* the normal state is darkness, and colour is spent only on what claims you.
|
|
7
|
+
*/
|
|
8
|
+
export interface ThemeColors {
|
|
9
|
+
/** Panel background. Kept near black: it's the backlight that wears out. */
|
|
10
|
+
bg: string;
|
|
11
|
+
/** Card background and its 1 px edge. */
|
|
12
|
+
surface: string;
|
|
13
|
+
surfaceEdge: string;
|
|
14
|
+
/** Text: primary, secondary and hints. */
|
|
15
|
+
ink: string;
|
|
16
|
+
inkDim: string;
|
|
17
|
+
inkFaint: string;
|
|
18
|
+
/** Brand/selection colour. */
|
|
19
|
+
accent: string;
|
|
20
|
+
/** Semaphore shared by every widget: needs you now / soon / informative / fine. */
|
|
21
|
+
alert: string;
|
|
22
|
+
warn: string;
|
|
23
|
+
info: string;
|
|
24
|
+
ok: string;
|
|
25
|
+
}
|
|
26
|
+
export interface ThemeFonts {
|
|
27
|
+
/** Condensed sans: titles, numbers. */
|
|
28
|
+
sans: string;
|
|
29
|
+
sansBold: string;
|
|
30
|
+
mono: string;
|
|
31
|
+
monoBold: string;
|
|
32
|
+
}
|
|
33
|
+
export interface Theme {
|
|
34
|
+
id: string;
|
|
35
|
+
name: string;
|
|
36
|
+
colors: ThemeColors;
|
|
37
|
+
fonts: ThemeFonts;
|
|
38
|
+
/** Card corner radius in px. */
|
|
39
|
+
radius: number;
|
|
40
|
+
/** Card title size in px (scaled for small panels by the compositor). */
|
|
41
|
+
titleSize: number;
|
|
42
|
+
}
|
|
43
|
+
/** Font families registered by the runtime (see core/fonts.ts). */
|
|
44
|
+
export declare const FONT_FAMILIES: ThemeFonts;
|
|
45
|
+
export declare const THEMES: Theme[];
|
|
46
|
+
export declare const DEFAULT_THEME: Theme;
|
|
47
|
+
export declare function themeById(id: string | undefined): Theme;
|
package/dist/theme.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Font families registered by the runtime (see core/fonts.ts). */
|
|
2
|
+
export const FONT_FAMILIES = {
|
|
3
|
+
sans: 'KioskoSans',
|
|
4
|
+
sansBold: 'KioskoSansBold',
|
|
5
|
+
mono: 'KioskoMono',
|
|
6
|
+
monoBold: 'KioskoMonoBold',
|
|
7
|
+
};
|
|
8
|
+
const base = { fonts: FONT_FAMILIES, radius: 12, titleSize: 30 };
|
|
9
|
+
export const THEMES = [
|
|
10
|
+
{
|
|
11
|
+
id: 'midnight',
|
|
12
|
+
name: 'Midnight',
|
|
13
|
+
...base,
|
|
14
|
+
colors: { bg: '#05080D', surface: '#0D131A', surfaceEdge: '#1C2631', ink: '#D3DFEA', inkDim: '#627180', inkFaint: '#384450', accent: '#4DA3FF', alert: '#F5453D', warn: '#FFB020', info: '#4DA3FF', ok: '#3FCB7E' },
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
id: 'graphite',
|
|
18
|
+
name: 'Graphite',
|
|
19
|
+
...base,
|
|
20
|
+
radius: 6,
|
|
21
|
+
colors: { bg: '#111214', surface: '#1A1C1F', surfaceEdge: '#2A2D31', ink: '#ECEDEE', inkDim: '#8B9096', inkFaint: '#4A4F55', accent: '#E8E8E8', alert: '#FF5F57', warn: '#FEBC2E', info: '#64B5F6', ok: '#28C840' },
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
id: 'aurora',
|
|
25
|
+
name: 'Aurora',
|
|
26
|
+
...base,
|
|
27
|
+
radius: 16,
|
|
28
|
+
colors: { bg: '#0B0A1A', surface: '#15132B', surfaceEdge: '#2A2550', ink: '#EDE9FF', inkDim: '#8D86B8', inkFaint: '#4A4475', accent: '#9F7AEA', alert: '#FF4D8D', warn: '#FFC857', info: '#4CC9F0', ok: '#43E5A0' },
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: 'phosphor',
|
|
32
|
+
name: 'Phosphor',
|
|
33
|
+
...base,
|
|
34
|
+
radius: 2,
|
|
35
|
+
fonts: { ...FONT_FAMILIES, sans: FONT_FAMILIES.mono, sansBold: FONT_FAMILIES.monoBold },
|
|
36
|
+
colors: { bg: '#020602', surface: '#061006', surfaceEdge: '#0F2A0F', ink: '#7CFF7C', inkDim: '#2F9E3A', inkFaint: '#165C1E', accent: '#7CFF7C', alert: '#FF5A3C', warn: '#FFD23C', info: '#5CE1E6', ok: '#7CFF7C' },
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: 'paper',
|
|
40
|
+
name: 'Paper',
|
|
41
|
+
...base,
|
|
42
|
+
radius: 10,
|
|
43
|
+
colors: { bg: '#F2EFE8', surface: '#FFFFFF', surfaceEdge: '#DDD8CC', ink: '#1F2328', inkDim: '#6B6F76', inkFaint: '#B4B0A6', accent: '#2F6FEB', alert: '#D92D20', warn: '#C27803', info: '#2F6FEB', ok: '#1E8E3E' },
|
|
44
|
+
},
|
|
45
|
+
];
|
|
46
|
+
export const DEFAULT_THEME = THEMES[0];
|
|
47
|
+
export function themeById(id) {
|
|
48
|
+
return THEMES.find((t) => t.id === id) ?? DEFAULT_THEME;
|
|
49
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The user's working week, set once in Settings and handed to every plugin
|
|
3
|
+
* as `ctx.workday`: which days, from when to when, how many hours are due,
|
|
4
|
+
* days with other hours (a short Friday) and days off. Plugins default to it
|
|
5
|
+
* instead of asking for their own start, end and holidays.
|
|
6
|
+
*/
|
|
7
|
+
/** Other hours on one weekday. Unset fields are the week's. */
|
|
8
|
+
export interface DayHours {
|
|
9
|
+
start?: string;
|
|
10
|
+
end?: string;
|
|
11
|
+
hours?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface Workday {
|
|
14
|
+
/** Working days, 0 = Sunday … 6 = Saturday. */
|
|
15
|
+
days: number[];
|
|
16
|
+
/** `HH:MM`, 24 h. */
|
|
17
|
+
start: string;
|
|
18
|
+
end: string;
|
|
19
|
+
/** Hours due on a working day (what a timesheet expects), not the span from start to end. */
|
|
20
|
+
hours: number;
|
|
21
|
+
/** Days off, `YYYY-MM-DD`: no work hours, nothing due. */
|
|
22
|
+
holidays: string[];
|
|
23
|
+
/** Days with other hours, by weekday (`"5"` = Friday): `{ "5": { "end": "15:00", "hours": 6 } }`. */
|
|
24
|
+
byDay: Partial<Record<string, DayHours>>;
|
|
25
|
+
}
|
|
26
|
+
export declare const DEFAULT_WORKDAY: Workday;
|
|
27
|
+
/** `"09:30"` → 570. Anything unreadable is 0. */
|
|
28
|
+
export declare function minutesOf(hhmm: string | undefined): number;
|
|
29
|
+
/** `YYYY-MM-DD` of the local day `at` falls in. */
|
|
30
|
+
export declare function dayKey(at: number | Date): string;
|
|
31
|
+
export interface DaySchedule {
|
|
32
|
+
/** A working day that is not a holiday. */
|
|
33
|
+
working: boolean;
|
|
34
|
+
holiday: boolean;
|
|
35
|
+
start: string;
|
|
36
|
+
end: string;
|
|
37
|
+
/** Minutes from local midnight. */
|
|
38
|
+
startMin: number;
|
|
39
|
+
endMin: number;
|
|
40
|
+
/** Hours due that day: 0 on days off. */
|
|
41
|
+
hours: number;
|
|
42
|
+
}
|
|
43
|
+
/** The schedule of the local day `at` falls in. */
|
|
44
|
+
export declare function scheduleOn(w: Workday, at: number | Date): DaySchedule;
|
|
45
|
+
/** Inside work hours at `at`: a working day, not a holiday, between its start and end. */
|
|
46
|
+
export declare function isWorkingAt(w: Workday, at: number | Date): boolean;
|
|
47
|
+
/** Hours due in a normal week (no holidays): the sum over the working days. */
|
|
48
|
+
export declare function weeklyHours(w: Workday): number;
|
package/dist/workday.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The user's working week, set once in Settings and handed to every plugin
|
|
3
|
+
* as `ctx.workday`: which days, from when to when, how many hours are due,
|
|
4
|
+
* days with other hours (a short Friday) and days off. Plugins default to it
|
|
5
|
+
* instead of asking for their own start, end and holidays.
|
|
6
|
+
*/
|
|
7
|
+
export const DEFAULT_WORKDAY = { days: [1, 2, 3, 4, 5], start: '09:00', end: '17:00', hours: 8, holidays: [], byDay: {} };
|
|
8
|
+
/** `"09:30"` → 570. Anything unreadable is 0. */
|
|
9
|
+
export function minutesOf(hhmm) {
|
|
10
|
+
const m = /^(\d{1,2}):(\d{2})/.exec(hhmm ?? '');
|
|
11
|
+
return m ? Number(m[1]) * 60 + Number(m[2]) : 0;
|
|
12
|
+
}
|
|
13
|
+
/** `YYYY-MM-DD` of the local day `at` falls in. */
|
|
14
|
+
export function dayKey(at) {
|
|
15
|
+
const d = new Date(at);
|
|
16
|
+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
|
|
17
|
+
}
|
|
18
|
+
/** The schedule of the local day `at` falls in. */
|
|
19
|
+
export function scheduleOn(w, at) {
|
|
20
|
+
const day = new Date(at).getDay();
|
|
21
|
+
const own = w.byDay?.[String(day)] ?? {};
|
|
22
|
+
const start = own.start || w.start;
|
|
23
|
+
const end = own.end || w.end;
|
|
24
|
+
const holiday = (w.holidays ?? []).includes(dayKey(at));
|
|
25
|
+
const working = w.days.includes(day) && !holiday;
|
|
26
|
+
return { working, holiday, start, end, startMin: minutesOf(start), endMin: minutesOf(end), hours: working ? (own.hours ?? w.hours ?? 0) : 0 };
|
|
27
|
+
}
|
|
28
|
+
/** Inside work hours at `at`: a working day, not a holiday, between its start and end. */
|
|
29
|
+
export function isWorkingAt(w, at) {
|
|
30
|
+
const s = scheduleOn(w, at);
|
|
31
|
+
if (!s.working)
|
|
32
|
+
return false;
|
|
33
|
+
const d = new Date(at);
|
|
34
|
+
const mins = d.getHours() * 60 + d.getMinutes();
|
|
35
|
+
return mins >= s.startMin && mins < s.endMin;
|
|
36
|
+
}
|
|
37
|
+
/** Hours due in a normal week (no holidays): the sum over the working days. */
|
|
38
|
+
export function weeklyHours(w) {
|
|
39
|
+
return w.days.reduce((sum, d) => sum + (w.byDay?.[String(d)]?.hours ?? w.hours ?? 0), 0);
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kiosko/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Plugin SDK for Kiosko: define widgets, data sources and interludes for small always-on LCD panels.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"source": "./src/index.ts",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./workday": {
|
|
14
|
+
"source": "./src/workday.ts",
|
|
15
|
+
"types": "./dist/workday.d.ts",
|
|
16
|
+
"default": "./dist/workday.js"
|
|
17
|
+
},
|
|
18
|
+
"./format": {
|
|
19
|
+
"source": "./src/format.ts",
|
|
20
|
+
"types": "./dist/format.d.ts",
|
|
21
|
+
"default": "./dist/format.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"src",
|
|
27
|
+
"!src/**/*.test.ts",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=22"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc -p tsconfig.json",
|
|
36
|
+
"check": "tsc -p tsconfig.json --noEmit",
|
|
37
|
+
"prepublishOnly": "npm run build"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@napi-rs/canvas": ">=0.1.65 <2",
|
|
41
|
+
"@loksly/lcd-canvas-kit": "*"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/kiosko-lcd/kiosko.git",
|
|
46
|
+
"directory": "packages/sdk"
|
|
47
|
+
},
|
|
48
|
+
"author": "Loksly"
|
|
49
|
+
}
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of a plugin context that don't depend on the host: language,
|
|
3
|
+
* texts, formatters, place and working week. Kiosko builds every context on
|
|
4
|
+
* top of this; plugin tests can build theirs with it instead of by hand:
|
|
5
|
+
*
|
|
6
|
+
* const ctx = { ...createBaseContext({ now, locale: 'es' }), options, signal, intervalMs: 60_000, timeoutMs: 20_000 };
|
|
7
|
+
*/
|
|
8
|
+
import { createFormatters, type TimeFormat } from './format.ts';
|
|
9
|
+
import { localize, type Localized } from './i18n.ts';
|
|
10
|
+
import type { BaseContext, Location, Logger } from './plugin.ts';
|
|
11
|
+
import { DEFAULT_WORKDAY, type Workday } from './workday.ts';
|
|
12
|
+
|
|
13
|
+
export function createBaseContext(o: { now: number; locale?: string; timeFormat?: TimeFormat; workday?: Workday; location?: Location; log?: Logger; dataDir?: string; cacheDir?: string }): BaseContext {
|
|
14
|
+
const locale = o.locale ?? 'en';
|
|
15
|
+
return {
|
|
16
|
+
now: o.now,
|
|
17
|
+
locale,
|
|
18
|
+
t: (text: Localized) => localize(text, locale),
|
|
19
|
+
fmt: createFormatters({ locale, timeFormat: o.timeFormat ?? 'auto', now: o.now }),
|
|
20
|
+
log: o.log ?? (() => undefined),
|
|
21
|
+
dataDir: o.dataDir ?? '',
|
|
22
|
+
cacheDir: o.cacheDir ?? o.dataDir ?? '',
|
|
23
|
+
...(o.location ? { location: o.location } : {}),
|
|
24
|
+
workday: o.workday ?? DEFAULT_WORKDAY,
|
|
25
|
+
};
|
|
26
|
+
}
|
package/src/filter.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Item filters, the same in every widget that lists projects, repos,
|
|
3
|
+
* calendars, servers or people: a list of patterns, edited with `t.filter()`
|
|
4
|
+
* and applied with `matchFilter`.
|
|
5
|
+
*
|
|
6
|
+
* [] everything
|
|
7
|
+
* ["web", "api"] only these (by full name or by its last part)
|
|
8
|
+
* ["acme/*"] everything under acme/ (`*` matches anything, `/` too)
|
|
9
|
+
* ["!legacy-*"] everything except what starts with legacy-
|
|
10
|
+
* ["acme/*", "!*-old"] under acme/, but not the *-old ones
|
|
11
|
+
*
|
|
12
|
+
* Case doesn't matter, `?` is one character, and `*text*` is "contains". A
|
|
13
|
+
* value matches a pattern by its full form (`group/sub/project`) or by its
|
|
14
|
+
* last `/` segment (`project`), like `aliases` do.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** A filter: patterns as a list, or one text with commas or line breaks between them. */
|
|
18
|
+
export type Filter = readonly string[] | string;
|
|
19
|
+
|
|
20
|
+
const cache = new Map<string, RegExp>();
|
|
21
|
+
function globRe(pattern: string): RegExp {
|
|
22
|
+
let re = cache.get(pattern);
|
|
23
|
+
if (!re) {
|
|
24
|
+
const body = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.');
|
|
25
|
+
re = new RegExp(`^${body}$`, 'i');
|
|
26
|
+
if (cache.size > 500) cache.clear();
|
|
27
|
+
cache.set(pattern, re);
|
|
28
|
+
}
|
|
29
|
+
return re;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The patterns of a filter, trimmed, without empty ones. */
|
|
33
|
+
export function filterPatterns(filter: Filter | undefined): string[] {
|
|
34
|
+
const list = typeof filter === 'string' ? filter.split(/[,\n]/) : (filter ?? []);
|
|
35
|
+
return list.map((p) => p.trim()).filter((p) => p && p !== '!');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Whether `value` passes `filter`. `value` may be several names of the same
|
|
40
|
+
* item (a repo's full name and its alias): it passes if any of them matches
|
|
41
|
+
* an include pattern and none matches an exclude one. An empty filter (or
|
|
42
|
+
* one with only exclusions) lets everything else through.
|
|
43
|
+
*/
|
|
44
|
+
export function matchFilter(value: string | readonly string[], filter: Filter | undefined): boolean {
|
|
45
|
+
const patterns = filterPatterns(filter);
|
|
46
|
+
if (!patterns.length) return true;
|
|
47
|
+
const names = (typeof value === 'string' ? [value] : [...value]).flatMap((v) => {
|
|
48
|
+
const last = v.split('/').pop() ?? v;
|
|
49
|
+
return last === v ? [v] : [v, last];
|
|
50
|
+
});
|
|
51
|
+
const hit = (p: string) => names.some((n) => globRe(p).test(n));
|
|
52
|
+
const includes = patterns.filter((p) => !p.startsWith('!'));
|
|
53
|
+
const excludes = patterns.filter((p) => p.startsWith('!')).map((p) => p.slice(1).trim());
|
|
54
|
+
if (excludes.some(hit)) return false;
|
|
55
|
+
return !includes.length || includes.some(hit);
|
|
56
|
+
}
|