diff --git a/src/cache.ts b/src/cache.ts index a6b2bbc..f121bbd 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -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 { diff --git a/src/error.ts b/src/error.ts new file mode 100644 index 0000000..638944b --- /dev/null +++ b/src/error.ts @@ -0,0 +1,5 @@ +export const ErrorLinkProject = () => { + const error: NodeJS.ErrnoException = new Error('Link project error') + error.code = 'ERR_LINK_PROJECT' + throw error +} diff --git a/src/index.ts b/src/index.ts index a4651f4..3f1e988 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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() @@ -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 }