Skip to content

Commit 1ddfb28

Browse files
committed
Update template
1 parent 9cd8ae6 commit 1ddfb28

16 files changed

+279
-27
lines changed

.eslintrc.jest.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
env:
22
jest: true
3+
rules:
4+
# Disable for convenience
5+
react/display-name: off
6+
# Disable for convenience
7+
react/prop-types: off

.eslintrc.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ extends:
88
overrides:
99
- extends: .eslintrc.jest.yml
1010
files:
11+
- '__tests__/**'
12+
- '__tests__/**'
13+
- '__tests__/**'
14+
- '__tests__/**'
1115
- '*.spec.js'
16+
- '*.spec.jsx'
17+
- '*.spec.ts'
18+
- '*.spec.tsx'
1219
- '*.test.js'
20+
- '*.test.jsx'
21+
- '*.test.ts'
22+
- '*.test.tsx'
1323
parser: '@typescript-eslint/parser'
1424
parserOptions:
1525
ecmaVersion: latest

.github/workflows/continuous-deployment.yml

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ name: Continuous deployment
33
on:
44
push:
55
branches: [main]
6+
paths-ignore:
7+
- '.devcontainer/**'
8+
- '.github/**'
69

710
jobs:
811
build:
@@ -11,6 +14,7 @@ jobs:
1114
strategy:
1215
matrix:
1316
node-version: [16.x]
17+
package-name: [use-memo-map]
1418

1519
steps:
1620
- uses: actions/checkout@v3
@@ -26,19 +30,28 @@ jobs:
2630
echo version=`cat package.json | jq -r '.version'` >> $GITHUB_OUTPUT
2731
- name: Propagate versions
2832
run: |
29-
cd packages/use-memo-map
33+
cd packages/${{ matrix.package-name }}
3034
npm version --no-git-tag-version ${{ steps.set-version.outputs.version }}
3135
- run: npm clean-install
3236
- run: npm run build --if-present
3337
- name: Run npm pack
3438
run: |
35-
cd packages/use-memo-map
39+
cd packages/${{ matrix.package-name }}
3640
npm pack
3741
- name: Upload tarball artifact
3842
uses: actions/[email protected]
3943
with:
4044
name: tarball
41-
path: packages/use-memo-map/*.tgz
45+
path: packages/${{ matrix.package-name }}/*.tgz
46+
- name: Build pages
47+
run: |
48+
cd packages/pages
49+
npm install ../${{ matrix.package-name }}/*.tgz
50+
npm run build
51+
- name: Upload pages artifact
52+
uses: actions/upload-pages-artifact@v1
53+
with:
54+
path: packages/pages/public
4255

4356
publish-package:
4457
needs: build
@@ -69,3 +82,18 @@ jobs:
6982
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
7083
- name: Summarize job
7184
run: echo NPM package published to https://npmjs.com/package/${{ steps.read-package-json.outputs.package-name }}/v/${{ steps.read-package-json.outputs.version }}. >> $GITHUB_STEP_SUMMARY
85+
86+
publish-pages:
87+
environment:
88+
name: github-pages
89+
url: ${{ steps.deployment.outputs.page_url }}
90+
needs: build
91+
permissions:
92+
pages: write
93+
id-token: write
94+
runs-on: ubuntu-latest
95+
96+
steps:
97+
- id: deployment
98+
name: Deploy to GitHub Pages
99+
uses: actions/deploy-pages@v2

.github/workflows/publish-release.yml

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ on:
77
jobs:
88
build:
99
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
package-name: [use-memo-map]
13+
1014
steps:
1115
- uses: actions/checkout@v3
1216
- name: Use Node.js 16
@@ -19,13 +23,13 @@ jobs:
1923
run: echo version=`cat package.json | jq -r '.version'` >> $GITHUB_OUTPUT
2024
- name: Propagate versions
2125
run: |
22-
cd packages/use-memo-map
26+
cd packages/${{ matrix.package-name }}
2327
npm version --no-git-tag-version ${{ steps.read-package-json.outputs.version }}
2428
- run: npm clean-install
2529
- run: npm run build --if-present
2630
- name: Run npm pack
2731
run: |
28-
cd packages/use-memo-map
32+
cd packages/${{ matrix.package-name }}
2933
npm pack
3034
- name: Upload changelog artifact
3135
uses: actions/[email protected]
@@ -36,7 +40,16 @@ jobs:
3640
uses: actions/[email protected]
3741
with:
3842
name: tarball
39-
path: packages/use-memo-map/*.tgz
43+
path: packages/${{ matrix.package-name }}/*.tgz
44+
- name: Build pages
45+
run: |
46+
cd packages/pages
47+
npm install ../${{ matrix.package-name }}/*.tgz
48+
npm run build
49+
- name: Upload pages artifact
50+
uses: actions/upload-pages-artifact@v1
51+
with:
52+
path: packages/pages/public
4053

4154
draft-release:
4255
needs:
@@ -82,9 +95,9 @@ jobs:
8295
run: |
8396
echo ::error::Cannot publish prerelease version ${{ steps.read-package-json.outputs.version }}
8497
exit 1
85-
# - run: npm publish `ls ./*.tgz`
86-
# env:
87-
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
98+
- run: npm publish `ls ./*.tgz`
99+
env:
100+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
88101
- name: Summarize job
89102
run: echo NPM package published to https://npmjs.com/package/${{ steps.read-package-json.outputs.package-name }}/v/${{ steps.read-package-json.outputs.version }}. >> $GITHUB_STEP_SUMMARY
90103

@@ -104,6 +117,8 @@ jobs:
104117
run: echo GitHub release created at https://github.com/${{ github.repository }}/releases/tag/${{ github.ref_name }}. >> $GITHUB_STEP_SUMMARY
105118

106119
create-bump-pull-request:
120+
continue-on-error: true
121+
environment: production
107122
needs:
108123
- publish-package
109124
permissions:
@@ -126,20 +141,43 @@ jobs:
126141
npm version prepatch --no-git-tag-version
127142
echo version=`cat package.json | jq -r .version` >> $GITHUB_OUTPUT
128143
- name: Create branch
144+
id: create-branch
129145
run: |
130-
git checkout -b bump-${{ steps.bump-version.outputs.version }}
146+
BRANCH_NAME=bump-${{ steps.bump-version.outputs.version }}
147+
echo branch-name=$BRANCH_NAME >> $GITHUB_OUTPUT
148+
149+
git checkout -b $BRANCH_NAME
131150
git add --all
132151
git commit --message "Bump to ${{ steps.bump-version.outputs.version }}"
133-
git push --set-upstream origin bump-${{ steps.bump-version.outputs.version }}
134-
- env:
135-
GH_TOKEN: ${{ github.token }}
152+
git push --set-upstream origin $BRANCH_NAME
153+
- continue-on-error: true
154+
env:
155+
GH_TOKEN: ${{ secrets.GH_TOKEN_CREATE_PULL_REQUEST }}
136156
id: create-pull-request
137157
name: Create pull request
138-
run: echo url=`gh pr create --assignee ${{ github.actor }} --base main --body "" --title "Bump to ${{ steps.bump-version.outputs.version }}" || true` >> $GITHUB_OUTPUT
158+
run: |
159+
PULL_REQUEST_NUMBER=`gh api repos/${{ github.repository }}/pulls --field base=main --field head=${{ steps.create-branch.outputs.branch-name }} --field title="Bump to ${{ steps.bump-version.outputs.version }}" | jq -r '.number'`
160+
gh api repos/${{ github.repository }}/issues/$PULL_REQUEST_NUMBER/assignees --field assignees[]=${{ github.actor }}
161+
echo url=https://github.com/${{ github.repository }}/pull/$PULL_REQUEST_NUMBER >> $GITHUB_OUTPUT
139162
- name: Summarize job
140163
run: |
141164
if [ "${{ steps.create-pull-request.outputs.url }}" = "" ]; then
142-
echo "Branch created at [bump-${{ steps.bump-version.outputs.version }}](https://github.com/${{ github.repository }}/compare/main...bump-${{ steps.bump-version.outputs.version }})" >> $GITHUB_STEP_SUMMARY
165+
echo "Branch created at [bump-${{ steps.bump-version.outputs.version }}](https://github.com/${{ github.repository }}/compare/main...${{ steps.create-branch.outputs.branch-name }})" >> $GITHUB_STEP_SUMMARY
143166
else
144167
echo Pull request created at ${{ steps.create-pull-request.outputs.url }}. >> $GITHUB_STEP_SUMMARY
145168
fi
169+
170+
publish-pages:
171+
environment:
172+
name: github-pages
173+
url: ${{ steps.deployment.outputs.page_url }}
174+
needs: build
175+
permissions:
176+
pages: write
177+
id-token: write
178+
runs-on: ubuntu-latest
179+
180+
steps:
181+
- id: deployment
182+
name: Deploy to GitHub Pages
183+
uses: actions/deploy-pages@v2

.github/workflows/pull-request-validation.yml

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: Pull request validation
33
on:
44
pull_request:
55
branches: [main]
6+
workflow_dispatch: {}
67

78
jobs:
89
build:
@@ -11,6 +12,7 @@ jobs:
1112
strategy:
1213
matrix:
1314
node-version: [16.x]
15+
package-name: [use-memo-map]
1416

1517
steps:
1618
- uses: actions/checkout@v3
@@ -25,25 +27,69 @@ jobs:
2527
- run: npm run build --if-present
2628
- name: Run npm pack
2729
run: |
28-
cd packages/use-memo-map
30+
cd packages/${{ matrix.package-name }}
2931
npm pack
3032
- name: Upload tarball artifact
3133
uses: actions/[email protected]
3234
with:
3335
name: tarball
34-
path: packages/use-memo-map/*.tgz
36+
path: packages/${{ matrix.package-name }}/*.tgz
37+
- name: Build pages
38+
run: |
39+
cd packages/pages
40+
npm install
41+
npm run build
42+
- name: Upload pages artifact
43+
uses: actions/upload-pages-artifact@v1
44+
with:
45+
path: packages/pages/public
3546
- name: Run unit tests
3647
run: |
37-
cd packages/use-memo-map
48+
cd packages/${{ matrix.package-name }}
3849
npm test
3950
51+
sanity-check:
52+
needs: build
53+
runs-on: ubuntu-latest
54+
55+
strategy:
56+
matrix:
57+
package-name: [use-memo-map]
58+
59+
steps:
60+
- name: Download tarball artifact
61+
uses: actions/download-artifact@v3
62+
with:
63+
name: tarball
64+
- name: Extract tarball to root
65+
run: tar -xvf ${{ matrix.package-name }}-*.tgz --strip-components=1
66+
- name: "Sanity check: has LICENSE"
67+
run: compgen -G LICENSE
68+
- name: "Sanity check: has README.md"
69+
run: compgen -G README.md
70+
- name: "Sanity check: main exists"
71+
run: cat package.json | jq -r '.main' | xargs -L1 test -f $1
72+
- name: "Sanity check: typings exists"
73+
run: cat package.json | jq -r '.types // .typings' | xargs -L1 test -f $1
74+
- name: "Sanity check: all exports exists"
75+
run: cat package.json | jq '.exports | map(flatten) | flatten | .[]' | xargs -L1 test -f $1
76+
- name: "Sanity check: has source maps"
77+
run: compgen -G lib/**/*.js.map
78+
- name: "Sanity check: no lib/**/*.spec.*"
79+
run: "! compgen -G lib/**/*.spec.*"
80+
- name: "Sanity check: no lib/**/*.test.*"
81+
run: "! compgen -G lib/**/*.test.*"
82+
- name: "Sanity check: no dependencies are pre-release"
83+
run: cat package.json | jq '.dependencies | to_entries | map(.value | select(contains("-"))) | if length > 0 then halt_error(1) else empty end'
84+
4085
integration-test:
4186
needs: build
4287
runs-on: ubuntu-latest
4388

4489
strategy:
4590
matrix:
4691
node-version: [16.x]
92+
package-name: [use-memo-map]
4793

4894
steps:
4995
- uses: actions/checkout@v3
@@ -56,7 +102,7 @@ jobs:
56102
uses: actions/[email protected]
57103
with:
58104
name: tarball
59-
path: packages/use-memo-map
105+
path: packages/${{ matrix.package-name }}
60106
- name: Run npm install
61107
run: |
62108
cd packages/integration-test

packages/pages/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/node_modules/
2+
/package-lock.json
3+
/public/static/css/main.css
4+
/public/static/js/main.js

packages/pages/package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "use-memo-map-pages",
3+
"version": "0.0.0-0",
4+
"description": "",
5+
"scripts": {
6+
"build": "npm run build:typescript && npm run build:esbuild",
7+
"build:esbuild": "esbuild --bundle --entry-names=[dir]/[ext]/main --minify --outdir=./public/static/ ./src/index.tsx",
8+
"build:typescript": "tsc --project ./src/tsconfig.json",
9+
"bump": "npm run bump:prod && npm run bump:dev && npm run bump:auditfix",
10+
"bump:auditfix": "npm audit fix || exit 0",
11+
"bump:dev": "if [ `cat package.json | jq -r '(.devDependencies // {}) | length'` -ne 0 ]; then npm install $(cat package.json | jq -r '(.pinDependencies // {}) as $p | ((.devDependencies // {}) | keys) | map(. + \"@\" + ($p[.] // [\"latest\"])[0]) | .[]'); fi",
12+
"bump:prod": "if [ `cat package.json | jq -r '(.dependencies // {}) | length'` -ne 0 ]; then npm install --save-exact $(cat package.json | jq -r '(.pinDependencies // {}) as $p | ((.dependencies // {}) | keys) | map(. + \"@\" + ($p[.] // [\"latest\"])[0]) | .[]'); fi",
13+
"postbump": "cat package.json | jq '. + (.dependencies = (((.dependencies // {}) + (.localPeerDependencies // {})) | to_entries | sort_by(.key) | from_entries)) | (.devDependencies = (((.devDependencies // {}) + (.localPeerDevDependencies // {})) | to_entries | sort_by(.key) | from_entries))' > package-temp.json && mv package-temp.json package.json",
14+
"prebump": "cat package.json | jq '(((.localPeerDependencies // {}) | keys | map([\"dependencies\", .])) + ((.localPeerDevDependencies // {}) | keys | map([\"devDependencies\", .]))) as $localPeerPaths | delpaths($localPeerPaths)' > package-temp.json && mv package-temp.json package.json",
15+
"start": "esbuild --bundle --entry-names=[dir]/[ext]/main --minify --outdir=./public/static/ --servedir=./public ./src/index.tsx"
16+
},
17+
"author": "William Wong (https://github.com/compulim)",
18+
"license": "MIT",
19+
"devDependencies": {
20+
"@types/react": "^18.0.28",
21+
"@types/react-dom": "^18.0.11",
22+
"esbuild": "^0.17.10",
23+
"typescript": "^4.9.5"
24+
},
25+
"dependencies": {
26+
"react": "18.2.0",
27+
"react-dom": "18.2.0",
28+
"use-memo-map": "file:../use-memo-map/use-memo-map-0.0.0-0.tgz"
29+
},
30+
"localPeerDependencies": {
31+
"use-memo-map": "file:../use-memo-map/use-memo-map-0.0.0-0.tgz"
32+
}
33+
}

packages/pages/public/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>use-memo-map demo</title>
5+
<link href="./assets/css/effect.css" rel="stylesheet" type="text/css" />
6+
<link href="./static/css/main.css" rel="stylesheet" type="text/css" />
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script crossorigin="anonymous" src="./static/js/main.js"></script>
11+
</body>
12+
</html>

packages/pages/src/App.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React, { useCallback } from 'react';
2+
import { useMemoMap } from 'use-memo-map';
3+
4+
const App = () => {
5+
const multiplyBy10 = useCallback<(value: number) => number>(value => {
6+
// Calls to this function will be memoized based on its first argument.
7+
// You can do expensive calls here.
8+
return value * 10;
9+
}, []);
10+
11+
// useMemoMap() will return a function that take an array.
12+
const map = useMemoMap(multiplyBy10);
13+
14+
const output = map([1, 2, 3]); // Returns [10, 20, 30].
15+
16+
return (
17+
<ul>
18+
{output.map(value => (
19+
<li>{value}</li>
20+
))}
21+
</ul>
22+
);
23+
};
24+
25+
export default App;

packages/pages/src/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createRoot } from 'react-dom/client';
2+
import React from 'react';
3+
4+
import App from './App';
5+
6+
const rootElement = document.getElementById('root');
7+
8+
rootElement && createRoot(rootElement).render(<App />);

0 commit comments

Comments
 (0)