@clerc/logger 0.0.0 → 2.0.0-beta.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Ray <https://github.com/so1ve>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # @clerc/logger
2
+
3
+ Minimal CLI logger with right-aligned word labels, `node:util` `inspect`, Clack-powered `box` / `spinner` / `task`, and structured JSON output.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import { createLogger } from "@clerc/logger";
9
+
10
+ const log = createLogger({ level: "debug" });
11
+
12
+ log.child("db").step("Deploying to staging", "port=3000", "cache=true");
13
+
14
+ const s = log.spinner();
15
+ s.start("Checking remote state...");
16
+ s.stop("Remote state verified");
17
+
18
+ await log.task("Building bundle...", async (task) => {
19
+ task.message("Generating declarations...");
20
+
21
+ return "Bundle ready";
22
+ });
23
+
24
+ log.box.info(
25
+ "Dry run summary",
26
+ "No remote resources were mutated.",
27
+ `Run ${log.theme.accent("acme deploy --confirm")} to publish.`,
28
+ );
29
+ ```
@@ -0,0 +1,69 @@
1
+ import { Theme } from "@clerc/theme";
2
+ import { SpinnerOptions as SpinnerOptions$1 } from "@clack/prompts";
3
+ type LogLevel = "silent" | "error" | "warn" | "info" | "debug";
4
+ type LogFormat = "pretty" | "json";
5
+ interface LogEntry {
6
+ readonly line: (...values: unknown[]) => LogEntry;
7
+ }
8
+ type LogMethod<TReturn = void> = (message: unknown, ...details: unknown[]) => TReturn;
9
+ interface LogMethods<TReturn = LogEntry> {
10
+ step: LogMethod<TReturn>;
11
+ info: LogMethod<TReturn>;
12
+ success: LogMethod<TReturn>;
13
+ warn: LogMethod<TReturn>;
14
+ error: LogMethod<TReturn>;
15
+ debug: LogMethod<TReturn>;
16
+ }
17
+ interface SpinnerOptions {
18
+ readonly indicator?: SpinnerOptions$1["indicator"];
19
+ readonly cancelMessage?: string;
20
+ readonly errorMessage?: string;
21
+ readonly frames?: string[];
22
+ readonly delay?: number;
23
+ readonly signal?: AbortSignal;
24
+ }
25
+ interface SpinnerHandle {
26
+ start: (message?: string) => void;
27
+ message: (message: string) => void;
28
+ stop: (message?: string) => void;
29
+ error: (message?: string) => void;
30
+ cancel: (message?: string) => void;
31
+ clear: () => void;
32
+ readonly isCancelled: boolean;
33
+ }
34
+ type TaskSpinner = Omit<SpinnerHandle, "start">;
35
+ interface LoggerTerminal {
36
+ readonly stdout: (text: string) => void;
37
+ readonly stderr: (text: string) => void;
38
+ readonly isTTY?: boolean;
39
+ readonly color?: boolean;
40
+ readonly unicode?: boolean;
41
+ readonly columns?: number;
42
+ }
43
+ interface LoggerOptions {
44
+ level?: LogLevel;
45
+ format?: LogFormat;
46
+ scope?: string;
47
+ }
48
+ interface CreateLoggerOptions extends LoggerOptions {
49
+ readonly terminal?: LoggerTerminal;
50
+ readonly theme?: Theme;
51
+ readonly color?: boolean;
52
+ readonly unicode?: boolean;
53
+ }
54
+ interface Logger extends LogMethods {
55
+ level: LogLevel;
56
+ format: LogFormat;
57
+ readonly scope?: string;
58
+ readonly theme: Theme;
59
+ readonly box: LogMethods<void>;
60
+ raw: (text?: string) => void;
61
+ spinner: (options?: SpinnerOptions) => SpinnerHandle;
62
+ task: <T>(title: string, fn: (spinner: TaskSpinner) => T | Promise<T>, options?: SpinnerOptions) => Promise<T>;
63
+ child: (scope: string) => Logger;
64
+ }
65
+ export declare const LOG_LABEL_WIDTH = 5;
66
+ export declare const LOG_GUTTER: string;
67
+ export declare const formatLogLabel: (label: string, style?: (text: string) => string) => string;
68
+ export declare function createLogger(options?: CreateLoggerOptions): Logger;
69
+ export type { CreateLoggerOptions, LogEntry, LogFormat, LogLevel, LogMethod, LogMethods, Logger, LoggerOptions, LoggerTerminal, SpinnerHandle, SpinnerOptions, TaskSpinner };
package/dist/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ import{createTheme as e,detectColor as t,detectUnicode as n}from"@clerc/theme";import{inspect as r}from"node:util";import{createRequire as i}from"node:module";const a=5,o=` `.repeat(6),s={step:{level:`info`,stderr:!1,token:`brand`,label:`step`},info:{level:`info`,stderr:!1,token:`info`,label:`info`},success:{level:`info`,stderr:!1,token:`success`,label:`done`},warn:{level:`warn`,stderr:!0,token:`warn`,label:`warn`},error:{level:`error`,stderr:!0,token:`error`,label:`error`},debug:{level:`debug`,stderr:!1,token:`muted`,label:`debug`}},c=e=>e,l=(e,t=c)=>`${` `.repeat(Math.max(0,5-e.length))}${t(e)}`,u=(e,t,n,r)=>l(e,e=>r?t.bold(t[n](e)):t[n](e)),d=e=>JSON.stringify(e,(e,t)=>Error.isError(t)?{name:t.name,message:t.message,stack:t.stack,...t.cause===void 0?{}:{cause:t.cause}}:t),f=e=>e.message.length>0?`${e.name}: ${e.message}`:e.name;function p(e){if(!e.stack)return[];let t=f(e),n=(e.stack.startsWith(t)?e.stack.slice(t.length):e.stack).split(`
2
+ `).map(e=>e.trim()).filter(e=>e.startsWith(`at `)),r=n.filter(e=>!e.includes(`node:internal/`));return r.length>0?r:n}function m(e,t,n,i,a=/* @__PURE__ */ new Set){a.add(e);let o=[];i&&o.push(t(f(e)));for(let t of p(e))o.push(n(` ${t}`));return e.cause!==void 0&&!a.has(e.cause)&&(Error.isError(e.cause)?(o.push(t(`Caused by: ${f(e.cause)}`)),o.push(...m(e.cause,t,n,!1,a))):o.push(t(`Caused by: ${r(e.cause,{colors:!1,depth:2})}`))),o}function h(e,t,n,i,a){let o=t?a:n.muted;return typeof e==`string`?e.split(`
3
+ `).map(e=>o(e)):Error.isError(e)?m(e,o,a,!0):r(e,{colors:!t&&i,depth:4}).split(`
4
+ `).map(e=>o(e))}function g(e,t,n,i,a,o){let s=n.flatMap(t=>h(t,e,i,a,o));if(typeof t==`string`)return{headline:t,extraLines:s,jsonMessage:t};if(Error.isError(t)&&n.length===0)return{headline:t.message.length>0?t.message:t.name,extraLines:m(t,e?o:i.muted,o,t.name!==`Error`),jsonMessage:t};let c=r(t,{colors:!e&&a,depth:4}).split(`
5
+ `);return{headline:c[0]??``,extraLines:[...c.slice(1).map(t=>e?o(t):i.muted(t)),...s],jsonMessage:t}}let _;const v=()=>_??=i(import.meta.url)(`@clack/prompts`);function y(e){let{body:t,title:n,columns:r,unicode:i,faint:a}=e,s=[],c={columns:Math.max(40,r-o.length),write(e){let t=e.replace(/\n$/,``);return t.length>0&&s.push(...t.split(`
6
+ `)),!0}};return v().box(t,n,{output:c,withGuide:!1,rounded:i,width:`auto`,titlePadding:1,contentPadding:2,contentAlign:`left`,titleAlign:`left`,formatBorder:e=>a(i?e:e===`─`?`-`:e===`│`?`|`:`+`)}),s}function b(e){let{options:t,theme:n,unicode:r,noopEntry:i,shouldLogInfo:a,canAnimate:o,formatScope:s,emit:c}=e,l=!1,u=!1,d=i,f;function p(){f&&l&&f.clear(),l=!1,d=i}return{start(e=``){if(p(),u=!1,!a())l=!1,d=i;else if(l=!0,o()){let i=s();f??=v().spinner({...t,output:process.stdout,withGuide:!1,frames:t?.frames??(r?[`◒`,`◐`,`◓`,`◑`]:[`-`,`\\`,`|`,`/`]),styleFrame:e=>`${` `.repeat(Math.max(0,4-e.length))}${n.bold(n.brand(e))}`}),f.start(e.length>0?`${i}${e}`:i.trimEnd())}else f=void 0,d=e.length>0?c(`step`,e):i},message(e){l&&(f?f.message(`${s()}${e}`):e.length>0&&d.line(e))},stop(e){p(),e&&c(`success`,e)},error(e){p(),c(`error`,e??t?.errorMessage??`Failed`)},cancel(e){p(),u=!0,c(`warn`,e??t?.cancelMessage??`Cancelled`)},clear:p,get isCancelled(){return u||!!f?.isCancelled},get active(){return l}}}const x={silent:0,error:1,warn:2,info:3,debug:4},S=Object.freeze({line:()=>S}),C={stdout:e=>process.stdout.write(`${e}\n`),stderr:e=>process.stderr.write(`${e}\n`)},w=e=>({step:(t,...n)=>e(`step`,t,...n),info:(t,...n)=>e(`info`,t,...n),success:(t,...n)=>e(`success`,t,...n),warn:(t,...n)=>e(`warn`,t,...n),error:(t,...n)=>e(`error`,t,...n),debug:(t,...n)=>e(`debug`,t,...n)});function T(e,t){let{terminal:n,color:r,theme:i,unicode:a,state:c}=e,l=a?`│`:`|`,f=a?`›`:`>`,p=e=>i.dim(i.muted(e)),m=e=>x[c.level]>=x[e],_=`${o}${p(l)} `;function v(e){return t?e?`${p(`${t} ${f}`)} `:`${i.muted(t)} ${p(f)} `:``}function E(e,n,a,o){let s={line(...l){if(!m(e)||l.length===0)return s;if(c.format===`json`){for(let e of l)n(d({...o,...t?{scope:t}:{},line:e}));return s}for(let e of l)for(let t of h(e,a,i,r,p))n(`${_}${t}`);return s}};return s}function D(e,n,r,i){let a={level:n,message:r};t&&(a.scope=t),i.length===1?a.detail=i[0]:i.length>1&&(a.details=i),e(d(a))}function O(e,t,...a){let o=s[e];if(!m(o.level))return S;let l=e===`debug`,{headline:d,extraLines:f,jsonMessage:h}=g(l,t,a,i,r,p),y=o.stderr?n.stderr:n.stdout,b=E(o.level,y,l,{level:e,message:h});if(c.format===`json`)return D(y,e,h,a),b;y(`${u(o.label,i,o.token,!l)} ${v(l)}${l?p(d):e===`step`||e===`error`?i.bold(d):d}`);for(let e of f)y(`${_}${e}`);return b}function k(e,l,...d){let h=s[e];if(!m(h.level))return;let _=e===`debug`,{headline:v,extraLines:b,jsonMessage:x}=g(_,l,d,i,r,p),S=h.stderr?n.stderr:n.stdout;if(c.format===`json`){D(S,e,x,d);return}let C=b.length>0,w=C?b.join(`
7
+ `):(_?p:i.muted)(v),T=[];t&&T.push(_?p(`${t} ${f}`):`${i.muted(t)} ${p(f)}`),C&&v.length>0&&T.push(_?p(v):i.bold(v));let E=y({body:w,title:T.length>0?` ${T.join(` `)} `:void 0,columns:n.columns??(h.stderr?process.stderr.columns:process.stdout.columns)??80,unicode:a,faint:p}),O=u(h.label,i,h.token,!_);for(let[e,t]of E.entries())S(`${e===0?`${O} `:o}${t}`)}let A=e=>b({options:e,theme:i,unicode:a,noopEntry:S,shouldLogInfo:()=>m(`info`),canAnimate:()=>m(`info`)&&n===C&&!!process.stdout.isTTY&&c.format===`pretty`,formatScope:()=>v(!1),emit:O});return{get level(){return c.level},set level(e){c.level=e},get format(){return c.format},set format(e){c.format=e},scope:t,theme:i,...w(O),box:w(k),raw:(e=``)=>{c.level!==`silent`&&n.stdout(e)},spinner:e=>A(e),async task(e,t,n){let r=A(n);r.start(e);try{let n=await t(r);if(r.active){let t=typeof n==`string`&&n.length>0?n:e.replace(/\.+$/,``);r.stop(t)}return n}catch(e){throw r.active&&(n?.errorMessage===void 0?r.clear():r.error(n.errorMessage)),e}},child:n=>T(e,t?`${t}:${n}`:n)}}function E(r={}){let i=r.terminal??C,a=r.color??i.color??t();return T({terminal:i,color:a,theme:r.theme??e(a),unicode:r.unicode??i.unicode??n(),state:{level:r.level??`info`,format:r.format??`pretty`}},r.scope)}export{o as LOG_GUTTER,a as LOG_LABEL_WIDTH,E as createLogger,l as formatLogLabel};
@@ -0,0 +1 @@
1
+ export * from "@clack/prompts";
@@ -0,0 +1 @@
1
+ export*from"@clack/prompts";export{};
package/package.json CHANGED
@@ -1,4 +1,44 @@
1
1
  {
2
2
  "name": "@clerc/logger",
3
- "version": "0.0.0"
4
- }
3
+ "version": "2.0.0-beta.2",
4
+ "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
+ "type": "module",
6
+ "description": "Minimal CLI logger with aligned word labels.",
7
+ "keywords": [
8
+ "clerc",
9
+ "cli",
10
+ "log",
11
+ "logger",
12
+ "terminal"
13
+ ],
14
+ "homepage": "https://github.com/clercjs/clerc/tree/main/packages/logger#readme",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/clercjs/clerc.git",
18
+ "directory": "/"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/clercjs/clerc/issues"
22
+ },
23
+ "license": "MIT",
24
+ "sideEffects": false,
25
+ "exports": {
26
+ ".": "./dist/index.mjs",
27
+ "./prompts": "./dist/prompts.mjs",
28
+ "./package.json": "./package.json"
29
+ },
30
+ "types": "./dist/index.d.mts",
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "engines": {
35
+ "node": ">=24"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "dependencies": {
41
+ "@clack/prompts": "^1.9.0",
42
+ "@clerc/theme": "2.0.0-beta.2"
43
+ }
44
+ }