/**
 * Conventional Commit module.
 *
 * Process file paths and how they changed and generate a type like 'chore' or 'docs'.
 */ import { ACTION , CONVENTIONAL_TYPE } from "../lib/constants" ; import { splitPath } from "../lib/paths" ; const PACKAGE_DIRS = [ ".cargo" , ] , PACKAGE_NAMES = [ "requirements.txt" , "requirements-dev.txt" , "dev-requirements.txt" , "requirements-test.txt" , "test-requirements.txt" , "Pipfile" , "Pipefile.lock" , "pyproject.toml" , "setup.py" , "Gemfile" , "Gemfile.lock" , "package-lock.json" , "shrinkwrap.json" , "yarn.lock" , "composer.json" , "composer.lock" , "go.mod" , "go.sum" , "Cargo.toml" , "Cargo.lock" , "deps.ts" , "test_deps.ts" , "dev_deps.ts" , "import_map.json" , ] ; const BUILD_NAMES = [ ".dockerignore" , "Dockerfile" , "docker-compose.yml" , "GNUmakefile" , "makefile" , "Makefile" , "rakefile" , "Rakefile" , "rakefile.rb" , "Rakefile.rb" , "package.json" , "gradlew" , "grailsw" , "micronaut-cli.yml" , ] , BUILD_EXTENSIONS = [ ".gemspec" , ".bat" , ".gradle" , ] ; const CONFIG_EXTENSIONS = [ ".yml" , ".yaml" , ".json" , ".toml" , ".ini" , ".cfg" ] , CONFIG_DIRS = [ ".vscode" ] , CONFIG_NAMES = [ ".npmignore" , ".npmrc" , ".gitignore" , ".editorconfig" , ".pylintrc" , "setup.cfg" , ".browserslistrc" , "browserslist" , "commitlint.config.js" , "tsconfig.json" , "tslint.json" , ".env" , ".env.dev" , ".env.prod" , ".env.tempate" , ] ; const CI_DIRS = [ ".circleci" , ".github/workflows" ] , CI_NAMES = [ "netlify.toml" , "travis.yml" , "tox.ini" , ".vscodeignore" , "codecov.yml" , ".codecov.yml" , ".codeclimate.yml" , "now.json" , ".nowignore" , "vercel.json" , ".vercelignore" , ] ; const SCRIPT_EXTENSIONS = [ ".html" , ".css" , ".less" , ".scss" , ".js" , ".jsx" , ".ts" , ".tsx" , ".mjs" , ".py" , ".rb" , ".java" , ".jar" , ".c" , ".h" , ".rs" , ".go" , ".php" , ] , SHELL_SCRIPT_EXTENSION = ".sh" ; const LICENSE_NAMES = [ "LICENSE" , "LICENSE.txt" , "License.txt" , "LICENSE-source" , ] ; const DOCS_DIRS = [ "docs" , ".github/ISSUE_TEMPLATE" , ".github/PULL_REQUEST_TEMPLATE" , ] ; const DOC_NAMES = [ "README" , "README.md" , "README.txt" , "installation.md" , "usage.md" , "development.md" , "deploy.md" , "SECURITY.md" , "CONTRIBUTING.md" , "CHANGELOG.md" , "RELEASES.md" , "FUNDING.md" , "PULL_REQUEST_TEMPLATE.md" , "ISSUE_TEMPLATE.md" , "CODE_OF_CONDUCT.md" , "MAINTAINERS.txt" , "CODEOWNERS" , "sample.png" , "sample-1.png" , "sample-2.png" , "screenshot.png" , "preview.png" , ] . map ( name => name . toLowerCase ( ) ) ; /**
 * Evaluate conventional commit prefix for a given file.
 *
 * This ignores the action such as create/delete file.
 *
 * For move or rename cases, the input path is assumed to be the `to` path path as that would more
 * useful than knowing the `from` path.
 */ export class ConventionalCommit { atRoot : boolean ; dirPath : string ; name : string ; extension : string ; constructor ( filePath ) { const { atRoot , dirPath , name , extension } = splitPath ( filePath ) ; this . atRoot = atRoot ; this . dirPath = dirPath ; this . name = name ; this . extension = extension ; } /**
     * Check if doc-related.
     *
     * Return true for `.rst`, README files and anything in the docs directory.
     *
     * TODO: For static sites, not all .md files are docs but that could be configured with a global
     * flag. Or recognize Jekyll config.
     */ isDocsRelated ( ) { if ( this . extension === ".rst" ) { return true ; } const lowerName = this . name . toLowerCase ( ) ; return DOC_NAMES . includes ( lowerName ) || this . dirPath . startsWith ( "docs" ) ; } isTestRelated ( ) { const dir = `${ this . dirPath }/` ; return ( dir . includes ( "test/" ) || dir . includes ( "tests/" ) || dir . includes ( "spec/" ) || dir . startsWith ( "unit" ) || this . name . includes ( ".test." ) || this . name . includes ( ".spec." ) || this . name . startsWith ( "test_" ) || this . name === ".coveragerc" ) ; } isCIRelated ( ) { return CI_DIRS . includes ( this . dirPath ) || CI_NAMES . includes ( this . name ) ; } isConfigRelated ( ) { return ( CONFIG_EXTENSIONS . includes ( this . extension ) || CONFIG_DIRS . includes ( this . dirPath ) || CONFIG_NAMES . includes ( this . name ) || this . name . includes ( ".eslintrc" ) || this . name . includes ( ".eslintignore" ) || this . name . includes ( ".eslintcache" ) || this . name . includes ( ".prettier" ) || this . name . includes ( "tslint" ) || this . name . includes ( "webpack" ) ) ; } isPackageRelated ( ) { return PACKAGE_NAMES . includes ( this . name ) ; } isBuildRelated ( ) { return ( BUILD_NAMES . includes ( this . name ) || BUILD_EXTENSIONS . includes ( this . extension ) ) ; } isLicenseRelated ( ) { return LICENSE_NAMES . includes ( this . name ) ; } isChoreRelated ( ) { return this . isLicenseRelated ( ) || this . isConfigRelated ( ) ; } /**
     * Return Conventional Commit type.
     *
     * Order of checks is important here.
     *
     * Return the unknown/null value if no rule matches.
     */ getType ( ) { if ( this . isCIRelated ( ) ) { return CONVENTIONAL_TYPE . CI ; } if ( this . isPackageRelated ( ) ) { return CONVENTIONAL_TYPE . BUILD_DEPENDENCIES ; } if ( this . isBuildRelated ( ) ) { return CONVENTIONAL_TYPE . BUILD ; } if ( this . isChoreRelated ( ) ) { return CONVENTIONAL_TYPE . CHORE ; } if ( this . isDocsRelated ( ) ) { return CONVENTIONAL_TYPE . DOCS ; } if ( this . isTestRelated ( ) ) { return CONVENTIONAL_TYPE . TEST ; } return CONVENTIONAL_TYPE . UNKNOWN ; } } /**
 * Get the Conventional Commit type.
 *
 * Relies on both the action performed and the path.
 *
 * Don't distinguish `ACTION.M` vs `ACTION.A`, as it could be a fix or feature. So just use
 * unknown/null value. Though it could be set as always feature or docs as a general rule or config
 * option on the project level or extension level.
 */ export function getConventionType ( action , filePath ) { if ( action === ACTION . R || action === ACTION . D ) { return CONVENTIONAL_TYPE . CHORE ; } const convCommit = new ConventionalCommit ( filePath ) ; const commitType = convCommit . getType ( ) ; if ( action === ACTION . A ) { return commitType || CONVENTIONAL_TYPE . FEAT ; } return commitType ; }