in src/cli/commands/init/init.ts [11:145]
export async function init(options: SWACLIConfig, showHints: boolean = true) {
const configFilePath = options.config!;
const disablePrompts = options.yes ?? false;
const outputFolder = process.cwd();
let configName: string = options.configName?.trim() ?? "";
if (configName === "") {
const response = await promptOrUseDefault(disablePrompts, {
type: "text",
name: "configName",
message: "Choose a configuration name:",
initial: dasherize(path.basename(outputFolder)),
validate: (value: string) => value.trim() !== "" || "Configuration name cannot be empty",
format: (value: string) => dasherize(value.trim()),
});
configName = response.configName;
}
// TODO: start from template
// if (isEmptyFolder(outputFolder)) {
// // Do you want to create a new project from a template?
// }
const detectedFolders = await detectProjectFolders();
let app: DetectedFolder | undefined = detectedFolders.app[0];
let api: DetectedFolder | undefined = detectedFolders.api[0];
if (detectedFolders.app.length > 1) {
logger.silly(`More than one (${detectedFolders.app.length}) app folders found`);
const response = await promptOrUseDefault(disablePrompts, {
type: "select",
name: "app",
message: "Which app folder do you want to use?",
choices: detectedFolders.app.map((folder) => ({ title: folder.rootPath, value: folder })),
initial: 0,
});
// Workaround for bug https://github.com/terkelg/prompts/issues/205
app = typeof response.app === "number" ? detectedFolders.app[response.app] : response.app;
}
// Check if we can find api folders under selected app folder, and filter selection if we found some
if (app !== undefined) {
const childApiFolders = detectedFolders.api.filter((folder) => isDescendantPath(folder.rootPath, app!.rootPath));
if (childApiFolders.length > 0) {
logger.silly(`Found (${childApiFolders.length}) api folders under the app folder`);
logger.silly(`- ${childApiFolders.map((f) => `${f.rootPath} (${f.frameworks.map((fr) => fr.name).join(", ")})`).join("\n- ")}`);
detectedFolders.api = childApiFolders;
}
}
if (detectedFolders.api.length > 1) {
logger.silly(`More than one (${detectedFolders.api.length}) api folders found`);
const response = await promptOrUseDefault(disablePrompts, {
type: "select",
name: "api",
message: "Which api folder do you want to use?",
choices: detectedFolders.api.map((folder) => ({ title: folder.rootPath, value: folder })),
initial: 0,
});
// Workaround for bug https://github.com/terkelg/prompts/issues/205
api = typeof response.api === "number" ? detectedFolders.api[response.api] : response.api;
} else {
api = detectedFolders.api[0];
}
const dbConfigFiles = await detectDbConfigFiles();
let dataApi: DetectedDbConfigFolder | undefined = dbConfigFiles[0];
if (dbConfigFiles.length > 1) {
logger.silly(`More than one (${dbConfigFiles.length}) data api folders found`);
const response = await promptOrUseDefault(disablePrompts, {
type: "select",
name: "dataApi",
message: "Which data api folder do you want to use?",
choices: dbConfigFiles.map((file) => ({ title: file.rootPath, value: file })),
initial: 0,
});
dataApi = typeof response.dataApi === "number" ? dbConfigFiles[response.dataApi] : response.dataApi;
}
let projectConfig;
try {
projectConfig = await generateConfiguration(app, api, dataApi);
} catch (error) {
logger.error(`Cannot generate your project configuration:`);
logger.error(error as Error, true);
return;
}
printFrameworkConfig(projectConfig);
const { confirmSettings } = await promptOrUseDefault(disablePrompts, {
type: "confirm",
name: "confirmSettings",
message: "Are these settings correct?",
initial: true,
});
if (!confirmSettings) {
// Ask for each settings
projectConfig = await promptConfigSettings(disablePrompts, projectConfig);
}
if (swaCliConfigFileExists(configFilePath) && (await hasConfigurationNameInConfigFile(configFilePath, configName))) {
const { confirmOverwrite } = await promptOrUseDefault(disablePrompts, {
type: "confirm",
name: "confirmOverwrite",
message: `Configuration with name "${configName}" already exists, overwrite?`,
initial: true,
});
if (!confirmOverwrite) {
logger.log("Aborted, configuration not saved.");
return;
}
}
const cliConfig = convertToCliConfig(projectConfig);
await writeConfigFile(configFilePath, configName, cliConfig);
logger.log(chalk.green(`\nConfiguration successfully saved to ${swaCliConfigFilename}.\n`));
if (showHints) {
logger.log(chalk.bold(`Get started with the following commands:`));
logger.log(`- Use ${chalk.cyan("swa start")} to run your app locally.`);
if (cliConfig.appBuildCommand || cliConfig.apiBuildCommand) {
logger.log(`- Use ${chalk.cyan("swa build")} to build your app.`);
}
logger.log(`- Use ${chalk.cyan("swa deploy")} to deploy your app to Azure.\n`);
}
}