semola 0.6.0 → 0.6.2
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 +19 -5
- package/dist/index-DISN0WKZ.d.mts +78 -0
- package/dist/lib/api/index.cjs +1 -537
- package/dist/lib/api/index.d.cts +1 -271
- package/dist/lib/api/index.d.mts +100 -164
- package/dist/lib/api/index.mjs +1 -535
- package/dist/lib/cache/index.cjs +1 -99
- package/dist/lib/cache/index.d.cts +1 -27
- package/dist/lib/cache/index.mjs +1 -98
- package/dist/lib/cli/index.cjs +3 -0
- package/dist/lib/cli/index.d.cts +1 -0
- package/dist/lib/cli/index.d.mts +90 -0
- package/dist/lib/cli/index.mjs +3 -0
- package/dist/lib/cron/index.cjs +1 -735
- package/dist/lib/cron/index.d.cts +1 -146
- package/dist/lib/cron/index.d.mts +99 -36
- package/dist/lib/cron/index.mjs +1 -726
- package/dist/lib/errors/index.cjs +1 -30
- package/dist/lib/errors/index.d.cts +1 -13
- package/dist/lib/errors/index.mjs +1 -26
- package/dist/lib/i18n/index.cjs +1 -42
- package/dist/lib/i18n/index.d.cts +1 -28
- package/dist/lib/i18n/index.d.mts +2 -2
- package/dist/lib/i18n/index.mjs +1 -41
- package/dist/lib/logging/index.cjs +1 -388
- package/dist/lib/logging/index.d.cts +1 -108
- package/dist/lib/logging/index.mjs +1 -374
- package/dist/lib/orm/index.cjs +1 -1642
- package/dist/lib/orm/index.d.cts +1 -402
- package/dist/lib/orm/index.d.mts +27 -25
- package/dist/lib/orm/index.mjs +1 -1630
- package/dist/lib/policy/index.cjs +1 -285
- package/dist/lib/policy/index.d.cts +1 -77
- package/dist/lib/policy/index.d.mts +1 -1
- package/dist/lib/policy/index.mjs +1 -265
- package/dist/lib/prompts/index.cjs +6 -769
- package/dist/lib/prompts/index.d.cts +1 -75
- package/dist/lib/prompts/index.mjs +6 -762
- package/dist/lib/pubsub/index.cjs +1 -141
- package/dist/lib/pubsub/index.d.cts +1 -26
- package/dist/lib/pubsub/index.mjs +1 -140
- package/dist/lib/queue/index.cjs +1 -212
- package/dist/lib/queue/index.d.cts +1 -81
- package/dist/lib/queue/index.mjs +1 -209
- package/dist/lib/workflow/index.cjs +1 -537
- package/dist/lib/workflow/index.d.cts +1 -150
- package/dist/lib/workflow/index.d.mts +0 -1
- package/dist/lib/workflow/index.mjs +1 -527
- package/dist/rolldown-runtime-CMqjfN_6.cjs +1 -0
- package/package.json +17 -5
- package/dist/chunk-CKQMccvm.cjs +0 -28
package/dist/lib/cache/index.mjs
CHANGED
|
@@ -1,98 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
//#region src/lib/cache/errors.ts
|
|
3
|
-
var CacheError = class extends Error {
|
|
4
|
-
constructor(message) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.name = "CacheError";
|
|
7
|
-
}
|
|
8
|
-
};
|
|
9
|
-
var InvalidTTLError = class extends Error {
|
|
10
|
-
constructor(message) {
|
|
11
|
-
super(message);
|
|
12
|
-
this.name = "InvalidTTLError";
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
var NotFoundError = class extends Error {
|
|
16
|
-
constructor(message) {
|
|
17
|
-
super(message);
|
|
18
|
-
this.name = "NotFoundError";
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
var SerializationError = class extends Error {
|
|
22
|
-
constructor(message) {
|
|
23
|
-
super(message);
|
|
24
|
-
this.name = "SerializationError";
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
var DeserializationError = class extends Error {
|
|
28
|
-
constructor(message) {
|
|
29
|
-
super(message);
|
|
30
|
-
this.name = "DeserializationError";
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/lib/cache/index.ts
|
|
35
|
-
var Cache = class {
|
|
36
|
-
options;
|
|
37
|
-
serialize;
|
|
38
|
-
deserialize;
|
|
39
|
-
constructor(options) {
|
|
40
|
-
this.options = options;
|
|
41
|
-
this.serialize = options.serializer ?? JSON.stringify;
|
|
42
|
-
this.deserialize = options.deserializer ?? ((raw) => JSON.parse(raw));
|
|
43
|
-
}
|
|
44
|
-
async get(key) {
|
|
45
|
-
if (!this.isEnabled) throw new NotFoundError(`Key ${key} not found`);
|
|
46
|
-
const resolvedKey = this.resolveKey(key);
|
|
47
|
-
const [error, value] = await mightThrow(this.options.redis.get(resolvedKey));
|
|
48
|
-
if (error) throw new CacheError(`Unable to get value for key ${key}`);
|
|
49
|
-
if (value === null || value === void 0) throw new NotFoundError(`Key ${key} not found`);
|
|
50
|
-
const [deserializeErr, deserialized] = mightThrowSync(() => this.deserialize(value));
|
|
51
|
-
if (deserializeErr) throw new DeserializationError(`Unable to deserialize value for key ${key}`);
|
|
52
|
-
return deserialized;
|
|
53
|
-
}
|
|
54
|
-
async set(key, value) {
|
|
55
|
-
if (!this.isEnabled) return value;
|
|
56
|
-
const [serializeErr, serialized] = mightThrowSync(() => this.serialize(value));
|
|
57
|
-
if (serializeErr) throw new SerializationError(`Unable to serialize value for key ${key}`);
|
|
58
|
-
if (serialized === null || serialized === void 0) throw new SerializationError(`Unable to serialize value for key ${key}`);
|
|
59
|
-
const [ttlErr, ttl] = mightThrowSync(() => this.resolveTTL(key, value));
|
|
60
|
-
if (ttlErr) throw new InvalidTTLError(`Unable to resolve ttl for key ${key}`);
|
|
61
|
-
if (!this.isTTLValid(ttl)) throw new InvalidTTLError(`Unable to save records with ttl equal to ${ttl}`);
|
|
62
|
-
const resolvedKey = this.resolveKey(key);
|
|
63
|
-
const [setError] = await mightThrow(this.getSetPromise(resolvedKey, serialized, ttl));
|
|
64
|
-
if (setError) throw new CacheError(`Unable to set value for key ${key}`);
|
|
65
|
-
return value;
|
|
66
|
-
}
|
|
67
|
-
async delete(key) {
|
|
68
|
-
if (!this.isEnabled) return 0;
|
|
69
|
-
const resolvedKey = this.resolveKey(key);
|
|
70
|
-
const [error, data] = await mightThrow(this.options.redis.del(resolvedKey));
|
|
71
|
-
if (error) throw new CacheError(`Unable to delete key ${key}`);
|
|
72
|
-
return data;
|
|
73
|
-
}
|
|
74
|
-
get isEnabled() {
|
|
75
|
-
return this.options.enabled !== false;
|
|
76
|
-
}
|
|
77
|
-
resolveKey(key) {
|
|
78
|
-
if (this.options.prefix) return `${this.options.prefix}:${key}`;
|
|
79
|
-
return key;
|
|
80
|
-
}
|
|
81
|
-
resolveTTL(key, value) {
|
|
82
|
-
if (typeof this.options.ttl === "function") return this.options.ttl(key, value);
|
|
83
|
-
return this.options.ttl;
|
|
84
|
-
}
|
|
85
|
-
getSetPromise(key, value, ttl) {
|
|
86
|
-
if (ttl === void 0 || ttl === null) return this.options.redis.set(key, value);
|
|
87
|
-
return this.options.redis.set(key, value, "PX", ttl);
|
|
88
|
-
}
|
|
89
|
-
isTTLValid(ttl) {
|
|
90
|
-
if (ttl === void 0 || ttl === null) return true;
|
|
91
|
-
if (!Number.isFinite(ttl)) return false;
|
|
92
|
-
if (!Number.isInteger(ttl)) return false;
|
|
93
|
-
if (ttl < 0) return false;
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
//#endregion
|
|
98
|
-
export { Cache };
|
|
1
|
+
import{mightThrow as e,mightThrowSync as t}from"../errors/index.mjs";var n=class extends Error{constructor(e){super(e),this.name=`CacheError`}},r=class extends Error{constructor(e){super(e),this.name=`InvalidTTLError`}},i=class extends Error{constructor(e){super(e),this.name=`NotFoundError`}},a=class extends Error{constructor(e){super(e),this.name=`SerializationError`}},o=class extends Error{constructor(e){super(e),this.name=`DeserializationError`}},s=class{options;serialize;deserialize;constructor(e){this.options=e,this.serialize=e.serializer??JSON.stringify,this.deserialize=e.deserializer??(e=>JSON.parse(e))}async get(r){if(!this.isEnabled)throw new i(`Key ${r} not found`);let a=this.resolveKey(r),[s,c]=await e(this.options.redis.get(a));if(s)throw new n(`Unable to get value for key ${r}`);if(c==null)throw new i(`Key ${r} not found`);let[l,u]=t(()=>this.deserialize(c));if(l)throw new o(`Unable to deserialize value for key ${r}`);return u}async set(i,o){if(!this.isEnabled)return o;let[s,c]=t(()=>this.serialize(o));if(s||c==null)throw new a(`Unable to serialize value for key ${i}`);let[l,u]=t(()=>this.resolveTTL(i,o));if(l)throw new r(`Unable to resolve ttl for key ${i}`);if(!this.isTTLValid(u))throw new r(`Unable to save records with ttl equal to ${u}`);let d=this.resolveKey(i),[f]=await e(this.getSetPromise(d,c,u));if(f)throw new n(`Unable to set value for key ${i}`);return o}async delete(t){if(!this.isEnabled)return 0;let r=this.resolveKey(t),[i,a]=await e(this.options.redis.del(r));if(i)throw new n(`Unable to delete key ${t}`);return a}get isEnabled(){return this.options.enabled!==!1}resolveKey(e){return this.options.prefix?`${this.options.prefix}:${e}`:e}resolveTTL(e,t){return typeof this.options.ttl==`function`?this.options.ttl(e,t):this.options.ttl}getSetPromise(e,t,n){return n==null?this.options.redis.set(e,t):this.options.redis.set(e,t,`PX`,n)}isTTLValid(e){return e==null||!(!Number.isFinite(e)||!Number.isInteger(e)||e<0)}};export{s as Cache};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class e{root;constructor(e){this.root=e}command(t,n){let r=this.root.create(t,n);return new e(r)}argument(e,t){for(let t of this.root.arguments)if(t.name===e)throw Error(`Argument "${e}" already exists`);return this.root.arguments.push({name:e,schema:t.schema}),this}option(e,t){let n=t.aliases??[];for(let t of this.root.options){if(t.name===e)throw Error(`Option "${e}" already exists`);if(t.aliases?.includes(e))throw Error(`Option "${e}" conflicts with alias of option "${t.name}"`);for(let e of n){if(t.name===e)throw Error(`Option alias "${e}" conflicts with option "${t.name}"`);if(t.aliases?.includes(e))throw Error(`Option alias "${e}" already exists`)}}return this.root.options.push({name:e,schema:t.schema,aliases:t.aliases}),this}action(e){return this.root.handler=e,this.root.cli}};const t={label:`-h, --help`,description:`Show help`},n=[t,{label:`-v, --version`,description:`Show version`}],r=[t],i=(e,t)=>!(!e||!t.includes(e)),a=e=>i(e,[`--help`,`-h`]),o=e=>i(e,[`--version`,`-v`]),s=e=>e.map(e=>`<${e.name}>`).join(` `),c=e=>Array.from(e,([e,t])=>{let n=[e,s(t.arguments)].filter(e=>e.length>0);return t.description?` ${n.join(` `)} ${t.description}`:` ${n.join(` `)}`}),l=e=>{let t=(e.aliases??[]).map(e=>`-${e}`),n=`--${e.name}`;return[...t,n].join(`, `)},u=e=>({label:l(e),description:f(e.schema)}),d=e=>{let t=e.filter(e=>e.description).reduce((e,t)=>Math.max(e,t.label.length),0);return e.map(e=>e.description?` ${e.label.padEnd(t)} ${e.description}`:` ${e.label}`)},f=e=>{if(!(`jsonSchema`in e[`~standard`]))return``;let{description:t}=e[`~standard`].jsonSchema.input({target:`draft-2020-12`});return typeof t==`string`?t:``},p=e=>{e&&console.log(`${e}\n`)};var m=class{path(e){let t=[],n=e;for(;n?.parent;)t.unshift(n.name),n=n.parent;return t}readCommands(e){return e.commands.keys().toArray()}resolve(e,t){let n=e,r=0;for(;r<t.length;){let e=t[r];if(!e||e.startsWith(`-`))break;let i=n.commands.get(e);if(!i)break;n=i,r++}return r===0?{command:void 0,rest:t.slice(r)}:{command:n,rest:t.slice(r)}}printHelp(e,t){let n=s(e.arguments),i=this.path(e).join(` `),a=[t.name,i,n,`[options]`].filter(e=>e.length>0).join(` `);if(console.log(`Usage: ${a}\n`),p(t.description),e.arguments.length>0){console.log(`Arguments:`);let t=e.arguments.map(e=>({label:e.name,description:f(e.schema)}));for(let e of d(t))console.log(e);console.log(``)}if(e.commands.size>0){console.log(`Commands:`);for(let t of c(e.commands))console.log(t);console.log(``)}console.log(`Options:`);let o=[...e.options.map(u),...r];for(let e of d(o))console.log(e)}},h=class e{cli;name;parent;description;arguments=[];options=[];commands=new Map;handler;constructor(e,t,n,r){this.cli=e,this.name=t,this.parent=n,this.description=r}create(t,n){if(this.commands.has(t))throw Error(`Command "${t}" already exists`);let r=new e(this.cli,t,this,n?.description);return this.commands.set(t,r),r}},g=class extends Error{name=`CliError`},_=class extends g{name=`CliValidationError`},v=class extends g{name=`UnknownCommandError`},y=class extends g{name=`CliConfigurationError`},b=class extends g{name=`MissingArgumentError`};const x=(e,t,n,r)=>{let i=n[r],a=n[r+1];return i!==void 0&&a!==void 0&&(i.indexOf(`=`)===-1&&(a=a.trim()),!a.startsWith(`-`))?(e[t]=a,r+2):(e[t]=!0,r+1)},S=(e,t,n)=>{let r=e.get(t);if(!r)throw new _(n);return r},C=(e,t)=>{let n=new Map;for(let e of t){n.set(e.name,e.name);for(let t of e.aliases??[])n.set(t,e.name)}let r=[],i={},a=0;for(;a<e.length;){let t=e[a];if(!t)break;let o=`Unknown option: ${t}`,s=t.indexOf(`=`),c=s===-1?void 0:s;if(t.charAt(0)===`-`){if(t.length<2)throw new _(`Invalid option: ${t}`);let r=1;t.charAt(1)===`-`&&(r=2);let l=t.slice(r,c).trim();if(l.length===1&&r===2)throw new _(`Invalid option: ${t}`);let u=S(n,l,o);if(s!==-1){i[u]=t.slice(s+1),a++;continue}a=x(i,u,e,a);continue}r.push(t),a++}return{positional:r,options:i}},w=.6;function T(e,t){if(e.length<t.length)return T(t,e);if(t.length===0)return e.length;let n=Array.from({length:t.length+1},(e,t)=>t);for(let r=0;r<e.length;r++){let i=e.charAt(r),a=Array.from({length:t.length}).fill(0),o=[r+1,...a];for(let e=0;e<t.length;e++){let r=t.charAt(e),a=n[e+1],s=o[e],c=n[e];if(a===void 0||s===void 0||c===void 0)return-1;let l=a+1,u=s+1,d=c+(i===r?0:1);o[e+1]=Math.min(l,u,d)}n=o}let r=n[n.length-1];return r===void 0?-1:r}function E(e,t){return D(e.map(e=>{let n=T(e,t),r=Math.max(e.length,t.length);return r===0?{command:e,cost:0}:{command:e,cost:1-n/r}}).filter(e=>{let t=e.cost>w,n=Math.abs(e.cost-w)<2**-52;return t||n}))}function D(e){return e.sort((e,t)=>t.cost-e.cost).slice(0,2).map(e=>e.command)}const O=async(e,t,n)=>{let r=await e[`~standard`].validate(t);if(!r.issues)return r.value;throw new _(r.issues.map(e=>{let t=n;if(Array.isArray(e.path)){let r=e.path.map(String).join(`.`);r.length>0&&(t=`${n}.${r}`)}return`${t}: ${e.message??`validation failed`}`}).join(`, `))},k=async(e,t)=>{if(t.length<e.length){let n=e[t.length];throw n?new b(`Missing argument: ${n.name}`):new b(`Missing argument`)}let n={};for(let[r,i]of e.entries())n[i.name]=await O(i.schema,t[r],i.name);return n},A=async(e,t)=>{let n={};for(let r of e){let e=t[r.name];n[r.name]=await O(r.schema,e,r.name)}return n};var j=class{config;root=new h(this,``);commandHelper=new m;constructor(e){this.config=e}command(t,n){return new e(this.root.create(t,n))}async parse(e){let t=e??process.argv.slice(2),[n]=t;if(n||(this.printHelp(),process.exit(1)),a(n)){this.printHelp();return}if(o(n)){this.printVersion();return}let{command:r,rest:i}=this.commandHelper.resolve(this.root,t),[s]=i;if(!r){let e=this.createSuggestion(n);this.handleCliError(new v(this.formatUnknownCommandMessage(n,e)))}if(a(s)){this.commandHelper.printHelp(r,this.config);return}let c=r.handler;if(!c){if(r.commands.size>0){if(s!==void 0){let e=this.createSuggestion(s);this.handleCliError(new v(this.formatUnknownCommandMessage(s,e)))}this.commandHelper.printHelp(r,this.config);return}let e=this.commandHelper.path(r).join(` `);this.handleCliError(new y(`Command "${e}" has no action handler`))}try{let e=C(i,r.options);await c(await k(r.arguments,e.positional),await A(r.options,e.options))}catch(e){this.handleCliError(e)}}printVersion(){let e=this.config.version??`0.0.0`;console.log(e)}printHelp(){console.log(`Usage: ${this.config.name} <command> [options]\n`),p(this.config.description),console.log(`Commands:`);for(let e of c(this.root.commands))console.log(e);console.log(``),console.log(`Options:`);for(let e of d(n))console.log(e)}handleCliError(e){throw e instanceof g&&(console.error(e.message),process.exit(1)),e}createSuggestion(e){let t=E(this.commandHelper.readCommands(this.root),e);return t.length===0?``:t.length===1?`Did you mean ${t[0]}?`:t.reduce((e,n,r)=>{let i=e.concat(` ${n}`);return r!==t.length-1&&(i=i.concat(`
|
|
2
|
+
`)),i},`Did you mean
|
|
3
|
+
`)}formatUnknownCommandMessage(e,t){return t.length===0?`Unknown command: ${e}`:`Unknown command: ${e}\n${t}`}};exports.CLI=j,exports.CliConfigurationError=y,exports.CliValidationError=_,exports.Command=h,exports.MissingArgumentError=b,exports.UnknownCommandError=v;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type * from './index.d.mts'
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { t as StandardSchemaV1 } from "../../index-DISN0WKZ.mjs";
|
|
2
|
+
//#region src/lib/cli/types.d.ts
|
|
3
|
+
type HandlerFnType = (args: Record<string, unknown>, options: Record<string, unknown>) => void | Promise<void>;
|
|
4
|
+
type CLIConfig = {
|
|
5
|
+
name: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
version?: string;
|
|
8
|
+
};
|
|
9
|
+
type ArgumentConfig = {
|
|
10
|
+
name: string;
|
|
11
|
+
schema: StandardSchemaV1;
|
|
12
|
+
};
|
|
13
|
+
type OptionConfig = {
|
|
14
|
+
name: string;
|
|
15
|
+
schema: StandardSchemaV1;
|
|
16
|
+
aliases?: string[];
|
|
17
|
+
};
|
|
18
|
+
type SafeTypeAccess<T, K extends "input" | "output"> = T extends StandardSchemaV1 ? T["~standard"] extends {
|
|
19
|
+
types?: infer U;
|
|
20
|
+
} ? U extends Record<K, infer V> ? V : never : never : undefined;
|
|
21
|
+
type InferOutput<T extends StandardSchemaV1> = SafeTypeAccess<T, "output">;
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/lib/cli/command.d.ts
|
|
24
|
+
declare class Command {
|
|
25
|
+
readonly cli: Cli;
|
|
26
|
+
readonly name: string;
|
|
27
|
+
readonly parent?: Command | undefined;
|
|
28
|
+
readonly description?: string | undefined;
|
|
29
|
+
readonly arguments: ArgumentConfig[];
|
|
30
|
+
readonly options: OptionConfig[];
|
|
31
|
+
readonly commands: Map<string, Command>;
|
|
32
|
+
handler?: HandlerFnType;
|
|
33
|
+
constructor(cli: Cli, name: string, parent?: Command | undefined, description?: string | undefined);
|
|
34
|
+
create(name: string, config?: {
|
|
35
|
+
description?: string;
|
|
36
|
+
}): Command;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/lib/cli/command.control.d.ts
|
|
40
|
+
declare class CommandControl<TArgs extends Record<string, unknown> = Record<string, unknown>, TOptions extends Record<string, unknown> = Record<string, unknown>> {
|
|
41
|
+
private root;
|
|
42
|
+
constructor(root: Command);
|
|
43
|
+
command(name: string, config?: {
|
|
44
|
+
description?: string;
|
|
45
|
+
}): CommandControl<Record<string, unknown>, Record<string, unknown>>;
|
|
46
|
+
argument<K extends string, S extends StandardSchemaV1>(name: K, config: {
|
|
47
|
+
schema: S;
|
|
48
|
+
}): CommandControl<TArgs & Record<K, InferOutput<S>>, TOptions>;
|
|
49
|
+
option<K extends string, S extends StandardSchemaV1>(name: K, config: {
|
|
50
|
+
schema: S;
|
|
51
|
+
aliases?: string[];
|
|
52
|
+
}): CommandControl<TArgs, TOptions & Record<K, InferOutput<S>>>;
|
|
53
|
+
action(handler: (args: TArgs, options: TOptions) => void | Promise<void>): Cli;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/lib/cli/cli.d.ts
|
|
57
|
+
declare class Cli {
|
|
58
|
+
private readonly config;
|
|
59
|
+
private readonly root;
|
|
60
|
+
private readonly commandHelper;
|
|
61
|
+
constructor(config: CLIConfig);
|
|
62
|
+
command(name: string, config?: {
|
|
63
|
+
description?: string;
|
|
64
|
+
}): CommandControl<Record<string, unknown>, Record<string, unknown>>;
|
|
65
|
+
parse(argv?: string[]): Promise<void>;
|
|
66
|
+
private printVersion;
|
|
67
|
+
private printHelp;
|
|
68
|
+
private handleCliError;
|
|
69
|
+
private createSuggestion;
|
|
70
|
+
private formatUnknownCommandMessage;
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/lib/cli/errors.d.ts
|
|
74
|
+
declare class CliError extends Error {
|
|
75
|
+
name: string;
|
|
76
|
+
}
|
|
77
|
+
declare class CliValidationError extends CliError {
|
|
78
|
+
name: string;
|
|
79
|
+
}
|
|
80
|
+
declare class UnknownCommandError extends CliError {
|
|
81
|
+
name: string;
|
|
82
|
+
}
|
|
83
|
+
declare class CliConfigurationError extends CliError {
|
|
84
|
+
name: string;
|
|
85
|
+
}
|
|
86
|
+
declare class MissingArgumentError extends CliError {
|
|
87
|
+
name: string;
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
export { Cli as CLI, CliConfigurationError, CliValidationError, Command, MissingArgumentError, UnknownCommandError };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
var e=class e{root;constructor(e){this.root=e}command(t,n){let r=this.root.create(t,n);return new e(r)}argument(e,t){for(let t of this.root.arguments)if(t.name===e)throw Error(`Argument "${e}" already exists`);return this.root.arguments.push({name:e,schema:t.schema}),this}option(e,t){let n=t.aliases??[];for(let t of this.root.options){if(t.name===e)throw Error(`Option "${e}" already exists`);if(t.aliases?.includes(e))throw Error(`Option "${e}" conflicts with alias of option "${t.name}"`);for(let e of n){if(t.name===e)throw Error(`Option alias "${e}" conflicts with option "${t.name}"`);if(t.aliases?.includes(e))throw Error(`Option alias "${e}" already exists`)}}return this.root.options.push({name:e,schema:t.schema,aliases:t.aliases}),this}action(e){return this.root.handler=e,this.root.cli}};const t={label:`-h, --help`,description:`Show help`},n=[t,{label:`-v, --version`,description:`Show version`}],r=[t],i=(e,t)=>!(!e||!t.includes(e)),a=e=>i(e,[`--help`,`-h`]),o=e=>i(e,[`--version`,`-v`]),s=e=>e.map(e=>`<${e.name}>`).join(` `),c=e=>Array.from(e,([e,t])=>{let n=[e,s(t.arguments)].filter(e=>e.length>0);return t.description?` ${n.join(` `)} ${t.description}`:` ${n.join(` `)}`}),l=e=>{let t=(e.aliases??[]).map(e=>`-${e}`),n=`--${e.name}`;return[...t,n].join(`, `)},u=e=>({label:l(e),description:f(e.schema)}),d=e=>{let t=e.filter(e=>e.description).reduce((e,t)=>Math.max(e,t.label.length),0);return e.map(e=>e.description?` ${e.label.padEnd(t)} ${e.description}`:` ${e.label}`)},f=e=>{if(!(`jsonSchema`in e[`~standard`]))return``;let{description:t}=e[`~standard`].jsonSchema.input({target:`draft-2020-12`});return typeof t==`string`?t:``},p=e=>{e&&console.log(`${e}\n`)};var m=class{path(e){let t=[],n=e;for(;n?.parent;)t.unshift(n.name),n=n.parent;return t}readCommands(e){return e.commands.keys().toArray()}resolve(e,t){let n=e,r=0;for(;r<t.length;){let e=t[r];if(!e||e.startsWith(`-`))break;let i=n.commands.get(e);if(!i)break;n=i,r++}return r===0?{command:void 0,rest:t.slice(r)}:{command:n,rest:t.slice(r)}}printHelp(e,t){let n=s(e.arguments),i=this.path(e).join(` `),a=[t.name,i,n,`[options]`].filter(e=>e.length>0).join(` `);if(console.log(`Usage: ${a}\n`),p(t.description),e.arguments.length>0){console.log(`Arguments:`);let t=e.arguments.map(e=>({label:e.name,description:f(e.schema)}));for(let e of d(t))console.log(e);console.log(``)}if(e.commands.size>0){console.log(`Commands:`);for(let t of c(e.commands))console.log(t);console.log(``)}console.log(`Options:`);let o=[...e.options.map(u),...r];for(let e of d(o))console.log(e)}},h=class e{cli;name;parent;description;arguments=[];options=[];commands=new Map;handler;constructor(e,t,n,r){this.cli=e,this.name=t,this.parent=n,this.description=r}create(t,n){if(this.commands.has(t))throw Error(`Command "${t}" already exists`);let r=new e(this.cli,t,this,n?.description);return this.commands.set(t,r),r}},g=class extends Error{name=`CliError`},_=class extends g{name=`CliValidationError`},v=class extends g{name=`UnknownCommandError`},y=class extends g{name=`CliConfigurationError`},b=class extends g{name=`MissingArgumentError`};const x=(e,t,n,r)=>{let i=n[r],a=n[r+1];return i!==void 0&&a!==void 0&&(i.indexOf(`=`)===-1&&(a=a.trim()),!a.startsWith(`-`))?(e[t]=a,r+2):(e[t]=!0,r+1)},S=(e,t,n)=>{let r=e.get(t);if(!r)throw new _(n);return r},C=(e,t)=>{let n=new Map;for(let e of t){n.set(e.name,e.name);for(let t of e.aliases??[])n.set(t,e.name)}let r=[],i={},a=0;for(;a<e.length;){let t=e[a];if(!t)break;let o=`Unknown option: ${t}`,s=t.indexOf(`=`),c=s===-1?void 0:s;if(t.charAt(0)===`-`){if(t.length<2)throw new _(`Invalid option: ${t}`);let r=1;t.charAt(1)===`-`&&(r=2);let l=t.slice(r,c).trim();if(l.length===1&&r===2)throw new _(`Invalid option: ${t}`);let u=S(n,l,o);if(s!==-1){i[u]=t.slice(s+1),a++;continue}a=x(i,u,e,a);continue}r.push(t),a++}return{positional:r,options:i}},w=.6;function T(e,t){if(e.length<t.length)return T(t,e);if(t.length===0)return e.length;let n=Array.from({length:t.length+1},(e,t)=>t);for(let r=0;r<e.length;r++){let i=e.charAt(r),a=Array.from({length:t.length}).fill(0),o=[r+1,...a];for(let e=0;e<t.length;e++){let r=t.charAt(e),a=n[e+1],s=o[e],c=n[e];if(a===void 0||s===void 0||c===void 0)return-1;let l=a+1,u=s+1,d=c+(i===r?0:1);o[e+1]=Math.min(l,u,d)}n=o}let r=n[n.length-1];return r===void 0?-1:r}function E(e,t){return D(e.map(e=>{let n=T(e,t),r=Math.max(e.length,t.length);return r===0?{command:e,cost:0}:{command:e,cost:1-n/r}}).filter(e=>{let t=e.cost>w,n=Math.abs(e.cost-w)<2**-52;return t||n}))}function D(e){return e.sort((e,t)=>t.cost-e.cost).slice(0,2).map(e=>e.command)}const O=async(e,t,n)=>{let r=await e[`~standard`].validate(t);if(!r.issues)return r.value;throw new _(r.issues.map(e=>{let t=n;if(Array.isArray(e.path)){let r=e.path.map(String).join(`.`);r.length>0&&(t=`${n}.${r}`)}return`${t}: ${e.message??`validation failed`}`}).join(`, `))},k=async(e,t)=>{if(t.length<e.length){let n=e[t.length];throw n?new b(`Missing argument: ${n.name}`):new b(`Missing argument`)}let n={};for(let[r,i]of e.entries())n[i.name]=await O(i.schema,t[r],i.name);return n},A=async(e,t)=>{let n={};for(let r of e){let e=t[r.name];n[r.name]=await O(r.schema,e,r.name)}return n};var j=class{config;root=new h(this,``);commandHelper=new m;constructor(e){this.config=e}command(t,n){return new e(this.root.create(t,n))}async parse(e){let t=e??process.argv.slice(2),[n]=t;if(n||(this.printHelp(),process.exit(1)),a(n)){this.printHelp();return}if(o(n)){this.printVersion();return}let{command:r,rest:i}=this.commandHelper.resolve(this.root,t),[s]=i;if(!r){let e=this.createSuggestion(n);this.handleCliError(new v(this.formatUnknownCommandMessage(n,e)))}if(a(s)){this.commandHelper.printHelp(r,this.config);return}let c=r.handler;if(!c){if(r.commands.size>0){if(s!==void 0){let e=this.createSuggestion(s);this.handleCliError(new v(this.formatUnknownCommandMessage(s,e)))}this.commandHelper.printHelp(r,this.config);return}let e=this.commandHelper.path(r).join(` `);this.handleCliError(new y(`Command "${e}" has no action handler`))}try{let e=C(i,r.options);await c(await k(r.arguments,e.positional),await A(r.options,e.options))}catch(e){this.handleCliError(e)}}printVersion(){let e=this.config.version??`0.0.0`;console.log(e)}printHelp(){console.log(`Usage: ${this.config.name} <command> [options]\n`),p(this.config.description),console.log(`Commands:`);for(let e of c(this.root.commands))console.log(e);console.log(``),console.log(`Options:`);for(let e of d(n))console.log(e)}handleCliError(e){throw e instanceof g&&(console.error(e.message),process.exit(1)),e}createSuggestion(e){let t=E(this.commandHelper.readCommands(this.root),e);return t.length===0?``:t.length===1?`Did you mean ${t[0]}?`:t.reduce((e,n,r)=>{let i=e.concat(` ${n}`);return r!==t.length-1&&(i=i.concat(`
|
|
2
|
+
`)),i},`Did you mean
|
|
3
|
+
`)}formatUnknownCommandMessage(e,t){return t.length===0?`Unknown command: ${e}`:`Unknown command: ${e}\n${t}`}};export{j as CLI,y as CliConfigurationError,_ as CliValidationError,h as Command,b as MissingArgumentError,v as UnknownCommandError};
|