in src/core/account.ts [213:270]
async function chooseOrCreateStaticSite(
options: SWACLIConfig,
credentialChain: TokenCredential,
subscriptionId: string
): Promise<string | StaticSiteARMResource> {
const staticSites = await listStaticSites(credentialChain, subscriptionId);
// 1- when there are no static sites
if (staticSites.length === 0) {
const confirm = await wouldYouLikeToCreateStaticSite();
if (confirm) {
return (await createStaticSite(options, credentialChain, subscriptionId)) as StaticSiteARMResource;
} else {
logger.error("No projects found. Create a new project and try again.", true);
}
}
// 2- when there is only one static site
else if (staticSites.length === 1) {
logger.silly("Only one project found. Trying to use it if the name matches...");
const staticSite = staticSites[0];
if (options.appName === staticSite.name) {
return staticSite;
} else {
// if the name doesn't match, ask the user if they want to create a new project
const confirm = await wouldYouLikeToCreateStaticSite();
if (confirm) {
return (await createStaticSite(options, credentialChain, subscriptionId)) as StaticSiteARMResource;
} else {
logger.error(`The provided project name "${options.appName}" was not found.`, true);
}
}
}
// 3- when there are multiple static sites
if (options.appName) {
// if the user provided a project name, try to find it and use it
logger.silly(`Looking for project "${options.appName}"...`);
const staticSite = staticSites.find((s) => s.name === options.appName);
if (staticSite) {
return staticSite;
}
}
// otherwise, ask the user to choose one
const staticSite = await chooseStaticSite(staticSites, options.appName);
if (staticSite === "NEW") {
// if the user chose to create a new project, switch to the create project flow
return (await createStaticSite(options, credentialChain, subscriptionId)) as StaticSiteARMResource;
}
return staticSites.find((s) => s.name === staticSite) as StaticSiteARMResource;
}