@hncode/hncode 0.8.6 → 1.0.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 +30 -0
- package/README_CN.md +27 -0
- package/package.json +1 -1
- package/src/agent.js +172 -43
- package/src/bootstrap.js +63 -56
- package/src/ci.js +247 -247
- package/src/colors.js +5 -1
- package/src/commands.js +121 -0
- package/src/composer.js +31 -1
- package/src/config.js +177 -45
- package/src/context.js +29 -0
- package/src/files.js +32 -0
- package/src/fx.js +19 -2
- package/src/i18n.js +180 -131
- package/src/index.js +558 -77
- package/src/permission-gate.js +103 -0
- package/src/plugin.js +221 -2
- package/src/prompt-presets.js +36 -0
- package/src/protocol.js +184 -0
- package/src/registry-browser.js +14 -3
- package/src/remote.js +117 -5
- package/src/schedule.js +148 -11
- package/src/serve.js +613 -0
- package/src/session-client.js +148 -0
- package/src/session-host.js +52 -0
- package/src/session.js +26 -3
- package/src/settings-schema.js +2 -2
- package/src/status-line.js +48 -0
- package/src/toml.js +18 -4
- package/src/tool-status.js +45 -0
- package/src/tools/bash.js +43 -24
- package/src/tools/edit.js +10 -5
- package/src/tools/web-search.js +8 -5
- package/src/tools/write.js +28 -3
- package/src/tui.js +448 -1380
- package/src/ws.js +248 -0
- package/src/session-hub.js +0 -445
- package/src/web-auth.js +0 -98
- package/src/web-daemon-cli.js +0 -63
- package/src/web-daemon-client.js +0 -203
- package/src/web-daemon.js +0 -894
- package/src/web-live.js +0 -250
- package/src/web-public/app.css +0 -1524
- package/src/web-public/app.js +0 -2969
- package/src/web-public/complete.js +0 -154
- package/src/web-public/fonts/JetBrainsMono-Bold.woff2 +0 -0
- package/src/web-public/fonts/JetBrainsMono-Regular.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-100-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-101-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-102-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-103-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-104-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-105-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-106-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-107-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-108-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-109-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-110-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-111-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-112-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-113-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-114-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-115-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-116-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-117-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-118-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-119-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-21-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-22-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-23-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-24-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-25-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-26-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-27-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-28-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-29-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-30-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-31-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-32-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-33-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-34-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-35-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-36-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-37-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-38-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-39-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-4-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-40-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-41-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-42-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-43-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-44-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-45-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-46-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-47-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-48-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-49-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-5-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-50-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-51-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-52-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-53-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-54-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-55-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-56-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-57-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-58-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-59-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-6-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-60-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-61-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-62-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-63-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-64-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-65-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-66-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-67-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-68-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-69-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-70-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-71-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-72-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-73-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-74-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-75-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-76-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-77-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-78-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-79-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-80-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-81-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-82-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-83-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-84-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-85-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-86-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-87-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-88-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-89-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-90-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-91-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-97-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-98-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc-99-400-normal.woff2 +0 -0
- package/src/web-public/fonts/noto-sans-sc.css +0 -775
- package/src/web-public/fonts.css +0 -31
- package/src/web-public/home.css +0 -222
- package/src/web-public/home.html +0 -33
- package/src/web-public/home.js +0 -160
- package/src/web-public/i18n.js +0 -983
- package/src/web-public/index.html +0 -196
- package/src/web-public/login.css +0 -123
- package/src/web-public/login.html +0 -25
- package/src/web-public/login.js +0 -46
- package/src/web-public/settings.js +0 -290
- package/src/web-ws.js +0 -226
- package/src/web.js +0 -430
package/src/remote.js
CHANGED
|
@@ -118,6 +118,30 @@ function rawUrl(repo, ref, file) {
|
|
|
118
118
|
return `https://raw.githubusercontent.com/${repo}/${ref}/${file}`;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
/**
|
|
122
|
+
* One file's text, through the CONTENTS API.
|
|
123
|
+
*
|
|
124
|
+
* `raw.githubusercontent.com` is a separate host, and on some networks it is unreachable while
|
|
125
|
+
* `api.github.com` answers fine — which is the state this was found in: the plugin list loaded and every
|
|
126
|
+
* row came back without a description, because the description needs the file and the file needed the
|
|
127
|
+
* blocked host. The contents API returns the same bytes as base64 from the host that already works.
|
|
128
|
+
*
|
|
129
|
+
* Falls back to the raw URL so a network where the opposite is true still gets its descriptions.
|
|
130
|
+
*/
|
|
131
|
+
async function fetchFileText(repo, ref, file, { timeoutMs = 20000 } = {}) {
|
|
132
|
+
try {
|
|
133
|
+
const r = await ghJson(`https://api.github.com/repos/${repo}/contents/${file}${ref ? `?ref=${encodeURIComponent(ref)}` : ''}`);
|
|
134
|
+
if (r.ok && r.data && typeof r.data.content === 'string') {
|
|
135
|
+
// THE API WRAPS THE BASE64 IN NEWLINES, which `atob` rejects.
|
|
136
|
+
const clean = r.data.content.replace(/\s+/g, '');
|
|
137
|
+
try {
|
|
138
|
+
return { ok: true, text: Buffer.from(clean, 'base64').toString('utf8') };
|
|
139
|
+
} catch { /* fall through to the raw host */ }
|
|
140
|
+
}
|
|
141
|
+
} catch { /* fall through */ }
|
|
142
|
+
return fetchText(rawUrl(repo, ref, file), { timeoutMs });
|
|
143
|
+
}
|
|
144
|
+
|
|
121
145
|
// Each item is a DIRECTORY in the repo (skills/<name>/, plugins/<name>/). A loose file
|
|
122
146
|
// at the top level is ignored: the convention is the contract, and accepting both
|
|
123
147
|
// would make the listing ambiguous about what a name refers to.
|
|
@@ -135,6 +159,82 @@ export async function listRemoteSkills({ repo = DEFAULT_REPO, ref = DEFAULT_REF
|
|
|
135
159
|
return { ok: true, items: dirsOnly(r.data) };
|
|
136
160
|
}
|
|
137
161
|
|
|
162
|
+
/**
|
|
163
|
+
* The one-line summary at the top of a plugin file.
|
|
164
|
+
*
|
|
165
|
+
* A plugin has no front-matter, so the header comment is the only place the author says what it does.
|
|
166
|
+
* The first sentence of the first block comment is taken as-is; a file with no comment returns an empty
|
|
167
|
+
* string rather than a guess.
|
|
168
|
+
*/
|
|
169
|
+
function describePlugin(text) {
|
|
170
|
+
const src = String(text || '');
|
|
171
|
+
// THE FIRST COMMENT, WHICHEVER FORM IT TAKES.
|
|
172
|
+
//
|
|
173
|
+
// // alias plugin — user-defined command aliases ← this repository
|
|
174
|
+
// /** Echo back the given text. */ ← a library-style file
|
|
175
|
+
//
|
|
176
|
+
// Reading only the block form found nothing for most plugins, and a catalogue of blank rows is the
|
|
177
|
+
// same as no catalogue.
|
|
178
|
+
let body = [];
|
|
179
|
+
|
|
180
|
+
const blockMatch = /\/\*\*?([\s\S]*?)\*\//.exec(src);
|
|
181
|
+
const lineMatch = /^(?:\s*\/\/[^\n]*\n?)+/.exec(src);
|
|
182
|
+
// WHICHEVER COMES FIRST is the header; a file may legitimately have a leading `//` run before a
|
|
183
|
+
// block comment, and the first one written is the one that says what the file is.
|
|
184
|
+
const blockAt = blockMatch ? blockMatch.index : -1;
|
|
185
|
+
const lineAt = lineMatch ? lineMatch.index : -1;
|
|
186
|
+
|
|
187
|
+
if (lineAt >= 0 && (blockAt < 0 || lineAt < blockAt)) {
|
|
188
|
+
body = lineMatch[0]
|
|
189
|
+
.split(/\r?\n/)
|
|
190
|
+
.map((l) => l.replace(/^\s*\/\/\s?/, '').trim())
|
|
191
|
+
.filter(Boolean);
|
|
192
|
+
} else if (blockAt >= 0) {
|
|
193
|
+
body = blockMatch[1]
|
|
194
|
+
.split(/\r?\n/)
|
|
195
|
+
.map((l) => l.replace(/^\s*\*?\s?/, '').trim())
|
|
196
|
+
.filter(Boolean);
|
|
197
|
+
}
|
|
198
|
+
if (!body.length) return '';
|
|
199
|
+
|
|
200
|
+
// THE FIRST PARAGRAPH, JOINED: a description wrapped over three lines is still one sentence.
|
|
201
|
+
const para = [];
|
|
202
|
+
for (const line of body) {
|
|
203
|
+
// A blank line ends it, and so does a tag or a heading — those start something else.
|
|
204
|
+
if (!line) break;
|
|
205
|
+
if (/^[@#]/.test(line) || /^[A-Z][A-Za-z ]+:$/.test(line)) break;
|
|
206
|
+
para.push(line);
|
|
207
|
+
}
|
|
208
|
+
const joined = para.join(' ').trim();
|
|
209
|
+
if (!joined) return '';
|
|
210
|
+
// UP TO THE FIRST FULL STOP, so a long header does not overflow a list row.
|
|
211
|
+
const stop = joined.search(/[.!?](\s|$)/);
|
|
212
|
+
return stop > 0 ? joined.slice(0, stop + 1) : joined;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* List the plugins in the repo WITH what each one is for.
|
|
217
|
+
*
|
|
218
|
+
* One request per plugin, the same shape as `listRemoteSkillsDetailed`: a failure leaves that entry
|
|
219
|
+
* without a description instead of dropping it, because a plugin you cannot describe is still a plugin
|
|
220
|
+
* you can install.
|
|
221
|
+
*/
|
|
222
|
+
export async function listRemotePluginsDetailed({ repo = DEFAULT_REPO, ref = DEFAULT_REF } = {}) {
|
|
223
|
+
const listed = await listRemotePlugins({ repo, ref });
|
|
224
|
+
if (!listed.ok) return listed;
|
|
225
|
+
const items = await Promise.all((listed.items || []).map(async (it) => {
|
|
226
|
+
// THE ENTRY FILE, by either layout: a directory with `index.js`, or a single `.js` beside it.
|
|
227
|
+
const candidates = [`${it.path}/index.js`, `${it.path}.js`];
|
|
228
|
+
for (const file of candidates) {
|
|
229
|
+
const r = await fetchFileText(repo, ref, file);
|
|
230
|
+
if (!r.ok) continue;
|
|
231
|
+
return { ...it, file, description: describePlugin(r.text), readable: true };
|
|
232
|
+
}
|
|
233
|
+
return { ...it, file: candidates[0], description: '', readable: false };
|
|
234
|
+
}));
|
|
235
|
+
return { ok: true, items };
|
|
236
|
+
}
|
|
237
|
+
|
|
138
238
|
/** List the plugins available in the repo: [{ name, path }]. */
|
|
139
239
|
export async function listRemotePlugins({ repo = DEFAULT_REPO, ref = DEFAULT_REF } = {}) {
|
|
140
240
|
const r = await ghJson(apiContents(repo, ref, 'plugins'));
|
|
@@ -168,7 +268,7 @@ export async function listRemoteSkillsDetailed({ repo = DEFAULT_REPO, ref = DEFA
|
|
|
168
268
|
if (!listed.ok) return listed;
|
|
169
269
|
const items = await Promise.all(listed.items.map(async (it) => {
|
|
170
270
|
const file = `${it.path}/SKILL.md`;
|
|
171
|
-
const r = await
|
|
271
|
+
const r = await fetchFileText(repo, ref, file);
|
|
172
272
|
return { ...it, file, description: r.ok ? describeSkill(r.text) : '', readable: r.ok };
|
|
173
273
|
}));
|
|
174
274
|
return { ok: true, items };
|
|
@@ -191,7 +291,7 @@ export async function installRemoteSkill(name, { repo = DEFAULT_REPO, ref = DEFA
|
|
|
191
291
|
}
|
|
192
292
|
|
|
193
293
|
const file = `skills/${clean}/SKILL.md`;
|
|
194
|
-
const r = await
|
|
294
|
+
const r = await fetchFileText(repo, ref, file);
|
|
195
295
|
if (!r.ok) {
|
|
196
296
|
return { ok: false, error: r.error === 'HTTP 404'
|
|
197
297
|
? `No SKILL.md at ${file} (each skill directory must contain SKILL.md).`
|
|
@@ -261,7 +361,7 @@ export async function installRemotePlugin(name, { repo = DEFAULT_REPO, ref = DEF
|
|
|
261
361
|
|
|
262
362
|
const written = [];
|
|
263
363
|
for (const f of files) {
|
|
264
|
-
const r = await
|
|
364
|
+
const r = await fetchFileText(repo, ref, f.path);
|
|
265
365
|
if (!r.ok) return { ok: false, error: `Could not download ${f.path}: ${r.error}` };
|
|
266
366
|
const out = path.join(destDir, path.basename(f.path));
|
|
267
367
|
try {
|
|
@@ -289,6 +389,18 @@ export function removeLocalPlugin(name) {
|
|
|
289
389
|
return false;
|
|
290
390
|
}
|
|
291
391
|
|
|
392
|
+
/** The header comment of a plugin file on disk, as one line. Empty when there is none. */
|
|
393
|
+
function describeLocalPlugin(file) {
|
|
394
|
+
try {
|
|
395
|
+
// ONLY THE HEAD of a plugin is a comment; reading whole files to list a directory would be a
|
|
396
|
+
// needless read of everything installed.
|
|
397
|
+
const fd = fs.openSync(file, 'r');
|
|
398
|
+
const buf = Buffer.alloc(4096);
|
|
399
|
+
const n = fs.readSync(fd, buf, 0, buf.length, 0);
|
|
400
|
+
fs.closeSync(fd);
|
|
401
|
+
return describePlugin(buf.subarray(0, n).toString('utf8'));
|
|
402
|
+
} catch { return ''; }
|
|
403
|
+
}
|
|
292
404
|
/** Installed plugin names, read from the plugin directory. */
|
|
293
405
|
export function listLocalPlugins() {
|
|
294
406
|
let entries;
|
|
@@ -299,8 +411,8 @@ export function listLocalPlugins() {
|
|
|
299
411
|
const full = path.join(pluginsDir(), e);
|
|
300
412
|
let st;
|
|
301
413
|
try { st = fs.statSync(full); } catch { continue; }
|
|
302
|
-
if (st.isDirectory()) out.push({ name: e, kind: 'dir', path: full });
|
|
303
|
-
else if (/\.(mjs?|cjs)$/i.test(e)) out.push({ name: e.replace(/\.[^.]+$/, ''), kind: 'file', path: full });
|
|
414
|
+
if (st.isDirectory()) out.push({ name: e, kind: 'dir', path: full, description: describeLocalPlugin(path.join(full, 'index.js')) });
|
|
415
|
+
else if (/\.(mjs?|cjs)$/i.test(e)) out.push({ name: e.replace(/\.[^.]+$/, ''), kind: 'file', path: full, description: describeLocalPlugin(full) });
|
|
304
416
|
}
|
|
305
417
|
out.sort((a, b) => a.name.localeCompare(b.name));
|
|
306
418
|
return out;
|
package/src/schedule.js
CHANGED
|
@@ -125,17 +125,154 @@ export function parseCron(expr) {
|
|
|
125
125
|
};
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
128
|
+
const DOW_NAMES = {
|
|
129
|
+
en: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
|
|
130
|
+
zh: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The step of a set that walks the whole span evenly (an `0/15` spec -> 15), or 0.
|
|
135
|
+
*
|
|
136
|
+
* `list` must already be sorted. A set that covers every value is not a step: that is the
|
|
137
|
+
* wildcard, and the caller says so in its own words.
|
|
138
|
+
*/
|
|
139
|
+
function stepOf(list, span) {
|
|
140
|
+
if (list.length < 2) return 0;
|
|
141
|
+
const step = list[1] - list[0];
|
|
142
|
+
if (step < 2) return 0;
|
|
143
|
+
if (list[0] % step !== 0) return 0;
|
|
144
|
+
for (let i = 2; i < list.length; i++) if (list[i] - list[i - 1] !== step) return 0;
|
|
145
|
+
// `list.length * step > span` is what tells `0/30` (two entries, 60 span) from the
|
|
146
|
+
// wildcard (60 entries, 60 span). Equal is a real step; covering the span is the wildcard.
|
|
147
|
+
return list.length * step > span ? step : 0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Say what a cron expression does, in either language.
|
|
152
|
+
*
|
|
153
|
+
* The old version knew five expressions and echoed everything else, so a schedule written by
|
|
154
|
+
* hand read back as its own syntax — which is exactly the thing a reader wanted translated.
|
|
155
|
+
* This describes the SETS the parser produced, so every shape gets an answer and the answer
|
|
156
|
+
* cannot disagree with what the scheduler will do.
|
|
157
|
+
*/
|
|
158
|
+
const MONTH_NAMES = {
|
|
159
|
+
en: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
|
|
160
|
+
'September', 'October', 'November', 'December'],
|
|
161
|
+
zh: ['1 月', '2 月', '3 月', '4 月', '5 月', '6 月', '7 月', '8 月',
|
|
162
|
+
'9 月', '10 月', '11 月', '12 月'],
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/** `1st`, `2nd`, `3rd`, `11th`. */
|
|
166
|
+
function ordinal(n) {
|
|
167
|
+
const suffix = ['th', 'st', 'nd', 'rd'];
|
|
168
|
+
const v = n % 100;
|
|
169
|
+
return `${n}${suffix[(v - 20) % 10] || suffix[v] || suffix[0]}`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** True when the sorted list is 1, 2, 3, … with no gap. */
|
|
173
|
+
function contiguous(list) {
|
|
174
|
+
if (list.length < 2) return false;
|
|
175
|
+
for (let i = 1; i < list.length; i++) if (list[i] !== list[i - 1] + 1) return false;
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Say what a cron expression does, in either language.
|
|
181
|
+
*
|
|
182
|
+
* The old version knew five expressions and echoed everything else, so a schedule written by
|
|
183
|
+
* hand read back as its own syntax — the one thing a reader wanted translated. This describes
|
|
184
|
+
* the SETS the parser produced, so every shape gets an answer and the answer cannot disagree
|
|
185
|
+
* with what the scheduler will do.
|
|
186
|
+
*/
|
|
187
|
+
export function describeCron(expr, lang = 'en') {
|
|
188
|
+
const zh = lang === 'zh';
|
|
189
|
+
let p;
|
|
190
|
+
try { p = parseCron(expr); } catch { return String(expr == null ? '' : expr).trim(); }
|
|
191
|
+
const f = p.fields;
|
|
192
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
193
|
+
const minutes = [...f.minute].sort((x, y) => x - y);
|
|
194
|
+
const hours = [...f.hour].sort((x, y) => x - y);
|
|
195
|
+
const dows = [...f.dow].sort((x, y) => x - y);
|
|
196
|
+
const doms = [...f.dom].sort((x, y) => x - y);
|
|
197
|
+
const months = [...f.month].sort((x, y) => x - y);
|
|
198
|
+
const everyMinute = minutes.length === 60;
|
|
199
|
+
const everyHour = hours.length === 24;
|
|
200
|
+
const anyMonth = months.length === 12;
|
|
201
|
+
const dowNames = DOW_NAMES[zh ? 'zh' : 'en'];
|
|
202
|
+
const monthNames = MONTH_NAMES[zh ? 'zh' : 'en'];
|
|
203
|
+
|
|
204
|
+
// THE CLOCK.
|
|
205
|
+
const m0 = minutes.length === 1 ? pad(minutes[0]) : null;
|
|
206
|
+
let clock;
|
|
207
|
+
if (everyMinute) {
|
|
208
|
+
clock = zh ? '每分钟' : 'every minute';
|
|
209
|
+
} else if (everyHour) {
|
|
210
|
+
const step = stepOf(minutes, 60);
|
|
211
|
+
if (step) clock = zh ? `每 ${step} 分钟` : `every ${step} minutes`;
|
|
212
|
+
else if (m0 === '00') clock = zh ? '整点' : 'on the hour';
|
|
213
|
+
else if (m0) clock = zh ? `每小时的 ${m0} 分` : `hourly at :${m0}`;
|
|
214
|
+
else clock = zh ? `每小时 ${minutes.map(pad).join('、')} 分` : `hourly at :${minutes.map(pad).join(', :')}`;
|
|
215
|
+
} else if (hours.length === 1) {
|
|
216
|
+
// One hour, possibly several minutes: 09:00, or 09:00 and 09:30.
|
|
217
|
+
const h = pad(hours[0]);
|
|
218
|
+
clock = zh ? `${h}:${minutes.map(pad).join('、')}` : `at ${h}:${minutes.map(pad).join(' / ')}`;
|
|
219
|
+
} else if (m0 && contiguous(hours)) {
|
|
220
|
+
// A WINDOW, not a list: `9-17` is a range and printing nine clock times is not.
|
|
221
|
+
const from = pad(hours[0]);
|
|
222
|
+
const to = pad(hours[hours.length - 1]);
|
|
223
|
+
clock = zh ? `${from}:${m0} 至 ${to}:${m0}` : `hourly from ${from}:${m0} to ${to}:${m0}`;
|
|
224
|
+
} else {
|
|
225
|
+
const step = stepOf(hours, 24);
|
|
226
|
+
const tail = m0 ? `:${m0}` : ':00';
|
|
227
|
+
clock = step ? (zh ? `每 ${step} 小时${tail}` : `every ${step} hours${tail}`)
|
|
228
|
+
: (zh ? `${hours.map(pad).join('、')} 点${tail}` : `at ${hours.map(pad).join(', ')}${tail}`);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// THE DAY AND THE MONTH.
|
|
232
|
+
let day = '';
|
|
233
|
+
if (p.restricted.dow) {
|
|
234
|
+
const weekend = dows.length === 2 && dows[0] === 0 && dows[1] === 6;
|
|
235
|
+
const weekdays = dows.length === 5 && dows[0] === 1 && dows[4] === 5;
|
|
236
|
+
if (weekend) day = zh ? '周末' : 'weekends';
|
|
237
|
+
else if (weekdays) day = zh ? '工作日' : 'weekdays';
|
|
238
|
+
else if (dows.length === 1) day = zh ? `每${dowNames[dows[0]]}` : `${dowNames[dows[0]]}s`;
|
|
239
|
+
else day = (zh ? '每' : '') + dows.map((d) => dowNames[d]).join(zh ? '、' : ', ');
|
|
240
|
+
}
|
|
241
|
+
if (p.restricted.dom) {
|
|
242
|
+
const which = zh ? doms.join('、') : doms.map(ordinal).join(', ');
|
|
243
|
+
if (!anyMonth) {
|
|
244
|
+
const m = monthNames[months.length - 1];
|
|
245
|
+
day = zh ? `${m} ${which} 日` : `on ${which} ${monthNames[months.map((x) => x - 1)[0]]}`;
|
|
246
|
+
} else {
|
|
247
|
+
day = zh ? `每月 ${which} 日` : `the ${which} of each month`;
|
|
248
|
+
}
|
|
249
|
+
} else if (!anyMonth) {
|
|
250
|
+
const m = months.map((x) => monthNames[x - 1]).join(zh ? '、' : ', ');
|
|
251
|
+
day = day ? `${day}, ${m}` : m;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (!day) return clock;
|
|
255
|
+
return zh ? `${day} ${clock}` : `${day} ${clock}`;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* When this expression next fires, as a timestamp, or null if it never does.
|
|
260
|
+
*
|
|
261
|
+
* Walks minute by minute against the same `matches` the scheduler uses, so a preview cannot
|
|
262
|
+
* promise a time the scheduler would not honour. Four years covers a leap-day rule.
|
|
263
|
+
*/
|
|
264
|
+
export function nextRun(expr, from = Date.now()) {
|
|
265
|
+
let p;
|
|
266
|
+
try { p = parseCron(expr); } catch { return null; }
|
|
267
|
+
const d = new Date(from);
|
|
268
|
+
d.setSeconds(0, 0);
|
|
269
|
+
d.setMinutes(d.getMinutes() + 1);
|
|
270
|
+
const limit = 366 * 4 * 24 * 60;
|
|
271
|
+
for (let i = 0; i < limit; i++) {
|
|
272
|
+
if (p.matches(d)) return d.getTime();
|
|
273
|
+
d.setMinutes(d.getMinutes() + 1);
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
139
276
|
}
|
|
140
277
|
|
|
141
278
|
/** Validate a schedule entry's fields. Returns { ok, error }. */
|