Skip to content

Commit

Permalink
feat: read file path from regex-doctor config file
Browse files Browse the repository at this point in the history
  • Loading branch information
wendraw committed May 28, 2024
1 parent 2f29623 commit a343ddc
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 41 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"prepare": "simple-git-hooks"
},
"dependencies": {
"c12": "^1.10.0",
"cac": "^6.7.14",
"error-stack-parser-es": "^0.1.4",
"exit-hook": "^4.0.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import process from 'node:process'
import { loadConfig } from 'c12'
import type { RegexDoctorConfig } from './types'

export function defineRegexDoctorConfig(config: RegexDoctorConfig) {
return config
}

const defaultFileData: Required<RegexDoctorConfig> = {
rootDir: '.',
outputDir: '.regex-doctor',
outputFileName: 'output.bin',
}

export async function loadRegexDoctorConfig() {
const { config, cwd } = await loadConfig({
name: 'regex-doctor',
configFile: 'regex-doctor.config',
})

return {
...defaultFileData,
rootDir: cwd ?? process.cwd(),
...config,
} satisfies Required<RegexDoctorConfig>
}
27 changes: 10 additions & 17 deletions src/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import type { RecordRegexInfo } from './types/record'
import type { RegexDoctorDumpOptions, RegexDoctorOptions } from './types/options'
import { hijack, listeners, restore } from './hijack'
import { dump } from './dump'

// TODO: read file path from regex-doctor config file
const defaultFileData = {
cwd: '.',
folder: '.regex-doctor',
file: 'output.bin',
}
import { loadRegexDoctorConfig } from './config'

export class RegexDoctor {
map = new Map<RegExp, RecordRegexInfo>()
Expand Down Expand Up @@ -90,20 +84,19 @@ export class RegexDoctor {
this.map.clear()
}

static get filePath() {
return path.join(defaultFileData.cwd, defaultFileData.folder, defaultFileData.file)
static async getFilePath() {
const config = await loadRegexDoctorConfig()
return path.join(config.rootDir, config.outputDir, config.outputFileName)
}

dump(options: RegexDoctorDumpOptions = {}) {
async dump(options: RegexDoctorDumpOptions = {}) {
this.saveDuration()
mkdirSync(path.join(defaultFileData.cwd, defaultFileData.folder), { recursive: true })
defaultFileData.cwd = options.cwd || defaultFileData.cwd
defaultFileData.folder = options.folder || defaultFileData.folder
defaultFileData.file = options.file || defaultFileData.file
writeFileSync(RegexDoctor.filePath, pack(dump(this, options)))
const config = await loadRegexDoctorConfig()
mkdirSync(path.join(config.rootDir, config.outputDir), { recursive: true })
writeFileSync(await RegexDoctor.getFilePath(), pack(dump(this, options)))
}

static pickup(filePath?: string): RegexDoctorResult {
return unpack(readFileSync(filePath || RegexDoctor.filePath))
static async pickup(filePath?: string): Promise<RegexDoctorResult> {
return unpack(readFileSync(filePath || (await RegexDoctor.getFilePath())))
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export function startRegexDoctor() {
doctor.start()
return doctor
}

export { defineRegexDoctorConfig } from './config'
12 changes: 6 additions & 6 deletions src/register.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable no-console */
import process from 'node:process'
import exitHook from 'exit-hook'
import { asyncExitHook } from 'exit-hook'
import c from 'picocolors'
import { RegexDoctor } from './doctor'

console.log(`[regex-doctor] start tracking`)

const doctor = new RegexDoctor()
const cwd = process.cwd()
doctor.start()

exitHook(() => {
asyncExitHook(async () => {
doctor.stop()
doctor.dump({ cwd })
console.log(`[regex-doctor] output saved to ${c.blue(RegexDoctor.filePath)}, run ${c.green(c.bold('npx regex-doctor view'))} to view it in an interactive UI.`)
await doctor.dump()
console.log(`[regex-doctor] output saved to ${c.blue(await RegexDoctor.getFilePath())}, run ${c.green(c.bold('npx regex-doctor view'))} to view it in an interactive UI.`)
}, {
wait: 10000,
})
5 changes: 5 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface RegexDoctorConfig {
rootDir?: string
outputDir?: string
outputFileName?: string
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './data'
export * from './options'
export * from './record'
export * from './config'
15 changes: 0 additions & 15 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@ export interface RegexDoctorOptions {
}

export interface RegexDoctorDumpOptions {
/**
* Provide current working directory for relative paths
*/
cwd?: string

/**
* Output folder name
*/
folder?: string

/**
* Output file name
* @default output.json
*/
file?: string

/**
* Limit of the numbers of calls to dump. Sorted by duration cost.
Expand Down
4 changes: 1 addition & 3 deletions test/fixtures/vite/build.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import process from 'node:process'
import { build } from 'vite'
import { startRegexDoctor } from 'regex-doctor'

Expand All @@ -7,8 +6,7 @@ import { startRegexDoctor } from 'regex-doctor'

await build()

doctor.dump({
await doctor.dump({
stacktrace: true,
cwd: process.cwd(),
})
}
3 changes: 3 additions & 0 deletions ui/regex-doctor.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineRegexDoctorConfig } from 'regex-doctor'

export default defineRegexDoctorConfig({})

0 comments on commit a343ddc

Please sign in to comment.