Interactive login first phase.

This commit is contained in:
2026-01-27 07:43:57 +01:00
parent 347aee10fd
commit 6dc72ab8d2
8 changed files with 203 additions and 21 deletions

View File

@@ -2,6 +2,7 @@
import { exec, execSync, spawnSync } from "child_process";
import { writeFileSync } from "fs";
import { env } from "process";
import { parseArgs } from "util";
const args = parseArgs({
@@ -9,11 +10,18 @@ const args = parseArgs({
"app-name": { type: "string", short: "a" },
help: { type: "boolean", short: "h" },
"generate-client-secret": { type: "boolean", short: "s" },
"write-config": { type: "boolean", short: "w" },
"write-env": { type: "boolean", short: "e" },
"config": { type: "string", short: "c", default: "config.js" },
"write-config": { type: "string", short: "w" },
},
});
if (args.values["write-config"] && !["js", "env", "both"].includes(args.values["write-config"])) {
console.error(
"Invalid value for --write-config. Allowed values are: js, env, both.",
);
process.exit(1);
}
const config = {};
if (args.values["app-name"]) {
@@ -134,14 +142,17 @@ if (args.values["generate-client-secret"]) {
}
}
if (args.values["write-env"] || args.values["generate-client-secret"]) {
// Write the APP_ID to the .env file
const envContent = `AZ_APP_NAME="${config.appName}"
let envContent = `AZ_APP_NAME="${config.appName}"
ARM_TENANT_ID=${config.tenantId}
ARM_CLIENT_ID=${config.clientId}
ARM_CLIENT_SECRET=${config.clientSecret || ""}
`;
if (config.clientSecret) {
envContent += `ARM_CLIENT_SECRET=${config.clientSecret}\n`;
}
if (["env", "both"].includes(args.values["write-config"])) {
// Write the APP_ID to the .env file
writeFileSync(".env", envContent);
try {
execSync("chmod 600 .env");
@@ -151,23 +162,29 @@ ARM_CLIENT_SECRET=${config.clientSecret || ""}
);
}
console.log(".env file created with application configuration.");
} else {
console.log("\nThe .env file for the application:");
console.log(envContent);
}
if (args.values["write-config"] || args.values["generate-client-secret"]) {
if (["js", "both"].includes(args.values["write-config"])) {
// Save the config to the 'config.js' file.
writeFileSync(
"config.js",
args.values["config"],
`export const config = ${JSON.stringify(config, null, 4)};\n`,
);
try {
execSync("chmod 600 config.js");
execSync(`chmod 600 ${args.values["config"]}`);
} catch (error) {
console.warn(
"Could not set file permissions for config.js. Please ensure it is secured appropriately.",
`Could not set file permissions for ${args.values["config"]}. Please ensure it is secured appropriately.`,
);
}
console.log("config.js file created.");
console.log(`${args.values["config"]} file created.`);
} else {
console.log(`The ${args.values["config"]} file content:`);
console.log(
`export const config = ${JSON.stringify(config, null, 4)};\n`,
);
}
console.log("Setup complete.");