Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to random port if the default is taken. #330

Merged
merged 2 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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