Skip to content

Commit

Permalink
perf: cache env utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Jan 2, 2024
1 parent 29f6673 commit 5b15fb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"get-port-please": "^3.1.1",
"graphql-yoga": "^5.0.2",
"jiti": "^1.21.0",
"lru-cache": "^10.1.0",
"nanoid": "^5.0.4",
"pathe": "^1.1.1",
"perfect-debounce": "^1.0.0",
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/util/env.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import process from 'node:process'
import fs from 'node:fs'
import path from 'pathe'
import { LRUCache } from 'lru-cache'
import { findUp } from './find-up.js'

export function getCwd() {
return path.resolve(process.env.MOQUERIE_OVERRIDE_CWD ?? process.cwd())
}

const projectNameCache = new LRUCache<string, string>({
max: 100,
ttl: 5000,
})

export function getProjectName(): string {
const cwd = getCwd()
const cached = projectNameCache.get(cwd)
if (cached) {
return cached
}
const pkgFile = findUp(cwd, ['package.json'])
if (pkgFile) {
try {
const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf-8'))
projectNameCache.set(cwd, pkg.name)
return pkg.name
}
catch (e: any) {
Expand All @@ -22,8 +33,18 @@ export function getProjectName(): string {
throw new Error(`[moquerie] package.json not found looking from ${cwd}`)
}

const projectHasTypescriptCache = new LRUCache<string, boolean>({
max: 100,
ttl: 1000 * 60 * 5,
})

export function projectHasTypescript(): boolean {
const cwd = getCwd()
const cached = projectHasTypescriptCache.get(cwd)
if (cached !== undefined) {
return cached
}
const tsconfigFile = findUp(cwd, ['tsconfig.json'])
projectHasTypescriptCache.set(cwd, !!tsconfigFile)
return !!tsconfigFile
}
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.

0 comments on commit 5b15fb4

Please sign in to comment.