diff --git a/src/cli/commands/deploy/deploy.ts b/src/cli/commands/deploy/deploy.ts index b0e98bb4..9e2c131c 100644 --- a/src/cli/commands/deploy/deploy.ts +++ b/src/cli/commands/deploy/deploy.ts @@ -13,7 +13,7 @@ import { updateSwaCliConfigFile, } from "../../../core"; import { chooseOrCreateProjectDetails, getStaticSiteDeployment } from "../../../core/account"; -import { DEFAULT_RUNTIME_LANGUAGE } from "../../../core/constants"; +import { DEFAULT_RUNTIME_LANGUAGE, STATIC_SITE_CLIENT_WORKING_FOLDER } from "../../../core/constants"; import { cleanUp, getDeployClientPath } from "../../../core/deploy-client"; import { swaCLIEnv } from "../../../core/env"; import { getDefaultVersion } from "../../../core/functions-versions"; @@ -63,13 +63,17 @@ export async function deploy(options: SWACLIConfig) { } } - // make sure outputLocation is set - const resolvedOutputLocation = path.resolve(appLocation, outputLocation || process.cwd()); + logger.silly(`Resolving outputLocation=${outputLocation} full path...`); + let resolvedOutputLocation = path.resolve(appLocation, outputLocation as string); // if folder exists, deploy from a specific build folder (outputLocation), relative to appLocation if (!fs.existsSync(resolvedOutputLocation)) { - logger.error(`The folder "${resolvedOutputLocation}" is not found. Exit.`, true); - return; + if (!fs.existsSync(outputLocation as string)) { + logger.error(`The folder "${resolvedOutputLocation}" is not found. Exit.`, true); + return; + } + // otherwise, build folder (outputLocation) is using the absolute location + resolvedOutputLocation = path.resolve(outputLocation as string); } logger.log(`Deploying front-end files from folder:`); @@ -240,8 +244,8 @@ export async function deploy(options: SWACLIConfig) { DEPLOYMENT_TOKEN: deploymentToken, // /!\ Static site client doesn't use OUTPUT_LOCATION at all if SKIP_APP_BUILD is set, // so you need to provide the output path as the app location - APP_LOCATION: userWorkflowConfig?.appLocation, - OUTPUT_LOCATION: userWorkflowConfig?.outputLocation, + APP_LOCATION: userWorkflowConfig?.outputLocation, + // OUTPUT_LOCATION: outputLocation, API_LOCATION: userWorkflowConfig?.apiLocation, DATA_API_LOCATION: userWorkflowConfig?.dataApiLocation, // If config file is not in output location, we need to tell where to find it @@ -251,6 +255,11 @@ export async function deploy(options: SWACLIConfig) { FUNCTION_LANGUAGE_VERSION: apiVersion, }; + const clientWorkingDir = path.resolve(deployClientEnv.REPOSITORY_BASE ?? "", STATIC_SITE_CLIENT_WORKING_FOLDER); + if (!fs.existsSync(clientWorkingDir)) { + fs.mkdirSync(clientWorkingDir); + } + // set the DEPLOYMENT_ENVIRONMENT env variable only when the user has provided // a deployment environment which is not "production". if (options.env?.toLowerCase() !== "production" && options.env?.toLowerCase() !== "prod") { @@ -271,7 +280,7 @@ export async function deploy(options: SWACLIConfig) { logger.silly(`Deploying using ${cliEnv.SWA_CLI_DEPLOY_BINARY}`); logger.silly(`Deploying using the following options:`); logger.silly({ env: { ...cliEnv, ...deployClientEnv } }); - logger.silly(`StaticSiteClient working directory: ${path.dirname(userWorkflowConfig?.appLocation!)}`); + logger.silly(`StaticSiteClient working directory: ${clientWorkingDir}`); spinner.start(`Preparing deployment. Please wait...`); @@ -279,7 +288,7 @@ export async function deploy(options: SWACLIConfig) { env: { ...swaCLIEnv(cliEnv, deployClientEnv), }, - cwd: path.dirname(userWorkflowConfig?.appLocation!), + cwd: clientWorkingDir, }); let projectUrl = ""; @@ -321,7 +330,7 @@ export async function deploy(options: SWACLIConfig) { }); child.on("close", (code) => { - cleanUp(); + cleanUp(clientWorkingDir); if (code === 0) { spinner.succeed(chalk.green(`Project deployed to ${chalk.underline(projectUrl)} 🚀`)); @@ -338,7 +347,7 @@ export async function deploy(options: SWACLIConfig) { ); logGitHubIssueMessageAndExit(); } finally { - cleanUp(); + cleanUp(clientWorkingDir); } } diff --git a/src/core/constants.ts b/src/core/constants.ts index 5c5a88f5..213e0e78 100644 --- a/src/core/constants.ts +++ b/src/core/constants.ts @@ -8,6 +8,7 @@ export const DEPLOY_BINARY_NAME = "StaticSitesClient"; export const DEPLOY_BINARY_STABLE_TAG = "stable"; export const DEPLOY_FOLDER = path.join(os.homedir(), ".swa", "deploy"); export const STATIC_SITE_CLIENT_RELEASE_METADATA_URL = "https://swalocaldeploy.azureedge.net/downloads/versions.json"; +export const STATIC_SITE_CLIENT_WORKING_FOLDER = "staticsites-cli"; // Data-api-builder related constants export const DATA_API_BUILDER_BINARY_NAME = "DataApiBuilder"; diff --git a/src/core/deploy-client.ts b/src/core/deploy-client.ts index 20b704d7..b88388f7 100644 --- a/src/core/deploy-client.ts +++ b/src/core/deploy-client.ts @@ -109,9 +109,9 @@ export async function fetchClientVersionDefinition(releaseVersion: string): Prom // TODO: get StaticSiteClient to remove zip files // TODO: can these ZIPs be created under /tmp? -export function cleanUp() { +export function cleanUp(clientWorkingDir: string) { const clean = (file: string) => { - const filepath = path.join(process.cwd(), file); + const filepath = path.join(clientWorkingDir, file); if (fs.existsSync(filepath)) { try { fs.unlinkSync(filepath); @@ -121,4 +121,10 @@ export function cleanUp() { clean(".\\app.zip"); clean(".\\api.zip"); + + if (fs.existsSync(clientWorkingDir)) { + try { + fs.rmdirSync(clientWorkingDir, { recursive: true }); + } catch {} + } } diff --git a/src/core/utils/user-config.ts b/src/core/utils/user-config.ts index 0f180bf0..622c8767 100644 --- a/src/core/utils/user-config.ts +++ b/src/core/utils/user-config.ts @@ -172,7 +172,10 @@ async function loadSWAConfigSchema(): Promise<JSONSchemaType<SWACLIConfigFile> | logger.silly(`Schema loaded successfully from ${schemaUrl}`); return (await res.json()) as JSONSchemaType<SWACLIConfigFile>; } - } catch {} + logger.silly(`Status: ${res.status} ${res.statusText}.`); + } catch (err) { + logger.warn((err as any).message); + } logger.silly(`Failed to load schema from ${schemaUrl}`); return null;