Skip to content

Commit

Permalink
feat: supprt line at the end of file, so that you can open the log fi…
Browse files Browse the repository at this point in the history
…le exactly at vscode
  • Loading branch information
baozouai committed Jul 29, 2023
1 parent b3c0c4c commit bce5e5e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
Binary file added assets/file_end_line.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions playgrounds/astro/astro.config.mjs
Expand Up @@ -7,6 +7,7 @@ export default defineConfig({
vite: {
plugins: [enhanceLog({
splitBy: '\n',
endLine: true,
}), inspect()],
},
})
12 changes: 11 additions & 1 deletion playgrounds/astro/src/pages/index.astro
Expand Up @@ -11,6 +11,13 @@ const b = {
}
console.log(1, 2, 3, a, b)
console.log(1, 2, 3, a, b)
console.log(1, 2, 3, a, b)
console.log(1,
2,
3,
a,
b)
---
Expand All @@ -23,7 +30,10 @@ const bb = {
}
}

console.log(1, 2, 3, a, bb)
console.log(1, 2, 3,


a, bb)


</script>
Expand Down
32 changes: 26 additions & 6 deletions src/index.ts
Expand Up @@ -53,6 +53,22 @@ export interface Options {
endLine?: boolean
}

const colorGreen = '\x1B[32m'
const colorBlue = '\x1B[34m'
const colorReset = '\x1B[0m'

function handleStartFileNameTip(filePath: string, lineNumber: number) {
if (!filePath)
return ''
return ` ~ ${colorGreen}${filePath}:${colorBlue}${lineNumber}${colorReset}`
}

function handleEndFileNameTip(filePath: string, lineNumber: number) {
if (!filePath)
return ''
return ` ~ ${filePath}:${lineNumber}`
}

function generateStrNode(str: string): StringLiteral & { skip: boolean } {
const node = stringLiteral(str)

Expand All @@ -77,6 +93,10 @@ export default function enhanceLogPlugin(options: Options = {}): PluginOption {
[/\.[jt]sx?$/, /\.vue$/, /\.svelte$/, /\.astro$/],
[/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/],
)

function generateLineOfTip(relativeFilename: string, lineNumber: number) {
return `${relativeFilename ? '' : `line of ${lineNumber} `}${preTip}`
}
return {
name: 'enhance-log',
configResolved(config) {
Expand Down Expand Up @@ -132,15 +152,15 @@ export default function enhanceLogPlugin(options: Options = {}): PluginOption {
column,
}) || {}
startLine = originStartLine
let combinePreTip = preTip

let relativeFilename = ''

if (enableFileName) {
let relativeFilename = id.replace(`${root}/`, '')
relativeFilename = id.replace(`${root}`, '').split('?')[0]
if (typeof enableFileName === 'object' && !enableFileName.enableDir)
relativeFilename = relativeFilename.replace(/.*\//, '')

combinePreTip = `~ ${relativeFilename} ${combinePreTip}`
}
const startLineTipNode = stringLiteral(`line of ${startLine} ${combinePreTip}:\n`)
const startLineTipNode = stringLiteral(`${generateLineOfTip(relativeFilename, startLine!)}${handleStartFileNameTip(relativeFilename, startLine!)}\n`)
nodeArguments.unshift(startLineTipNode)
if (enableEndLine) {
const { line, column } = loc.end
Expand All @@ -151,7 +171,7 @@ export default function enhanceLogPlugin(options: Options = {}): PluginOption {
// if startLine === endLine, needn't log endLine
if (startLine === endLine)
return
const endLineTipNode = stringLiteral(`\nline of ${endLine} ${combinePreTip}:\n`)
const endLineTipNode = stringLiteral(`\n${generateLineOfTip(relativeFilename, endLine!)}${handleEndFileNameTip(relativeFilename, endLine!)}\n`)
nodeArguments.push(endLineTipNode)
}
}
Expand Down

0 comments on commit bce5e5e

Please sign in to comment.