Compare commits
29 Commits
v1.0.2-rc.
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
905444068f | ||
|
|
1526ced25e | ||
|
|
f66842bcf8 | ||
|
|
dcdf20756a | ||
|
|
6b34e9a272 | ||
|
|
579bcf1afe | ||
|
|
1018f7a937 | ||
|
|
4edf90f41b | ||
|
|
faf76e6eb6 | ||
|
|
5bede02e06 | ||
|
|
4f7ff9bc90 | ||
|
|
b0d592375a | ||
|
|
672fe6bf0e | ||
|
|
829a59da98 | ||
|
|
58176f658e | ||
|
|
9c881735b7 | ||
|
|
3c697c687e | ||
|
|
825e58e5fd | ||
|
|
9232c14971 | ||
|
|
cd3320efff | ||
|
|
553d8d8efa | ||
|
|
e80a44ca5f | ||
|
|
4ced502e11 | ||
|
|
0374e20a50 | ||
|
|
7ba88d1fbd | ||
|
|
909a9c7fce | ||
|
|
e1dcae75b9 | ||
|
|
5df41911e6 | ||
|
|
c9b130d9da |
2
.github/workflows/main.yml
vendored
2
.github/workflows/main.yml
vendored
@@ -36,7 +36,7 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: update action.yml
|
||||
- name: update action.yml to use image from local Dockerfile
|
||||
run: |
|
||||
sed -i "s/image: .*/image: Dockerfile/" action.yml
|
||||
|
||||
|
||||
47
.github/workflows/release.yml
vendored
47
.github/workflows/release.yml
vendored
@@ -11,11 +11,6 @@ on:
|
||||
description:
|
||||
description: "Version description"
|
||||
type: string
|
||||
prerelease:
|
||||
description: "Prerelease"
|
||||
default: false
|
||||
required: true
|
||||
type: boolean
|
||||
|
||||
jobs:
|
||||
release:
|
||||
@@ -32,7 +27,8 @@ jobs:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
|
||||
- uses: docker/build-push-action@v2
|
||||
- name: build and publish the docker image
|
||||
uses: docker/build-push-action@v2
|
||||
id: docker-build
|
||||
with:
|
||||
push: true
|
||||
@@ -40,7 +36,7 @@ jobs:
|
||||
platane/snk:${{ github.sha }}
|
||||
platane/snk:${{ github.event.inputs.version }}
|
||||
|
||||
- name: update action.yml
|
||||
- name: update action.yml to point to the newly created docker image
|
||||
run: |
|
||||
sed -i "s/image: .*/image: docker:\/\/platane\/snk@${{ steps.docker-build.outputs.digest }}/" action.yml
|
||||
|
||||
@@ -49,21 +45,36 @@ jobs:
|
||||
cache: yarn
|
||||
node-version: 16
|
||||
|
||||
- name: bump version
|
||||
run: yarn version --no-git-tag-version --new-version ${{ github.event.inputs.version }}
|
||||
|
||||
- name: build svg-only action
|
||||
run: |
|
||||
yarn install --frozen-lockfile
|
||||
yarn build:action
|
||||
mv packages/action/dist/* svg-only/
|
||||
rm -r svg-only/dist
|
||||
mv packages/action/dist svg-only/dist
|
||||
|
||||
- name: push new commit
|
||||
uses: EndBug/add-and-commit@v7
|
||||
with:
|
||||
add: package.json svg-only action.yml
|
||||
message: 📦 ${{ github.event.inputs.version }}
|
||||
tag: v${{ github.event.inputs.version }}
|
||||
- name: bump package version
|
||||
run: yarn version --no-git-tag-version --new-version ${{ github.event.inputs.version }}
|
||||
|
||||
- name: push new build, tag version and push
|
||||
id: push-tags
|
||||
run: |
|
||||
VERSION=${{ github.event.inputs.version }}
|
||||
|
||||
git config --global user.email "bot@platane.me"
|
||||
git config --global user.name "release bot"
|
||||
git add package.json svg-only/dist action.yml
|
||||
git commit -m "📦 $VERSION"
|
||||
git tag v$VERSION
|
||||
git push origin master --tags
|
||||
|
||||
if [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
|
||||
git tag v$( echo $VERSION | cut -d. -f 1-1 )
|
||||
git tag v$( echo $VERSION | cut -d. -f 1-2 )
|
||||
git push origin --tags --force
|
||||
echo ::set-output name=prerelease::false
|
||||
else
|
||||
echo ::set-output name=prerelease::true
|
||||
fi
|
||||
|
||||
- uses: actions/create-release@v1
|
||||
env:
|
||||
@@ -71,4 +82,4 @@ jobs:
|
||||
with:
|
||||
tag_name: v${{ github.event.inputs.version }}
|
||||
body: ${{ github.event.inputs.description }}
|
||||
prerelease: ${{ github.event.inputs.prerelease }}
|
||||
prerelease: ${{ steps.push-tags.outputs.prerelease }}
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,4 +2,5 @@ node_modules
|
||||
npm-debug.log*
|
||||
yarn-error.log*
|
||||
dist
|
||||
!svg-only/dist
|
||||
build
|
||||
@@ -1,5 +1,6 @@
|
||||
# snk
|
||||
|
||||
[](https://github.com/platane/snk/releases/latest)
|
||||
[](https://github.com/marketplace/actions/generate-snake-game-from-github-contribution-grid)
|
||||

|
||||

|
||||
@@ -20,7 +21,7 @@ Available as github action. Automatically generate a new image at the end of the
|
||||
**github action**
|
||||
|
||||
```yaml
|
||||
- uses: Platane/snk@master
|
||||
- uses: Platane/snk@v1
|
||||
with:
|
||||
# github user name to read the contribution graph from (**required**)
|
||||
# using action context var `github.repository_owner` or specified user
|
||||
@@ -35,7 +36,9 @@ Available as github action. Automatically generate a new image at the end of the
|
||||
svg_out_path: dist/github-snake.svg
|
||||
```
|
||||
|
||||
> [example with cron job](https://github.com/Platane/Platane/blob/master/.github/workflows/main.yml#L24-L29)
|
||||
[example with cron job](https://github.com/Platane/Platane/blob/master/.github/workflows/main.yml#L24-L29)
|
||||
|
||||
If you are only interested in generating a svg, you can use this other faster action: `uses: Platane/snk/svg-only@v1`
|
||||
|
||||
**interactive demo**
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ author: "platane"
|
||||
|
||||
runs:
|
||||
using: docker
|
||||
image: docker://platane/snk@sha256:c0130b001a33f4781baf5d182701b82a483513da2886ab694de8fd00d51272f9
|
||||
image: docker://platane/snk@sha256:e2cd3268901a65f1402bea9133b8d6ed4a34796ca92ead9686f5aa048f22487a
|
||||
|
||||
inputs:
|
||||
github_user_name:
|
||||
|
||||
10
package.json
10
package.json
@@ -1,23 +1,23 @@
|
||||
{
|
||||
"name": "snk",
|
||||
"description": "Generates a snake game from a github user contributions grid",
|
||||
"version": "v1.0.2-rc.4",
|
||||
"version": "1.1.4",
|
||||
"private": true,
|
||||
"repository": "github:platane/snk",
|
||||
"devDependencies": {
|
||||
"@types/jest": "27.4.1",
|
||||
"@types/node": "16.11.7",
|
||||
"jest": "27.5.1",
|
||||
"prettier": "2.6.0",
|
||||
"ts-jest": "27.1.3",
|
||||
"typescript": "4.6.2"
|
||||
"prettier": "2.6.2",
|
||||
"ts-jest": "27.1.4",
|
||||
"typescript": "4.6.3"
|
||||
},
|
||||
"workspaces": [
|
||||
"packages/**"
|
||||
],
|
||||
"scripts": {
|
||||
"type": "tsc --noEmit",
|
||||
"lint": "yarn prettier -c '**/*.{ts,js,json,md,yml,yaml}' '!packages/*/dist/**'",
|
||||
"lint": "yarn prettier -c '**/*.{ts,js,json,md,yml,yaml}' '!packages/*/dist/**' '!svg-only/dist/**'",
|
||||
"test": "jest --verbose --passWithNoTests --no-cache",
|
||||
"dev:demo": "( cd packages/demo ; yarn dev )",
|
||||
"build:demo": "( cd packages/demo ; yarn build )",
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { getGithubUserContribution } from "@snk/github-user-contribution";
|
||||
import { userContributionToGrid } from "./userContributionToGrid";
|
||||
import { getBestRoute } from "@snk/solver/getBestRoute";
|
||||
import { createGif } from "@snk/gif-creator";
|
||||
import { createSvg } from "../svg-creator";
|
||||
import { snake4 } from "@snk/types/__fixtures__/snake";
|
||||
import { getPathToPose } from "@snk/solver/getPathToPose";
|
||||
|
||||
@@ -41,11 +39,13 @@ export const generateContributionSnake = async (
|
||||
|
||||
if (format.gif) {
|
||||
console.log("📹 creating gif");
|
||||
const { createGif } = await import("@snk/gif-creator");
|
||||
output.gif = await createGif(grid, chain, drawOptions, gifOptions);
|
||||
}
|
||||
|
||||
if (format.svg) {
|
||||
console.log("🖌 creating svg");
|
||||
const { createSvg } = await import("@snk/svg-creator");
|
||||
output.svg = createSvg(grid, chain, drawOptions, gifOptions);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,10 +4,13 @@
|
||||
"dependencies": {
|
||||
"@actions/core": "1.6.0",
|
||||
"@snk/gif-creator": "1.0.0",
|
||||
"@snk/github-user-contribution": "1.0.0"
|
||||
"@snk/github-user-contribution": "1.0.0",
|
||||
"@snk/solver": "1.0.0",
|
||||
"@snk/svg-creator": "1.0.0",
|
||||
"@snk/types": "1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@zeit/ncc": "0.22.3",
|
||||
"@vercel/ncc": "0.24.1",
|
||||
"ts-node": "10.7.0"
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
import { getBestRoute } from "@snk/solver/getBestRoute";
|
||||
import { Color, copyGrid, Grid } from "@snk/types/grid";
|
||||
import { step } from "@snk/solver/step";
|
||||
import { isStableAndBound, stepSpring } from "./springUtils";
|
||||
import { Res } from "@snk/github-user-contribution";
|
||||
import { Snake } from "@snk/types/snake";
|
||||
import type { Res } from "@snk/github-user-contribution";
|
||||
import type { Snake } from "@snk/types/snake";
|
||||
import {
|
||||
drawLerpWorld,
|
||||
getCanvasWorldSize,
|
||||
Options,
|
||||
} from "@snk/draw/drawWorld";
|
||||
import { userContributionToGrid } from "../action/userContributionToGrid";
|
||||
import { snake4 as snake } from "@snk/types/__fixtures__/snake";
|
||||
import { getPathToPose } from "@snk/solver/getPathToPose";
|
||||
import { createSvg } from "../svg-creator";
|
||||
import { userContributionToGrid } from "@snk/action/userContributionToGrid";
|
||||
import { createSvg } from "@snk/svg-creator";
|
||||
import { createRpcClient } from "./worker-utils";
|
||||
import type { API as WorkerAPI } from "./demo.interactive.worker";
|
||||
|
||||
const createForm = ({
|
||||
onSubmit,
|
||||
@@ -47,15 +46,24 @@ const createForm = ({
|
||||
|
||||
form.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
onSubmit(input.value).catch((err) => {
|
||||
label.innerText = "error :(";
|
||||
throw err;
|
||||
});
|
||||
|
||||
onSubmit(input.value)
|
||||
.finally(() => {
|
||||
clearTimeout(timeout);
|
||||
})
|
||||
.catch((err) => {
|
||||
label.innerText = "error :(";
|
||||
throw err;
|
||||
});
|
||||
|
||||
input.disabled = true;
|
||||
submit.disabled = true;
|
||||
form.appendChild(label);
|
||||
label.innerText = "loading ...";
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
label.innerText = "loading ( it might take a while ) ... ";
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
//
|
||||
@@ -75,6 +83,7 @@ const createGithubProfile = () => {
|
||||
container.style.opacity = "0";
|
||||
container.style.display = "flex";
|
||||
container.style.flexDirection = "column";
|
||||
container.style.height = "120px";
|
||||
container.style.alignItems = "flex-start";
|
||||
const image = document.createElement("img");
|
||||
image.style.width = "100px";
|
||||
@@ -232,13 +241,24 @@ const onSubmit = async (userName: string) => {
|
||||
};
|
||||
|
||||
const grid = userContributionToGrid(cells, colorScheme);
|
||||
const chain = getBestRoute(grid, snake)!;
|
||||
chain.push(...getPathToPose(chain.slice(-1)[0], snake)!);
|
||||
|
||||
const chain = await getChain(grid);
|
||||
|
||||
dispose();
|
||||
|
||||
createViewer({ grid0: grid, chain, drawOptions });
|
||||
};
|
||||
|
||||
const worker = new Worker(
|
||||
new URL(
|
||||
"./demo.interactive.worker.ts",
|
||||
// @ts-ignore
|
||||
import.meta.url
|
||||
)
|
||||
);
|
||||
|
||||
const { getChain } = createRpcClient<WorkerAPI>(worker);
|
||||
|
||||
const profile = createGithubProfile();
|
||||
const { dispose } = createForm({
|
||||
onSubmit,
|
||||
|
||||
17
packages/demo/demo.interactive.worker.ts
Normal file
17
packages/demo/demo.interactive.worker.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { getBestRoute } from "@snk/solver/getBestRoute";
|
||||
import { getPathToPose } from "@snk/solver/getPathToPose";
|
||||
import { snake4 as snake } from "@snk/types/__fixtures__/snake";
|
||||
import type { Grid } from "@snk/types/grid";
|
||||
import { createRpcServer } from "./worker-utils";
|
||||
|
||||
const getChain = (grid: Grid) => {
|
||||
const chain = getBestRoute(grid, snake)!;
|
||||
chain.push(...getPathToPose(chain.slice(-1)[0], snake)!);
|
||||
|
||||
return chain;
|
||||
};
|
||||
|
||||
const api = { getChain };
|
||||
export type API = typeof api;
|
||||
|
||||
createRpcServer(api);
|
||||
@@ -1,6 +1,6 @@
|
||||
import "./menu";
|
||||
import { getBestRoute } from "@snk/solver/getBestRoute";
|
||||
import { createSvg } from "../svg-creator";
|
||||
import { createSvg } from "@snk/svg-creator";
|
||||
import { grid, snake } from "./sample";
|
||||
import { drawOptions } from "./canvas";
|
||||
import { getPathToPose } from "@snk/solver/getPathToPose";
|
||||
|
||||
@@ -2,20 +2,22 @@
|
||||
"name": "@snk/demo",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@snk/action": "1.0.0",
|
||||
"@snk/draw": "1.0.0",
|
||||
"@snk/github-user-contribution": "1.0.0",
|
||||
"@snk/solver": "1.0.0",
|
||||
"canvas": "2.9.1",
|
||||
"gifsicle": "5.3.0"
|
||||
"@snk/svg-creator": "1.0.0",
|
||||
"@snk/types": "1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/dat.gui": "0.7.7",
|
||||
"dat.gui": "0.7.7",
|
||||
"dat.gui": "0.7.9",
|
||||
"html-webpack-plugin": "5.5.0",
|
||||
"ts-loader": "9.2.6",
|
||||
"ts-loader": "9.2.8",
|
||||
"ts-node": "10.7.0",
|
||||
"webpack": "5.70.0",
|
||||
"webpack": "5.72.0",
|
||||
"webpack-cli": "4.9.2",
|
||||
"webpack-dev-server": "4.7.4"
|
||||
"webpack-dev-server": "4.8.1"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "webpack",
|
||||
|
||||
59
packages/demo/worker-utils.ts
Normal file
59
packages/demo/worker-utils.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
type API = Record<string, (...args: any[]) => any>;
|
||||
|
||||
const symbol = "worker-rpc__";
|
||||
|
||||
export const createRpcServer = (api: API) =>
|
||||
self.addEventListener("message", async (event) => {
|
||||
if (event.data?.symbol === symbol) {
|
||||
try {
|
||||
const res = await api[event.data.methodName](...event.data.args);
|
||||
self.postMessage({ symbol, key: event.data.key, res });
|
||||
} catch (error: any) {
|
||||
postMessage({ symbol, key: event.data.key, error: error.message });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const createRpcClient = <API_ extends API>(worker: Worker) => {
|
||||
const originalTerminate = worker.terminate;
|
||||
worker.terminate = () => {
|
||||
worker.dispatchEvent(new Event("terminate"));
|
||||
originalTerminate.call(worker);
|
||||
};
|
||||
|
||||
return new Proxy(
|
||||
{} as {
|
||||
[K in keyof API_]: (
|
||||
...args: Parameters<API_[K]>
|
||||
) => Promise<Awaited<ReturnType<API_[K]>>>;
|
||||
},
|
||||
{
|
||||
get:
|
||||
(_, methodName) =>
|
||||
(...args: any[]) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const key = Math.random().toString();
|
||||
|
||||
const onTerminate = () => {
|
||||
worker.removeEventListener("terminate", onTerminate);
|
||||
worker.removeEventListener("message", onMessageHandler);
|
||||
reject(new Error("worker terminated"));
|
||||
};
|
||||
|
||||
const onMessageHandler = (event: MessageEvent) => {
|
||||
if (event.data?.symbol === symbol && event.data.key === key) {
|
||||
if (event.data.error) reject(event.data.error);
|
||||
else if (event.data.res) resolve(event.data.res);
|
||||
|
||||
worker.removeEventListener("terminate", onTerminate);
|
||||
worker.removeEventListener("message", onMessageHandler);
|
||||
}
|
||||
};
|
||||
|
||||
worker.addEventListener("message", onMessageHandler);
|
||||
worker.addEventListener("terminate", onTerminate);
|
||||
worker.postMessage({ symbol, key, methodName, args });
|
||||
}),
|
||||
}
|
||||
);
|
||||
};
|
||||
@@ -12,7 +12,7 @@
|
||||
"devDependencies": {
|
||||
"@types/gifsicle": "5.2.0",
|
||||
"@types/tmp": "0.2.3",
|
||||
"@zeit/ncc": "0.22.3"
|
||||
"@vercel/ncc": "0.24.1"
|
||||
},
|
||||
"scripts": {
|
||||
"benchmark": "ncc run __tests__/benchmark.ts --quiet"
|
||||
|
||||
@@ -47,7 +47,7 @@ describe("getGithubUserContribution", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should match snapshot for year=2019", async () => {
|
||||
xit("should match snapshot for year=2019", async () => {
|
||||
expect(
|
||||
await getGithubUserContribution("platane", { year: 2019 })
|
||||
).toMatchSnapshot();
|
||||
|
||||
@@ -56,13 +56,17 @@ const parseUserPage = (content: string) => {
|
||||
|
||||
//
|
||||
// parse cells
|
||||
const rawCells = $(".js-calendar-graph rect[data-count]")
|
||||
const rawCells = $(".js-calendar-graph rect[data-level][data-date]")
|
||||
.toArray()
|
||||
.map((x) => {
|
||||
const level = +x.attribs["data-level"];
|
||||
const count = +x.attribs["data-count"];
|
||||
const date = x.attribs["data-date"];
|
||||
|
||||
const literalCount = $(x)
|
||||
.text()
|
||||
.match(/(No|\d+) contributions? on/)![1];
|
||||
const count = literalCount === "No" ? 0 : +literalCount;
|
||||
|
||||
const color = colorScheme[level];
|
||||
|
||||
if (!color) throw new Error("could not determine the color of the cell");
|
||||
@@ -124,8 +128,6 @@ const getSvgPosition = (
|
||||
return p;
|
||||
};
|
||||
|
||||
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T;
|
||||
|
||||
export type Res = ThenArg<ReturnType<typeof getGithubUserContribution>>;
|
||||
export type Res = Awaited<ReturnType<typeof getGithubUserContribution>>;
|
||||
|
||||
export type Cell = Res["cells"][number];
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"cheerio": "1.0.0-rc.10",
|
||||
"node-fetch": "2.6.1"
|
||||
"node-fetch": "2.6.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node-fetch": "2.6.1"
|
||||
|
||||
@@ -22,8 +22,6 @@ export const createGrid = (
|
||||
const styles = [
|
||||
`.c{
|
||||
shape-rendering: geometricPrecision;
|
||||
rx: ${sizeBorderRadius};
|
||||
ry: ${sizeBorderRadius};
|
||||
fill: var(--ce);
|
||||
stroke-width: 1px;
|
||||
stroke: var(--cb);
|
||||
@@ -56,6 +54,8 @@ export const createGrid = (
|
||||
class: ["c", id].filter(Boolean).join(" "),
|
||||
x: x * s + m,
|
||||
y: y * s + m,
|
||||
rx: sizeBorderRadius,
|
||||
ry: sizeBorderRadius,
|
||||
width: d,
|
||||
height: d,
|
||||
})
|
||||
|
||||
@@ -115,6 +115,10 @@ export const createSvg = (
|
||||
xmlns: "http://www.w3.org/2000/svg",
|
||||
}).replace("/>", ">"),
|
||||
|
||||
"<desc>",
|
||||
"Generated with https://github.com/Platane/snk",
|
||||
"</desc>",
|
||||
|
||||
"<style>",
|
||||
optimizeCss(style),
|
||||
"</style>",
|
||||
|
||||
@@ -6,4 +6,4 @@ As a drawback, it can not generate gif image.
|
||||
|
||||
## Build process
|
||||
|
||||
file is built and push on release, by the release action.
|
||||
dist file are built and push on release, by the release action.
|
||||
|
||||
@@ -4,7 +4,7 @@ author: "platane"
|
||||
|
||||
runs:
|
||||
using: node16
|
||||
main: index.js
|
||||
main: dist/index.js
|
||||
|
||||
inputs:
|
||||
github_user_name:
|
||||
|
||||
81391
svg-only/index.js → svg-only/dist/index.js
vendored
81391
svg-only/index.js → svg-only/dist/index.js
vendored
File diff suppressed because one or more lines are too long
@@ -6,7 +6,8 @@
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"esModuleInterop": true
|
||||
"esModuleInterop": true,
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user