Skip to content

Commit 072bc8c

Browse files
authored
feat: add client working dir to support deploying next.js (#765)
1 parent f07e351 commit 072bc8c

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

src/cli/commands/deploy/deploy.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
updateSwaCliConfigFile,
1414
} from "../../../core";
1515
import { chooseOrCreateProjectDetails, getStaticSiteDeployment } from "../../../core/account";
16-
import { DEFAULT_RUNTIME_LANGUAGE } from "../../../core/constants";
16+
import { DEFAULT_RUNTIME_LANGUAGE, STATIC_SITE_CLIENT_WORKING_FOLDER } from "../../../core/constants";
1717
import { cleanUp, getDeployClientPath } from "../../../core/deploy-client";
1818
import { swaCLIEnv } from "../../../core/env";
1919
import { getDefaultVersion } from "../../../core/functions-versions";
@@ -63,13 +63,17 @@ export async function deploy(options: SWACLIConfig) {
6363
}
6464
}
6565

66-
// make sure outputLocation is set
67-
const resolvedOutputLocation = path.resolve(appLocation, outputLocation || process.cwd());
66+
logger.silly(`Resolving outputLocation=${outputLocation} full path...`);
67+
let resolvedOutputLocation = path.resolve(appLocation, outputLocation as string);
6868

6969
// if folder exists, deploy from a specific build folder (outputLocation), relative to appLocation
7070
if (!fs.existsSync(resolvedOutputLocation)) {
71-
logger.error(`The folder "${resolvedOutputLocation}" is not found. Exit.`, true);
72-
return;
71+
if (!fs.existsSync(outputLocation as string)) {
72+
logger.error(`The folder "${resolvedOutputLocation}" is not found. Exit.`, true);
73+
return;
74+
}
75+
// otherwise, build folder (outputLocation) is using the absolute location
76+
resolvedOutputLocation = path.resolve(outputLocation as string);
7377
}
7478

7579
logger.log(`Deploying front-end files from folder:`);
@@ -240,8 +244,8 @@ export async function deploy(options: SWACLIConfig) {
240244
DEPLOYMENT_TOKEN: deploymentToken,
241245
// /!\ Static site client doesn't use OUTPUT_LOCATION at all if SKIP_APP_BUILD is set,
242246
// so you need to provide the output path as the app location
243-
APP_LOCATION: userWorkflowConfig?.appLocation,
244-
OUTPUT_LOCATION: userWorkflowConfig?.outputLocation,
247+
APP_LOCATION: userWorkflowConfig?.outputLocation,
248+
// OUTPUT_LOCATION: outputLocation,
245249
API_LOCATION: userWorkflowConfig?.apiLocation,
246250
DATA_API_LOCATION: userWorkflowConfig?.dataApiLocation,
247251
// 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) {
251255
FUNCTION_LANGUAGE_VERSION: apiVersion,
252256
};
253257

258+
const clientWorkingDir = path.resolve(deployClientEnv.REPOSITORY_BASE ?? "", STATIC_SITE_CLIENT_WORKING_FOLDER);
259+
if (!fs.existsSync(clientWorkingDir)) {
260+
fs.mkdirSync(clientWorkingDir);
261+
}
262+
254263
// set the DEPLOYMENT_ENVIRONMENT env variable only when the user has provided
255264
// a deployment environment which is not "production".
256265
if (options.env?.toLowerCase() !== "production" && options.env?.toLowerCase() !== "prod") {
@@ -271,15 +280,15 @@ export async function deploy(options: SWACLIConfig) {
271280
logger.silly(`Deploying using ${cliEnv.SWA_CLI_DEPLOY_BINARY}`);
272281
logger.silly(`Deploying using the following options:`);
273282
logger.silly({ env: { ...cliEnv, ...deployClientEnv } });
274-
logger.silly(`StaticSiteClient working directory: ${path.dirname(userWorkflowConfig?.appLocation!)}`);
283+
logger.silly(`StaticSiteClient working directory: ${clientWorkingDir}`);
275284

276285
spinner.start(`Preparing deployment. Please wait...`);
277286

278287
const child = spawn(binary, [], {
279288
env: {
280289
...swaCLIEnv(cliEnv, deployClientEnv),
281290
},
282-
cwd: path.dirname(userWorkflowConfig?.appLocation!),
291+
cwd: clientWorkingDir,
283292
});
284293

285294
let projectUrl = "";
@@ -321,7 +330,7 @@ export async function deploy(options: SWACLIConfig) {
321330
});
322331

323332
child.on("close", (code) => {
324-
cleanUp();
333+
cleanUp(clientWorkingDir);
325334

326335
if (code === 0) {
327336
spinner.succeed(chalk.green(`Project deployed to ${chalk.underline(projectUrl)} 🚀`));
@@ -338,7 +347,7 @@ export async function deploy(options: SWACLIConfig) {
338347
);
339348
logGitHubIssueMessageAndExit();
340349
} finally {
341-
cleanUp();
350+
cleanUp(clientWorkingDir);
342351
}
343352
}
344353

src/core/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const DEPLOY_BINARY_NAME = "StaticSitesClient";
88
export const DEPLOY_BINARY_STABLE_TAG = "stable";
99
export const DEPLOY_FOLDER = path.join(os.homedir(), ".swa", "deploy");
1010
export const STATIC_SITE_CLIENT_RELEASE_METADATA_URL = "https://swalocaldeploy.azureedge.net/downloads/versions.json";
11+
export const STATIC_SITE_CLIENT_WORKING_FOLDER = "staticsites-cli";
1112

1213
// Data-api-builder related constants
1314
export const DATA_API_BUILDER_BINARY_NAME = "DataApiBuilder";

src/core/deploy-client.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ export async function fetchClientVersionDefinition(releaseVersion: string): Prom
109109

110110
// TODO: get StaticSiteClient to remove zip files
111111
// TODO: can these ZIPs be created under /tmp?
112-
export function cleanUp() {
112+
export function cleanUp(clientWorkingDir: string) {
113113
const clean = (file: string) => {
114-
const filepath = path.join(process.cwd(), file);
114+
const filepath = path.join(clientWorkingDir, file);
115115
if (fs.existsSync(filepath)) {
116116
try {
117117
fs.unlinkSync(filepath);
@@ -121,4 +121,10 @@ export function cleanUp() {
121121

122122
clean(".\\app.zip");
123123
clean(".\\api.zip");
124+
125+
if (fs.existsSync(clientWorkingDir)) {
126+
try {
127+
fs.rmdirSync(clientWorkingDir, { recursive: true });
128+
} catch {}
129+
}
124130
}

src/core/utils/user-config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,10 @@ async function loadSWAConfigSchema(): Promise<JSONSchemaType<SWACLIConfigFile> |
172172
logger.silly(`Schema loaded successfully from ${schemaUrl}`);
173173
return (await res.json()) as JSONSchemaType<SWACLIConfigFile>;
174174
}
175-
} catch {}
175+
logger.silly(`Status: ${res.status} ${res.statusText}.`);
176+
} catch (err) {
177+
logger.warn((err as any).message);
178+
}
176179

177180
logger.silly(`Failed to load schema from ${schemaUrl}`);
178181
return null;

0 commit comments

Comments
 (0)