From 4508599aa743387004ce87cab5a14be41a1ff3c0 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 5 Aug 2021 16:22:27 +0800 Subject: [PATCH] feat: provide pure module version --- README.md | 22 +++++++++++++++++++++- package.json | 12 +++++++++++- src/console.ts | 11 +++++++++++ src/index.ts | 6 +++--- src/types.ts | 10 +--------- 5 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 src/console.ts diff --git a/README.md b/README.md index 1edd103..a591158 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ npm i -D log-editor Add the following statement at the very beginning of your script: ```ts -import 'log-editor' +// inject to `console` +import 'log-editor/console' ``` Then use `console.logEditor` instead of `console.log` whenever you want to see the result in editor. It will launch the corresponsing editor powered by [`launch_editor`](https://github.com/yyx990803/launch-editor). @@ -29,6 +30,12 @@ Then use `console.logEditor` instead of `console.log` whenever you want to see t console.logEditor(largeObject) ``` +or directly import without injection + +```ts +import { logEditor } from 'log-editor' +``` + ## Options ### Named log @@ -46,6 +53,19 @@ console.logEditor('message 1', 'key', { override: false }) console.logEditor('message 2', 'key', { override: false }) ``` +### File Extension + +By default, `log-editor` will use `log` or `json` as the temp file's extension. You can change it by passing `extension` in the options so your editor could provide proper syntax hightlight for you. + +```ts +const code = `import 'log-editor'` + +console.logEditor(code, 'code', { extension: 'ts' }) + +console.logEditor({ foo: 'bar' }) // will auto infer to use `json` as extension +console.logEditor('bar') // will use `log` as extension +``` + ## Sponsors

diff --git a/package.json b/package.json index a92391c..0affcaa 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,16 @@ "main": "dist/index.js", "module": "dist/index.mjs", "types": "dist/index.d.ts", + "exports": { + ".": { + "require": "./dist/index.js", + "import": "./dist/index.mjs" + }, + "./console": { + "require": "./dist/console.js", + "import": "./dist/console.mjs" + } + }, "funding": "https://github.com/sponsors/antfu", "author": "Anthony Fu ", "license": "MIT", @@ -24,7 +34,7 @@ "prepublishOnly": "nr build", "dev": "nr build --watch", "start": "esno src/index.ts", - "build": "tsup src/index.ts --format cjs,esm --dts", + "build": "tsup src/index.ts src/console.ts --format cjs,esm --dts --no-splitting", "release": "bumpp --commit --push --tag && pnpm publish", "lint": "eslint \"{src,test}/**/*.ts\"", "lint:fix": "nr lint -- --fix" diff --git a/src/console.ts b/src/console.ts new file mode 100644 index 0000000..f5c77d4 --- /dev/null +++ b/src/console.ts @@ -0,0 +1,11 @@ +import { Options } from './types' +import { logEditor } from './index' + +declare global { + interface Console { + logEditor(content: any, key?: string, options?: Options): Promise + } +} + +// side effects +console.logEditor = logEditor diff --git a/src/index.ts b/src/index.ts index b354d26..cbf2ac2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,14 +2,14 @@ import { promises as fs } from 'fs' import temp from 'tempy' // @ts-expect-error import launch from 'launch-editor' -import { LogFile } from './types' +import { Options, LogFile } from './types' export * from './types' const map = new Map() -console.logEditor = async(content, key, options = {}) => { +export async function logEditor(content: any, key?: string, options: Options = {}) { const override = options.override ?? true - const extension = options.extension ?? (typeof content === 'string' ? 'txt' : 'json') + const extension = options.extension ?? (typeof content === 'string' ? 'log' : 'json') const stringified = typeof content === 'string' ? content : JSON.stringify(content, null, 2) let file: LogFile | undefined diff --git a/src/types.ts b/src/types.ts index 450fe4a..ba593ef 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,15 +10,7 @@ export interface Options { */ override?: boolean /** - * File extension, default to `txt` or `json` based on the type of content provided + * File extension, default to `log` or `json` based on the type of content provided */ extension?: string } - -declare global { - interface Console { - logEditor(content: any, key?: string, options?: Options): Promise - } -} - -export {}