Skip to content

Commit

Permalink
Merge pull request #9 from vercel-labs/error
Browse files Browse the repository at this point in the history
chore: show an error when project folder isn't vercel
  • Loading branch information
Kikobeats authored Dec 18, 2024
2 parents 9fee7f0 + 81346a3 commit 955a8cd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import findCacheDirectory from 'find-cache-dir'
import { existsSync } from 'fs'
import path from 'path'

import { ErrorLinkProject } from './error.js'

export const createCache = () => {
const cacheDir = findCacheDirectory({ name: 'vercel-open' })
const cachePath = path.resolve(cacheDir!, 'cache.json')
if (cacheDir === undefined) throw ErrorLinkProject()

const cachePath = path.resolve(cacheDir, 'cache.json')

const cacheFilePromise = (async () => {
try {
Expand Down
5 changes: 5 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const ErrorLinkProject = () => {
const error: NodeJS.ErrnoException = new Error('Link project error')
error.code = 'ERR_LINK_PROJECT'
throw error
}
9 changes: 2 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { readFile } from 'fs/promises'
import { existsSync } from 'fs'
import path from 'path'

import { ErrorLinkProject } from './error.js'
import { createCache } from './cache.js'

const cache = createCache()
Expand Down Expand Up @@ -51,13 +52,7 @@ async function readProjectFile (): Promise<{
teamId: string
}> {
const filepath = path.resolve(process.cwd(), '.vercel/project.json')

if (!existsSync(filepath)) {
const error: NodeJS.ErrnoException = new Error('Link project error')
error.code = 'ERR_LINK_PROJECT'
throw error
}

if (!existsSync(filepath)) throw ErrorLinkProject()
const fileContent = await readFile(filepath, 'utf-8')
const { projectId, orgId: teamId } = JSON.parse(fileContent)
return { projectId, teamId }
Expand Down

0 comments on commit 955a8cd

Please sign in to comment.