Skip to content

Commit

Permalink
refactor: move package.json existence check to top of handlerOptionBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulCatalinas committed May 3, 2024
1 parent cbd40c5 commit 360518b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/controllers/handlers-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ 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
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()

Expand Down
16 changes: 3 additions & 13 deletions src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +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 packageJsonPath = `${process.cwd()}/package.json`

const existPackageJsonInTheCurrentDirectory =
await existPackageJson(packageJsonPath)

if (!existPackageJsonInTheCurrentDirectory) {
console.error(ErrorMessages.NotFound)

process.exit(1)
}

const packageJsonData = await fs.readFile(packageJsonPath, {
encoding: UTF8_ENCODING
})
Expand All @@ -48,7 +38,7 @@ export async function addScript({ key, value }: Props) {
}
}

async function existPackageJson(path: string) {
export async function existPackageJson(path: string) {
try {
return await fs.exists(path)
} catch {
Expand Down

0 comments on commit 360518b

Please sign in to comment.