Skip to content

Commit

Permalink
feat: added Husky configuration for Yarn
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulCatalinas committed May 10, 2024
1 parent 85330b0 commit 01cfac0
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/constants/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion src/controllers/handlers-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)
Expand All @@ -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 })
Expand Down
5 changes: 5 additions & 0 deletions src/types/package-json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export interface PackageJson {
dependencies: Record<string, string>
devDependencies: Record<string, string>
}

export interface PackageJsonScript {
key: string
value: string
}
4 changes: 2 additions & 2 deletions src/utils/commitlint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function generateCommitlintConfig({

writeMessage({
type: 'info',
message: 'package.json modified successfully'
message: 'Modifying package.json...'
})

await addScript({
Expand All @@ -93,7 +93,7 @@ export async function generateCommitlintConfig({

writeMessage({
type: 'info',
message: 'Modified package.json'
message: 'package.json modified successfully'
})

writeMessage({
Expand Down
33 changes: 28 additions & 5 deletions src/utils/husky-library.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Types
import type { PackageJsonScript } from '@/types/package-json'
import type { PackageManager } from '@/types/package-manger'

// Utils
Expand All @@ -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({
Expand All @@ -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
})

Expand All @@ -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',
Expand Down
28 changes: 28 additions & 0 deletions src/utils/npm.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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)
}
}
7 changes: 1 addition & 6 deletions src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down

0 comments on commit 01cfac0

Please sign in to comment.