Skip to content

Commit

Permalink
feat: add logic to include unnecessary files in .npmignore
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulCatalinas committed May 22, 2024
1 parent 8d58886 commit 6c0474f
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 129 deletions.
6 changes: 5 additions & 1 deletion src/constants/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ export const ERROR_MESSAGES = {

CheckSection: `An error occurred while checking for a section in package.json. Please try again later, if the error persists, please report it on ${ISSUES}.`,

CreateSection: `An error occurred while creating a section in package.json. Please try again later, if the error persists, please report it on ${ISSUES}.`
CreateSection: `An error occurred while creating a section in package.json. Please try again later, if the error persists, please report it on ${ISSUES}.`,

NpmIgnoreWrite: `An error occurred while writing to the '.npmignore' file. Please try again later, if the error persists please report it on ${ISSUES}.`,

EmptyFileCreate: `An error occurred while creating the empty file: {fileName}. Please try again later, if the error persists please report it on ${ISSUES}.`
} as const
1 change: 1 addition & 0 deletions src/constants/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SPECIAL_CHARS_REGEX = /[\r\n\t\f\v]/g
12 changes: 6 additions & 6 deletions src/controllers/handlers-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import { getPackageManger } from '@/user-input/package-managers'

export const handlerOptionBuild = async () => {
try {
let shouldPublishToNpm = false

const packageJsonPath = `${process.cwd()}/package.json`

const existPackageJsonInTheCurrentDirectory = await exists(packageJsonPath)
Expand All @@ -38,9 +36,7 @@ export const handlerOptionBuild = async () => {

const packageManagerToUse = await getPackageManger()

if (packageManagerToUse === 'yarn') {
shouldPublishToNpm = await shouldPublishToNPM()
}
const shouldPublishToNpm = await shouldPublishToNPM()

const useCommitlint = await addCommitlint()

Expand All @@ -52,7 +48,11 @@ export const handlerOptionBuild = async () => {
})

if (useCommitlint) {
await generateCommitlintConfig({ packageManagerToUse, packageJsonPath })
await generateCommitlintConfig({
packageManagerToUse,
packageJsonPath,
shouldPublishToNpm
})
}
} catch {
writeMessage({
Expand Down
157 changes: 88 additions & 69 deletions src/utils/commitlint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,95 +12,47 @@ import { UTF8_ENCODING } from '@/constants/encoding'
import { writeMessage } from './console'
import { installDependencies } from './dependencies'
import { getErrorMessage } from './errors'
import { modifyNpmIgnore } from './npm'
import { addScript } from './package-json'

interface Props {
packageManagerToUse: PackageManager
packageJsonPath: string
shouldPublishToNpm: boolean
}

export async function generateCommitlintConfig({
packageManagerToUse,
packageJsonPath
packageJsonPath,
shouldPublishToNpm
}: Props) {
try {
writeMessage({
type: 'config',
message: 'Configuring commitlint...'
})

await installDependencies({
packageManagerToUse,
packagesToInstall: [
'lint-staged',
'@commitlint/cli',
'@commitlint/config-conventional'
]
})
await Promise.all(
[
installDependencies({
packageManagerToUse,
packagesToInstall: [
'lint-staged',
'@commitlint/cli',
'@commitlint/config-conventional'
]
}),

writeMessage({
type: 'info',
message: 'Creating configuration files...'
})
addNecessaryScriptsToPackageJson(packageJsonPath),

await Promise.all([
fs.writeFile(
'.husky/commit-msg',
COMMITLINT_CONFIG[packageManagerToUse],
{
encoding: UTF8_ENCODING
}
),
fs.writeFile(
'commitlint.config.js',
"export default { extends: ['@commitlint/config-conventional'] }",
{
encoding: UTF8_ENCODING
}
),
fs.writeFile(
'.lintstagedrc',
JSON.stringify(
{
'src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}': [
'prettier src --check',
'eslint src --max-warnings 0'
]
},
null,
2
),
{
encoding: UTF8_ENCODING
}
)
])
createCommitlintConfigFiles(packageManagerToUse),

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

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: 'package.json modified successfully'
})
shouldPublishToNpm
? modifyNpmIgnore(['.lintstagedrc', 'commitlint.config.js'])
: null
].filter(promise => promise != null)
)

writeMessage({
type: 'info',
message:
'Configuration files (commit-msg, commitlint.config.js and .lintstagedrc) created successfully'
})
writeMessage({
type: 'success',
message: "commitlint's configuration generated successfully"
Expand All @@ -110,6 +62,73 @@ export async function generateCommitlintConfig({
type: 'error',
message: getErrorMessage('Commitlint')
})

process.exit(1)
}
}

async function createCommitlintConfigFiles(
packageManagerToUse: PackageManager
) {
writeMessage({
type: 'info',
message: 'Creating configuration files...'
})

await Promise.all([
fs.writeFile('.husky/commit-msg', COMMITLINT_CONFIG[packageManagerToUse], {
encoding: UTF8_ENCODING
}),
fs.writeFile(
'commitlint.config.js',
"export default { extends: ['@commitlint/config-conventional'] }",
{
encoding: UTF8_ENCODING
}
),
fs.writeFile(
'.lintstagedrc',
JSON.stringify(
{
'src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}': [
'prettier src --check',
'eslint src --max-warnings 0'
]
},
null,
2
),
{
encoding: UTF8_ENCODING
}
)
])

writeMessage({
type: 'info',
message:
'Configuration files (commit-msg, commitlint.config.js and .lintstagedrc) created successfully'
})
}

async function addNecessaryScriptsToPackageJson(packageJsonPath: string) {
writeMessage({
type: 'info',
message: 'Modifying package.json...'
})

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: 'package.json modified successfully'
})
}
Loading

0 comments on commit 6c0474f

Please sign in to comment.