Skip to content

Commit 1d7a83d

Browse files
authored
feat(lint): Add prettier (#76)
## Problem Want to add [`prettier`](https://prettier.io/) support mainly to make development easier so that code auto-formats on save. But if we have `prettier,` we might as well integrate it into CI somehow. ## Solution The solution is multi-pronged: - Added `.prettierrc.json` and `.vscode/settings.json` in order to have code formatting on save. This applies to all file times `.ts`, `.js`, `.json`, `.md`, etc - Added a `.github/workflows/format.yml` workflow that runs the `prettier` CLI and overwrites the files with the changes. If there are file changes, it pushes a new auto-commit to the branch with new changes. That way any changes that were missed will get caught by CI. As a result of the push, CI will run _again_ and we can make sure that running `prettier` didn't break any test with malformed files. Since this was the first run of `prettier`, **lots** of files were updated - Added `prettier` check as part of linting with [`@typescript-eslint/eslint-plugin`](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin). This will fail `benmvp test` if there are malformed files, but most should be fixable when running `benmvp start` - `benmvp integrate` runs `benmvp test` within the `integration-tests/` project, but it needs the same `.prettierrc.json` so those are copied over as part of the script that runs the integration test In the end, it'll be the responsibility of `benmvp start` to create a new project with prettier configs. They will be something that each library can change themselves, allowing a bit of configuration, but the idea is that they wouldn't be. I had to make the file part of the host library instead of within `benmvp-cli` in order to make the dev environment work properly. BREAKING CHANGE: Although CI will auto-format, `benmvp test` will fail because prettier now runs on all the files
1 parent ec5997d commit 1d7a83d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+839
-455
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ jobs:
1212
node: [8, 10, 12, 13]
1313

1414
steps:
15-
- name: Checkout repo
16-
uses: actions/checkout@v1
17-
18-
- name: Use Node ${{ matrix.node }}
19-
uses: actions/setup-node@v1
20-
with:
21-
node-version: ${{ matrix.node }}
22-
23-
- name: Install NPM dependencies
24-
run: npm ci
25-
26-
- name: Run unit tests
27-
run: npm test
28-
env:
29-
CI: true
30-
31-
- name: Run integration tests
32-
run: npm run integrate
33-
env:
34-
CI: true
15+
- name: Checkout repo
16+
uses: actions/checkout@v1
17+
18+
- name: Use Node ${{ matrix.node }}
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: ${{ matrix.node }}
22+
23+
- name: Install NPM dependencies
24+
run: npm ci
25+
26+
- name: Run unit tests
27+
run: npm test
28+
env:
29+
CI: true
30+
31+
- name: Run integration tests
32+
run: npm run integrate
33+
env:
34+
CI: true

.github/workflows/format.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Format files
2+
3+
on:
4+
push:
5+
branches-ignore:
6+
# don't format files on push to master because pushing
7+
# to master triggers version releases and we don't want
8+
# to trigger a re-release just for formatting. We'll wait
9+
# until the next PR to fix the formatting issues
10+
- master
11+
12+
jobs:
13+
prettier:
14+
name: Format files
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout repo
19+
uses: actions/checkout@v1
20+
21+
- name: Use Node 12
22+
uses: actions/setup-node@v1
23+
with:
24+
node-version: 12
25+
26+
- name: Install NPM dependencies
27+
run: npm ci
28+
29+
- name: Format files
30+
run: npx prettier --write "**/*.*"
31+
32+
- name: Show git working tree
33+
run: git status
34+
35+
- name: Commit formatted files on branch
36+
run: |
37+
git config --local user.email "[email protected]"
38+
git config --local user.name "Automated Formatter"
39+
git add -A
40+
# Do nothing if there are no changes
41+
git diff-index --quiet HEAD || git commit -m "Automated format of files"
42+
43+
- name: Push data to repo
44+
uses: ad-m/[email protected]
45+
with:
46+
branch: ${{ github.ref }}
47+
48+
# Need to use special repo-scoped token so that when
49+
# the push happens, it triggers CI again
50+
github_token: ${{ secrets.REPO_SCOPED_TOKEN }}

.prettierignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Ignore dotfiles
2+
**/.*
3+
4+
# Ignore other extensions prettier doesn't know about
5+
**/*.sh
6+
**/*.log
7+
8+
# Ignore NPM files
9+
**/package.json
10+
**/package-lock.json
11+
12+
# Ignore all built files
13+
lib/**/*.*

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true,
4+
"trailingComma": "all",
5+
"arrowParens": "always"
6+
}

.vscode/settings.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
2-
"jest.enableInlineErrorMessages": false,
3-
"jest.enableSnapshotPreviews": false,
4-
"jest.enableSnapshotUpdateMessages": false,
5-
"jest.autoEnable": false
2+
"editor.defaultFormatter": "esbenp.prettier-vscode",
3+
"[json]": {
4+
"editor.defaultFormatter": "esbenp.prettier-vscode"
5+
},
6+
"[markdown]": {
7+
"editor.defaultFormatter": "esbenp.prettier-vscode"
8+
},
9+
"editor.formatOnSave": true,
10+
"json.format.enable": false
611
}

CONTRIBUTING.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ Thank you for your interest in contributing to my CLI!
44

55
## Table of Contents
66

7-
* [Setup](#setup)
8-
* [Branches](#using-branches-to-submit-changes)
9-
* [Keeping up to date](#keeping-your-local-repo-up-to-date)
10-
* [Creating issues](#creating-issues)
11-
* [Working on and submitting changes](#working-on-and-submitting-changes)
12-
* [Steps to submit](#steps-to-submit)
7+
- [Setup](#setup)
8+
- [Branches](#using-branches-to-submit-changes)
9+
- [Keeping up to date](#keeping-your-local-repo-up-to-date)
10+
- [Creating issues](#creating-issues)
11+
- [Working on and submitting changes](#working-on-and-submitting-changes)
12+
- [Steps to submit](#steps-to-submit)
1313

1414
## Setup
1515

@@ -61,4 +61,4 @@ Please try to conform to the coding style of the code base.
6161
1. Check to make sure that your changes are documented properly (inline comments for interesting lines, READMEs, etc.)
6262
1. Run `npm test` to ensure that all tests pass, the linter is satisfied and your changes are typescript compliant.
6363
1. PR titles must be prefixed by the type of changes the PR contains followed by the scope of what the PR touches. We are following the [angular commit guidelines](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines). Please use one of `feat, fix, docs, style, refactor, perf, test, chore` as the prefix. The " is the the direct product your changes affect. Example: `chore(build): Add encrypted ssh key for semantic-release` because its a chore and it touches the build. For multiple scope items, you can comma separate 2 or 3 but if there are more than that please use a `*` instead.
64-
1. Please use a [closing issue keyword](https://help.github.com/articles/closing-issues-using-keywords/) to indicate the issue that your fix addresses in the description section of the pull request template. Example: `fixes #32` to close issue #32
64+
1. Please use a [closing issue keyword](https://help.github.com/articles/closing-issues-using-keywords/) to indicate the issue that your fix addresses in the description section of the pull request template. Example: `fixes #32` to close issue #32

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ A highly-opinionated, zero-config CLI for consistent infra for Ben Ilegbodu's Ty
2222

2323
## ToC
2424

25-
* [Installation](#installation)
26-
* [Quick Usage](#quick-usage)
27-
* [Docs](docs/)
28-
* [Supported Node Versions](#supported-node-versions)
29-
* [Technologies Used](#technologies-used)
30-
* [Contributing](CONTRIBUTING.md)
31-
* [Project philosophy](#project-philosophy)
32-
* [License](LICENSE)
25+
- [Installation](#installation)
26+
- [Quick Usage](#quick-usage)
27+
- [Docs](docs/)
28+
- [Supported Node Versions](#supported-node-versions)
29+
- [Technologies Used](#technologies-used)
30+
- [Contributing](CONTRIBUTING.md)
31+
- [Project philosophy](#project-philosophy)
32+
- [License](LICENSE)
3333

3434
## Installation
3535

docs/api/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ The Node API exposes the following functions:
88
- [`build()`](build.md) - Builds the library into the desired module formats at the specified location
99
- [`integrate()`](integrate.md) - Runs additional integration tests for the library
1010
- [`run()`](run.md) - Parses the specified array of CLI arguments to runs the desired command
11-

docs/api/build.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ Looking for CLI docs? View companion [`benmvp build` documentation](../cli/build
99
Build all module formats in default output directory:
1010

1111
```js
12-
import {build} from '@benmvp/cli'
12+
import { build } from '@benmvp/cli'
1313

1414
build()
1515
```
1616

1717
To specify an alternate output directory:
1818

1919
```js
20-
import {build} from '@benmvp/cli'
20+
import { build } from '@benmvp/cli'
2121

22-
build({out: './built'})
22+
build({ out: './built' })
2323
```
2424

2525
To exclude type definitions:
2626

2727
```js
28-
import {build} from '@benmvp/cli'
28+
import { build } from '@benmvp/cli'
2929

3030
build({
3131
formats: ['esm', 'cjs'],
@@ -35,7 +35,7 @@ build({
3535
To put ESM & type declarations in an alternate build location with continuous watch:
3636

3737
```js
38-
import {build} from '@benmvp/cli'
38+
import { build } from '@benmvp/cli'
3939

4040
build({
4141
formats: ['esm', 'type'],

docs/api/create.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
> NOTE: `create()` is still under development
44
5-
65
Creates a new library with the specified name set up with infrastructure using `@benmvp/cli`, returning a `Promise` indicating whether the creation succeeded or failed.
76

7+
It will:
8+
89
- Add `"test"`, `"start"`, `"build"` and `"integrate"` scripts in the `package.json` to call [`benmvp test`](test.md), [`benmvp start`](start.md), [`benmvp build`](build.md), and [`benmvp integrate`](integrate.md), respectively
910
- After the `package.json` is created (or updated), it will install `@benmvp/cli` as a dev dependency, using [Yarn](https://yarnpkg.com/) if available. If Yarn is unavailable, it will fallback to [npm](https://docs.npmjs.com/)
10-
- Will add (or overwrite) a `.travis.yml` file w/ [build stages](https://docs.travis-ci.com/user/build-stages/) for testing and deploying the library
11+
- Add (or overwrite) `.prettierrc.json`, `.prettierignore` & `.vscode/settings.json` files to format all code
12+
- Add (or overwrite) a `.github/workflows/ci.yml` [Github workflow](https://help.github.com/en/actions) for testing your code when a branch is pushed to or a PR is updated.
13+
- Add (or overwrite) a `.github/workflows/format.yml` [Github workflow](https://help.github.com/en/actions) for formatting your files when a non-`master` branch is pushed to. Formatted code will be pushed as a new commit to the branch.
14+
- Add (or overwrite) a `.github/workflows/release.yml` [Github workflow](https://help.github.com/en/actions) for release a new version of your package with new commits to `master`.
1115

1216
Looking for CLI docs? View companion [`benmvp create` documentation](../cli/create.md).
1317

@@ -16,15 +20,15 @@ Looking for CLI docs? View companion [`benmvp create` documentation](../cli/crea
1620
Create a new lib named `lib-of-fun` with the default settings (simplest setup):
1721

1822
```js
19-
import {create} from '@benmvp/cli'
23+
import { create } from '@benmvp/cli'
2024

21-
create({name: 'lib-of-fun'})
25+
create({ name: 'lib-of-fun' })
2226
```
2327

2428
Add lint verification to an existing library:
2529

2630
```js
27-
import {create} from '@benmvp/cli'
31+
import { create } from '@benmvp/cli'
2832

2933
create({
3034
modes: ['lint'],
@@ -34,7 +38,7 @@ create({
3438
Create a new library named `my-lib` that only outputs ESM format:
3539

3640
```js
37-
import {create} from '@benmvp/cli'
41+
import { create } from '@benmvp/cli'
3842

3943
create({
4044
name: 'my-lib',
@@ -45,7 +49,7 @@ create({
4549
Add custom setup to an existing library:
4650

4751
```js
48-
import {create} from '@benmvp/cli'
52+
import { create } from '@benmvp/cli'
4953

5054
create({
5155
modes: ['type', 'spec'],
@@ -71,10 +75,12 @@ The optional `Options` object supports the following properties:
7175
The name of the library to create or update.
7276

7377
When `name` is unspecified:
78+
7479
- If a `package.json` does not already exist, it creates a new `package.json` with the name matching the directory it's within.
7580
- If a `package.json` does exist, it does nothing to the existing `package.json`.
7681

7782
When `name` is specified:
83+
7884
- If a `package.json` does not already exist, it creates a new `package.json` with the specified name.
7985
- If a `package.json` does exist, it updates the `"name"` property of the `package.json` with specified name.
8086

@@ -112,7 +118,6 @@ Optional. Defaults to all modes when unspecified.
112118

113119
This will initialize the `"start"`, `"test"` and `"integrate"` scripts in the `package.json` to pass the matching argument.
114120

115-
116121
## Return Value
117122

118123
`create()` returns a `Promise`.

0 commit comments

Comments
 (0)