Skip to content

Commit 1162eb0

Browse files
committed
feat: add color to identify file and folder in terminal (closes #6)
1 parent 8ccc185 commit 1162eb0

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

.prettierrc.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export default {
22
trailingComma: 'es5',
33
tabWidth: 2,
44
semi: false,
5-
singleQuote: true
5+
singleQuote: true,
66
}

package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
"scripts": {
1111
"prepublishOnly": "nr build",
1212
"dev": "tsup src/index.ts --watch",
13-
"build": "tsup src/index.ts --minify",
13+
"build": "nr type && tsup src/index.ts --minify",
1414
"release": "bumpp --commit --push --tag && pnpm publish",
1515
"lint": "eslint src/**/*.ts",
1616
"lint:fix": "eslint src --fix",
17-
"test": "vitest"
17+
"test": "vitest",
18+
"type": "tsc --noEmit"
1819
},
1920
"bin": {
2021
"treei": "dist/index.js"
@@ -39,6 +40,8 @@
3940
"vitest": "^1.3.1"
4041
},
4142
"dependencies": {
42-
"commander": "^12.0.0"
43+
"commander": "^12.0.0",
44+
"picocolors": "^1.1.1"
4345
}
4446
}
47+

pnpm-lock.yaml

+8-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
export const emoji = {
22
directory: '📁',
3-
file: '📄'
3+
file: '📄',
44
}
55

66
export const characters = {
77
border: '|',
88
contain: '├',
99
line: '─',
10-
last: '└'
10+
last: '└',
1111
}
1212

1313
export const defaultOptions = {
1414
// strategy of finding tree structure, bfs by default
15-
strategy: 'bfs'
15+
strategy: 'bfs',
1616
}
1717

1818
export const NodeTypes = {
1919
ROOT: 'root',
2020
DIRECTORY: 'directory',
21-
FILE: 'file'
22-
}
21+
FILE: 'file',
22+
} as const

src/generate.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { emoji, characters, NodeTypes } from './config'
22
import { Options, TreeNode } from './type'
3+
import { blue } from 'picocolors'
34

45
const lastDirStack: boolean[] = []
56

@@ -25,9 +26,11 @@ export function generate(
2526
let contentPrefix =
2627
index === data!.length - 1 ? characters.last : characters.contain
2728
contentPrefix += characters.line.repeat(2)
29+
30+
const name = item.type === NodeTypes.DIRECTORY ? blue(item.name) : item.name
2831
const content = options.icon
29-
? `${emoji[item.type as keyof typeof emoji]}${item.name}`
30-
: `${item.name}`
32+
? `${emoji[item.type as keyof typeof emoji]}${name}`
33+
: `${name}`
3134

3235
let currentLineStr = `${borderPrefix}${contentPrefix}${content}`
3336
currentLineStr += '\n'

src/type.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { NodeTypes } from './config'
2+
13
export interface Options {
24
directory: string
35
ignore?: string | string[]
@@ -9,7 +11,7 @@ export interface Options {
911
}
1012

1113
export interface TreeNode {
12-
type: string
14+
type: (typeof NodeTypes)[keyof typeof NodeTypes]
1315
name: string
1416
path?: string
1517
children?: TreeNode[]

0 commit comments

Comments
 (0)