async function createStaticSite()

in src/core/account.ts [135:211]


async function createStaticSite(options: SWACLIConfig, credentialChain: TokenCredential, subscriptionId: string): Promise<StaticSiteARMResource> {
  let { appName, resourceGroup } = options;

  const maxProjectNameLength = 63; // azure convention is 64 characters (zero-indexed)
  const defaultStaticSiteName = appName || dasherize(path.basename(process.cwd())).substring(0, maxProjectNameLength);

  appName = await chooseProjectName(defaultStaticSiteName, maxProjectNameLength);
  const sku = await chooseProjectSku();
  resourceGroup = resourceGroup || `${appName}-rg`;

  let spinner = ora("Creating a new project...").start();

  // if the resource group does not exist, create it
  if ((await isResourceGroupExists(resourceGroup, subscriptionId, credentialChain)) === false) {
    logger.silly(`Resource group "${resourceGroup}" does not exist. Creating one...`);
    // create the resource group
    await createResourceGroup(resourceGroup, credentialChain, subscriptionId);
  }

  // create the static web app instance
  try {
    const websiteClient = new WebSiteManagementClient(credentialChain, subscriptionId);

    const { AZURE_REGION_LOCATION } = swaCLIEnv();

    const staticSiteEnvelope: StaticSiteARMResource = {
      location: AZURE_REGION_LOCATION || DEFAULT_AZURE_LOCATION,
      sku: { name: sku, tier: sku },

      // these are mandatory, otherwise the static site will not be created
      buildProperties: {
        appLocation: "",
        outputLocation: "",
        apiLocation: "",
      },
    };

    logger.silly(`Checking if project "${appName}" already exists...`);

    // check if the static site already exists
    const project = await gracefullyFail(websiteClient.staticSites.getStaticSite(resourceGroup, appName), 404);
    const projectExists = project !== undefined;

    if (projectExists) {
      spinner.stop();

      const confirm = await wouldYouLikeToOverrideStaticSite?.(appName);
      if (confirm === false) {
        return (await chooseOrCreateStaticSite(options, credentialChain, subscriptionId)) as StaticSiteARMResource;
      }
    }

    if (projectExists) {
      spinner.start(`Updating project "${appName}"...`);
    }

    logger.silly(`Creating static site "${appName}" in resource group "${resourceGroup}"...`);
    const result = await websiteClient.staticSites.beginCreateOrUpdateStaticSiteAndWait(resourceGroup, appName, staticSiteEnvelope);

    logger.silly(`Static site "${appName}" created successfully.`);
    logger.silly(result as any);

    if (result.id) {
      if (projectExists) {
        spinner.succeed(`Project "${appName}" updated successfully.`);
      } else {
        spinner.succeed(chalk.green("Project created successfully!"));
      }
    }

    return result as StaticSiteARMResource;
  } catch (error: any) {
    spinner.fail(chalk.red("Project creation failed."));
    logger.error(error.message, true);
    return undefined as any;
  }
}