10000 Feat(gallery): Add Pokemon examples by NgocNhi123 · Pull Request #220 · thien-do/moai · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feat(gallery): Add Pokemon examples #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/gallery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dist"
],
"scripts": {
"example": "ts-node ./scripts/example/generate.ts",
"build": "yarn && rollup --config",
"prepublishOnly": "yarn build"
},
Expand All @@ -32,6 +33,7 @@
"@moai/core": "^1.0.0-rc9",
"@rollup/plugin-alias": "^3.1.2",
"@types/react": "^17.0.11",
"node-fetch": "^2.6.1",
"postcss": "^8.3.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand All @@ -41,6 +43,7 @@
"rollup-plugin-delete": "^2.0.0",
"rollup-plugin-postcss": "^4.0.0",
"rollup-plugin-typescript2": "^0.30.0",
"ts-node": "^10.0.0",
"tslib": "^2.3.0",
"typescript": "~4.3.4"
}
Expand Down
68 changes: 68 additions & 0 deletions lib/gallery/scripts/example/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import fetch from "node-fetch";
import * as fs from "fs/promises";
import * as path from "path";
import { RawPokemon, RawPokemonList } from "./raw";
import { normalizePokemon } from "./normalize";
import { Pokemon } from "./interface";

type PokemonType = Pokemon["types"][0];

const getTypes = (pokemons: Pokemon[]): PokemonType[] => {
const set = pokemons.reduce((set, pokemon) => {
pokemon.types.forEach((type) => {
if (set.has(type)) return;
set.add(type);
});
return set;
}, new Set<PokemonType>());
return [...set];
};

const printProgress = (progress: string) => {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write(progress + "%");
};

const main = async () => {
// Skip if already populated (remove this to manually force a re-fetch)
const distPath = path.resolve(__dirname, "../../src/examples");
try {
await fs.access(distPath);
console.log("Examples found");
return;
} catch (e) {
console.log("Examples not found, generating...");
}

// Fetch list of pokemons
const resp = await fetch("https://pokeapi.co/api/v2/pokemon?limit=100");
const raw = (await resp.json()) as RawPokemonList;

// Fetch each pokemon
const count = { value: 0 };
const promises = raw.results.map(async (result) => {
const resp = await fetch(result.url);
const raw = (await resp.json()) as RawPokemon;
count.value++;
printProgress(count.value.toString());
const pokemon = normalizePokemon(raw);
return pokemon;
});
const pokemons = await Promise.all(promises);
const types = getTypes(pokemons);

// Write the result
fs.mkdir(distPath);
fs.writeFile(
path.resolve(distPath, "pokemons.json"),
JSON.stringify(pokemons, null, 2)
);
fs.writeFile(
path.resolve(distPath, "types.json"),
JSON.stringify(types, null, 2)
);
return pokemons;
};

main();
16 changes: 16 additions & 0 deletions lib/gallery/scripts/example/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface Pokemon {
id: number;
name: string;
image: {
static: string;
animated: string;
};
weight: number;
height: number;
moves: string;
stats: {
name: string;
base: number;
}[];
types: string[];
}
22 changes: 22 additions & 0 deletions lib/gallery/scripts/example/normalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Pokemon } from "./interface";
import { RawPokemon } from "./raw";

const getBw = (raw: RawPokemon) =>
raw.sprites.versions["generation-v"]["black-white"];

export const normalizePokemon = (raw: RawPokemon): Pokemon => ({
height: raw.height,
id: raw.id,
image: {
static: getBw(raw).front_default,
animated: getBw(raw).animated.front_default,
},
moves: raw.moves.map((move) => move.move.name).join(", "),
name: raw.name,
stats: raw.stats.map((stat) => ({
base: stat.base_stat,
name: stat.stat.name,
})),
types: raw.types.map((type) => type.type.name),
weight: raw.weight,
});
87 changes: 87 additions & 0 deletions lib/gallery/scripts/example/raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
interface Ability2 {
name: string;
url: string;
}

interface Ability {
ability: Ability2;
is_hidden: boolean;
slot: number;
}

interface Move2 {
name: string;
url: string;
}

interface Move {
move: Move2;
}

interface Animated {
front_default: string;
}

interface BlackWhite {
animated: Animated;
front_default: string;
}

interface GenerationV {
"black-white": BlackWhite;
}

interface Versions {
"generation-v": GenerationV;
}

interface Sprites {
front_default: string;
versions: Versions;
}

interface Stat2 {
name: string;
url: string;
}

interface Stat {
base_stat: number;
effort: number;
stat: Stat2;
}

interface Type2 {
name: string;
url: string;
}

interface Type {
slot: number;
type: Type2;
}

export interface RawPokemon {
abilities: Ability[];
base_experience: number;
height: number;
id: number;
moves: Move[];
name: string;
sprites: Sprites;
stats: Stat[];
types: Type[];
weight: number;
}

interface Result {
name: string;
url: string;
}

export interface RawPokemonList {
count: number;
next: string;
previous: unknown;
results: Result[];
}
1 change: 1 addition & 0 deletions lib/gallery/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/examples
63 changes: 62 additions & 1 deletion lib/yarn.lock
F438
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,26 @@
dependencies:
tippy.js "^6.3.1"

"@tsconfig/node10@^1.0.7":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.8.tgz#c1e4e80d6f964fbecb3359c43bd48b40f7cadad9"
integrity sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==

"@tsconfig/node12@^1.0.7":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@tsconfig/node12/-/node12-1.0.9.tgz#62c1f6dee2ebd9aead80dc3afa56810e58e1a04c"
integrity sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==

"@tsconfig/node14@^1.0.0":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@tsconfig/node14/-/node14-1.0.1.tgz#95f2d167ffb9b8d2068b0b235302fafd4df711f2"
integrity sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==

"@tsconfig/node16@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.1.tgz#a6ca6a9a0ff366af433f42f5f0e124794ff6b8f1"
integrity sha512-FTgBI767POY/lKNDNbIzgAX6miIDBs6NTCbdlDb8TrWovHsSvaVIZDlTqym29C6UqhzwcJx4CYr+AlrMywA0cA==

"@types/braces@*":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/braces/-/braces-3.0.0.tgz#7da1c0d44ff1c7eb660a36ec078ea61ba7eb42cb"
Expand Down Expand Up @@ -2984,6 +3004,11 @@ are-we-there-yet@~1.1.2:
delegates "^1.0.0"
readable-stream "^2.0.6"

arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==

argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
Expand Down Expand Up @@ -4246,6 +4271,11 @@ create-react-context@0.3.0:
gud "^1.0.0"
warning "^4.0.3"

create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

cross-spawn@7.0.3, cross-spawn@^7.0.0:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
Expand Down Expand Up @@ -4596,6 +4626,11 @@ detect-port@^1.3.0:
address "^1.0.1"
debug "^2.6.0"

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

diffie-hellman@^5.0.0:
version "5.0.3"
resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.3.tgz#40e8ee98f55a2149607146921c63e1ae5f3d2875"
Expand Down Expand Up @@ -7049,6 +7084,11 @@ make-dir@^3.0.0, make-dir@^3.0.2, make-dir@^3.1.0:
dependencies:
semver "^6.0.0"

make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

makeerror@1.0.x:
version "1.0.11"
resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c"
Expand Down Expand Up @@ -9681,7 +9721,7 @@ source-map-resolve@^0.5.0:
source-map-url "^0.4.0"
urix "^0.1.0"

source-map-support@^0.5.16, source-map-support@~0.5.12, source-map-support@~0.5.19:
source-map-support@^0.5.16, source-map-support@^0.5.17, source-map-support@~0.5.12, source-map-support@~0.5.19:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
Expand Down Expand Up @@ -10305,6 +10345,22 @@ ts-essentials@^2.0.3:
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-2.0.12.tgz#c9303f3d74f75fa7528c3d49b80e089ab09d8745"
integrity sha512-3IVX4nI6B5cc31/GFFE+i8ey/N2eA0CZDbo6n0yrz0zDX8ZJ8djmU1p+XRz7G3is0F3bB3pu2pAroFdAWQKU3w==

ts-node@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.0.0.tgz#05f10b9a716b0b624129ad44f0ea05dac84ba3be"
integrity sha512-ROWeOIUvfFbPZkoDis0L/55Fk+6gFQNZwwKPLinacRl6tsxstTF1DbAcLKkovwnpKMVvOMHP1TIbnwXwtLg1gg==
dependencies:
"@tsconfig/node10" "^1.0.7"
"@tsconfig/node12" "^1.0.7"
"@tsconfig/node14" "^1.0.0"
"@tsconfig/node16" "^1.0.1"
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"

ts-pnp@^1.1.6:
version "1.2.0"
resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.2.0.tgz#a500ad084b0798f1c3071af391e65912c86bca92"
Expand Down Expand Up @@ -11010,6 +11066,11 @@ yargs@^16.2.0:
y18n "^5.0.5"
yargs-parser "^20.2.2"

yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
Expand Down
0