in src/cli/commands/login/login.ts [108:153]
async function storeProjectCredentialsInEnvFile(
subscriptionId: string | undefined,
tenantId: string | undefined,
clientId: string | undefined,
clientSecret: string | undefined,
) {
const envFile = path.join(process.cwd(), ENV_FILENAME);
const envFileExists = existsSync(envFile);
const envFileContent = envFileExists ? await fs.readFile(envFile, "utf8") : "";
const buf = Buffer.from(envFileContent);
// in case the .env file format changes in the future, we can use the following to parse the file
const config = dotenv.parse(buf);
const oldEnvFileLines = Object.keys(config).map((key) => `${key}=${config[key]}`);
const newEnvFileLines = [];
let entry = `AZURE_SUBSCRIPTION_ID=${subscriptionId}`;
if (subscriptionId && !envFileContent.includes(entry)) {
newEnvFileLines.push(entry);
}
entry = `AZURE_TENANT_ID=${tenantId}`;
if (tenantId && !envFileContent.includes(entry)) {
newEnvFileLines.push(entry);
}
entry = `AZURE_CLIENT_ID=${clientId}`;
if (clientId && !envFileContent.includes(entry)) {
newEnvFileLines.push(entry);
}
entry = `AZURE_CLIENT_SECRET=${clientSecret}`;
if (clientSecret && !envFileContent.includes(entry)) {
newEnvFileLines.push(entry);
}
// write file if we have at least one new env line
if (newEnvFileLines.length > 0) {
const envFileContentWithProjectDetails = [...oldEnvFileLines, ...newEnvFileLines].join("\n");
await fs.writeFile(envFile, envFileContentWithProjectDetails);
logger.log(chalk.green(`✔ Saved project credentials in ${ENV_FILENAME} file.`));
await updateGitIgnore(ENV_FILENAME);
}
}