Page MenuHomeSealhub

init.ts
No OneTemporary

import * as prompts from "prompts";
import { stat, promises, constants, writeFile } from "fs";
import { homedir } from "os";
import { promisify } from "util";
import { URL } from "url";
import { PromptAnswers } from "./interfaces/prompt_answers";
import resolveAPIToken from "./helpers/resolve_api_token";
import { resolve } from "path";
import { exit } from "process";
const _stat = promisify(stat);
const _writeFile = promisify(writeFile);
function onCancel() {
console.log("\x1b[33m%s\x1b[0m", "Exiting..");
exit(1);
}
async function promptUser() {
return await prompts(
[
{
type: "text",
name: "arc",
message: "Where would I find .arcrc config?",
initial: `${homedir()}/.arcrc`,
validate: async (path) => {
try {
await promises
.access(path, constants.F_OK | constants.W_OK)
.catch(() => {
throw new Error("Cannot find specified file!");
});
const stats = await _stat(path);
if (stats.isDirectory()) {
throw new Error("Entered path indicates folder");
}
} catch (e) {
return `${e.message}`;
}
return true;
},
},
{
type: "text",
name: "uri",
message: "Enter full Conduit API URI",
validate: (uri) => {
try {
new URL(uri);
} catch (_) {
return "Not a walid URI";
}
return true;
},
},
{
type: "select",
name: "defaultRequest",
message:
"Select default option for sending data to Conduit API",
choices: [
{
title: "User-defined query key",
value: "configQueryKey",
},
{
title: "User-defined query key from stdin",
value: "configQueryKeyStdin",
},
{ title: "JSON file", value: "configJson" },
{ title: "JSON from stdin", value: "configJsonStdin" },
],
},
{
type: (prev) =>
prev === "configJson" || prev === "configQueryKey"
? "text"
: null,
name: (prev) => prev,
message: (prev: "configJson" | "configQueryKey") => {
switch (prev) {
case "configJson": {
return "Enter path to JSON file";
}
case "configQueryKey": {
return "Enter query key";
}
}
},
},
],
{ onCancel }
);
}
async function initConfig(): Promise<void> {
const answer = (await promptUser()) as PromptAnswers;
console.log("\x1b[33m%s\x1b[0m", "Creating config file..");
const config = {
token: await resolveAPIToken(answer.arc, answer.uri),
...answer,
};
try {
await _writeFile(
resolve(homedir(), ".roficatorrc"),
JSON.stringify(config)
);
console.log(
"\x1b[33m%s\x1b[0m",
`Config file created: ${resolve(homedir(), ".roficatorrc")}`
);
} catch (e) {
if (e) {
console.error("Failed to create config file.");
}
}
}
export default initConfig;

File Metadata

Mime Type
text/x-java
Expires
Sat, Nov 23, 17:03 (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
548000
Default Alt Text
init.ts (2 KB)

Event Timeline