UNPKG

@clerc/core

Version:
1 lines 6.08 kB
import{t as e}from"./chunk-rq_z9THO.mjs";import{DOUBLE_DASH as t,DOUBLE_DASH as n,InvalidSchemaError as r,KNOWN_FLAG as i,PARAMETER as a,PARAMETER as o,UNKNOWN_FLAG as s,parse as c}from"@clerc/parser";import{camelCase as l,looseIsArray as u,toArray as d}from"@clerc/utils";import{LiteEmit as f}from"lite-emit";var p=class extends Error{},m=e({Enum:()=>h,FlagValidationError:()=>p,Range:()=>g,Regex:()=>_});function h(...e){let t=(t=>{if(!e.includes(t))throw new p(`Invalid value: ${t}. Must be one of: ${e.join(`, `)}`);return t});return t.display=e.join(` | `),t}function g(e,t){let n=(n=>{let r=Number(n);if(Number.isNaN(r)||r<e||r>t)throw new p(`Invalid value: ${n}. Must be a number between ${e} and ${t}`);return r});return n.display=`${e} - ${t}`,n}function _(e,t){let n=(t=>{if(!e.test(t))throw new p(`Invalid value: ${t}. Must match pattern: ${e}`);return t});return n.display=t??`Regex: ${e.toString()}`,n}function v(e,t){for(let n=t.length;n>=0;n--){let r=t.slice(0,n).join(` `);if(e.has(r))return[e.get(r),r]}return[void 0,void 0]}var y=class extends Error{constructor(e,t=`No such command: "${e}".`){super(t),this.commandName=e}},b=class extends Error{constructor(e=`No command specified.`){super(e)}},x=class extends Error{},S=class extends Error{constructor(e){super(`CLI ${e} is required.`)}},C=class extends Error{},w=class extends Error{constructor(e){let t=e.length>1?`s`:``;super(`Missing required flag${t}: ${e.join(`, `)}`)}};function T(e){return typeof e==`function`?{enforce:`normal`,handler:e}:{enforce:e.enforce??`normal`,handler:e.handler}}function E(e){let t=e.map(T),n=t.filter(e=>e.enforce===`pre`),r=t.filter(e=>e.enforce===`normal`),i=t.filter(e=>e.enforce===`post`),a=[...n,...r,...i];return async e=>{let t=0;async function n(){t>=a.length||await a[t++].handler(e,n)}await n()}}const D=typeof Deno<`u`,O=typeof process<`u`&&!D,k=process.versions.electron&&!process.defaultApp,A=e=>typeof e==`function`||u(e)?{type:e}:e,j=e=>typeof e==`string`?{key:e}:e;function M(e){let t=[];for(let n of e){if(n.startsWith(`-`))break;t.push(n)}return t}const N=/^(<|\[)([\w ]+)(\.\.\.)?(\]|>)$/,P=e=>e.startsWith(`<`)&&e.endsWith(`>`)||e.startsWith(`[`)&&e.endsWith(`]`);function F(e){let t=e.match(N);if(!t||!P(e))throw new C(`Invalid parameter definition: ${e}`);return{name:l(t[2]),isRequired:e.startsWith(`<`),isVariadic:!!t[3]}}function I(e,t){let n={},r=!1;for(let[i,a]of e.entries()){let o=j(a),{name:s,isRequired:c,isVariadic:l}=F(o.key);if(s in n)throw new C(`Duplicate parameter name: ${s}`);if(l&&i!==e.length-1)throw new C(`Variadic parameter must be the last parameter in the definition.`);if(c){if(r)throw new C(`Required parameter "${s}" cannot appear after an optional parameter.`)}else r=!0;let u=l?t.slice(i):t[i];if(c&&(l?u.length===0:u===void 0))throw new C(`Missing required ${l?`variadic `:``}parameter: ${s}`);o.type?l?n[s]=u.map(e=>o.type(e)):u===void 0?n[s]=u:n[s]=o.type(u):n[s]=u}return n}function L(e,t,r){let i=e.indexOf(n);if(i===-1)return I(e,t);{let n=e.slice(0,i),a=e.slice(i+1);return{...I(n,t),...I(a,r)}}}const R=O?process.argv.slice(k?1:2):D?Deno.args:[];var z=class e{#e=[];#t=new Map;#n=new f;#r={};#i={};#a=[];#o=[];#s=``;#c=``;#l=``;#u=``;constructor({name:e,scriptName:t,description:n,version:r}={}){e&&(this.#s=e),t&&(this.#c=t),n&&(this.#l=n),r&&(this.#u=r)}get _name(){return this.#s||this.#c}get _scriptName(){return this.#c}get _description(){return this.#l}get _version(){return this.#u}get _commands(){return this.#t}get _globalFlags(){return this.#r}get store(){return this.#i}static create(t){return new e(t)}name(e){return this.#s=e,this}scriptName(e){return this.#c=e,this}description(e){return this.#l=e,this}version(e){return this.#u=e,this}use(e){return e.setup(this),this}errorHandler(e){return this.#o.push(e),this}#d(e){if(this.#o.length>0)for(let t of this.#o)t(e);else throw e}#f(e){try{let t=e();return t instanceof Promise?t.catch(e=>{this.#d(e)}):t}catch(e){throw this.#d(e),e}}#p(e,t){if(this.#t.has(e))throw new x(`Command with name "${e}" already exists.`);for(let e of t)if(this.#t.has(e))throw new x(`Command with name "${e}" already exists.`)}command(e,t,n){if(Array.isArray(e)){for(let t of e)this.command(t);return this}let r=typeof t==`string`,i=typeof e==`string`?{name:e,description:r?t:void 0,...r?n:t}:e,a=d(i?.alias??[]);this.#f(()=>this.#p(i.name,a)),this.#t.set(i.name,i);for(let e of a)this.#t.set(e,{...i,__isAlias:!0});return i.handler&&this.on(i.name,i.handler),this}globalFlag(e,t,n){let r=typeof t==`string`;return this.#r[e]={description:r?t:void 0,...r?n:t},this}interceptor(e){return this.#a.push(e),this}on(e,t){return this.#n.on(e,t),this}#m(){if(!this.#c)throw new S(`script name`);if(!this.#u)throw new S(`version`)}#h(e,t){let{flags:n,ignore:r}=t??{};return this.#f(()=>c(e,{flags:{...this.#r,...n},ignore:r}))}async run(){let e=M(this.#e),[t,n]=v(this.#t,e),r=t&&n.length>0?this.#e.slice(n.split(` `).length):this.#e,i=this.#f(()=>this.#h(r,t)),a={},o;try{a=t?.parameters?L(t.parameters,i.parameters,i.doubleDash):{}}catch(e){o=e}let s={command:t,calledAs:n,parameters:a,flags:i.flags,ignored:i.ignored,rawParsed:i,store:{...this.#i}},c={enforce:`post`,handler:async n=>{if(i.missingRequiredFlags.length>0)throw new w(i.missingRequiredFlags);if(o)throw o;if(t)await this.#n.emit(t.name,n);else throw e.length>0?new y(e.join(` `)):new b}},l=E([...this.#a,c]);return this.#f(()=>l(s))}parse(e=R){this.#f(()=>this.#m()),Array.isArray(e)&&(e={argv:e});let{argv:t=R,run:n=!0}=e;return this.#e=t,n?this.run():this}};const B=(e,t)=>({...e,handler:t});function V(){let e=!1;return t=>t===o&&!e?(e=!0,!1):e}const H=e=>e;export{z as Clerc,t as DOUBLE_DASH,x as InvalidCommandError,C as InvalidParametersError,r as InvalidSchemaError,i as KNOWN_FLAG,w as MissingRequiredFlagError,S as MissingRequiredMetadataError,b as NoCommandSpecifiedError,y as NoSuchCommandError,a as PARAMETER,m as Types,s as UNKNOWN_FLAG,V as createStopAtFirstParameter,B as defineCommand,H as definePlugin,F as extractParameterInfo,A as normalizeFlagValue,j as normalizeParameterValue,v as resolveCommand};