Skip to content

Commit 78e652a

Browse files
authored
refactor: Move package.json and other front-end collateral into 'site' (#128)
This refactors the front-end collateral to all live within `site` - so no `package.json` at the root. The reason we had this initially is that the jest test run and NextJS actually require having _two_ different `tsconfig`s - Next needs `jsx:"preserve"`, while jest needs `jsx:"react"` - we were using `tsconfig`s at different levels at the hierarchy to manage this. I changed this behavior to still use two different `tsconfig.json`s, which is mandatory - but just side-by-side in `site`. Once that's fixed, it was easy to move everything into `site` Follow up from: #118 (comment)
1 parent 3ba8242 commit 78e652a

19 files changed

+177
-148
lines changed

.github/workflows/coder.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ jobs:
6161

6262
- name: Install node_modules
6363
run: yarn install
64+
working-directory: site
6465

6566
- name: "yarn lint"
6667
run: yarn lint
68+
working-directory: site
6769

6870
gen:
6971
name: "style/gen"
@@ -113,6 +115,7 @@ jobs:
113115

114116
- name: Install node_modules
115117
run: yarn install
118+
working-directory: site
116119

117120
- name: "make ${{ matrix.style }}"
118121
run: "make --output-sync -j ${{ matrix.style }}"
@@ -194,15 +197,18 @@ jobs:
194197
node-version: "14"
195198

196199
- run: yarn install
200+
working-directory: site
197201

198202
- run: yarn build
203+
working-directory: site
199204

200205
- run: yarn test:coverage
206+
working-directory: site
201207

202208
- uses: codecov/codecov-action@v2
203209
if: github.actor != 'dependabot[bot]'
204210
with:
205211
token: ${{ secrets.CODECOV_TOKEN }}
206-
files: ./coverage/lcov.info
212+
files: ./site/coverage/lcov.info
207213
flags: unittest-js
208214
fail_ci_if_error: true

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ yarn-error.log
1616

1717
# Front-end ignore
1818
.next/
19+
site/.eslintcache
1920
site/.next/
21+
site/node_modules/
22+
site/yarn-error.log
2023
coverage/
2124

2225
# Build
2326
bin/
24-
site/out/
27+
site/out/

Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ fmt/prettier:
2626
@echo "--- prettier"
2727
# Avoid writing files in CI to reduce file write activity
2828
ifdef CI
29-
yarn run format:check
29+
cd site && yarn run format:check
3030
else
31-
yarn run format:write
31+
cd site && yarn run format:write
3232
endif
3333
.PHONY: fmt/prettier
3434

@@ -74,6 +74,6 @@ provisionersdk/proto: provisionersdk/proto/provisioner.proto
7474
.PHONY: provisionersdk/proto
7575

7676
site/out:
77-
yarn build
78-
yarn export
77+
cd site && yarn build
78+
cd site && yarn export
7979
.PHONY: site/out

jest.config.js

-36
This file was deleted.

.eslintignore renamed to site/.eslintignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
# COPY PASTA OF .gitignore
33
###############################################################################
44
node_modules
5-
vendor
5+
vendor
6+
out
7+
coverage
8+
.next

.eslintrc.yaml renamed to site/.eslintrc.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ parser: "@typescript-eslint/parser"
1818
parserOptions:
1919
ecmaVersion: 2018
2020
project:
21-
- "./tsconfig.json"
22-
- "./site/tsconfig.json"
21+
- "./tsconfig.test.json"
2322
sourceType: module
2423
ecmaFeatures:
2524
jsx: true

.prettierignore renamed to site/.prettierignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ yarn-error.log
1212

1313
# Front-end ignore
1414
.next/
15-
site/.next/
16-
site/out/
17-
coverage/
15+
coverage/
16+
out/
File renamed without changes.

_jest/setupTests.ts renamed to site/_jest/setupTests.ts

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
// Set up 'next-router-mock' to with our front-end tests:
66
// https://github.com/scottrippey/next-router-mock#quick-start
77
jest.mock("next/router", () => require("next-router-mock"))
8+
9+
// Suppress isolated modules warning
10+
export {}
+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import React from "react"
22

33
export interface ErrorSummaryProps {
4-
error: Error
4+
error: Error | undefined
55
}
66

77
export const ErrorSummary: React.FC<ErrorSummaryProps> = ({ error }) => {
88
// TODO: More interesting error page
9+
10+
if (typeof error === "undefined") {
11+
return <div>{"Unknown error"}</div>
12+
}
13+
914
return <div>{error.toString()}</div>
1015
}
File renamed without changes.

site/jest.config.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module.exports = {
2+
projects: [
3+
{
4+
globals: {
5+
"ts-jest": {
6+
tsconfig: "tsconfig.test.json",
7+
},
8+
},
9+
coverageReporters: ["text", "lcov"],
10+
displayName: "test",
11+
preset: "ts-jest",
12+
roots: ["<rootDir>"],
13+
setupFilesAfterEnv: ["<rootDir>/_jest/setupTests.ts"],
14+
transform: {
15+
"^.+\\.tsx?$": "ts-jest",
16+
},
17+
testEnvironment: "jsdom",
18+
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
19+
testPathIgnorePatterns: ["/node_modules/", "/__tests__/fakes"],
20+
moduleDirectories: ["node_modules", "<rootDir>"],
21+
},
22+
{
23+
displayName: "lint",
24+
runner: "jest-runner-eslint",
25+
testMatch: ["<rootDir>/**/*.js", "<rootDir>/**/*.ts", "<rootDir>/**/*.tsx"],
26+
testPathIgnorePatterns: ["/.next/", "/out/", "/_jest/", "jest.config.js", "jest-runner.*.js", "next.config.js"],
27+
},
28+
],
29+
collectCoverageFrom: [
30+
"<rootDir>/**/*.js",
31+
"<rootDir>/**/*.ts",
32+
"<rootDir>/**/*.tsx",
33+
"!<rootDir>/**/*.stories.tsx",
34+
"!<rootDir>/.next/**/*.*",
35+
"!<rootDir>/api.ts",
36+
"!<rootDir>/dev.ts",
37+
"!<rootDir>/next-env.d.ts",
38+
"!<rootDir>/next.config.js",
39+
"!<rootDir>/out/**/*.*",
40+
],
41+
}

package.json renamed to site/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"repository": "https://github.com/coder/coder",
55
"private": true,
66
"scripts": {
7-
"build": "NODE_ENV=production next build site",
8-
"build:dev": "next build site",
9-
"dev": "ts-node site/dev.ts",
10-
"export": "next export site",
7+
"build": "NODE_ENV=production next build",
8+
"build:dev": "next build",
9+
"dev": "ts-node dev.ts",
10+
"export": "next export",
1111
"format:check": "prettier --check '**/*.{css,html,js,json,jsx,md,ts,tsx,yaml,yml}'",
1212
"format:write": "prettier --write '**/*.{css,html,js,json,jsx,md,ts,tsx,yaml,yml}' && sql-formatter -l postgresql ./database/query.sql -o ./database/query.sql",
1313
"lint": "jest --selectProjects lint",

site/pages/projects/[organization]/[project]/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ const ProjectPage: React.FC = () => {
7878
<div className={styles.root}>
7979
<Navbar user={me} onSignOut={signOut} />
8080
<Header
81-
title={firstOrItem(project)}
82-
description={firstOrItem(organization)}
81+
title={firstOrItem(project, "")}
82+
description={firstOrItem(organization, "")}
8383
subTitle={`${workspaces.length} workspaces`}
8484
action={{
8585
text: "Create Workspace",

site/tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"extends": "../tsconfig.json",
32
"compilerOptions": {
43
"outDir": "./dist/",
54
"noImplicitAny": true,
@@ -13,12 +12,13 @@
1312
"isolatedModules": true,
1413
"lib": ["dom", "dom.iterable", "esnext"],
1514
"skipLibCheck": true,
16-
"strict": false,
15+
"strict": true,
16+
"strictNullChecks": true,
1717
"forceConsistentCasingInFileNames": true,
1818
"noEmit": true,
1919
"incremental": true,
2020
"moduleResolution": "node"
2121
},
2222
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
23-
"exclude": ["node_modules"]
23+
"exclude": ["node_modules", "_jest"]
2424
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
2+
"extends": "./tsconfig.json",
23
"compilerOptions": {
34
"target": "es5",
45
"module": "commonjs",
56
"jsx": "react",
67
"downlevelIteration": true,
78
"strict": true,
9+
"strictNullChecks": true,
810
"esModuleInterop": true
911
}
1012
}

site/util/array.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { firstOrItem } from "./array"
33
describe("array", () => {
44
describe("firstOrItem", () => {
55
it("returns null if empty array", () => {
6-
expect(firstOrItem([])).toBeNull()
6+
expect(firstOrItem([], null)).toBeNull()
77
})
88

99
it("returns first item if array with more one item", () => {
10-
expect(firstOrItem(["a", "b"])).toEqual("a")
10+
expect(firstOrItem(["a", "b"], "c")).toEqual("a")
1111
})
1212

1313
it("returns item if single item", () => {
14-
expect(firstOrItem("c")).toEqual("c")
14+
expect(firstOrItem("c", "d")).toEqual("c")
1515
})
1616
})
1717
})

site/util/array.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
* - If an array with 1 or more elements, returns the first element
55
* - If a single item, returns that item
66
*/
7-
export const firstOrItem = <T>(itemOrItems: T | T[]): T | null => {
7+
export const firstOrItem = <T>(itemOrItems: undefined | T | T[], defaults: T): T => {
88
if (Array.isArray(itemOrItems)) {
9-
return itemOrItems.length > 0 ? itemOrItems[0] : null
9+
return itemOrItems.length > 0 ? itemOrItems[0] : defaults
10+
}
11+
12+
if (typeof itemOrItems === "undefined") {
13+
return defaults
1014
}
1115

1216
return itemOrItems

0 commit comments

Comments
 (0)