Skip to content

Commit

Permalink
feat: add client working dir to support deploying next.js (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjk7989 authored Nov 2, 2023
1 parent f07e351 commit 072bc8c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
31 changes: 20 additions & 11 deletions src/cli/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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:`);
Expand Down Expand Up @@ -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
Expand All @@ -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") {
Expand All @@ -271,15 +280,15 @@ 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...`);

const child = spawn(binary, [], {
env: {
...swaCLIEnv(cliEnv, deployClientEnv),
},
cwd: path.dirname(userWorkflowConfig?.appLocation!),
cwd: clientWorkingDir,
});

let projectUrl = "";
Expand Down Expand Up @@ -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)} 🚀`));
Expand All @@ -338,7 +347,7 @@ export async function deploy(options: SWACLIConfig) {
);
logGitHubIssueMessageAndExit();
} finally {
cleanUp();
cleanUp(clientWorkingDir);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
10 changes: 8 additions & 2 deletions src/core/deploy-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -121,4 +121,10 @@ export function cleanUp() {

clean(".\\app.zip");
clean(".\\api.zip");

if (fs.existsSync(clientWorkingDir)) {
try {
fs.rmdirSync(clientWorkingDir, { recursive: true });
} catch {}
}
}
5 changes: 4 additions & 1 deletion src/core/utils/user-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 072bc8c

Please sign in to comment.