Skip to content

Commit

Permalink
Fallback to random port if the default is taken. (#330)
Browse files Browse the repository at this point in the history
* Added: set port & setting methods to Config class (#328)

* Update port when taken and start live-server again (fixes #328)
  • Loading branch information
Almo7aya authored and ritwickdey committed Apr 8, 2019
1 parent 41df0b0 commit 0f34e13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Config.ts
Expand Up @@ -25,6 +25,10 @@ export class Config {
return Config.configuration.get(val) as T;
}

private static setSettings(key: string, val: number, isGlobal: boolean = false): Thenable<void> {
return Config.configuration.update(key, val, isGlobal);
}

public static get getHost(): string {
return Config.getSettings<string>('host');
}
Expand All @@ -37,6 +41,10 @@ export class Config {
return Config.getSettings<number>('port');
}

public static setPort(port: number): Thenable<void> {
return Config.setSettings('port', port);
}

public static get getRoot(): string {
return Config.getSettings<string>('root');
}
Expand Down
11 changes: 7 additions & 4 deletions src/appModel.ts
Expand Up @@ -83,7 +83,7 @@ export class AppModel implements IAppModel {
this.tagMissedCallback();
});

LiveServerHelper.StartServer(params, (serverInstance) => {
LiveServerHelper.StartServer(params, async (serverInstance) => {
if (serverInstance && serverInstance.address) {
this.LiveServerInstance = serverInstance;
this.runningPort = serverInstance.address().port;
Expand All @@ -100,10 +100,13 @@ export class AppModel implements IAppModel {
}
}
else {
if (!serverInstance.errorMsg)
this.showPopUpMsg(`Error on port ${Config.getPort}. Please try to change the port through settings or report on GitHub.`, true);
else
if (!serverInstance.errorMsg) {
await Config.setPort(Config.getPort + 1); // + 1 will be fine
this.showPopUpMsg(`The default port : ${Config.getPort - 1} is currently taken, changing port to : ${Config.getPort}.`);
this.Golive(pathUri);
} else {
this.showPopUpMsg(`Something is went wrong! Please check into Developer Console or report on GitHub.`, true);
}
this.IsServerRunning = true; // to revert status - cheat :p
this.ToggleStatusBar(); // reverted
}
Expand Down

0 comments on commit 0f34e13

Please sign in to comment.