Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions bin/lib/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ function loadCredentials() {
return {};
}

function normalizeCredentialValue(value) {
if (typeof value !== "string") return "";
return value.replace(/\r/g, "").trim();
}

function saveCredential(key, value) {
fs.mkdirSync(CREDS_DIR, { recursive: true, mode: 0o700 });
const creds = loadCredentials();
creds[key] = value;
creds[key] = normalizeCredentialValue(value);
fs.writeFileSync(CREDS_FILE, JSON.stringify(creds, null, 2), { mode: 0o600 });
}

function getCredential(key) {
if (process.env[key]) return process.env[key];
if (process.env[key]) return normalizeCredentialValue(process.env[key]);
const creds = loadCredentials();
return creds[key] || null;
const value = normalizeCredentialValue(creds[key]);
return value || null;
}

function promptSecret(question) {
Expand Down Expand Up @@ -73,7 +79,10 @@ function promptSecret(question) {
}

if (ch === "\u0008" || ch === "\u007f") {
answer = answer.slice(0, -1);
if (answer.length > 0) {
answer = answer.slice(0, -1);
output.write("\b \b");
}
continue;
}

Expand All @@ -91,6 +100,7 @@ function promptSecret(question) {

if (ch >= " ") {
answer += ch;
output.write("*");
}
}
}
Expand Down Expand Up @@ -125,7 +135,10 @@ function prompt(question, opts = {}) {
return;
}
const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
rl.question(question, (answer) => {
let finished = false;
function finish(fn, value) {
if (finished) return;
finished = true;
rl.close();
if (!process.stdin.isTTY) {
if (typeof process.stdin.pause === "function") {
Expand All @@ -135,7 +148,15 @@ function prompt(question, opts = {}) {
process.stdin.unref();
}
}
resolve(answer.trim());
fn(value);
}
rl.on("SIGINT", () => {
const err = Object.assign(new Error("Prompt interrupted"), { code: "SIGINT" });
finish(reject, err);
process.kill(process.pid, "SIGINT");
});
rl.question(question, (answer) => {
finish(resolve, answer.trim());
});
});
}
Expand All @@ -158,11 +179,20 @@ async function ensureApiKey() {
console.log(" └─────────────────────────────────────────────────────────────────┘");
console.log("");

key = await prompt(" NVIDIA API Key: ", { secret: true });
while (true) {
key = normalizeCredentialValue(await prompt(" NVIDIA API Key: ", { secret: true }));

if (!key || !key.startsWith("nvapi-")) {
console.error(" Invalid key. Must start with nvapi-");
process.exit(1);
if (!key) {
console.error(" NVIDIA API Key is required.");
continue;
}

if (!key.startsWith("nvapi-")) {
console.error(" Invalid key. Must start with nvapi-");
continue;
}

break;
}

saveCredential("NVIDIA_API_KEY", key);
Expand Down Expand Up @@ -229,6 +259,7 @@ module.exports = {
CREDS_DIR,
CREDS_FILE,
loadCredentials,
normalizeCredentialValue,
saveCredential,
getCredential,
prompt,
Expand Down
Loading
Loading