diff --git a/src/constants/errors.ts b/src/constants/errors.ts index bf080f8..251b6bb 100644 --- a/src/constants/errors.ts +++ b/src/constants/errors.ts @@ -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 diff --git a/src/utils/package-json.ts b/src/utils/package-json.ts index 8e3066b..025d544 100644 --- a/src/utils/package-json.ts +++ b/src/utils/package-json.ts @@ -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 }) @@ -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) } }