From 7c45806d4da34e7621d2cae822d289d5620ff146 Mon Sep 17 00:00:00 2001 From: Shiba Rin Date: Thu, 22 Jun 2023 00:07:50 +0800 Subject: [PATCH 1/6] chore: install husky --- .husky/pre-commit | 4 ++++ package-lock.json | 16 ++++++++++++++++ package.json | 6 ++++-- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100755 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000000000..610c2a54f1b57 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +npm test diff --git a/package-lock.json b/package-lock.json index 70e53388b1475..3980dfce7e4a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -65,6 +65,7 @@ "feed": "^4.2.2", "gray-matter": "^4.0.3", "handlebars": "^4.7.7", + "husky": "^8.0.0", "jest": "^29.5.0", "jest-environment-jsdom": "^29.5.0", "next-sitemap": "^4.1.3", @@ -15956,6 +15957,21 @@ "node": ">=10.17.0" } }, + "node_modules/husky": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/husky/-/husky-8.0.3.tgz", + "integrity": "sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg==", + "dev": true, + "bin": { + "husky": "lib/bin.js" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", diff --git a/package.json b/package.json index a749db48f4b36..5bc8c68d9ed5f 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "test:storybook": "concurrently -P -k -s first -n \"storybook,test-storybook\" -c \"magenta,blue\" \"npm:storybook -- --ci\" \"wait-on http://localhost:6006 && test-storybook {@}\"", "test:storybook:snapshot": "npm run test:storybook -- -- --updateSnapshot", "test:storybook:watch": "npm run test:storybook -- -- --watch", - "test": "concurrently -s all -n \"test:unit,test:storybook\" -c \"yellow,green\" \"npm:test:unit\" \"npm:test:storybook\"" + "test": "concurrently -s all -n \"test:unit,test:storybook\" -c \"yellow,green\" \"npm:test:unit\" \"npm:test:storybook\"", + "prepare": "husky install" }, "dependencies": { "@emotion/react": "^11.11.1", @@ -105,6 +106,7 @@ "stylelint-selector-bem-pattern": "^2.1.1", "typescript": "^5.0.4", "user-agent-data-types": "^0.3.1", - "wait-on": "^7.0.1" + "wait-on": "^7.0.1", + "husky": "^8.0.0" } } From 4cd40af10377e3ee5c63d104b39eeb1d35bcaba2 Mon Sep 17 00:00:00 2001 From: Shiba Rin Date: Thu, 22 Jun 2023 00:09:37 +0800 Subject: [PATCH 2/6] chore: add empty Node releases data JSON file --- .gitignore | 1 - public/node-releases-data.json | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 public/node-releases-data.json diff --git a/.gitignore b/.gitignore index f82222f8decc4..aa56284814f58 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,6 @@ build public/robots.txt public/sitemap.xml public/en/feed/*.xml -public/node-releases-data.json pages/en/blog/year-[0-9][0-9][0-9][0-9].md # Jest diff --git a/public/node-releases-data.json b/public/node-releases-data.json new file mode 100644 index 0000000000000..fe51488c7066f --- /dev/null +++ b/public/node-releases-data.json @@ -0,0 +1 @@ +[] From 8f927d0ceb7ded0594c92eef0aa96b506da54e32 Mon Sep 17 00:00:00 2001 From: Shiba Rin Date: Thu, 22 Jun 2023 00:12:24 +0800 Subject: [PATCH 3/6] chore: overwrite node-releases-data.json pre-commit --- .husky/pre-commit | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 610c2a54f1b57..3a813a1b7cf3b 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,8 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -npm test +DIR=$(cd `dirname $0` && pwd -P) + +echo "[]" > $DIR/../public/node-releases-data.json + +git add $DIR/../public/node-releases-data.json From 91bbe081741d1682ba65bb1ead0e4bd862ac89e5 Mon Sep 17 00:00:00 2001 From: Shiba Rin Date: Thu, 22 Jun 2023 00:28:24 +0800 Subject: [PATCH 4/6] chore: include Node releases data in bundle --- hooks/useFetchNodeReleases.ts | 60 ------------------------------ providers/nodeReleasesProvider.tsx | 48 ++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 63 deletions(-) delete mode 100644 hooks/useFetchNodeReleases.ts diff --git a/hooks/useFetchNodeReleases.ts b/hooks/useFetchNodeReleases.ts deleted file mode 100644 index b7d484125288b..0000000000000 --- a/hooks/useFetchNodeReleases.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { useMemo } from 'react'; -import useSWR from 'swr'; -import { useRouter } from './useRouter'; -import { getNodeReleaseStatus } from '../util/nodeRelease'; -import type { NodeRelease } from '../types'; - -interface NodeReleaseJSON { - major: number; - version: string; - codename?: string; - currentStart: string; - ltsStart?: string; - maintenanceStart?: string; - endOfLife: string; - npm?: string; - v8?: string; - releaseDate?: string; - modules?: string; -} - -const fetcher = (...args: Parameters) => - fetch(...args).then(res => res.json()); - -export const useFetchNodeReleases = (): NodeRelease[] => { - const { basePath } = useRouter(); - - const { data = [] } = useSWR( - `${basePath}/node-releases-data.json`, - fetcher - ); - - return useMemo(() => { - const now = new Date(); - - return data.map(raw => { - const support = { - currentStart: raw.currentStart, - ltsStart: raw.ltsStart, - maintenanceStart: raw.maintenanceStart, - endOfLife: raw.endOfLife, - }; - - const status = getNodeReleaseStatus(now, support); - - return { - ...support, - major: raw.major, - version: raw.version, - versionWithPrefix: `v${raw.version}`, - codename: raw.codename || '', - isLts: status === 'Active LTS' || status === 'Maintenance LTS', - status: status, - npm: raw.npm || '', - v8: raw.v8 || '', - releaseDate: raw.releaseDate || '', - modules: raw.modules || '', - }; - }); - }, [data]); -}; diff --git a/providers/nodeReleasesProvider.tsx b/providers/nodeReleasesProvider.tsx index 821c29ed6f84c..1f05dba5f0198 100644 --- a/providers/nodeReleasesProvider.tsx +++ b/providers/nodeReleasesProvider.tsx @@ -1,12 +1,54 @@ -import { createContext } from 'react'; -import { useFetchNodeReleases } from '../hooks/useFetchNodeReleases'; +import { createContext, useMemo } from 'react'; +import nodeReleasesData from '../public/node-releases-data.json'; +import { getNodeReleaseStatus } from '../util/nodeRelease'; import type { FC, PropsWithChildren } from 'react'; import type { NodeRelease } from '../types'; +type NodeReleaseJSON = { + major: number; + version: string; + codename?: string; + currentStart: string; + ltsStart?: string; + maintenanceStart?: string; + endOfLife: string; + npm?: string; + v8?: string; + releaseDate?: string; + modules?: string; +}; + export const NodeReleasesContext = createContext([]); export const NodeReleasesProvider: FC = ({ children }) => { - const releases = useFetchNodeReleases(); + const releases = useMemo(() => { + const now = new Date(); + + return nodeReleasesData.map((raw: NodeReleaseJSON) => { + const support = { + currentStart: raw.currentStart, + ltsStart: raw.ltsStart, + maintenanceStart: raw.maintenanceStart, + endOfLife: raw.endOfLife, + }; + + const status = getNodeReleaseStatus(now, support); + + return { + ...support, + major: raw.major, + version: raw.version, + versionWithPrefix: `v${raw.version}`, + codename: raw.codename || '', + isLts: status === 'Active LTS' || status === 'Maintenance LTS', + status: status, + npm: raw.npm || '', + v8: raw.v8 || '', + releaseDate: raw.releaseDate || '', + modules: raw.modules || '', + }; + }); + }, []); return ( From f5f90acadd51a1058bcbee0f3dae0bebb89c3253 Mon Sep 17 00:00:00 2001 From: Shiba Rin Date: Mon, 26 Jun 2023 17:02:22 +0800 Subject: [PATCH 5/6] chore: add pre-commit hooks to CONTRIBUTING.md --- CONTRIBUTING.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 00f14f5cf9b7a..5113880bebc76 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -164,6 +164,12 @@ Commits should be signed. You can read more about [Commit Signing][] here. - Commit messages **must** start with a capital letter - Commit messages **must not** end with a period `.` +### Pre-commit Hooks + +This project uses [husky][] for pre-commit hooks. + +Some JSON files are generated during Build time with empty files as placeholders. Build time happens when you run `npx turbo serve` or `npx turbo build`. We don't want to commit those unnecessary changes. Since these files exist in the repository, `.gitignore` won't work for them. As the workaround, we have a pre-commit hook to discard those changes. + # Pull Request Policy This policy governs how contributions should land within this repository. The lines below state the checks and policies to be followed before merging and on the act of merging. @@ -219,3 +225,4 @@ By contributing to this project, I certify that: [`squash`]: https://help.github.com/en/articles/about-pull-request-merges#squash-and-merge-your-pull-request-commits [Conventional Commits]: https://www.conventionalcommits.org/ [Commit Signing]: https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits +[husky]: https://typicode.github.io/husky/ From 396c317766a74dc80ddc6a4aa3a45857f827604b Mon Sep 17 00:00:00 2001 From: Claudio Wunder Date: Mon, 26 Jun 2023 18:37:14 +0200 Subject: [PATCH 6/6] chore: simplify relese types and fix pre-commit hook --- .husky/pre-commit | 2 +- providers/nodeReleasesProvider.tsx | 18 ++---------------- types/releases.ts | 21 ++++++++++++--------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 3a813a1b7cf3b..c306023e82ea8 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -5,4 +5,4 @@ DIR=$(cd `dirname $0` && pwd -P) echo "[]" > $DIR/../public/node-releases-data.json -git add $DIR/../public/node-releases-data.json +git add --sparse $DIR/../public/node-releases-data.json diff --git a/providers/nodeReleasesProvider.tsx b/providers/nodeReleasesProvider.tsx index 1f05dba5f0198..f1cbe3d9d4e25 100644 --- a/providers/nodeReleasesProvider.tsx +++ b/providers/nodeReleasesProvider.tsx @@ -2,21 +2,7 @@ import { createContext, useMemo } from 'react'; import nodeReleasesData from '../public/node-releases-data.json'; import { getNodeReleaseStatus } from '../util/nodeRelease'; import type { FC, PropsWithChildren } from 'react'; -import type { NodeRelease } from '../types'; - -type NodeReleaseJSON = { - major: number; - version: string; - codename?: string; - currentStart: string; - ltsStart?: string; - maintenanceStart?: string; - endOfLife: string; - npm?: string; - v8?: string; - releaseDate?: string; - modules?: string; -}; +import type { NodeReleaseSource, NodeRelease } from '../types'; export const NodeReleasesContext = createContext([]); @@ -24,7 +10,7 @@ export const NodeReleasesProvider: FC = ({ children }) => { const releases = useMemo(() => { const now = new Date(); - return nodeReleasesData.map((raw: NodeReleaseJSON) => { + return nodeReleasesData.map((raw: NodeReleaseSource) => { const support = { currentStart: raw.currentStart, ltsStart: raw.ltsStart, diff --git a/types/releases.ts b/types/releases.ts index 7b98efe595143..db708a3489e89 100644 --- a/types/releases.ts +++ b/types/releases.ts @@ -16,21 +16,24 @@ export type NodeReleaseStatus = | 'End-of-life' | 'Pending'; -export interface NodeRelease { +export interface NodeReleaseSource { major: number; version: string; - versionWithPrefix: string; - codename: string; - isLts: boolean; - status: NodeReleaseStatus; + codename?: string; currentStart: string; ltsStart?: string; maintenanceStart?: string; endOfLife: string; - npm: string; - v8: string; - releaseDate: string; - modules: string; + npm?: string; + v8?: string; + releaseDate?: string; + modules?: string; +} + +export interface NodeRelease extends NodeReleaseSource { + versionWithPrefix: string; + isLts: boolean; + status: NodeReleaseStatus; } export type NodeReleaseSupport = Pick<