diff --git a/src/controllers/handlers-options.ts b/src/controllers/handlers-options.ts index 9aa9449..ba50b65 100644 --- a/src/controllers/handlers-options.ts +++ b/src/controllers/handlers-options.ts @@ -37,7 +37,7 @@ export const handlerOptionBuild = async () => { await generateHuskyConfig({ packageManagerToUse, packageJsonPath }) if (useCommitlint) { - await generateCommitlintConfig(packageManagerToUse) + await generateCommitlintConfig({ packageManagerToUse, packageJsonPath }) } } catch { writeMessage({ diff --git a/src/utils/commitlint.ts b/src/utils/commitlint.ts index 9c3f751..cc8ca58 100644 --- a/src/utils/commitlint.ts +++ b/src/utils/commitlint.ts @@ -12,10 +12,17 @@ import { UTF8_ENCODING } from '@/constants/encoding' import { writeMessage } from './console' import { installDependencies } from './dependencies' import { getErrorMessage } from './errors' +import { addScript } from './package-json' -export async function generateCommitlintConfig( +interface Props { packageManagerToUse: PackageManager -) { + packageJsonPath: string +} + +export async function generateCommitlintConfig({ + packageManagerToUse, + packageJsonPath +}: Props) { try { writeMessage({ type: 'config', @@ -55,7 +62,10 @@ export async function generateCommitlintConfig( '.lintstagedrc', JSON.stringify( { - 'src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}': [] + 'src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}': [ + 'prettier src --check', + 'eslint src --max-warnings 0' + ] }, null, 2 @@ -66,6 +76,26 @@ export async function generateCommitlintConfig( ) ]) + writeMessage({ + type: 'info', + message: 'package.json modified successfully' + }) + + await addScript({ + packageJsonPath, + scriptsToAdd: [ + { key: 'lint', value: 'eslint src' }, + { key: 'lint:fix', value: 'eslint src --fix' }, + { key: 'format', value: 'prettier src --check' }, + { key: 'format:write', value: 'prettier src --write' } + ] + }) + + writeMessage({ + type: 'info', + message: 'Modified package.json' + }) + writeMessage({ type: 'info', message: diff --git a/src/utils/husky-library.ts b/src/utils/husky-library.ts index 0174d74..9f816d3 100644 --- a/src/utils/husky-library.ts +++ b/src/utils/husky-library.ts @@ -58,10 +58,13 @@ export async function generateHuskyConfig({ writeMessage({ type: 'info', - message: 'Modifying package.json' + message: 'package.json modified successfully' }) - await addScript({ key: 'prepare', value: 'husky', packageJsonPath }) + await addScript({ + packageJsonPath, + scriptsToAdd: { key: 'prepare', value: 'husky' } + }) writeMessage({ type: 'info', diff --git a/src/utils/package-json.ts b/src/utils/package-json.ts index 11f0d08..1503b10 100644 --- a/src/utils/package-json.ts +++ b/src/utils/package-json.ts @@ -12,13 +12,17 @@ import type { PackageJson } from '@/types/package-json' import { writeMessage } from './console' import { getErrorMessage } from './errors' -interface Props { +interface PackageJsonScript { key: string value: string +} + +interface Props { packageJsonPath: string + scriptsToAdd: PackageJsonScript | PackageJsonScript[] } -export async function addScript({ key, value, packageJsonPath }: Props) { +export async function addScript({ scriptsToAdd, packageJsonPath }: Props) { try { const packageJsonData = await fs.readFile(packageJsonPath, { encoding: UTF8_ENCODING @@ -26,6 +30,26 @@ export async function addScript({ key, value, packageJsonPath }: Props) { const packageJsonObj: PackageJson = JSON.parse(packageJsonData) + const scriptsToAddIsAnArray = Array.isArray(scriptsToAdd) + + if (scriptsToAddIsAnArray) { + for (const { key, value } of scriptsToAdd) { + packageJsonObj.scripts[key] = value + } + + await fs.writeFile( + packageJsonPath, + JSON.stringify(packageJsonObj, null, 2), + { + encoding: UTF8_ENCODING + } + ) + + return + } + + const { key, value } = scriptsToAdd + packageJsonObj.scripts[key] = value await fs.writeFile(