-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatic port checking and command args
- Loading branch information
Mario Reder
committed
Jan 14, 2019
1 parent
b557994
commit f0afc2e
Showing
7 changed files
with
126 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ArgumentParser } from 'argparse' | ||
|
||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
import { Settings, DEFAULT_SETTINGS } from './models/Settings.model' | ||
|
||
export class Arguments { | ||
public settings: Settings | ||
|
||
private parser = new ArgumentParser({ | ||
addHelp: true, | ||
description: 'Net64+ server' | ||
}) | ||
|
||
constructor () { | ||
let settings: Settings | undefined | ||
let settingsPath = path.join(__dirname, '../settings.json') | ||
try { | ||
settings = JSON.parse(fs.readFileSync(settingsPath, { | ||
encoding: 'utf8' | ||
})) | ||
} catch (err) { | ||
fs.writeFileSync(settingsPath, JSON.stringify(DEFAULT_SETTINGS)) | ||
console.info('Failed to find or parse settings.json file. Using default settings instead and created a settings.json just for you.') | ||
} | ||
|
||
this.parser.addArgument([ '--port', '-P' ], { | ||
type: (int: string) => parseInt(int) | ||
}) | ||
this.parser.addArgument([ '--gamemode', '-g' ]) | ||
this.parser.addArgument([ '--disableGamemodeVote', '-G' ], { | ||
action: 'storeFalse' | ||
}) | ||
this.parser.addArgument([ '--passwordRequired', '-pr' ], { | ||
action: 'storeTrue' | ||
}) | ||
this.parser.addArgument([ '--password', '-p' ]) | ||
this.parser.addArgument([ '--name', '-n' ]) | ||
this.parser.addArgument([ '--domain', '-D' ]) | ||
this.parser.addArgument([ '--description', '-d' ]) | ||
this.parser.addArgument([ '--enableWebHook', '-w' ], { | ||
action: 'storeTrue' | ||
}) | ||
this.parser.addArgument([ '--apiKey', '-k' ]) | ||
const parsed = this.parser.parseArgs() as Settings | ||
|
||
this.settings = {} as any | ||
Object.entries(DEFAULT_SETTINGS).forEach(([ key, defaultValue ]: [ string, any ]) => { | ||
// @ts-ignore | ||
this.settings[key] = parsed[key] || (settings ? settings[key] : null) || defaultValue | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters