Skip to content

Commit

Permalink
chore: improve error messages
Browse files Browse the repository at this point in the history
- Enhanced error messages for better clarity and understanding.
  • Loading branch information
RaulCatalinas committed May 5, 2024
1 parent c9770d4 commit aa5f419
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 50 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
},
"dependencies": {
"@npmcli/promise-spawn": "7.0.2",
"chalk": "5.3.0",
"commander": "12.0.0",
"fs-extra": "11.2.0",
"inquirer": "9.2.20",
"opener": "1.5.2"
},
"devDependencies": {
"@commitlint/cli": "19.3.0",
"@commitlint/config-conventional": "19.2.2",
"@types/bun": "1.1.1",
"@types/fs-extra": "11.0.4",
"@types/inquirer": "9.0.7",
"@types/npmcli__promise-spawn": "6.0.3",
"@types/opener": "1.4.3",
Expand Down
23 changes: 16 additions & 7 deletions src/constants/errors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { ISSUES } from './github'

export enum ErrorMessages {
NotFound = "The package.json file wasn't found in the current directory.",
export const ERROR_MESSAGES = {
NotFound: "The package.json file wasn't found in the current directory.",

Default = `Something went wrong, try again later, please try again later, if the error persists please report it on ${ISSUES}.`,
Default: `Something went wrong, please try again later, if the error persists please report it on ${ISSUES}.`,

Dependencies = `An error occurred while installing dependencies, please try again later, if the error persists please report it on ${ISSUES}.`,
Dependencies: `An error occurred while installing dependencies, please try again later, if the error persists please report it on ${ISSUES}.`,

Commitlint = `An error has occurred during the Commitlint configuration process, please try again later, if the error persists please report it on ${ISSUES}.`,
Commitlint: `An error has occurred during the Commitlint configuration process, please try again later, if the error persists please report it on ${ISSUES}.`,
Husky: `An error has occurred during the Husky configuration process, please try again later, if the error persists please report it on ${ISSUES}.`,

Husky = `An error has occurred during the Husky configuration process, please try again later, if the error persists please report it on ${ISSUES}.`
}
PackageManagerSelection: `An error occurred while selecting the package manager, please try again later, if the error persists, please report it on ${ISSUES}.`,

CommitlintSelection: `An error occurred while determining your choice for Commitlint, please try again later, if the error persists, please report it on ${ISSUES}.`,

AddScript: `An error occurred while adding a script to the package.json file, please try again later, if the error persists, please report it on ${ISSUES}.`,

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}.`
} as const
18 changes: 14 additions & 4 deletions src/controllers/handlers-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Constants
import { ErrorMessages } from '@/constants/errors'
import { REPOSITORY } from '@/constants/github'

// Third-Party libraries
Expand All @@ -8,6 +7,8 @@ import opener from 'opener'
// Utils
import { addCommitlint } from '@/utils/add-commitlint'
import { generateCommitlintConfig } from '@/utils/commitlint'
import { writeMessage } from '@/utils/console'
import { getErrorMessage } from '@/utils/errors'
import { generateHuskyConfig } from '@/utils/husky-library'
import { getPackageManger } from '@/utils/package-managers'
import { exists } from '@/utils/user-os'
Expand All @@ -22,7 +23,10 @@ export const handlerOptionBuild = async () => {
const existPackageJsonInTheCurrentDirectory = await exists(packageJsonPath)

if (!existPackageJsonInTheCurrentDirectory) {
console.error(ErrorMessages.NotFound)
writeMessage({
type: 'error',
message: getErrorMessage('NotFound')
})

process.exit(1)
}
Expand All @@ -36,13 +40,19 @@ export const handlerOptionBuild = async () => {
await generateCommitlintConfig(packageManagerToUse)
}
} catch {
console.error(ErrorMessages.Default)
writeMessage({
type: 'error',
message: getErrorMessage('Default')
})
process.exit(1)
}
}

export const handlerOptionCollaborate = () => {
console.log('Opening the GitHub repository...')
writeMessage({
type: 'info',
message: 'Opening the GitHub repository...'
})

setTimeout(() => opener(REPOSITORY), 500)
}
1 change: 1 addition & 0 deletions src/types/message.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type MessageType = 'success' | 'error' | 'info' | 'config'
12 changes: 8 additions & 4 deletions src/utils/add-commitlint.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Third-Party libraries
import inquirer from 'inquirer'

// Constants
import { ErrorMessages } from '@/constants/errors'

// NodeJS
import process from 'node:process'

// Utils
import { writeMessage } from './console'
import { getErrorMessage } from './errors'

export async function addCommitlint(): Promise<boolean> {
try {
const { addCommitlint } = await inquirer.prompt({
Expand All @@ -18,7 +19,10 @@ export async function addCommitlint(): Promise<boolean> {

return addCommitlint
} catch {
console.error(ErrorMessages.Default)
writeMessage({
type: 'error',
message: getErrorMessage('CommitlintSelection')
})
process.exit(1)
}
}
41 changes: 29 additions & 12 deletions src/utils/commitlint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import fs from 'node:fs/promises'
// Constants
import { COMMITLINT_CONFIG } from '@/constants/commitlint'
import { UTF8_ENCODING } from '@/constants/encoding'
import { ErrorMessages } from '@/constants/errors'

// Utils
import { writeMessage } from './console'
import { installDependencies } from './dependencies'
import { getErrorMessage } from './errors'

export async function generateCommitlintConfig(
packageManagerToUse: PackageManager
) {
try {
console.log('Configuring commitlint...')
writeMessage({
type: 'config',
message: 'Configuring commitlint...'
})

await installDependencies({
packageManagerToUse,
Expand All @@ -27,7 +31,10 @@ export async function generateCommitlintConfig(
]
})

console.log('Creating configuration files...')
writeMessage({
type: 'info',
message: 'Creating configuration files...'
})

await Promise.all([
fs.writeFile(
Expand All @@ -46,23 +53,33 @@ export async function generateCommitlintConfig(
),
fs.writeFile(
'.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}': []
},
null,
2
),
{
encoding: UTF8_ENCODING
}
)
])

console.log(
'Configuration files (commit-msg, commitlint.config.js and .lintstagedrc) created'
)
console.log("commitlint's configuration generated successfully")
writeMessage({
type: 'info',
message:
'Configuration files (commit-msg, commitlint.config.js and .lintstagedrc) created'
})
writeMessage({
type: 'success',
message: "commitlint's configuration generated successfully"
})
} catch (error) {
console.error(ErrorMessages.Commitlint)
writeMessage({
type: 'error',
message: getErrorMessage('Commitlint')
})
process.exit(1)
}
}
20 changes: 20 additions & 0 deletions src/utils/console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Third-Party libraries
import chalk from 'chalk'

// Constants
import type { ERROR_MESSAGES } from '@/constants/errors'

// Types
import type { MessageType } from '@/types/message'

interface Props {
type: MessageType
message: typeof ERROR_MESSAGES | string
}

export function writeMessage({ type, message }: Props) {
if (type === 'success') console.log(chalk.green(message))
if (type === 'info') console.log(chalk.blue(message))
if (type === 'error') console.log(chalk.red(message))
if (type === 'config') console.log(chalk.white(message))
}
20 changes: 16 additions & 4 deletions src/utils/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import type { PackageManager } from '@/types/package-manger'

// Constants
import { INSTALLATION_COMMANDS } from '@/constants/dependencies'
import { ErrorMessages } from '@/constants/errors'

// Third-Party libraries
import promiseSpawn from '@npmcli/promise-spawn'

// NodeJS
import process from 'node:process'

// Utils
import { writeMessage } from './console'
import { getErrorMessage } from './errors'

interface Props {
packageManagerToUse: PackageManager
packagesToInstall: string | string[]
Expand All @@ -23,16 +26,25 @@ export async function installDependencies({
try {
const installationCommand = INSTALLATION_COMMANDS[packageManagerToUse]

console.log(`Installing dependencies using: ${packageManagerToUse}...`)
writeMessage({
type: 'info',
message: `Installing dependencies using: ${packageManagerToUse}...`
})

await promiseSpawn(
packageManagerToUse,
[installationCommand, packagesToInstall, '-D'].flat()
)

console.log('Installed dependencies')
writeMessage({
type: 'success',
message: 'Dependencies installed successfully'
})
} catch (error) {
console.error(ErrorMessages.Dependencies)
writeMessage({
type: 'error',
message: getErrorMessage('Dependencies')
})
process.exit(1)
}
}
5 changes: 5 additions & 0 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ERROR_MESSAGES } from '@/constants/errors'

export function getErrorMessage(error: keyof typeof ERROR_MESSAGES) {
return ERROR_MESSAGES[error]
}
38 changes: 30 additions & 8 deletions src/utils/husky-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import type { PackageManager } from '@/types/package-manger'

// Utils
import { writeMessage } from './console'
import { installDependencies } from './dependencies'
import { getErrorMessage } from './errors'
import { addScript } from './package-json'
import { createFolder, exists } from './user-os'

Expand All @@ -12,7 +14,6 @@ import process from 'node:process'

// Constants
import { UTF8_ENCODING } from '@/constants/encoding'
import { ErrorMessages } from '@/constants/errors'
import { HUSKY_CONFIG } from '@/constants/husky-library'

interface Props {
Expand All @@ -25,14 +26,20 @@ export async function generateHuskyConfig({
packageJsonPath
}: Props) {
try {
console.log("Generating Husky's configuration...")
writeMessage({
type: 'config',
message: "Generating Husky's configuration..."
})

await installDependencies({
packageManagerToUse,
packagesToInstall: 'husky'
})

console.log('Creating configuration file...')
writeMessage({
type: 'info',
message: 'Creating configuration file...'
})

const existsHuskyFolder = await exists(`${process.cwd()}/.husky`)

Expand All @@ -44,16 +51,31 @@ export async function generateHuskyConfig({
encoding: UTF8_ENCODING
})

console.log('Configuration file (pre-commit) created')
writeMessage({
type: 'info',
message: 'Configuration file (pre-commit) created successfully'
})

console.log('Modifying package.json')
writeMessage({
type: 'info',
message: 'Modifying package.json'
})

await addScript({ key: 'prepare', value: 'husky', packageJsonPath })

console.log('Modified package.json')
console.log("Husky's configuration generated successfully")
writeMessage({
type: 'info',
message: 'Modified package.json'
})
writeMessage({
type: 'success',
message: "Husky's configuration generated successfully"
})
} catch {
console.error(ErrorMessages.Husky)
writeMessage({
type: 'error',
message: getErrorMessage('Husky')
})
process.exit(1)
}
}
10 changes: 8 additions & 2 deletions src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import process from 'node:process'

// Constants
import { UTF8_ENCODING } from '@/constants/encoding'
import { ErrorMessages } from '@/constants/errors'

// Types
import type { PackageJson } from '@/types/package-json'

// Utils
import { writeMessage } from './console'
import { getErrorMessage } from './errors'

interface Props {
key: string
value: string
Expand All @@ -33,7 +36,10 @@ export async function addScript({ key, value, packageJsonPath }: Props) {
}
)
} catch {
console.error(ErrorMessages.Default)
writeMessage({
type: 'error',
message: getErrorMessage('AddScript')
})
process.exit(1)
}
}
Loading

0 comments on commit aa5f419

Please sign in to comment.