Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

"./src/index.js" commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

"./src/index.js" pre-commit
1 change: 0 additions & 1 deletion .huskyrc.js

This file was deleted.

69 changes: 64 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ linting, testing, building, and more.
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Installation](#installation)
- [Husky Setup](#husky-setup)
- [Usage](#usage)
- [Overriding Config](#overriding-config)
- [TypeScript Support](#typescript-support)
Expand All @@ -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
Expand All @@ -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`:

Expand All @@ -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
},
Expand Down
1 change: 0 additions & 1 deletion husky.js

This file was deleted.

1 change: 1 addition & 0 deletions lint-staged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/config/lintstagedrc.js')
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"eslint.js",
"config.js",
"eslint.js",
"husky.js",
"jest.js",
"prettier.js",
"shared-tsconfig.json"
Expand All @@ -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",
Expand All @@ -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",
Expand Down
10 changes: 0 additions & 10 deletions src/config/huskyrc.js

This file was deleted.

1 change: 0 additions & 1 deletion src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
6 changes: 3 additions & 3 deletions src/config/lintstagedrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 7 additions & 3 deletions src/scripts/commitlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 6 additions & 0 deletions src/scripts/pre-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down