async function updateLocalSafeSettings()

in packages/MSBot/src/msbot-clone-services.ts [924:1011]


async function updateLocalSafeSettings(azBot?: IBotService): Promise<void> {
    if (azBot) {

        // update local environment settings
        if (fs.existsSync('appsettings.json')) {
            console.log(`Updating appsettings.json with botFilePath=${config.getPath()}`);
            let settings = fsExtra.readJsonSync('appsettings.json');
            settings.botFilePath = config.getPath();
            fs.writeFileSync('appsettings.json', JSON.stringify(settings, null, 4), { encoding: 'utf8' });

            if (args.secret) {

                // some projfiles won't have a userSecret set, check for it
                if (args.projFile) {
                    let proj = readTextFile.readSync(args.projFile);
                    if (proj.indexOf('<UserSecretsId>') < 0) {
                        // doesn't have it, add one
                        let end = proj.indexOf('</Project');
                        let newProj = proj.substring(0, end);
                        newProj += `<PropertyGroup><UserSecretsId>${uuid.v4()}</UserSecretsId></PropertyGroup>\n`;
                        newProj += proj.substring(end);
                        fs.writeFileSync(args.projFile, newProj, { encoding: 'utf8' });
                    }
                }

                // save secret
                await runCommand(`dotnet user-secrets set botFileSecret ${args.secret}`, `Saving the botFileSecret with dotnet user-secrets`);

                // make sure that startup.cs has configuration information
                if (fs.existsSync('startup.cs')) {
                    let startup = readTextFile.readSync('startup.cs');
                    // if it doesn't have .AddUserSecrets call
                    if (startup.indexOf('.AddUserSecrets') < 0) {
                        let i = startup.indexOf('Configuration = builder.Build();');
                        if (i > 0) {
                            let newStartup = startup.substring(0, i);
                            console.log('Updating startup.cs to use user-secrets');
                            newStartup += 'if (env.IsDevelopment()) \n                builder.AddUserSecrets<Startup>();\n\n                ' + startup.substring(i);
                            fs.writeFileSync('startup.cs', newStartup, { encoding: 'utf8' });
                        } else {
                            console.log(chalk.default.yellow('You need to add following code to your dotnet configuration\n'));
                            console.log('if (env.IsDevelopment()) builder.AddUserSecrets<Startup>();')
                        }
                    }
                }
            }
        } else if (fs.existsSync('.env')) {
            console.log(`Updating .env with path and secret`);
            let lines = readTextFile.readSync('.env').split('\n');
            let newEnv = '';
            let pathLine = `botFilePath="${config.getPath()}"\n`;
            let secretLine = `botFileSecret="${args.secret}"\n`;
            let foundPath = false;
            let foundSecret = false;
            for (let line of lines) {
                let i = line.indexOf('=');
                if (i > 0) {
                    let name = line.substring(0, i);
                    let value = line.substring(i + 1);
                    switch (name) {
                        case 'botFilePath':
                            newEnv += pathLine;
                            foundPath = true;
                            break;
                        case "botFileSecret":
                            newEnv += secretLine;
                            foundSecret = true;
                            break;
                        default:
                            newEnv += line + '\n';
                            break;
                    }
                } else {
                    // pass through
                    newEnv += line + '\n';
                }
            }
            if (!foundPath) {
                newEnv += pathLine;
            }
            if (!foundSecret) {
                newEnv += secretLine;
            }

            fs.writeFileSync('.env', newEnv.trimRight(), { encoding: 'utf8' });
        }
    }
}