Skip to content

Commit

Permalink
refactor(periodic-data): extract periodic data into separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
GervinFung committed Apr 9, 2024
1 parent c497d9e commit d5231c4
Show file tree
Hide file tree
Showing 15 changed files with 493 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@emotion/styled": "^11.11.5",
"@fontsource-variable/jetbrains-mono": "^5.0.20",
"@mui/joy": "5.0.0-beta.32",
"@periotable/data": "workspace:^",
"@poolofdeath20/util": "^0.8.0",
"bowser": "^2.11.0",
"next": "^14.1.4",
Expand Down
3 changes: 2 additions & 1 deletion apps/web/pages/compounds/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Box from '@mui/joy/Box';

import { Optional } from '@poolofdeath20/util';

import data from '../../src/web/generated/data';
import data from '@periotable/data';

import useSearchQuery from '../../src/web/hooks/search';
import Seo from '../../src/web/components/seo';
import ListOfCompounds, {
Expand Down
3 changes: 2 additions & 1 deletion apps/web/pages/elements/[name]/[section].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { GetStaticPaths, GetStaticProps } from 'next';

import { Defined } from '@poolofdeath20/util';

import data from '../../../src/web/generated/data';
import data from '@periotable/data';

import Element, {
listOfPropertiesTitle,
titleToId,
Expand Down
3 changes: 2 additions & 1 deletion apps/web/pages/elements/[name]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
Defined,
} from '@poolofdeath20/util';

import data from '../../../src/web/generated/data';
import data from '@periotable/data';

import Seo from '../../../src/web/components/seo';
import BohrTwoDimensional from '../../../src/web/components/bohr/two-dimensional';
import BohrThreeDimensional from '../../../src/web/components/bohr/three-dimensional';
Expand Down
3 changes: 2 additions & 1 deletion apps/web/script/assets/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import axios from 'axios';

import { isNotUndefined } from '@poolofdeath20/util';

import data from '../../src/web/generated/data';
import data from '@periotable/data';

import constants from '../../src/web/constant';
import { obtainNameFromUrl } from '../../src/web/util/asset';

Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/web/components/pages/index/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {

import { CgClose } from 'react-icons/cg';

import data from '../../../generated/data';
import data from '@periotable/data';

import Seo from '../../../components/seo';
import { DemoTile, EmptyTile, Tile } from '../../../components/table/element';
import SearchBar from '../../../components/common/input';
Expand Down
3 changes: 3 additions & 0 deletions packages/data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
index.ts
.env
env.d.ts
3 changes: 3 additions & 0 deletions packages/data/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
index.ts
.env
env.d.ts
6 changes: 6 additions & 0 deletions packages/data/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## .env
generate-environment-type-definition:
pnpm vite-node script/type-def.ts

generate-data:
pnpm vite-node script/data.ts
15 changes: 15 additions & 0 deletions packages/data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@periotable/data",
"version": "0.0.0",
"author": "PoolOfDeath20",
"private": true,
"license": "GPL",
"devDependencies": {
"@poolofdeath20/tsconfig": "^0.0.0",
"axios": "^1.6.8",
"dotenv": "^16.4.5",
"gen-env-type-def": "^0.0.4",
"octokit": "^3.2.0",
"vite-node": "^1.4.0"
}
}
36 changes: 36 additions & 0 deletions packages/data/script/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from 'fs';

import { Octokit } from 'octokit';

import dotenv from 'dotenv';

import axios from 'axios';

const main = async () => {
dotenv.config();

const octokit = new Octokit({
auth: process.env.TOKEN,
});

const result = await octokit.rest.repos.getContent({
owner: process.env.OWNER,
repo: process.env.REPO,
path: process.env.DATA_PATH,
});

// @ts-expect-error: Download URL exists because data is of type "file" but it doesn't have type
const url = result.data.download_url as string;

const content = await axios.get(url).then((response) => {
if (typeof response.data !== 'string') {
throw new Error('Data is not a string');
}

return response.data;
});

fs.writeFileSync('index.ts', content);
};

main();
12 changes: 12 additions & 0 deletions packages/data/script/type-def.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { genEnvTypeDef } from 'gen-env-type-def';

const main = () => {
genEnvTypeDef([
{
inDir: '.',
envType: 'process.env',
},
]);
};

main();
5 changes: 5 additions & 0 deletions packages/data/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": ["@poolofdeath20/tsconfig/node"],
"include": ["*.d.ts", "**/*.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit d5231c4

Please sign in to comment.