feat: cli improvements
This commit is contained in:
parent
4a58389976
commit
a3eea5a98e
|
@ -1,37 +1,14 @@
|
||||||
import { platforms, processPlatform } from "../platforms";
|
import { platforms } from "../platforms";
|
||||||
import { rebuildAlgoliaDatabase } from "../utils/algolia";
|
import { executeJobByName } from "./jobs";
|
||||||
import { updateHistory } from "./history/updateHistory";
|
|
||||||
import { mergeEverything } from "./mergeEverything";
|
|
||||||
import { rebuildNetlifySiteWithNewData } from "./rebuildNetliftySiteWithNewData";
|
|
||||||
|
|
||||||
/* Do everything */
|
/* 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() {
|
export async function doEverything() {
|
||||||
let functions = [
|
let jobNames = [
|
||||||
...platforms.map((platform) => () => processPlatform(platform)),
|
...platforms.map((platform) => platform.name),
|
||||||
mergeEverything,
|
"merge",
|
||||||
rebuildAlgoliaDatabase,
|
"algolia",
|
||||||
updateHistory,
|
"history",
|
||||||
rebuildNetlifySiteWithNewData,
|
"netlify",
|
||||||
];
|
];
|
||||||
// Removed Good Judgment from the fetcher, doing it using cron instead because cloudflare blocks the utility on heroku.
|
// 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("");
|
||||||
console.log("");
|
console.log("");
|
||||||
|
|
||||||
for (let fun of functions) {
|
for (let name of jobNames) {
|
||||||
console.log("");
|
console.log("");
|
||||||
console.log("");
|
console.log("");
|
||||||
console.log("****************************");
|
console.log("****************************");
|
||||||
console.log(fun.name);
|
console.log(name);
|
||||||
console.log("****************************");
|
console.log("****************************");
|
||||||
await tryCatchTryAgain(fun);
|
await executeJobByName(name);
|
||||||
console.log("****************************");
|
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 "dotenv/config";
|
||||||
|
|
||||||
import readline from "readline";
|
import readline from "readline";
|
||||||
|
import util from "util";
|
||||||
|
|
||||||
import { pgInitialize } from "./database/pg-wrapper";
|
import { executeJobByName, jobs } from "./flow/jobs";
|
||||||
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,
|
|
||||||
];
|
|
||||||
|
|
||||||
let generateWhatToDoMessage = () => {
|
let generateWhatToDoMessage = () => {
|
||||||
let l = platforms.length;
|
const color = "\x1b[36m";
|
||||||
let messagesForFetchers = platforms.map(
|
const resetColor = "\x1b[0m";
|
||||||
(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}`
|
|
||||||
);
|
|
||||||
let completeMessages = [
|
let completeMessages = [
|
||||||
...messagesForFetchers,
|
...jobs.map((job) => {
|
||||||
...otherMessagesWithNums,
|
return (
|
||||||
`\nChoose one option, wisely: #`,
|
(job.separate ? "\n" : "") +
|
||||||
|
`[${color}${job.name}${resetColor}]:`.padStart(30) +
|
||||||
|
" " +
|
||||||
|
job.message
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
`\nChoose one option, wisely: `,
|
||||||
].join("\n");
|
].join("\n");
|
||||||
return completeMessages;
|
return completeMessages;
|
||||||
};
|
};
|
||||||
|
@ -54,39 +27,24 @@ let whattodoMessage = generateWhatToDoMessage();
|
||||||
|
|
||||||
/* BODY */
|
/* BODY */
|
||||||
let commandLineUtility = async () => {
|
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({
|
const rl = readline.createInterface({
|
||||||
input: process.stdin,
|
input: process.stdin,
|
||||||
output: process.stdout,
|
output: process.stdout,
|
||||||
});
|
});
|
||||||
rl.question(message, async (answer) => {
|
|
||||||
|
const question = util.promisify(rl.question).bind(rl);
|
||||||
|
const answer = await question(whattodoMessage);
|
||||||
rl.close();
|
rl.close();
|
||||||
await callback(answer);
|
return answer;
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let executeoption = async (option) => {
|
executeJobByName(await pickOption());
|
||||||
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]);
|
|
||||||
}
|
|
||||||
process.exit();
|
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();
|
commandLineUtility();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user