Skip to content

Commit

Permalink
fix: cname and date formate
Browse files Browse the repository at this point in the history
  • Loading branch information
pmespresso committed Aug 1, 2021
1 parent 1501b68 commit dd134ae
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"axios": "^0.21.1",
"dotenv": "^8.2.0"
"dotenv": "^8.2.0",
"moment": "^2.29.1"
}
}
59 changes: 36 additions & 23 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
import fs from "fs";
import mkdirp from "mkdirp";
import moment from "moment";
import protocolsInfo from "../protocolsInfoForScreener.json";
import { CalendarEvent, CalendarEvents, dateKeyFormat } from "../types";

const protocols = fs.readdirSync("./protocols");
let protocols = fs.readdirSync("./protocols");

const protocolInfo = protocols
.filter((protocol) => protocol !== "__example")
.map((protocol) => ({
...JSON.parse(fs.readFileSync(`./protocols/${protocol}/index.json`, "utf8")),
folder: protocol,
}));
protocols = protocols.filter((protocol) => protocol !== "__example");

const events = protocols
.filter((protocol) => protocol !== "__example")
.map((protocol) => {
const fileEvents = fs.readFileSync(`./protocols/${protocol}/events.json`, "utf8");
const protocolInfo = protocols.map((protocol) => ({
...JSON.parse(fs.readFileSync(`./protocols/${protocol}/index.json`, "utf8")),
folder: protocol,
}));

if (fileEvents) {
return {
...JSON.parse(fileEvents),
protocolCname: protocol,
};
} else {
return {
protocolCname: protocol,
let calendarEvents: CalendarEvents = {};

for (let i = 0; i < protocols.length; i++) {
const protocol = protocols[i];

const protocolInfo = fs.readFileSync(`./protocols/${protocol}/index.json`, "utf8");
const protocolInfoEvents = fs.readFileSync(`./protocols/${protocol}/events.json`, "utf8");
const cname = JSON.parse(protocolInfo).cname;

if (protocolInfoEvents) {
const parsedProtocolInfoEvents = JSON.parse(protocolInfoEvents) as Record<string, CalendarEvent>;

Object.values(parsedProtocolInfoEvents).map((event) => {
const formattedEvent = {
...event,
date: moment(event.date).unix() * 1000,
protocolCname: cname,
};
}
});

if (calendarEvents[moment(event.date).format(dateKeyFormat)]) {
calendarEvents[moment(event.date).format(dateKeyFormat)].push(formattedEvent);
} else {
calendarEvents[moment(event.date).format(dateKeyFormat)] = [formattedEvent];
}
});
}
}

mkdirp.sync("./dist");
fs.copyFileSync("./types.ts", "./dist/types.ts");
fs.writeFileSync(
"./dist/index.ts",
`
import { CalendarEvent, Protocol, ProtocolForScreeener } from "../types";
import { CalendarEvents, Protocol, ProtocolForScreeener } from "../types";
const protocolInfoList = ${JSON.stringify(protocolInfo)} as Protocol[];
const protocolEvents = ${JSON.stringify(events)} as CalendarEvent[];
const protocolEvents = ${JSON.stringify(calendarEvents)} as CalendarEvents;
const protocolsInfoForScreener: Record<string, ProtocolForScreeener> = ${JSON.stringify(protocolsInfo)};
export {protocolInfoList, protocolEvents, protocolsInfoForScreener};
`,
Expand Down
18 changes: 10 additions & 8 deletions scripts/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ for (const protocol of protocolInfoList) {
}

// console.log("protocolEvents => ", protocolEvents);
for (const event of protocolEvents) {
try {
validator(ProtocolEvents).decodeSync(event);
} catch (e) {
errors.push({
protocol: event?.protocolCname || undefined,
message: e,
});
for (const events of Object.values(protocolEvents)) {
for (const event of events) {
try {
validator(ProtocolEvents).decodeSync(event);
} catch (e) {
errors.push({
protocol: event?.protocolCname || undefined,
message: e,
});
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ export type ProtocolForScreeener = t.TypeOf<typeof ProtocolForScreeenerIo>;

export type Protocol = t.TypeOf<typeof ProtocolIo>;
export type CalendarEvent = t.TypeOf<typeof ProtocolEvents>;
export type CalendarEvents = Record<string, CalendarEvent[]>;

export const dateKeyFormat = "YYYY-MM-DD";
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ mkdirp@^1.0.4:
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

moment@^2.29.1:
version "2.29.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==

prettier@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
Expand Down

0 comments on commit dd134ae

Please sign in to comment.