From ffd0b857b77001413090e1a502c505efcea501a4 Mon Sep 17 00:00:00 2001 From: Raul Catalinas <105791463+RaulCatalinas@users.noreply.github.com> Date: Sat, 4 May 2024 23:24:05 +0200 Subject: [PATCH] Revert "fix: dynamic require of 'events' is not supported" This reverts commit 65501893896c3f9205c5794022fc0441c482c88e. --- package.json | 2 +- src/constants/errors.ts | 2 ++ src/controllers/handlers-options.ts | 11 +++++++++++ src/utils/package-json.ts | 16 +++++++++++----- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 0e9fc83..1ac6c95 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint:fix": "eslint src --fix", "format": "prettier src --check", "format:write": "prettier src --write", - "build": "esbuild --minify --bundle --format=esm --platform=node --outfile=dist/index.js --packages=external src/index.ts", + "build": "esbuild --minify --bundle --format=esm --platform=node --outfile=dist/index.js src/index.ts", "prepublishOnly": "bun run build" }, "dependencies": { diff --git a/src/constants/errors.ts b/src/constants/errors.ts index 293f3fb..61a889f 100644 --- a/src/constants/errors.ts +++ b/src/constants/errors.ts @@ -1,6 +1,8 @@ import { ISSUES } from './github' export enum ErrorMessages { + NotFound = "The package.json file wasn't found in the current directory.", + Default = `Something went wrong, try again later, please try again later, if the error persists please report it on ${ISSUES}.`, Dependencies = `An error occurred while installing dependencies, please try again later, if the error persists please report it on ${ISSUES}.`, diff --git a/src/controllers/handlers-options.ts b/src/controllers/handlers-options.ts index df2f863..2d30606 100644 --- a/src/controllers/handlers-options.ts +++ b/src/controllers/handlers-options.ts @@ -9,6 +9,7 @@ import opener from 'opener' import { addCommitlint } from '@/utils/add-commitlint' import { generateCommitlintConfig } from '@/utils/commitlint' import { generateHuskyConfig } from '@/utils/husky-library' +import { existPackageJson } from '@/utils/package-json' import { getPackageManger } from '@/utils/package-managers' // NodeJS @@ -16,6 +17,16 @@ import process from 'node:process' export const handlerOptionBuild = async () => { try { + const packageJsonPath = `${process.cwd()}/package.json` + const existPackageJsonInTheCurrentDirectory = + await existPackageJson(packageJsonPath) + + if (!existPackageJsonInTheCurrentDirectory) { + console.error(ErrorMessages.NotFound) + + process.exit(1) + } + const packageManagerToUse = await getPackageManger() const useCommitlint = await addCommitlint() diff --git a/src/utils/package-json.ts b/src/utils/package-json.ts index b6b206e..b52bfb2 100644 --- a/src/utils/package-json.ts +++ b/src/utils/package-json.ts @@ -12,14 +12,11 @@ import type { PackageJson } from '@/types/package-json' interface Props { key: string value: string + packageJsonPath: string } -export async function addScript({ key, value }: Props) { +export async function addScript({ key, value, packageJsonPath }: Props) { try { - const isBuild = process.env.NODE_ENV === 'production' - - const packageJsonPath = isBuild ? '../package.json' : './package.json' - const packageJsonData = await fs.readFile(packageJsonPath, { encoding: UTF8_ENCODING }) @@ -40,3 +37,12 @@ export async function addScript({ key, value }: Props) { process.exit(1) } } + +export async function existPackageJson(path: string) { + try { + return await fs.exists(path) + } catch { + console.error(ErrorMessages.Default) + process.exit(1) + } +}