feat: cli improvements
This commit is contained in:
parent
4a58389976
commit
a3eea5a98e
|
@ -1,37 +1,14 @@
|
|||
import { platforms, processPlatform } from "../platforms";
|
||||
import { rebuildAlgoliaDatabase } from "../utils/algolia";
|
||||
import { updateHistory } from "./history/updateHistory";
|
||||
import { mergeEverything } from "./mergeEverything";
|
||||
import { rebuildNetlifySiteWithNewData } from "./rebuildNetliftySiteWithNewData";
|
||||
import { platforms } from "../platforms";
|
||||
import { executeJobByName } from "./jobs";
|
||||
|
||||
/* Do everything */
|
||||
function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export async function tryCatchTryAgain(fun) {
|
||||
try {
|
||||
console.log("Initial try");
|
||||
await fun();
|
||||
} catch (error) {
|
||||
sleep(10000);
|
||||
console.log("Second try");
|
||||
console.log(error);
|
||||
try {
|
||||
await fun();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function doEverything() {
|
||||
let functions = [
|
||||
...platforms.map((platform) => () => processPlatform(platform)),
|
||||
mergeEverything,
|
||||
rebuildAlgoliaDatabase,
|
||||
updateHistory,
|
||||
rebuildNetlifySiteWithNewData,
|
||||
let jobNames = [
|
||||
...platforms.map((platform) => platform.name),
|
||||
"merge",
|
||||
"algolia",
|
||||
"history",
|
||||
"netlify",
|
||||
];
|
||||
// Removed Good Judgment from the fetcher, doing it using cron instead because cloudflare blocks the utility on heroku.
|
||||
|
||||
|
@ -47,13 +24,13 @@ export async function doEverything() {
|
|||
console.log("");
|
||||
console.log("");
|
||||
|
||||
for (let fun of functions) {
|
||||
for (let name of jobNames) {
|
||||
console.log("");
|
||||
console.log("");
|
||||
console.log("****************************");
|
||||
console.log(fun.name);
|
||||
console.log(name);
|
||||
console.log("****************************");
|
||||
await tryCatchTryAgain(fun);
|
||||
await executeJobByName(name);
|
||||
console.log("****************************");
|
||||
}
|
||||
}
|
||||
|
|
90
src/backend/flow/jobs.ts
Normal file
90
src/backend/flow/jobs.ts
Normal file
|
@ -0,0 +1,90 @@
|
|||
import { pgInitialize } from "../database/pg-wrapper";
|
||||
import { doEverything } from "../flow/doEverything";
|
||||
import { updateHistory } from "../flow/history/updateHistory";
|
||||
import { mergeEverything } from "../flow/mergeEverything";
|
||||
import { rebuildNetlifySiteWithNewData } from "../flow/rebuildNetliftySiteWithNewData";
|
||||
import { rebuildFrontpage } from "../frontpage";
|
||||
import { platforms, processPlatform } from "../platforms";
|
||||
import { rebuildAlgoliaDatabase } from "../utils/algolia";
|
||||
|
||||
interface Job {
|
||||
name: string;
|
||||
message: string;
|
||||
run: () => Promise<void>;
|
||||
separate?: boolean;
|
||||
}
|
||||
|
||||
export const jobs: Job[] = [
|
||||
...platforms.map((platform) => ({
|
||||
name: platform.name,
|
||||
message: `Download predictions from ${platform.name}`,
|
||||
run: () => processPlatform(platform),
|
||||
})),
|
||||
{
|
||||
name: "merge",
|
||||
message:
|
||||
"Merge tables into one big table (and push the result to a pg database)",
|
||||
run: mergeEverything,
|
||||
separate: true,
|
||||
},
|
||||
{
|
||||
name: "algolia",
|
||||
message: 'Rebuild algolia database ("index")',
|
||||
run: rebuildAlgoliaDatabase,
|
||||
},
|
||||
{
|
||||
name: "history",
|
||||
message: "Update history",
|
||||
run: updateHistory,
|
||||
},
|
||||
{
|
||||
name: "netlify",
|
||||
message: `Rebuild netlify site with new data`,
|
||||
run: rebuildNetlifySiteWithNewData,
|
||||
},
|
||||
{
|
||||
name: "frontpage",
|
||||
message: "Rebuild frontpage",
|
||||
run: rebuildFrontpage,
|
||||
},
|
||||
{
|
||||
name: "all",
|
||||
message: "All of the above",
|
||||
run: doEverything,
|
||||
separate: true,
|
||||
},
|
||||
{
|
||||
name: "migrate",
|
||||
message: "Initialize postgres database",
|
||||
run: pgInitialize,
|
||||
},
|
||||
];
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function tryCatchTryAgain(fun: () => Promise<void>) {
|
||||
try {
|
||||
console.log("Initial try");
|
||||
await fun();
|
||||
} catch (error) {
|
||||
sleep(10000);
|
||||
console.log("Second try");
|
||||
console.log(error);
|
||||
try {
|
||||
await fun();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const executeJobByName = async (option: string) => {
|
||||
const job = jobs.find((job) => job.name === option);
|
||||
if (!job) {
|
||||
console.log(`Error, job ${option} not found`);
|
||||
} else {
|
||||
await tryCatchTryAgain(job.run);
|
||||
}
|
||||
};
|
|
@ -2,50 +2,23 @@
|
|||
import "dotenv/config";
|
||||
|
||||
import readline from "readline";
|
||||
import util from "util";
|
||||
|
||||
import { pgInitialize } from "./database/pg-wrapper";
|
||||
import { doEverything, tryCatchTryAgain } from "./flow/doEverything";
|
||||
import { updateHistory } from "./flow/history/updateHistory";
|
||||
import { mergeEverything } from "./flow/mergeEverything";
|
||||
import { rebuildNetlifySiteWithNewData } from "./flow/rebuildNetliftySiteWithNewData";
|
||||
import { rebuildFrontpage } from "./frontpage";
|
||||
import { platforms, processPlatform } from "./platforms";
|
||||
import { rebuildAlgoliaDatabase } from "./utils/algolia";
|
||||
|
||||
/* Support functions */
|
||||
let functions = [
|
||||
...platforms.map((platform) => () => processPlatform(platform)),
|
||||
mergeEverything,
|
||||
rebuildAlgoliaDatabase,
|
||||
updateHistory,
|
||||
rebuildNetlifySiteWithNewData,
|
||||
doEverything,
|
||||
pgInitialize,
|
||||
rebuildFrontpage,
|
||||
];
|
||||
import { executeJobByName, jobs } from "./flow/jobs";
|
||||
|
||||
let generateWhatToDoMessage = () => {
|
||||
let l = platforms.length;
|
||||
let messagesForFetchers = platforms.map(
|
||||
(platform, i) => `[${i}]: Download predictions from ${platform.name}`
|
||||
);
|
||||
let otherMessages = [
|
||||
"Merge tables into one big table (and push the result to a pg database)",
|
||||
`Rebuild algolia database ("index")`,
|
||||
`Update history`,
|
||||
`Rebuild netlify site with new data`,
|
||||
// `\n[${functionNames.length-1}]: Add to history` +
|
||||
`All of the above`,
|
||||
`Initialize postgres database`,
|
||||
"Rebuild frontpage",
|
||||
];
|
||||
let otherMessagesWithNums = otherMessages.map(
|
||||
(message, i) => `[${i + l}]: ${message}`
|
||||
);
|
||||
const color = "\x1b[36m";
|
||||
const resetColor = "\x1b[0m";
|
||||
let completeMessages = [
|
||||
...messagesForFetchers,
|
||||
...otherMessagesWithNums,
|
||||
`\nChoose one option, wisely: #`,
|
||||
...jobs.map((job) => {
|
||||
return (
|
||||
(job.separate ? "\n" : "") +
|
||||
`[${color}${job.name}${resetColor}]:`.padStart(30) +
|
||||
" " +
|
||||
job.message
|
||||
);
|
||||
}),
|
||||
`\nChoose one option, wisely: `,
|
||||
].join("\n");
|
||||
return completeMessages;
|
||||
};
|
||||
|
@ -54,39 +27,24 @@ let whattodoMessage = generateWhatToDoMessage();
|
|||
|
||||
/* BODY */
|
||||
let commandLineUtility = async () => {
|
||||
let whattodo = async (message, callback) => {
|
||||
const pickOption = async () => {
|
||||
if (process.argv.length === 3) {
|
||||
return process.argv[2]; // e.g., npm run cli polymarket
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
rl.question(message, async (answer) => {
|
||||
|
||||
const question = util.promisify(rl.question).bind(rl);
|
||||
const answer = await question(whattodoMessage);
|
||||
rl.close();
|
||||
await callback(answer);
|
||||
});
|
||||
return answer;
|
||||
};
|
||||
|
||||
let executeoption = async (option) => {
|
||||
option = Number(option);
|
||||
if (option < 0) {
|
||||
console.log(`Error, ${option} < 0`);
|
||||
} else if (option < functions.length) {
|
||||
console.log(`Running: ${functions[option].name}\n`);
|
||||
await tryCatchTryAgain(functions[option]);
|
||||
}
|
||||
executeJobByName(await pickOption());
|
||||
process.exit();
|
||||
};
|
||||
|
||||
if (process.argv.length == 3) {
|
||||
const option = process.argv[2]; // e.g., npm start 15 <-
|
||||
const optionNum = Number(option);
|
||||
if (!isNaN(optionNum)) {
|
||||
await executeoption(optionNum);
|
||||
} else if (option == "all") {
|
||||
await executeoption(functions.length - 3); // doEverything
|
||||
} else {
|
||||
await whattodo(whattodoMessage, executeoption);
|
||||
}
|
||||
} else await whattodo(whattodoMessage, executeoption);
|
||||
};
|
||||
|
||||
commandLineUtility();
|
||||
|
|
Loading…
Reference in New Issue
Block a user