Skip to content

Commit 96e5af1

Browse files
authored
Merge pull request #158 from nhooyr/ts
Switch CI to Typescript
2 parents d81a14e + 862b00f commit 96e5af1

24 files changed

+1979
-196
lines changed

.github/CONTRIBUTING.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,29 @@ CI must pass on your changes for them to be merged.
2727

2828
### CI
2929

30-
CI will ensure your code is formatted correctly, passes linting and tests.
30+
CI will ensure your code is formatted, lints and passes tests.
3131
It will collect coverage and report it to [codecov](https://codecov.io/gh/nhooyr/websocket)
32-
and also upload a `coverage` artifact that you can download to inspect browse coverage.
32+
and also upload a html `coverage` artifact that you can download to browse coverage.
3333

34-
You can run CI locally. The various steps are located in [ci/\*.sh](../ci).
34+
You can run CI locally. You only need [Go](https://golang.org), [nodejs](https://nodejs.org/en/) and [yarn](https://yarnpkg.com).
3535

36-
1. [fmt.sh](../ci/fmt.sh) which requires node (specifically prettier).
37-
1. [lint.sh](../ci/lint.sh) which requires [shellcheck](https://github.com/koalaman/shellcheck#installing).
38-
1. [test.sh](../ci/test.sh)
39-
1. [run.sh](../ci/run.sh) which runs the above scripts in order.
36+
See the scripts in [package.json](../package.json).
4037

41-
For coverage details locally, see `ci/out/coverage.html` after running [test.sh](../ci/test.sh).
38+
1. `yarn fmt` performs code generation and formatting.
39+
1. `yarn lint` performs linting.
40+
1. `yarn test` runs tests.
41+
1. `yarn all` runs the above scripts in parallel.
42+
43+
For coverage details locally, see `ci/out/coverage.html` after running `yarn test`.
44+
45+
CI is written with nodejs to enable running as much as possible concurrently.
4246

4347
See [ci/image/Dockerfile](../ci/image/Dockerfile) for the installation of the CI dependencies on Ubuntu.
4448

45-
You can also run tests normally with `go test`. [test.sh](../ci/test.sh) just passes a default set of flags to
46-
`go test` to collect coverage and also prettify the output.
49+
You can also run tests normally with `go test`. `yarn test` just passes a default set of flags to
50+
`go test` to collect coverage and runs the WASM tests.
4751

48-
You can pass flags to [test.sh](../ci/test.sh) if you want to run a specific test or otherwise
52+
You can pass flags to `yarn test` if you want to run a specific test or otherwise
4953
control the behaviour of `go test` but also get coverage.
5054

5155
Coverage percentage from codecov and the CI scripts will be different because they are calculated differently.

.github/workflows/ci.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,26 @@ on: [push]
44
jobs:
55
fmt:
66
runs-on: ubuntu-latest
7-
container: docker://nhooyr/websocket-ci@sha256:b6331f8f64803c8b1bbd2a0ee9e2547317e0de2348bccd9c8dbcc1d88ff5747f
7+
container: nhooyr/websocket-ci@sha256:f8b6e53a9fd256bcf6c90029276385b9ec730b76a0d7ccf3ff19084bce210c50
88
steps:
99
- uses: actions/checkout@v1
10-
- run: ./ci/fmt.sh
10+
- run: yarn --frozen-lockfile && yarn fmt
1111
lint:
1212
runs-on: ubuntu-latest
13-
container: docker://nhooyr/websocket-ci@sha256:b6331f8f64803c8b1bbd2a0ee9e2547317e0de2348bccd9c8dbcc1d88ff5747f
13+
container: nhooyr/websocket-ci@sha256:f8b6e53a9fd256bcf6c90029276385b9ec730b76a0d7ccf3ff19084bce210c50
1414
steps:
1515
- uses: actions/checkout@v1
16-
- run: ./ci/lint.sh
16+
- run: yarn --frozen-lockfile && yarn lint
1717
test:
1818
runs-on: ubuntu-latest
19-
container: docker://nhooyr/websocket-ci@sha256:b6331f8f64803c8b1bbd2a0ee9e2547317e0de2348bccd9c8dbcc1d88ff5747f
19+
container: nhooyr/websocket-ci@sha256:f8b6e53a9fd256bcf6c90029276385b9ec730b76a0d7ccf3ff19084bce210c50
2020
steps:
2121
- uses: actions/checkout@v1
22-
- run: ./ci/test.sh
22+
- run: yarn --frozen-lockfile && yarn test
2323
env:
2424
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2525
- name: Upload coverage.html
2626
uses: actions/upload-artifact@master
2727
with:
2828
name: coverage
2929
path: ci/out/coverage.html
30-
wasm:
31-
runs-on: ubuntu-latest
32-
container: docker://nhooyr/websocket-ci@sha256:b6331f8f64803c8b1bbd2a0ee9e2547317e0de2348bccd9c8dbcc1d88ff5747f
33-
steps:
34-
- uses: actions/checkout@v1
35-
- run: ./ci/wasm.sh

ci/.eslintrc.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
parser: "@typescript-eslint/parser"
2+
env:
3+
node: true
4+
5+
parserOptions:
6+
ecmaVersion: 2018
7+
sourceType: module
8+
9+
extends:
10+
# https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#usage
11+
- eslint:recommended
12+
- plugin:@typescript-eslint/eslint-recommended
13+
- plugin:@typescript-eslint/recommended
14+
# https://www.npmjs.com/package/eslint-plugin-import#typescript
15+
- plugin:import/recommended
16+
- plugin:import/typescript
17+
# https://dev.to/robertcoopercode/using-eslint-and-prettier-in-a-typescript-project-53jb
18+
- prettier/@typescript-eslint
19+
20+
rules:
21+
"@typescript-eslint/no-use-before-define": off
22+
"@typescript-eslint/explicit-function-return-type": off
23+
"@typescript-eslint/no-non-null-assertion": off

ci/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

ci/all.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env -S npx ts-node -P ci/tsconfig.json
2+
3+
import { fmt, gen } from "./fmt"
4+
import { main } from "./lib"
5+
import { lint } from "./lint"
6+
import { test } from "./test"
7+
8+
main(run)
9+
10+
async function run(ctx: Promise<unknown>) {
11+
await gen(ctx)
12+
13+
await Promise.all([
14+
fmt(ctx),
15+
lint(ctx),
16+
test(ctx),
17+
])
18+
}

ci/fmt.sh

Lines changed: 0 additions & 49 deletions
This file was deleted.

ci/fmt.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env -S npx ts-node -P ci/tsconfig.json
2+
3+
import { exec, main } from "./lib"
4+
5+
if (require.main === module) {
6+
main(async (ctx: Promise<unknown>) => {
7+
await gen(ctx)
8+
await fmt(ctx)
9+
})
10+
}
11+
12+
export async function fmt(ctx: Promise<unknown>) {
13+
await Promise.all([
14+
exec(ctx, "go mod tidy"),
15+
exec(ctx, "gofmt -w -s ."),
16+
exec(ctx, `go run go.coder.com/go-tools/cmd/goimports -w "-local=$(go list -m)" .`),
17+
exec(ctx, `npx prettier --write --print-width=120 --no-semi --trailing-comma=all --loglevel=silent $(git ls-files "*.yaml" "*.yml" "*.md")`),
18+
],
19+
)
20+
21+
if (process.env.CI) {
22+
const r = await exec(ctx, "git ls-files --other --modified --exclude-standard")
23+
const files = r.stdout.toString().trim()
24+
if (files.length) {
25+
console.log(`files need generation or are formatted incorrectly:
26+
${files}
27+
28+
please run:
29+
./ci/fmt.js`)
30+
process.exit(1)
31+
}
32+
}
33+
}
34+
35+
export async function gen(ctx: Promise<unknown>) {
36+
await exec(ctx, "go generate ./...")
37+
}

ci/image/Dockerfile

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
FROM golang:1
22

3-
ENV DEBIAN_FRONTEND=noninteractive
3+
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
4+
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
5+
6+
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
7+
RUN apt-get install -y nodejs chromium yarn
8+
9+
COPY ./ci/image/gitignore /etc/git/ignore
10+
RUN git config --system color.ui always
11+
# Need to set set this explicitly for the system since github uses HOME=/home/github.
12+
RUN git config --system core.excludesfile /etc/git/ignore
13+
414
ENV GOPATH=/root/gopath
515
ENV PATH=$GOPATH/bin:$PATH
616
ENV GOFLAGS="-mod=readonly"
717
ENV PAGER=cat
818
ENV CI=true
919

10-
RUN apt-get update && \
11-
apt-get install -y shellcheck npm chromium && \
12-
npm install -g prettier
13-
14-
# https://github.com/golang/go/wiki/WebAssembly#running-tests-in-the-browser
15-
RUN go get github.com/agnivade/wasmbrowsertest && \
16-
mv $GOPATH/bin/wasmbrowsertest $GOPATH/bin/go_js_wasm_exec
17-
18-
RUN git config --global color.ui always
19-
20-
# Cache go modules and build cache.
20+
# Cache go modules, build cache and yarn cache.
2121
COPY . /tmp/websocket
2222
RUN cd /tmp/websocket && \
23-
CI= ./ci/run.sh && \
23+
yarn && CI= yarn ci && \
2424
rm -rf /tmp/websocket

ci/image/dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

ci/image/gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.DS_Store
3+
.idea
4+
.gitignore
5+
.dockerignore

0 commit comments

Comments
 (0)