Skip to content

Commit

Permalink
feat: first version
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyao27 committed May 13, 2024
0 parents commit d56f583
Show file tree
Hide file tree
Showing 20 changed files with 491 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .commitlintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@commitlint/config-conventional"]
}
3 changes: 3 additions & 0 deletions .czrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"path": "cz-conventional-changelog"
}
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [xinyao27]
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Lint

on:
push:
branches:
- main

pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Install bun
uses: oven-sh/setup-bun@v1

- name: Install
run: bun install

- name: Lint
run: bun run lint
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Test

on:
push:
branches:
- main

pull_request:
branches:
- main

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: false

steps:
- uses: actions/checkout@v4

- name: Install bun
uses: oven-sh/setup-bun@v1

- name: Install
run: bun install

- name: Build
run: bun run build

- name: Test
run: bun run test
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# compiled output
dist
node_modules

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
coverage
/.nyc_output

# IDEs and editors
.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# config
.env
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
6 changes: 6 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"*.{js?(x),ts?(x),vue,html,md,json,yml}": [
"eslint --fix",
"git add"
]
}
11 changes: 11 additions & 0 deletions .release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"git": {
"commitMessage": "chore: release v${version}"
},
"plugins": {
"@release-it/conventional-changelog": {
"preset": "angular",
"infile": "CHANGELOG.md"
}
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 xinyao27 <https://github.com/xinyao27>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# [name]

[![NPM version](https://img.shields.io/npm/v/[name]?color=a1b858&label=)](https://www.npmjs.com/package/[name])

```ts
const a = 1
```

## License

[MIT](./LICENSE) License © 2022 [xinyao27](https://github.com/xinyao27)
17 changes: 17 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineBuildConfig } from 'unbuild'
import pkg from './package.json'

export default defineBuildConfig({
entries: ['src/index'],
declaration: true,
clean: true,
rollup: {
emitCJS: true,
esbuild: {
minify: true,
},
},
replace: {
'process.env.VERSION': JSON.stringify(pkg.version),
},
})
Binary file added bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { all } from '@xystack/style-guide/eslint'

export default all
78 changes: 78 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"name": "cli-high",
"version": "0.0.0",
"description": "tiny cli highlighter",
"type": "module",
"keywords": [
"cli",
"highlighter",
"highlight",
"color",
"terminal",
"console"
],
"license": "MIT",
"homepage": "https://github.com/xinyao27/cli-high#readme",
"bugs": {
"url": "https://github.com/xinyao27/cli-high/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/xinyao27/cli-high.git"
},
"author": {
"name": "xinyao",
"email": "[email protected]"
},
"funding": "https://github.com/sponsors/xinyao27",
"files": [
"dist/*",
"package.json"
],
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.mjs"
}
},
"sideEffects": false,
"scripts": {
"build": "unbuild",
"stub": "unbuild --stub",
"typecheck": "tsc --noEmit",
"lint": "nr typecheck && eslint . --fix",
"prepublishOnly": "nr build",
"release": "release-it",
"start": "bun run src/index.ts",
"test": "bun test",
"preinstall": "npx only-allow bun",
"up": "taze major -I",
"prepare": "husky install"
},
"dependencies": {
"picocolors": "^1.0.0",
"sugar-high": "^0.6.1"
},
"devDependencies": {
"@antfu/ni": "^0.21.12",
"@commitlint/cli": "^19.3.0",
"@release-it/conventional-changelog": "^8.0.1",
"@types/bun": "^1.1.1",
"@xystack/style-guide": "^0.0.4",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^9.2.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"release-it": "^17.2.1",
"taze": "^0.13.8",
"typescript": "^5.4.5",
"unbuild": "^2.0.0"
},
"prettier": "@xystack/style-guide/prettier"
}
70 changes: 70 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { highlight as highlightAsHtml } from 'sugar-high'
import * as c from 'picocolors'

export interface HighlightOptions {
showLineNumbers?: boolean
}
export function highlight(code: string, options: HighlightOptions = { showLineNumbers: false }) {
const html = highlightAsHtml(code)
const regex = /<span class="sh__line">(.*)<\/span>/g

let match
let result = ''
let i = 0

// eslint-disable-next-line no-cond-assign
while ((match = regex.exec(html)) !== null) {
const innerMatches = [...match[1].matchAll(/<span class="([^"]+)" style="color: ([^"]+)">(.*?)<\/span>/gs)]
result += options.showLineNumbers ? `${c.dim(`${i}`.padEnd(2, ' '))} ` : ''
innerMatches.forEach((innerMatch) => {
const classType = innerMatch[1]
const text = decode(innerMatch[3])

switch (classType) {
case 'sh__token--identifier':
result += c.black(text)
break
case 'sh__token--keyword':
result += c.red(text)
break
case 'sh__token--string':
result += c.green(text)
break
case 'sh__token--class':
result += c.cyan(text)
break
case 'sh__token--property':
result += c.cyan(text)
break
case 'sh__token--entity':
result += c.magenta(text)
break
case 'sh__token--jsxliterals':
result += c.green(text)
break
case 'sh__token--sign':
result += c.dim(text)
break
case 'sh__token--comment':
result += c.dim(text)
break
default:
result += text
break
}
})
result += '\n'
i++
}

return result
}

function decode(str: string) {
return str
.replace('&amp;', '&')
.replace('&lt;', '<')
.replace('&gt;', '>')
.replace('&quot;', '"')
.replace('&#039;', "'")
}
Loading

0 comments on commit d56f583

Please sign in to comment.