From 01cfac06455efb9bf81e2adc1cf51eb263f76128 Mon Sep 17 00:00:00 2001 From: Raul Catalinas <105791463+RaulCatalinas@users.noreply.github.com> Date: Fri, 10 May 2024 23:34:14 +0200 Subject: [PATCH] feat: added Husky configuration for Yarn --- src/constants/errors.ts | 4 +++- src/controllers/handlers-options.ts | 15 ++++++++++++- src/types/package-json.d.ts | 5 +++++ src/utils/commitlint.ts | 4 ++-- src/utils/husky-library.ts | 33 ++++++++++++++++++++++++----- src/utils/npm.ts | 28 ++++++++++++++++++++++++ src/utils/package-json.ts | 7 +----- 7 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 src/utils/npm.ts diff --git a/src/constants/errors.ts b/src/constants/errors.ts index e45332d..bf080f8 100644 --- a/src/constants/errors.ts +++ b/src/constants/errors.ts @@ -18,5 +18,7 @@ export const ERROR_MESSAGES = { CreateFolder: `An error occurred while creating the folder: {folderName}, please try again later, if the error persists, please report it on ${ISSUES}.`, - CheckFileExists: `An error occurred while checking if the file/folder exists, please try again later, if the error persists, please report it on ${ISSUES}.` + 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}.` } as const diff --git a/src/controllers/handlers-options.ts b/src/controllers/handlers-options.ts index ba50b65..4c634bd 100644 --- a/src/controllers/handlers-options.ts +++ b/src/controllers/handlers-options.ts @@ -10,6 +10,7 @@ import { generateCommitlintConfig } from '@/utils/commitlint' import { writeMessage } from '@/utils/console' import { getErrorMessage } from '@/utils/errors' import { generateHuskyConfig } from '@/utils/husky-library' +import { shouldPublishToNPM } from '@/utils/npm' import { getPackageManger } from '@/utils/package-managers' import { exists } from '@/utils/user-os' @@ -18,6 +19,8 @@ import process from 'node:process' export const handlerOptionBuild = async () => { try { + let shouldPublishToNpm = false + const packageJsonPath = `${process.cwd()}/package.json` const existPackageJsonInTheCurrentDirectory = await exists(packageJsonPath) @@ -32,9 +35,19 @@ export const handlerOptionBuild = async () => { } const packageManagerToUse = await getPackageManger() + + if (packageManagerToUse === 'yarn') { + shouldPublishToNpm = await shouldPublishToNPM() + } + const useCommitlint = await addCommitlint() - await generateHuskyConfig({ packageManagerToUse, packageJsonPath }) + await generateHuskyConfig({ + packageManagerToUse, + packageJsonPath, + useCommitlint, + shouldPublishToNpm + }) if (useCommitlint) { await generateCommitlintConfig({ packageManagerToUse, packageJsonPath }) diff --git a/src/types/package-json.d.ts b/src/types/package-json.d.ts index b4489a8..c389c96 100644 --- a/src/types/package-json.d.ts +++ b/src/types/package-json.d.ts @@ -7,3 +7,8 @@ export interface PackageJson { dependencies: Record devDependencies: Record } + +export interface PackageJsonScript { + key: string + value: string +} diff --git a/src/utils/commitlint.ts b/src/utils/commitlint.ts index cc8ca58..f39919e 100644 --- a/src/utils/commitlint.ts +++ b/src/utils/commitlint.ts @@ -78,7 +78,7 @@ export async function generateCommitlintConfig({ writeMessage({ type: 'info', - message: 'package.json modified successfully' + message: 'Modifying package.json...' }) await addScript({ @@ -93,7 +93,7 @@ export async function generateCommitlintConfig({ writeMessage({ type: 'info', - message: 'Modified package.json' + message: 'package.json modified successfully' }) writeMessage({ diff --git a/src/utils/husky-library.ts b/src/utils/husky-library.ts index 9f816d3..7d85d96 100644 --- a/src/utils/husky-library.ts +++ b/src/utils/husky-library.ts @@ -1,4 +1,5 @@ // Types +import type { PackageJsonScript } from '@/types/package-json' import type { PackageManager } from '@/types/package-manger' // Utils @@ -19,11 +20,15 @@ import { HUSKY_CONFIG } from '@/constants/husky-library' interface Props { packageManagerToUse: PackageManager packageJsonPath: string + useCommitlint: boolean + shouldPublishToNpm: boolean } export async function generateHuskyConfig({ packageManagerToUse, - packageJsonPath + packageJsonPath, + useCommitlint, + shouldPublishToNpm }: Props) { try { writeMessage({ @@ -47,7 +52,11 @@ export async function generateHuskyConfig({ await createFolder('.husky') } - await fs.writeFile('.husky/pre-commit', HUSKY_CONFIG[packageManagerToUse], { + const preCommitFileValue = useCommitlint + ? HUSKY_CONFIG[packageManagerToUse] + : `${packageManagerToUse} test` + + await fs.writeFile('.husky/pre-commit', preCommitFileValue, { encoding: UTF8_ENCODING }) @@ -58,17 +67,31 @@ export async function generateHuskyConfig({ writeMessage({ type: 'info', - message: 'package.json modified successfully' + message: 'Modifying package.json...' }) + const huskyScriptsForYarn: PackageJsonScript | PackageJsonScript[] = + !shouldPublishToNpm + ? { key: 'postinstall', value: 'husky' } + : [ + { key: 'postinstall', value: 'husky' }, + { key: 'prepack', value: 'pinst --disable' }, + { key: 'postpack', value: 'pinst --enable' } + ] + + const scriptsToAdd: PackageJsonScript | PackageJsonScript[] = + packageManagerToUse !== 'yarn' + ? { key: 'prepare', value: 'husky' } + : huskyScriptsForYarn + await addScript({ packageJsonPath, - scriptsToAdd: { key: 'prepare', value: 'husky' } + scriptsToAdd }) writeMessage({ type: 'info', - message: 'Modified package.json' + message: 'package.json modified successfully' }) writeMessage({ type: 'success', diff --git a/src/utils/npm.ts b/src/utils/npm.ts new file mode 100644 index 0000000..5156934 --- /dev/null +++ b/src/utils/npm.ts @@ -0,0 +1,28 @@ +// Third-Party libraries +import inquirer from 'inquirer' + +// NodeJS +import process from 'node:process' + +// Utils +import { writeMessage } from './console' +import { getErrorMessage } from './errors' + +export async function shouldPublishToNPM(): Promise { + try { + const { shouldPublishToNPM } = await inquirer.prompt({ + type: 'confirm', + default: false, + name: 'shouldPublishToNPM', + message: 'Will you publish it on npm?:' + }) + + return shouldPublishToNPM + } catch { + writeMessage({ + type: 'error', + message: getErrorMessage('PublishConfirmation') + }) + process.exit(1) + } +} diff --git a/src/utils/package-json.ts b/src/utils/package-json.ts index 1503b10..8e3066b 100644 --- a/src/utils/package-json.ts +++ b/src/utils/package-json.ts @@ -6,17 +6,12 @@ import process from 'node:process' import { UTF8_ENCODING } from '@/constants/encoding' // Types -import type { PackageJson } from '@/types/package-json' +import type { PackageJson, PackageJsonScript } from '@/types/package-json' // Utils import { writeMessage } from './console' import { getErrorMessage } from './errors' -interface PackageJsonScript { - key: string - value: string -} - interface Props { packageJsonPath: string scriptsToAdd: PackageJsonScript | PackageJsonScript[]