diff --git a/.husky/commit-msg b/.husky/commit-msg new file mode 100755 index 00000000..9962cb60 --- /dev/null +++ b/.husky/commit-msg @@ -0,0 +1,4 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +"./src/index.js" commitlint --edit $1 diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 00000000..96b9aea3 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +"./src/index.js" pre-commit diff --git a/.huskyrc.js b/.huskyrc.js deleted file mode 100644 index ae7e9373..00000000 --- a/.huskyrc.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./src/config/huskyrc') diff --git a/README.md b/README.md index 983d8d40..d3e67e95 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ linting, testing, building, and more. - [Installation](#installation) + - [Husky Setup](#husky-setup) - [Usage](#usage) - [Overriding Config](#overriding-config) - [TypeScript Support](#typescript-support) @@ -45,6 +46,42 @@ npm install --save-dev cod-scripts npm install --save @babel/runtime ``` +### Husky Setup + +In order to take advantage of the `pre-commit` script & `lint-staged` +configuration in `cod-scripts`, you'll need to setup husky in addition to +installing this package. + +#### First time installing `cod-scripts` + +If this is the first time installing `cod-scripts` in your project, run the +following: + +```sh +npx husky install +npm set-script prepare "husky install" +npx husky add .husky/pre-commit 'npx --no-install cod-scripts pre-commit' +npx husky add .husky/commit-msg 'npx --no-install cod-scripts commitlint --edit "$1"' +``` + +> **Note**: See the [overriding `lint-staged`](#lint-staged) section below to +> see how you can extend the `lint-staged` script from `cod-scripts`. + +#### Upgrading from an older version of `cod-scripts` + +Just running the following should work: + +```sh +npm exec -- github:typicode/husky-4-to-7 --remove-v4-config +npm set-script prepare "husky install" +``` + +**Important**: You will need to edit `.husky/commit-msg` after running the above +command. Change `-E HUSKY_GIT_PARAMS` --> `--edit $1`. + +> **Note**: Run `npm install -g npm` if the above command fails. You may be +> running an older version of `npm` that doesn't have the `exec` command. + ## Usage This is a CLI and exposes a bin called `cod-scripts`. I don't really plan on @@ -68,21 +105,27 @@ parts of the config you need to. This can be a very helpful way to make editor integration work for tools like ESLint which require project-based ESLint configuration to be present to work. +#### `eslint` + So, if we were to do this for ESLint, you could create an `.eslintrc` with the contents of: -``` +```json {"extends": "./node_modules/cod-scripts/eslint.js"} ``` > Note: for now, you'll have to include an `.eslintignore` in your project until > [this eslint issue is resolved](https://github.com/eslint/eslint/issues/9227). +#### `babel` + Or, for `babel`, a `.babelrc` with: +```json +{"presets": ["cod-scripts/babel"]} ``` -{ "presets": ["cod-scripts/babel"] } -``` + +#### `jest` Or, for `jest`: @@ -99,15 +142,31 @@ module.exports = Object.assign(jestConfig, { }) ``` +#### `lint-staged` + +Or, for `lint-staged`: + +```js +// lint-staged.config.js or .lintstagedrc.js +const {lintStaged} = require('cod-scripts/config') + +module.exports = { + ...lintStaged, + 'README.md': [`${doctoc} --maxlevel 3 --notitle`], +} +``` + +#### `commitlint` + Or, for `commitlint`, a `commitlint.config.js` file or `commitlint` prop in package.json: ```js // commitlint.config.js or .commitlintrc.js -const {commitlint: commitlintConfig} = require('cod-scripts/commitlint') +const {commitlint} = require('cod-scripts/config') module.exports = { - ...commitlintConfig, + ...commitlint, rules: { // overrides here }, diff --git a/husky.js b/husky.js deleted file mode 100644 index 204c296c..00000000 --- a/husky.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./dist/config/huskyrc') diff --git a/lint-staged.js b/lint-staged.js new file mode 100644 index 00000000..42435c73 --- /dev/null +++ b/lint-staged.js @@ -0,0 +1 @@ +module.exports = require('./dist/config/lintstagedrc.js') diff --git a/package.json b/package.json index b1fc2dc4..2021d1bf 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "eslint.js", "config.js", "eslint.js", - "husky.js", "jest.js", "prettier.js", "shared-tsconfig.json" @@ -44,8 +43,8 @@ "@babel/preset-react": "^7.13.13", "@babel/preset-typescript": "^7.13.0", "@babel/runtime": "^7.14.0", - "@commitlint/cli": "^12.1.4", - "@commitlint/config-conventional": "^12.1.4", + "@commitlint/cli": "^13.1.0", + "@commitlint/config-conventional": "^13.1.0", "@rollup/plugin-babel": "^5.3.0", "@rollup/plugin-commonjs": "^19.0.0", "@rollup/plugin-json": "^4.1.0", @@ -71,7 +70,7 @@ "eslint": "^7.28.0", "eslint-config-codfish": "^10.3.0", "glob": "^7.1.7", - "husky": "^6.0.0", + "husky": "^7.0.1", "is-ci": "^3.0.0", "jest": "^27.0.4", "jest-serializer-path": "^0.1.15", diff --git a/src/config/huskyrc.js b/src/config/huskyrc.js deleted file mode 100644 index b74b6480..00000000 --- a/src/config/huskyrc.js +++ /dev/null @@ -1,10 +0,0 @@ -const {resolveCodScripts} = require('../utils') - -const codScripts = resolveCodScripts() - -module.exports = { - hooks: { - 'pre-commit': `"${codScripts}" pre-commit`, - 'commit-msg': `"${codScripts}" commitlint -E HUSKY_GIT_PARAMS`, - }, -} diff --git a/src/config/index.js b/src/config/index.js index a616a205..589a4625 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -2,7 +2,6 @@ module.exports = { babel: require('./babelrc'), commitlint: require('./commitlint.config'), eslint: require('./eslintrc'), - husky: require('./huskyrc'), jest: require('./jest.config'), lintStaged: require('./lintstagedrc'), prettier: require('./prettierrc'), diff --git a/src/config/lintstagedrc.js b/src/config/lintstagedrc.js index d1770e7f..59e3062f 100644 --- a/src/config/lintstagedrc.js +++ b/src/config/lintstagedrc.js @@ -4,13 +4,13 @@ const codScripts = resolveCodScripts() const doctoc = resolveBin('doctoc') // differs from kcd because of my eslint setup. Want to format everything other -// than js first, then format + lint js using eslint with prettier plugin +// than js first, then format + lint js/ts using eslint with prettier plugin module.exports = { 'README.md': [`${doctoc} --maxlevel 3 --notitle`], - '*.+(json|yml|yaml|css|less|scss|ts|tsx|md|gql|graphql|mdx|vue)': [ + '*.+(json|yml|yaml|css|less|scss|md|gql|graphql|mdx|vue)': [ `${codScripts} format --no-eslint`, ].filter(Boolean), - '*.js': [ + '*.+(js|jsx|ts|tsx)': [ `${codScripts} lint --fix`, `${codScripts} test --findRelatedTests`, ].filter(Boolean), diff --git a/src/scripts/commitlint.js b/src/scripts/commitlint.js index fbbaa1ee..d7d36496 100644 --- a/src/scripts/commitlint.js +++ b/src/scripts/commitlint.js @@ -19,8 +19,12 @@ const config = useBuiltinConfig ? ['--config', hereRelative('../config/commitlint.config.js')] : [] -const result = spawn.sync(resolveBin('commitlint'), [...config, ...args], { - stdio: 'inherit', -}) +const result = spawn.sync( + resolveBin('@commitlint/cli', {executable: 'commitlint'}), + [...config, ...args], + { + stdio: 'inherit', + }, +) process.exit(result.status) diff --git a/src/scripts/pre-commit.js b/src/scripts/pre-commit.js index 97f084aa..67dcdb68 100644 --- a/src/scripts/pre-commit.js +++ b/src/scripts/pre-commit.js @@ -9,8 +9,14 @@ const args = process.argv.slice(2) const useBuiltInConfig = !args.includes('--config') && + !args.includes('-c') && !hasFile('.lintstagedrc') && !hasFile('lint-staged.config.js') && + !hasFile('lintstagedrc.json') && + !hasFile('lintstagedrc.yml') && + !hasFile('lintstagedrc.yaml') && + !hasFile('lintstagedrc.js') && + !hasFile('lintstagedrc.cjs') && !hasPkgProp('lint-staged') const config = useBuiltInConfig