Skip to content

Commit

Permalink
feat: support second object params
Browse files Browse the repository at this point in the history
  • Loading branch information
baozouai committed Jul 18, 2023
1 parent 74e7308 commit 37b7cb2
Show file tree
Hide file tree
Showing 66 changed files with 20,390 additions and 2,939 deletions.
8 changes: 8 additions & 0 deletions .codecov.yml
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
target: 95%
patch:
default:
target: 95%
3 changes: 3 additions & 0 deletions .eslintignore
@@ -0,0 +1,3 @@
*.md
test
vitest.config.ts
8 changes: 8 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,8 @@
module.exports = {
extends: '@antfu',
rules: {
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/prefer-ts-expect-error': 'off',
'no-console': 'off',
},
}
97 changes: 97 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,97 @@
name: CI

on:
push:
branches:
- master

pull_request:
branches:
- master

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install pnpm
uses: pnpm/action-setup@v2

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: pnpm

- name: Setup
run: npm i -g @antfu/ni

- name: Install
run: nci

- name: Lint
run: nr lint

typecheck:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install pnpm
uses: pnpm/action-setup@v2

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: pnpm

- name: Setup
run: npm i -g @antfu/ni

- name: Install
run: nci

test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
node: [16.x, 18.x]
os: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install pnpm
uses: pnpm/action-setup@v2

- name: Set node ${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: pnpm

- name: Setup
run: npm i -g @antfu/ni

- name: Install
run: nci

# - name: Test
# run: nr test --run --coverage

- name: Build
run: nr build

# - name: Upload coverage report to Codecov
# uses: codecov/codecov-action@v3
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
# files: coverage/lcov.info
# fail_ci_if_error: true
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
@@ -0,0 +1,42 @@
name: Release

permissions:
contents: write

on:
push:
tags:
- 'v*'

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install pnpm
uses: pnpm/action-setup@v2

- name: Set node
uses: actions/setup-node@v3
with:
node-version: 16.x
registry-url: 'https://registry.npmjs.org'

- name: ChangeLog
run: npx changelogithub
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

- name: Setup
run: npm i -g @antfu/ni

- name: Install
run: nci

- name: Release
run: nr release
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
18 changes: 10 additions & 8 deletions .gitignore
@@ -1,8 +1,10 @@
//.prettierignore
**/*.min.js
**/*.min.css

.idea/
node_modules/
dist/
.vscode
.DS_Store
.idea
.vscode
npm-debug.log
yarn-error.log
.stylelintcache
.eslintcache
node_modules
dist
coverage/
4 changes: 4 additions & 0 deletions .husky/commit-msg
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

lint-staged
5 changes: 0 additions & 5 deletions .npmignore

This file was deleted.

3 changes: 2 additions & 1 deletion .npmrc
@@ -1 +1,2 @@
registry = http://registry.npmjs.org
registry = http://registry.npmjs.org
auto-install-peers=false
1 change: 1 addition & 0 deletions .nvmrc
@@ -0,0 +1 @@
16.17
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 暴走老七
Copyright (c) 2023 暴走老七

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
153 changes: 153 additions & 0 deletions README-zh_CN.md
@@ -0,0 +1,153 @@

<p align="center">
<h1 align="center">taro-history</h1>
</p>

<div align="center">
一个用来为console.log添加代码所在文件名,所在行,log参数名以及添加分隔符的 vite 插件

[![NPM version][npm-image]][npm-url] ![NPM downloads][download-image]

![Test][test-badge]

<!-- ![codecov][codecov-badge] -->


[npm-image]: https://img.shields.io/npm/v/taro-history.svg?style=flat-square
[npm-url]: http://npmjs.org/package/taro-history


[download-image]: https://img.shields.io/npm/dm/taro-history.svg?style=flat-square



[test-badge]: https://github.com/baozouai/taro-history/actions/workflows/ci.yml/badge.svg

[codecov-badge]: https://codecov.io/github/baozouai/plugin-taro-history/branch/master/graph/badge.svg


</div>

中文 | [English](./README.md)

🔥 Features

- 支持打印文件名
- 支持打印所在行(加上endLine则支持结束行)
- 支持打印参数名称
- 支持分隔符来分开每个参数
- 支持不同的文件 —— 👉 .js、.jsx、.ts、.tsx、.vue、.svelte 和 .astro

> 更多用法,请看[示例](#-例子)
## 📦 安装

```sh
pnpm add taro-history -D
# or
yarn add taro-history -D
# or
npm i taro-history -D
```
## ⚙️ 参数

```ts
interface Options {
/**
* 打印文件名
* 如果你文件名太长,希望不显示文件path的目录,比如src/pages/xxx/yyy/a.tsx, 那么可以配置enableDir为false,则只打印a.tsx
*
* @default true
*/
enableFileName?: boolean | {
enableDir?: boolean
}
/**
* 打印的前缀提示,这样方便快速找到log 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
* @example
* console.log(' 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀', ...)
*/
preTip?: string
/** 每个参数分隔符,默认空字符串,你也可以使用换行符\n,分号;逗号,甚至猪猪🐖都行~ */
splitBy?: boolean
/**
* 是否需要endLine,默认false,如果为true,只有在开始和结束不相同才打印endLine
* @example
* console.log('line of 1 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 ~ main.tsx', ..., 'line of 10 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 ~ main.tsx')
* */
endLine?: boolean
}
```

## 🔨 使用

```ts
import { defineConfig } from 'vite'
import EnhanceLog from 'taro-history'

const config = defineConfig({
plugins: [
// 如果用vue, 请确保 vuePlugin 在 log plugin 之前
EnhanceLog({
splitBy: '\n',
preTip: '🐖🐖🐖🐖🐖🐖🐖🐖🐖🐖',
enableFileName: true, // or enableFileName: { enableDir: false}
}),
],
})

export default config

```

## 👇 例子

拉项目后通过运行启动playgrounds:
```shell
pnpm play # 对应vue
# 或者
pnpm play:react # 对应react
```

来启动项目

具体配置可以看 [vue/vite.config.ts](./playgrounds/vue/vite.config.ts) 或者 [react/vite.config.ts](./playgrounds/react/vite.config.ts)

比如说,你不喜欢小 🚀,你喜欢猪猪 🐖,那可以配置 preTip 为 🐖🐖🐖🐖🐖🐖🐖🐖🐖🐖:

![img](./assets/pig_pretip.png)

比如说,在参数较多的情况下,你希望 log 每个参数都换行,那可以配置 splitBy 为 `\n`

![img](./assets/linefeed.png)

或者分隔符是`;`:

![img](./assets/semicolon_delimiter.png)

当然,你也可以随意指定,比如用个狗头🐶来分隔:

![img](./assets/dog_delimiter.png)

比如说,你希望知道log所在的文件名,那么可以配置enableFileName为true(当然默认就是true):

![img](./assets/filename.png)

如果文件路径太长:
![img](./assets/deep_file.png)


你只希望打印文件名,不需要目录前缀,那么可以配置 `enableFileName: { enableDir: false }`:
![img](./assets/only_file_name.png)

又比如说,有个 log 跨了多行,你希望 log 开始和结束的行数,中间是 log 实体,那可以将 endLine 设置为 true:

![img](./assets/log_multi_line.png)

![img](./assets/log_multi_line_res.png)

> 我们可以看到开始的行数是29,结束的行数是44,跟源码一致
## 📄 协议

taro-history 遵循 [MIT 协议](./LICENSE).

0 comments on commit 37b7cb2

Please sign in to comment.