Skip to content

Commit

Permalink
feat: provide pure module version
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 5, 2021
1 parent 7971908 commit 4508599
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
Expand All @@ -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

<p align="center">
Expand Down
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"license": "MIT",
Expand All @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions src/console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Options } from './types'
import { logEditor } from './index'

declare global {
interface Console {
logEditor(content: any, key?: string, options?: Options): Promise<void>
}
}

// side effects
console.logEditor = logEditor
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, LogFile>()

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
Expand Down
10 changes: 1 addition & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>
}
}

export {}

0 comments on commit 4508599

Please sign in to comment.