Skip to content

Commit ae4ba98

Browse files
authored
fix: check if port before running http-server (#81)
Closes #80
1 parent 4443975 commit ae4ba98

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/cli/commands/start.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ export async function start(startContext: string, program: CLIConfig) {
7070
const { port } = parseUrl(useAppDevServer);
7171
appPort = port;
7272
} else {
73+
if (
74+
(await isPortAvailable({
75+
port: appPort,
76+
})) === false
77+
) {
78+
const randomPort = Math.floor(Math.random() * 49150) + 1024;
79+
// @Todo: don't block and just define a random port for static server
80+
// program.appPort = randomPort;
81+
82+
console.info(`INFO: Cannot start static server at "http://${program.host}:${appPort}". Port is already in use.`);
83+
console.info(`INFO: Choose a different port (1024 to 49151).`);
84+
console.info(`Hint: Try swa start ${startContext} --app-port=${randomPort}`);
85+
process.exit(0);
86+
}
87+
7388
const { command: hostCommand, args: hostArgs } = createRuntimeHost({
7489
appPort: appPort,
7590
proxyHost: program.host as string,

src/proxy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const SWA_CLI_PORT = parseInt(process.env.SWA_CLI_PORT || "", 10);
1515
const SWA_CLI_APP_URI = buildAdress(SWA_CLI_HOST, process.env.SWA_CLI_APP_PORT);
1616
const SWA_CLI_API_URI = buildAdress(SWA_CLI_HOST, process.env.SWA_CLI_API_PORT);
1717
const SWA_CLI_AUTH_URI = buildAdress(SWA_CLI_HOST, process.env.SWA_CLI_AUTH_PORT);
18+
const SWA_CLI_APP_ARTIFACT_LOCATION = process.env.SWA_CLI_APP_ARTIFACT_LOCATION;
1819

1920
if (!isHttpUrl(SWA_CLI_APP_URI)) {
2021
console.log(`The provided app URI is not a valid`);
@@ -56,7 +57,6 @@ const serveStatic = (file: string, res: http.ServerResponse, status = 200) => {
5657
res.end(JSON.stringify(err));
5758
return;
5859
}
59-
console.log("serving", file);
6060
res.writeHead(status);
6161
res.end(data);
6262
});
@@ -84,7 +84,7 @@ const readRoutes = (folder: string): UserDefinedRoute[] => {
8484
return require(path.join(folder, routesFile)).routes || [];
8585
};
8686

87-
const routes = readRoutes(process.env.SWA_CLI_APP_ARTIFACT_LOCATION || "");
87+
const routes = readRoutes(SWA_CLI_APP_ARTIFACT_LOCATION || "");
8888

8989
const routeTest = (userDefinedRoute: string, currentRoute: string) => {
9090
if (userDefinedRoute === currentRoute) {
@@ -185,7 +185,7 @@ const server = http.createServer(function (req, res) {
185185
// detected SPA mode
186186
else if (req.url.startsWith("/?")) {
187187
console.log("proxy>", req.method, req.headers.host + req.url);
188-
const fileIndex = path.join(process.env.SWA_CLI_APP_ARTIFACT_LOCATION || "", "index.html");
188+
const fileIndex = path.join(SWA_CLI_APP_ARTIFACT_LOCATION || "", "index.html");
189189
serveStatic(fileIndex, res);
190190
}
191191

src/utils.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ export const readConfigFile = ({ userConfig }: { userConfig?: Partial<GithubActi
219219
// if the user provides different app location, app artifact location or api location, use that information
220220
if (userConfig) {
221221
const { apiLocation, appArtifactLocation, appLocation } = validateUserConfig(userConfig);
222-
app_location = userConfig.appLocation || appLocation;
223-
app_artifact_location = userConfig.appArtifactLocation || appArtifactLocation;
224-
api_location = userConfig.apiLocation || apiLocation;
222+
app_location = appLocation;
223+
app_artifact_location = appArtifactLocation;
224+
api_location = apiLocation;
225225
}
226226

227227
// convert variable names to camelCase
@@ -234,9 +234,7 @@ export const readConfigFile = ({ userConfig }: { userConfig?: Partial<GithubActi
234234
appArtifactLocation: app_artifact_location,
235235
};
236236

237-
console.log({ config });
238-
239-
console.info(`INFO: Using SWA configuration file: ${githubActionFile}`);
237+
console.info(`INFO: Found SWA configuration file: ${githubActionFile}`);
240238
if (process.env.DEBUG) {
241239
console.info({ config });
242240
}

0 commit comments

Comments
 (0)