Skip to content

Commit

Permalink
feat: added logic to check for package.json section existence
Browse files Browse the repository at this point in the history
- In addition to checking for existence, it's created if it doesn't exist.
  • Loading branch information
RaulCatalinas committed May 13, 2024
1 parent 73ade87 commit 60680ef
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/constants/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ export const ERROR_MESSAGES = {

CheckFileExists: `An error occurred while checking if the file/folder exists, please try again later, if the error persists, please report it on ${ISSUES}.`,

PublishConfirmation: `An error occurred while confirming npm publication. Please try again later, if the error persists, please report it on ${ISSUES}.`
PublishConfirmation: `An error occurred while confirming npm publication. Please try again later, if the error persists, please report it on ${ISSUES}.`,

CheckSection: `An error occurred while checking for a section in package.json. Please try again later, if the error persists, please report it on ${ISSUES}.`,

CreateSection: `An error occurred while creating a section in package.json. Please try again later, if the error persists, please report it on ${ISSUES}.`
} as const
79 changes: 77 additions & 2 deletions src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,35 @@ import type { PackageJson, PackageJsonScript } from '@/types/package-json'
import { writeMessage } from './console'
import { getErrorMessage } from './errors'

interface Props {
interface AddScriptProps {
packageJsonPath: string
scriptsToAdd: PackageJsonScript | PackageJsonScript[]
}

export async function addScript({ scriptsToAdd, packageJsonPath }: Props) {
interface ExistsSectionProps {
packageJsonPath: string
sectionToCheck: string
}

interface CreateEmptySectionProps {
packageJsonPath: string
sectionToCreate: string
}

export async function addScript({
scriptsToAdd,
packageJsonPath
}: AddScriptProps) {
try {
const existsScriptsSection = await existsSection({
packageJsonPath,
sectionToCheck: 'scripts'
})

if (!existsScriptsSection) {
await createEmptySection({ packageJsonPath, sectionToCreate: 'scripts' })
}

const packageJsonData = await fs.readFile(packageJsonPath, {
encoding: UTF8_ENCODING
})
Expand Down Expand Up @@ -59,6 +81,59 @@ export async function addScript({ scriptsToAdd, packageJsonPath }: Props) {
type: 'error',
message: getErrorMessage('AddScript')
})

process.exit(1)
}
}

async function existsSection({
packageJsonPath,
sectionToCheck
}: ExistsSectionProps) {
try {
const packageJsonData = await fs.readFile(packageJsonPath, {
encoding: UTF8_ENCODING
})

const packageJsonObj = JSON.parse(packageJsonData)

return packageJsonObj[sectionToCheck] !== undefined
} catch {
writeMessage({
type: 'error',
message: getErrorMessage('CheckSection')
})

process.exit(1)
}
}

async function createEmptySection({
packageJsonPath,
sectionToCreate
}: CreateEmptySectionProps) {
try {
const packageJsonData = await fs.readFile(packageJsonPath, {
encoding: UTF8_ENCODING
})

const packageJsonObj = JSON.parse(packageJsonData)

packageJsonObj[sectionToCreate] = {}

await fs.writeFile(
packageJsonPath,
JSON.stringify(packageJsonObj, null, 2),
{
encoding: UTF8_ENCODING
}
)
} catch {
writeMessage({
type: 'error',
message: getErrorMessage('CreateSection')
})

process.exit(1)
}
}

0 comments on commit 60680ef

Please sign in to comment.