@lowlighter/run 0.2.0 → 1.0.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/command.mjs +1 -1
- package/command.ts +11 -1
- package/command_test.ts +5 -0
- package/deno.jsonc +2 -2
- package/mod.mjs +1 -1
- package/package.json +4 -2
package/command.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var e=class e{constructor({level:t,format:n,output:o,tags:r,options:s}={}){if("granted"===globalThis.Deno?.permissions.querySync?.({name:"env",variable:"LOG_LEVEL"}).state){const n=globalThis.Deno?.env.get("LOG_LEVEL")??"";n in e.level&&(t??=e.level[n]),Number.isNaN(Number.parseInt(n))||(t??=Number.parseInt(n))}this.level=t??e.level.log,this.#e=o||null===o?o:console,this.#t=n??e.format.text,this.tags=r??{},this.options={date:!1,time:!1,delta:!0,...s,caller:!1!==s?.caller&&{file:!1,name:!1,line:!1,...s?.caller}}}level;#t;#e;tags;options;error(...t){return this.level>=e.level.error&&this.#e?.error(...this.#t(this,{level:e.level.error,content:t})),this}warn(...t){return this.level>=e.level.warn&&this.#e?.warn(...this.#t(this,{level:e.level.warn,content:t})),this}info(...t){return this.level>=e.level.info&&this.#e?.info(...this.#t(this,{level:e.level.info,content:t})),this}log(...t){return this.level>=e.level.log&&this.#e?.log(...this.#t(this,{level:e.level.log,content:t})),this}debug(...t){return this.level>=e.level.debug&&this.#e?.debug(...this.#t(this,{level:e.level.debug,content:t})),this}with(t={}){return new e({level:this.level,format:this.#t,output:this.#e,options:{...this.options},tags:{...this.tags,...t}})}#n(e=3){const t=Error,n=t.prepareStackTrace;t.prepareStackTrace=(e,t)=>t;const{stack:o}=new Error;t.prepareStackTrace=n;const r=o[e];return{file:r.getFileName(),name:r.getFunctionName(),line:r.getLineNumber(),column:r.getColumnNumber()}}static level=Object.freeze({disabled:NaN,error:0,warn:1,info:2,log:3,debug:4});static format={text(t,{level:n=0,content:o}){const r=["red","orange","cyan","white","gray"][n],s=[`%c ${Object.keys(e.level).find((t=>e.level[t]===n)).toLocaleUpperCase().padEnd(5)} │%c`],i=[`color: black; background-color: ${r}`,""];if(t.options.date||t.options.time||t.options.delta){const e=(new Date).toISOString(),n=[];if(t.options.delta){const e=performance.now()/1e3;let t=e.toPrecision(4);e<1&&(t=e.toPrecision(2)),n.push(`+${t}`)}t.options.date&&t.options.time?n.push(e):t.options.date?n.push(e.slice(0,e.indexOf("T"))):t.options.time&&n.push(e.slice(e.indexOf("T")+1,-1)),s.push(`%c ${n.join(" ¦ ").trim()} %c`),i.push(`color: black; background-color: ${r}`,"")}if(t.options.caller){const e=t.#n();if(e){const n=[];t.options.caller.file&&n.push(`${e.file.replace(t.options.caller.fileformat,"$<file>")}`),t.options.caller.name&&e.name&&n.push(e.name),t.options.caller.line&&n.push(e.line,e.column),s.push(`%c ${n.join(":").trim()} %c`),i.push("color: black; background-color: gray","")}}{const n=[];for(const[o,r]of Object.entries(t.tags))n.push(`${o}:${e.inspect(r)}`);s.push(`%c ${n.join(" ").trim()} %c`),i.push("background-color: black","")}return[s.join(""),...i,...o.map(e.inspect)]},json(t,{level:n=0,content:o}){const r={level:Object.keys(e.level).find((t=>e.level[t]===n)),timestamp:Date.now(),tags:t.tags,content:o},s={};if(t.options.date||t.options.time){const e=new Date(r.timestamp).toISOString();t.options.date&&(s.date=e.slice(0,e.indexOf("T"))),t.options.time&&(s.time=e.slice(e.indexOf("T")+1,-1))}if(t.options.delta&&(s.delta=performance.now()/1e3),t.options.caller){const e=t.#n();e&&(s.caller={},t.options.caller.file&&(s.caller.file=`${e.file.replace(t.options.caller.fileformat,"$<file>")}`),t.options.caller.name&&e.name&&(s.caller.name=e.name),t.options.caller.line&&(s.caller.line=[e.line,e.column]))}return[JSON.stringify({...r,...s})]}};static inspect(e){return globalThis.Deno?.inspect(e,{colors:!0,depth:1/0})??e}},t=class extends Error{constructor(e){super(e),this.name="AssertionError"}};TransformStream;function n(e){let t=0;for(const n of e)t+=n.length;const n=new Uint8Array(t);let o=0;for(const t of e)n.set(t,o),o+=t.length;return n}function o(e){const t=e.length,n=new Uint8Array(t);n[0]=0;let o=0,r=1;for(;r<t;)e[r]===e[o]?(o++,n[r]=o,r++):0===o?(n[r]=0,r++):o=n[o-1];return n}TransformStream,TransformStream,TransformStream,TransformStream;var r=class extends TransformStream{#o="";constructor(e={allowCR:!1}){super({transform:(t,n)=>{for(t=this.#o+t;;){const o=t.indexOf("\n"),r=e.allowCR?t.indexOf("\r"):-1;if(-1!==r&&r!==t.length-1&&(-1===o||o-1>r)){n.enqueue(t.slice(0,r)),t=t.slice(r+1);continue}if(-1===o)break;const s="\r"===t[o-1]?o-1:o;n.enqueue(t.slice(0,s)),t=t.slice(o+1)}this.#o=t},flush:t=>{if(""===this.#o)return;const n=e.allowCR&&this.#o.endsWith("\r")?this.#o.slice(0,-1):this.#o;t.enqueue(n)}})}};new TextDecoder;var s=new TextEncoder,i=new TextDecoder;function l(t,n,{log:o=new e,stdin:l=null,stdout:
|
|
1
|
+
var e=class e{constructor({level:t,format:n,output:o,tags:r,options:s}={}){if("granted"===globalThis.Deno?.permissions.querySync?.({name:"env",variable:"LOG_LEVEL"}).state){const n=globalThis.Deno?.env.get("LOG_LEVEL")??"";n in e.level&&(t??=e.level[n]),Number.isNaN(Number.parseInt(n))||(t??=Number.parseInt(n))}this.level=t??e.level.log,this.#e=o||null===o?o:console,this.#t=n??e.format.text,this.tags=r??{},this.options={date:!1,time:!1,delta:!0,...s,caller:!1!==s?.caller&&{file:!1,name:!1,line:!1,...s?.caller}}}level;#t;#e;tags;options;error(...t){return this.level>=e.level.error&&this.#e?.error(...this.#t(this,{level:e.level.error,content:t})),this}warn(...t){return this.level>=e.level.warn&&this.#e?.warn(...this.#t(this,{level:e.level.warn,content:t})),this}info(...t){return this.level>=e.level.info&&this.#e?.info(...this.#t(this,{level:e.level.info,content:t})),this}log(...t){return this.level>=e.level.log&&this.#e?.log(...this.#t(this,{level:e.level.log,content:t})),this}debug(...t){return this.level>=e.level.debug&&this.#e?.debug(...this.#t(this,{level:e.level.debug,content:t})),this}with(t={}){return new e({level:this.level,format:this.#t,output:this.#e,options:{...this.options},tags:{...this.tags,...t}})}#n(e=3){const t=Error,n=t.prepareStackTrace;t.prepareStackTrace=(e,t)=>t;const{stack:o}=new Error;t.prepareStackTrace=n;const r=o[e];return{file:r.getFileName(),name:r.getFunctionName(),line:r.getLineNumber(),column:r.getColumnNumber()}}static level=Object.freeze({disabled:NaN,error:0,warn:1,info:2,log:3,debug:4});static format={text(t,{level:n=0,content:o}){const r=["red","orange","cyan","white","gray"][n],s=[`%c ${Object.keys(e.level).find((t=>e.level[t]===n)).toLocaleUpperCase().padEnd(5)} │%c`],i=[`color: black; background-color: ${r}`,""];if(t.options.date||t.options.time||t.options.delta){const e=(new Date).toISOString(),n=[];if(t.options.delta){const e=performance.now()/1e3;let t=e.toPrecision(4);e<1&&(t=e.toPrecision(2)),n.push(`+${t}`)}t.options.date&&t.options.time?n.push(e):t.options.date?n.push(e.slice(0,e.indexOf("T"))):t.options.time&&n.push(e.slice(e.indexOf("T")+1,-1)),s.push(`%c ${n.join(" ¦ ").trim()} %c`),i.push(`color: black; background-color: ${r}`,"")}if(t.options.caller){const e=t.#n();if(e){const n=[];t.options.caller.file&&n.push(`${e.file.replace(t.options.caller.fileformat,"$<file>")}`),t.options.caller.name&&e.name&&n.push(e.name),t.options.caller.line&&n.push(e.line,e.column),s.push(`%c ${n.join(":").trim()} %c`),i.push("color: black; background-color: gray","")}}{const n=[];for(const[o,r]of Object.entries(t.tags))n.push(`${o}:${e.inspect(r)}`);s.push(`%c ${n.join(" ").trim()} %c`),i.push("background-color: black","")}return[s.join(""),...i,...o.map(e.inspect)]},json(t,{level:n=0,content:o}){const r={level:Object.keys(e.level).find((t=>e.level[t]===n)),timestamp:Date.now(),tags:t.tags,content:o},s={};if(t.options.date||t.options.time){const e=new Date(r.timestamp).toISOString();t.options.date&&(s.date=e.slice(0,e.indexOf("T"))),t.options.time&&(s.time=e.slice(e.indexOf("T")+1,-1))}if(t.options.delta&&(s.delta=performance.now()/1e3),t.options.caller){const e=t.#n();e&&(s.caller={},t.options.caller.file&&(s.caller.file=`${e.file.replace(t.options.caller.fileformat,"$<file>")}`),t.options.caller.name&&e.name&&(s.caller.name=e.name),t.options.caller.line&&(s.caller.line=[e.line,e.column]))}return[JSON.stringify({...r,...s})]}};static inspect(e){return globalThis.Deno?.inspect(e,{colors:!0,depth:1/0})??e}},t=class extends Error{constructor(e){super(e),this.name="AssertionError"}};TransformStream;function n(e){let t=0;for(const n of e)t+=n.length;const n=new Uint8Array(t);let o=0;for(const t of e)n.set(t,o),o+=t.length;return n}function o(e){const t=e.length,n=new Uint8Array(t);n[0]=0;let o=0,r=1;for(;r<t;)e[r]===e[o]?(o++,n[r]=o,r++):0===o?(n[r]=0,r++):o=n[o-1];return n}TransformStream,TransformStream,TransformStream,TransformStream;var r=class extends TransformStream{#o="";constructor(e={allowCR:!1}){super({transform:(t,n)=>{for(t=this.#o+t;;){const o=t.indexOf("\n"),r=e.allowCR?t.indexOf("\r"):-1;if(-1!==r&&r!==t.length-1&&(-1===o||o-1>r)){n.enqueue(t.slice(0,r)),t=t.slice(r+1);continue}if(-1===o)break;const s="\r"===t[o-1]?o-1:o;n.enqueue(t.slice(0,s)),t=t.slice(o+1)}this.#o=t},flush:t=>{if(""===this.#o)return;const n=e.allowCR&&this.#o.endsWith("\r")?this.#o.slice(0,-1):this.#o;t.enqueue(n)}})}};new TextDecoder;var s=new TextEncoder,i=new TextDecoder;function l(t,n,{log:o=new e,stdin:l=null,stdout:a="debug",stderr:u="error",env:d,cwd:p,raw:h,callback:f,buffering:m,sync:w,throw:g,dryrun:b,winext:v="",os:T=Deno.build.os}={}){"windows"===T&&(t=`${t}${v}`),o=o.with({bin:t}),f&&"piped"!==c(l)&&(l="piped");const $=new Deno.Command(t,{args:n,stdin:w?"null":c(l),stdout:c(a),stderr:c(u),env:d,cwd:p,windowsRawArguments:h});if(b){o.debug(`dryrun: ${t} not executed`);const e={success:!0,code:0,stdio:[],stdin:"",stdout:"",stderr:""};return w?e:Promise.resolve(e)}return w?function(e,{bin:t,log:n,throw:o,stdout:r,stderr:s}){const l=Date.now(),a=e.outputSync(),{success:u,code:d}=a,p=Date.now()-l,h={get stdio(){return[[p,1,this.stdout],[p,2,this.stderr]]},stdin:"",stdout:"piped"===c(r)?i.decode(a.stdout):"",stderr:"piped"===c(s)?i.decode(a.stderr):""};for(const{channel:e,mode:t}of[{channel:"stdout",mode:r},{channel:"stderr",mode:s}])"piped"===c(t)&&h[e]&&n.with({t:p,channel:e})[t]?.(h[e]);if(!u&&o)throw new EvalError(`${t} exited with non-zero code ${d}:\n${h.stdout}\n${h.stderr}`);return{success:u,code:d,...h}}($,{bin:t,log:o,throw:g,stdout:a,stderr:u}):async function(e,{bin:t,log:n,callback:o=(({close:e})=>e?.()),buffering:i=250,throw:l,...a}){const u=e.spawn(),d=Date.now(),p={stdio:[],get stdin(){return this.stdio.filter((([e,t])=>0===t)).map((([e,t,n])=>n)).join("\n")},get stdout(){return this.stdio.filter((([e,t])=>1===t)).map((([e,t,n])=>n)).join("\n")},get stderr(){return this.stdio.filter((([e,t])=>2===t)).map((([e,t,n])=>n)).join("\n")}},h={};let f="";const m=function(e,t){let n=null,o=null;const r=(...s)=>{r.clear(),o=()=>{r.clear(),e.call(r,...s)},n=setTimeout(o,t)};return r.clear=()=>{"number"==typeof n&&(clearTimeout(n),n=null,o=null)},r.flush=()=>{o?.()},Object.defineProperty(r,"pending",{get:()=>"number"==typeof n}),r}((async e=>{n.with({t:e}).debug("debounced"),f="",await o({stdio:p,i:p.stdin.length,...h})}),i);if("piped"===c(a.stdin)){const e=u.stdin.getWriter();Object.assign(h,{async write(t,o=!0){const r=Date.now()-d;a.stdin&&n.with({t:r,channel:"stdin"})[a.stdin]?.(t),p.stdio.push([r,0,t]),o&&!t.endsWith("\n")&&(t+="\n"),await e.write(s.encode(t)),f="stdin",e.releaseLock()},async close(){try{e.releaseLock(),await u.stdin.close(),n.with({t:Date.now()-d,closed:"stdin"}).debug()}catch{}},async wait(e=1e3){const t=Date.now()-d;n.with({t:t,waiting:e}).debug(),await function(e,t={}){const{signal:n,persistent:o=!0}=t;return n?.aborted?Promise.reject(n.reason):new Promise(((t,r)=>{const s=()=>{clearTimeout(i),r(n?.reason)},i=setTimeout((()=>{n?.removeEventListener("abort",s),t()}),e);if(n?.addEventListener("abort",s,{once:!0}),!1===o)try{Deno.unrefTimer(i)}catch(e){if(!(e instanceof ReferenceError))throw e;console.error("`persistent` option is only available in Deno")}}))}(e),m(t)}}),m(Date.now()-d)}await Promise.all(["stdout","stderr"].filter((e=>"piped"===c(a[e]))).map((async e=>{for await(const t of u[e].pipeThrough(new TextDecoderStream).pipeThrough(new r)){const o=Date.now()-d,r={stdout:1,stderr:2}[e];if(a[e]&&n.with({t:o,channel:e})[a[e]]?.(t),p.stdio.length&&f===e){const e=p.stdio.at(-1);e[1]===r&&(e[2]+=`\n${t}`)}else p.stdio.push([o,r,t]);f=e,m(o)}}))),m.flush();const{success:w,code:g}=await u.status;if(!w&&l)throw new EvalError(`${t} exited with non-zero code ${g}:\n${p.stdout}\n${p.stderr}`);return{success:w,code:g,...p}}($,{bin:t,log:o,callback:f,buffering:m,throw:g,stdin:"piped"===c(l)?l:null,stdout:"piped"===c(a)?a:null,stderr:"piped"===c(u)?u:null})}function c(e){return["inherit","null"].includes(`${e}`)?`${e}`:"piped"}export{l as command};
|
|
2
2
|
/**
|
|
3
3
|
* Logger library
|
|
4
4
|
*
|
package/command.ts
CHANGED
|
@@ -45,6 +45,11 @@ export type options = {
|
|
|
45
45
|
os?: typeof Deno.build.os
|
|
46
46
|
/** Throw an error if exit code is non-zero rather than returning a result. */
|
|
47
47
|
throw?: boolean
|
|
48
|
+
/**
|
|
49
|
+
* Do not actually execute the command.
|
|
50
|
+
* In this case you will instead receive an empty successful result object.
|
|
51
|
+
*/
|
|
52
|
+
dryrun?: boolean
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
/** Run result. */
|
|
@@ -131,7 +136,7 @@ export function command(bin: string, args: string[], options?: options & { sync:
|
|
|
131
136
|
* command("deno", ["eval", "Deno.exit(1)"], { throw: true, sync: true })
|
|
132
137
|
* ```
|
|
133
138
|
*/
|
|
134
|
-
export function command(bin: string, args: string[], { log = new Logger(), stdin = null, stdout = "debug", stderr = "error", env, cwd, raw, callback, buffering, sync, throw: _throw, winext = "", os = Deno.build.os } = {} as options): Promisable<result> {
|
|
139
|
+
export function command(bin: string, args: string[], { log = new Logger(), stdin = null, stdout = "debug", stderr = "error", env, cwd, raw, callback, buffering, sync, throw: _throw, dryrun, winext = "", os = Deno.build.os } = {} as options): Promisable<result> {
|
|
135
140
|
if (os === "windows") {
|
|
136
141
|
bin = `${bin}${winext}`
|
|
137
142
|
}
|
|
@@ -140,6 +145,11 @@ export function command(bin: string, args: string[], { log = new Logger(), stdin
|
|
|
140
145
|
stdin = "piped"
|
|
141
146
|
}
|
|
142
147
|
const command = new Deno.Command(bin, { args, stdin: !sync ? handle(stdin) : "null", stdout: handle(stdout), stderr: handle(stderr), env, cwd, windowsRawArguments: raw })
|
|
148
|
+
if (dryrun) {
|
|
149
|
+
log.debug(`dryrun: ${bin} not executed`)
|
|
150
|
+
const result = { success: true, code: 0, stdio: [], stdin: "", stdout: "", stderr: "" }
|
|
151
|
+
return sync ? result : Promise.resolve(result)
|
|
152
|
+
}
|
|
143
153
|
if (sync) {
|
|
144
154
|
return exec(command, { bin, log, throw: _throw, stdout, stderr })
|
|
145
155
|
}
|
package/command_test.ts
CHANGED
|
@@ -116,3 +116,8 @@ test("deno")("command() throws an error when `throw` option is enabled and exit
|
|
|
116
116
|
expect(() => command("deno", ["eval", "Deno.exit(1)"], { env: { NO_COLOR: "true" }, throw: true, sync: true })).toThrow(EvalError)
|
|
117
117
|
await expect(command("deno", ["eval", "Deno.exit(1)"], { env: { NO_COLOR: "true" }, throw: true })).rejects.toThrow(EvalError)
|
|
118
118
|
}, { permissions: { run: ["deno"] } })
|
|
119
|
+
|
|
120
|
+
test("deno")("command() does nothing in dryrun", async () => {
|
|
121
|
+
expect(command("deno", ["--version"], { dryrun: true, sync: true })).toMatchObject({ success: true, code: 0, stdio: [], stdin: "", stderr: "", stdout: "" })
|
|
122
|
+
await expect(command("deno", ["--version"], { dryrun: true })).resolves.toMatchObject({ success: true, code: 0, stdio: [], stdin: "", stderr: "", stdout: "" })
|
|
123
|
+
}, { permissions: { run: ["deno"] } })
|
package/deno.jsonc
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"icon": "⏯️",
|
|
3
3
|
"name": "@libs/run",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"description": "Utilities to run subprocess.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"subprocess",
|
|
8
8
|
"esm"
|
|
9
9
|
],
|
|
10
|
-
"license": "MIT
|
|
10
|
+
"license": "MIT",
|
|
11
11
|
"author": "lowlighter (Simon Lecoq)",
|
|
12
12
|
"funding": "https://github.com/sponsors/lowlighter",
|
|
13
13
|
"homepage": "https://github.com/lowlighter/libs",
|
package/mod.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var e=class e{constructor({level:t,format:n,output:o,tags:r,options:s}={}){if("granted"===globalThis.Deno?.permissions.querySync?.({name:"env",variable:"LOG_LEVEL"}).state){const n=globalThis.Deno?.env.get("LOG_LEVEL")??"";n in e.level&&(t??=e.level[n]),Number.isNaN(Number.parseInt(n))||(t??=Number.parseInt(n))}this.level=t??e.level.log,this.#e=o||null===o?o:console,this.#t=n??e.format.text,this.tags=r??{},this.options={date:!1,time:!1,delta:!0,...s,caller:!1!==s?.caller&&{file:!1,name:!1,line:!1,...s?.caller}}}level;#t;#e;tags;options;error(...t){return this.level>=e.level.error&&this.#e?.error(...this.#t(this,{level:e.level.error,content:t})),this}warn(...t){return this.level>=e.level.warn&&this.#e?.warn(...this.#t(this,{level:e.level.warn,content:t})),this}info(...t){return this.level>=e.level.info&&this.#e?.info(...this.#t(this,{level:e.level.info,content:t})),this}log(...t){return this.level>=e.level.log&&this.#e?.log(...this.#t(this,{level:e.level.log,content:t})),this}debug(...t){return this.level>=e.level.debug&&this.#e?.debug(...this.#t(this,{level:e.level.debug,content:t})),this}with(t={}){return new e({level:this.level,format:this.#t,output:this.#e,options:{...this.options},tags:{...this.tags,...t}})}#n(e=3){const t=Error,n=t.prepareStackTrace;t.prepareStackTrace=(e,t)=>t;const{stack:o}=new Error;t.prepareStackTrace=n;const r=o[e];return{file:r.getFileName(),name:r.getFunctionName(),line:r.getLineNumber(),column:r.getColumnNumber()}}static level=Object.freeze({disabled:NaN,error:0,warn:1,info:2,log:3,debug:4});static format={text(t,{level:n=0,content:o}){const r=["red","orange","cyan","white","gray"][n],s=[`%c ${Object.keys(e.level).find((t=>e.level[t]===n)).toLocaleUpperCase().padEnd(5)} │%c`],i=[`color: black; background-color: ${r}`,""];if(t.options.date||t.options.time||t.options.delta){const e=(new Date).toISOString(),n=[];if(t.options.delta){const e=performance.now()/1e3;let t=e.toPrecision(4);e<1&&(t=e.toPrecision(2)),n.push(`+${t}`)}t.options.date&&t.options.time?n.push(e):t.options.date?n.push(e.slice(0,e.indexOf("T"))):t.options.time&&n.push(e.slice(e.indexOf("T")+1,-1)),s.push(`%c ${n.join(" ¦ ").trim()} %c`),i.push(`color: black; background-color: ${r}`,"")}if(t.options.caller){const e=t.#n();if(e){const n=[];t.options.caller.file&&n.push(`${e.file.replace(t.options.caller.fileformat,"$<file>")}`),t.options.caller.name&&e.name&&n.push(e.name),t.options.caller.line&&n.push(e.line,e.column),s.push(`%c ${n.join(":").trim()} %c`),i.push("color: black; background-color: gray","")}}{const n=[];for(const[o,r]of Object.entries(t.tags))n.push(`${o}:${e.inspect(r)}`);s.push(`%c ${n.join(" ").trim()} %c`),i.push("background-color: black","")}return[s.join(""),...i,...o.map(e.inspect)]},json(t,{level:n=0,content:o}){const r={level:Object.keys(e.level).find((t=>e.level[t]===n)),timestamp:Date.now(),tags:t.tags,content:o},s={};if(t.options.date||t.options.time){const e=new Date(r.timestamp).toISOString();t.options.date&&(s.date=e.slice(0,e.indexOf("T"))),t.options.time&&(s.time=e.slice(e.indexOf("T")+1,-1))}if(t.options.delta&&(s.delta=performance.now()/1e3),t.options.caller){const e=t.#n();e&&(s.caller={},t.options.caller.file&&(s.caller.file=`${e.file.replace(t.options.caller.fileformat,"$<file>")}`),t.options.caller.name&&e.name&&(s.caller.name=e.name),t.options.caller.line&&(s.caller.line=[e.line,e.column]))}return[JSON.stringify({...r,...s})]}};static inspect(e){return globalThis.Deno?.inspect(e,{colors:!0,depth:1/0})??e}},t=class extends Error{constructor(e){super(e),this.name="AssertionError"}};TransformStream;function n(e){let t=0;for(const n of e)t+=n.length;const n=new Uint8Array(t);let o=0;for(const t of e)n.set(t,o),o+=t.length;return n}function o(e){const t=e.length,n=new Uint8Array(t);n[0]=0;let o=0,r=1;for(;r<t;)e[r]===e[o]?(o++,n[r]=o,r++):0===o?(n[r]=0,r++):o=n[o-1];return n}TransformStream,TransformStream,TransformStream,TransformStream;var r=class extends TransformStream{#o="";constructor(e={allowCR:!1}){super({transform:(t,n)=>{for(t=this.#o+t;;){const o=t.indexOf("\n"),r=e.allowCR?t.indexOf("\r"):-1;if(-1!==r&&r!==t.length-1&&(-1===o||o-1>r)){n.enqueue(t.slice(0,r)),t=t.slice(r+1);continue}if(-1===o)break;const s="\r"===t[o-1]?o-1:o;n.enqueue(t.slice(0,s)),t=t.slice(o+1)}this.#o=t},flush:t=>{if(""===this.#o)return;const n=e.allowCR&&this.#o.endsWith("\r")?this.#o.slice(0,-1):this.#o;t.enqueue(n)}})}};new TextDecoder;var s=new TextEncoder,i=new TextDecoder;function l(t,n,{log:o=new e,stdin:l=null,stdout:
|
|
1
|
+
var e=class e{constructor({level:t,format:n,output:o,tags:r,options:s}={}){if("granted"===globalThis.Deno?.permissions.querySync?.({name:"env",variable:"LOG_LEVEL"}).state){const n=globalThis.Deno?.env.get("LOG_LEVEL")??"";n in e.level&&(t??=e.level[n]),Number.isNaN(Number.parseInt(n))||(t??=Number.parseInt(n))}this.level=t??e.level.log,this.#e=o||null===o?o:console,this.#t=n??e.format.text,this.tags=r??{},this.options={date:!1,time:!1,delta:!0,...s,caller:!1!==s?.caller&&{file:!1,name:!1,line:!1,...s?.caller}}}level;#t;#e;tags;options;error(...t){return this.level>=e.level.error&&this.#e?.error(...this.#t(this,{level:e.level.error,content:t})),this}warn(...t){return this.level>=e.level.warn&&this.#e?.warn(...this.#t(this,{level:e.level.warn,content:t})),this}info(...t){return this.level>=e.level.info&&this.#e?.info(...this.#t(this,{level:e.level.info,content:t})),this}log(...t){return this.level>=e.level.log&&this.#e?.log(...this.#t(this,{level:e.level.log,content:t})),this}debug(...t){return this.level>=e.level.debug&&this.#e?.debug(...this.#t(this,{level:e.level.debug,content:t})),this}with(t={}){return new e({level:this.level,format:this.#t,output:this.#e,options:{...this.options},tags:{...this.tags,...t}})}#n(e=3){const t=Error,n=t.prepareStackTrace;t.prepareStackTrace=(e,t)=>t;const{stack:o}=new Error;t.prepareStackTrace=n;const r=o[e];return{file:r.getFileName(),name:r.getFunctionName(),line:r.getLineNumber(),column:r.getColumnNumber()}}static level=Object.freeze({disabled:NaN,error:0,warn:1,info:2,log:3,debug:4});static format={text(t,{level:n=0,content:o}){const r=["red","orange","cyan","white","gray"][n],s=[`%c ${Object.keys(e.level).find((t=>e.level[t]===n)).toLocaleUpperCase().padEnd(5)} │%c`],i=[`color: black; background-color: ${r}`,""];if(t.options.date||t.options.time||t.options.delta){const e=(new Date).toISOString(),n=[];if(t.options.delta){const e=performance.now()/1e3;let t=e.toPrecision(4);e<1&&(t=e.toPrecision(2)),n.push(`+${t}`)}t.options.date&&t.options.time?n.push(e):t.options.date?n.push(e.slice(0,e.indexOf("T"))):t.options.time&&n.push(e.slice(e.indexOf("T")+1,-1)),s.push(`%c ${n.join(" ¦ ").trim()} %c`),i.push(`color: black; background-color: ${r}`,"")}if(t.options.caller){const e=t.#n();if(e){const n=[];t.options.caller.file&&n.push(`${e.file.replace(t.options.caller.fileformat,"$<file>")}`),t.options.caller.name&&e.name&&n.push(e.name),t.options.caller.line&&n.push(e.line,e.column),s.push(`%c ${n.join(":").trim()} %c`),i.push("color: black; background-color: gray","")}}{const n=[];for(const[o,r]of Object.entries(t.tags))n.push(`${o}:${e.inspect(r)}`);s.push(`%c ${n.join(" ").trim()} %c`),i.push("background-color: black","")}return[s.join(""),...i,...o.map(e.inspect)]},json(t,{level:n=0,content:o}){const r={level:Object.keys(e.level).find((t=>e.level[t]===n)),timestamp:Date.now(),tags:t.tags,content:o},s={};if(t.options.date||t.options.time){const e=new Date(r.timestamp).toISOString();t.options.date&&(s.date=e.slice(0,e.indexOf("T"))),t.options.time&&(s.time=e.slice(e.indexOf("T")+1,-1))}if(t.options.delta&&(s.delta=performance.now()/1e3),t.options.caller){const e=t.#n();e&&(s.caller={},t.options.caller.file&&(s.caller.file=`${e.file.replace(t.options.caller.fileformat,"$<file>")}`),t.options.caller.name&&e.name&&(s.caller.name=e.name),t.options.caller.line&&(s.caller.line=[e.line,e.column]))}return[JSON.stringify({...r,...s})]}};static inspect(e){return globalThis.Deno?.inspect(e,{colors:!0,depth:1/0})??e}},t=class extends Error{constructor(e){super(e),this.name="AssertionError"}};TransformStream;function n(e){let t=0;for(const n of e)t+=n.length;const n=new Uint8Array(t);let o=0;for(const t of e)n.set(t,o),o+=t.length;return n}function o(e){const t=e.length,n=new Uint8Array(t);n[0]=0;let o=0,r=1;for(;r<t;)e[r]===e[o]?(o++,n[r]=o,r++):0===o?(n[r]=0,r++):o=n[o-1];return n}TransformStream,TransformStream,TransformStream,TransformStream;var r=class extends TransformStream{#o="";constructor(e={allowCR:!1}){super({transform:(t,n)=>{for(t=this.#o+t;;){const o=t.indexOf("\n"),r=e.allowCR?t.indexOf("\r"):-1;if(-1!==r&&r!==t.length-1&&(-1===o||o-1>r)){n.enqueue(t.slice(0,r)),t=t.slice(r+1);continue}if(-1===o)break;const s="\r"===t[o-1]?o-1:o;n.enqueue(t.slice(0,s)),t=t.slice(o+1)}this.#o=t},flush:t=>{if(""===this.#o)return;const n=e.allowCR&&this.#o.endsWith("\r")?this.#o.slice(0,-1):this.#o;t.enqueue(n)}})}};new TextDecoder;var s=new TextEncoder,i=new TextDecoder;function l(t,n,{log:o=new e,stdin:l=null,stdout:a="debug",stderr:u="error",env:d,cwd:p,raw:h,callback:f,buffering:m,sync:w,throw:g,dryrun:b,winext:v="",os:T=Deno.build.os}={}){"windows"===T&&(t=`${t}${v}`),o=o.with({bin:t}),f&&"piped"!==c(l)&&(l="piped");const $=new Deno.Command(t,{args:n,stdin:w?"null":c(l),stdout:c(a),stderr:c(u),env:d,cwd:p,windowsRawArguments:h});if(b){o.debug(`dryrun: ${t} not executed`);const e={success:!0,code:0,stdio:[],stdin:"",stdout:"",stderr:""};return w?e:Promise.resolve(e)}return w?function(e,{bin:t,log:n,throw:o,stdout:r,stderr:s}){const l=Date.now(),a=e.outputSync(),{success:u,code:d}=a,p=Date.now()-l,h={get stdio(){return[[p,1,this.stdout],[p,2,this.stderr]]},stdin:"",stdout:"piped"===c(r)?i.decode(a.stdout):"",stderr:"piped"===c(s)?i.decode(a.stderr):""};for(const{channel:e,mode:t}of[{channel:"stdout",mode:r},{channel:"stderr",mode:s}])"piped"===c(t)&&h[e]&&n.with({t:p,channel:e})[t]?.(h[e]);if(!u&&o)throw new EvalError(`${t} exited with non-zero code ${d}:\n${h.stdout}\n${h.stderr}`);return{success:u,code:d,...h}}($,{bin:t,log:o,throw:g,stdout:a,stderr:u}):async function(e,{bin:t,log:n,callback:o=(({close:e})=>e?.()),buffering:i=250,throw:l,...a}){const u=e.spawn(),d=Date.now(),p={stdio:[],get stdin(){return this.stdio.filter((([e,t])=>0===t)).map((([e,t,n])=>n)).join("\n")},get stdout(){return this.stdio.filter((([e,t])=>1===t)).map((([e,t,n])=>n)).join("\n")},get stderr(){return this.stdio.filter((([e,t])=>2===t)).map((([e,t,n])=>n)).join("\n")}},h={};let f="";const m=function(e,t){let n=null,o=null;const r=(...s)=>{r.clear(),o=()=>{r.clear(),e.call(r,...s)},n=setTimeout(o,t)};return r.clear=()=>{"number"==typeof n&&(clearTimeout(n),n=null,o=null)},r.flush=()=>{o?.()},Object.defineProperty(r,"pending",{get:()=>"number"==typeof n}),r}((async e=>{n.with({t:e}).debug("debounced"),f="",await o({stdio:p,i:p.stdin.length,...h})}),i);if("piped"===c(a.stdin)){const e=u.stdin.getWriter();Object.assign(h,{async write(t,o=!0){const r=Date.now()-d;a.stdin&&n.with({t:r,channel:"stdin"})[a.stdin]?.(t),p.stdio.push([r,0,t]),o&&!t.endsWith("\n")&&(t+="\n"),await e.write(s.encode(t)),f="stdin",e.releaseLock()},async close(){try{e.releaseLock(),await u.stdin.close(),n.with({t:Date.now()-d,closed:"stdin"}).debug()}catch{}},async wait(e=1e3){const t=Date.now()-d;n.with({t:t,waiting:e}).debug(),await function(e,t={}){const{signal:n,persistent:o=!0}=t;return n?.aborted?Promise.reject(n.reason):new Promise(((t,r)=>{const s=()=>{clearTimeout(i),r(n?.reason)},i=setTimeout((()=>{n?.removeEventListener("abort",s),t()}),e);if(n?.addEventListener("abort",s,{once:!0}),!1===o)try{Deno.unrefTimer(i)}catch(e){if(!(e instanceof ReferenceError))throw e;console.error("`persistent` option is only available in Deno")}}))}(e),m(t)}}),m(Date.now()-d)}await Promise.all(["stdout","stderr"].filter((e=>"piped"===c(a[e]))).map((async e=>{for await(const t of u[e].pipeThrough(new TextDecoderStream).pipeThrough(new r)){const o=Date.now()-d,r={stdout:1,stderr:2}[e];if(a[e]&&n.with({t:o,channel:e})[a[e]]?.(t),p.stdio.length&&f===e){const e=p.stdio.at(-1);e[1]===r&&(e[2]+=`\n${t}`)}else p.stdio.push([o,r,t]);f=e,m(o)}}))),m.flush();const{success:w,code:g}=await u.status;if(!w&&l)throw new EvalError(`${t} exited with non-zero code ${g}:\n${p.stdout}\n${p.stderr}`);return{success:w,code:g,...p}}($,{bin:t,log:o,callback:f,buffering:m,throw:g,stdin:"piped"===c(l)?l:null,stdout:"piped"===c(a)?a:null,stderr:"piped"===c(u)?u:null})}function c(e){return["inherit","null"].includes(`${e}`)?`${e}`:"piped"}export{l as command};
|
|
2
2
|
/**
|
|
3
3
|
* Logger library
|
|
4
4
|
*
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowlighter/run",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {},
|
|
6
|
+
"dependencies": {},
|
|
7
|
+
"devDependencies": {},
|
|
6
8
|
"description": "Utilities to run subprocess.",
|
|
7
9
|
"keywords": [
|
|
8
10
|
"subprocess",
|
|
9
11
|
"esm"
|
|
10
12
|
],
|
|
11
|
-
"license": "MIT
|
|
13
|
+
"license": "MIT",
|
|
12
14
|
"author": "lowlighter (Simon Lecoq)",
|
|
13
15
|
"homepage": "https://github.com/lowlighter/libs",
|
|
14
16
|
"repository": {
|