Skip to content

Commit

Permalink
chore: example code added
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulCatalinas committed May 9, 2024
1 parent bb99307 commit 85330b0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/controllers/handlers-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const handlerOptionBuild = async () => {
await generateHuskyConfig({ packageManagerToUse, packageJsonPath })

if (useCommitlint) {
await generateCommitlintConfig(packageManagerToUse)
await generateCommitlintConfig({ packageManagerToUse, packageJsonPath })
}
} catch {
writeMessage({
Expand Down
36 changes: 33 additions & 3 deletions src/utils/commitlint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions src/utils/husky-library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
28 changes: 26 additions & 2 deletions src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,44 @@ 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
})

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(
Expand Down

0 comments on commit 85330b0

Please sign in to comment.