2022-03-02 21:14:55 +03:00
|
|
|
import kleur from "kleur";
|
2022-11-18 21:08:10 +03:00
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
2022-03-02 21:14:55 +03:00
|
|
|
|
2022-11-18 21:08:10 +03:00
|
|
|
kleur.enabled = true;
|
2022-10-19 01:34:33 +03:00
|
|
|
|
2022-11-18 21:08:10 +03:00
|
|
|
export async function environmentVariable(name: string) {
|
2022-03-02 21:14:55 +03:00
|
|
|
const result = process.env[name];
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
throw `No environment variable called ${kleur
|
|
|
|
.yellow()
|
|
|
|
.underline(name)}\n\nAvailable:\n\n${Object.keys(process.env)
|
|
|
|
.slice(0, 5)
|
|
|
|
.join("\n")}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:08:10 +03:00
|
|
|
export async function hello(name: string) {
|
2022-03-02 21:14:55 +03:00
|
|
|
await waitFor(1000);
|
|
|
|
return `147 ${name}!!`;
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:08:10 +03:00
|
|
|
function waitFor(ms: number) {
|
2022-03-02 21:14:55 +03:00
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
}
|
2022-10-19 01:34:33 +03:00
|
|
|
|
2022-11-18 21:08:10 +03:00
|
|
|
export async function addItem(name: string) {
|
|
|
|
let timeToWait = 0;
|
|
|
|
try {
|
|
|
|
timeToWait = parseInt(name);
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
console.log("Adding ", name);
|
|
|
|
await fs.promises.writeFile(path.join(folder, name), "");
|
|
|
|
await waitFor(timeToWait);
|
|
|
|
return await listFiles();
|
|
|
|
}
|
|
|
|
const folder = "./items-list";
|
|
|
|
|
|
|
|
async function listFiles(): Promise<string[]> {
|
2023-01-08 22:04:17 +03:00
|
|
|
return (await fs.promises.readdir(folder)).filter(
|
|
|
|
(file) => !file.startsWith(".")
|
|
|
|
);
|
2022-10-19 01:34:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getItems() {
|
2022-11-18 21:08:10 +03:00
|
|
|
return await listFiles();
|
2022-10-19 01:34:33 +03:00
|
|
|
}
|
|
|
|
|
2022-11-18 21:08:10 +03:00
|
|
|
export async function deleteAllItems(name: string) {
|
|
|
|
for (const file of await listFiles()) {
|
|
|
|
await fs.promises.unlink(path.join(folder, file));
|
|
|
|
}
|
|
|
|
|
|
|
|
return await listFiles();
|
2022-10-19 01:34:33 +03:00
|
|
|
}
|
2022-11-22 22:21:11 +03:00
|
|
|
|
|
|
|
export async function log(message: string) {
|
|
|
|
console.log(message);
|
|
|
|
return null;
|
|
|
|
}
|