-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
819 additions
and
994 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
main.js | ||
src/**/*.js | ||
app/**/*.js | ||
!app/**/preload.js | ||
*.js.map | ||
|
||
node_modules | ||
|
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
import { parseArgs } from 'util' | ||
|
||
export type ApplicationMode = 'UI' | 'API' | 'FULL' | ||
|
||
export class ParsedArgument { | ||
constructor( | ||
readonly serve: boolean, | ||
readonly mode: ApplicationMode, | ||
readonly host: string, | ||
readonly port: number, | ||
) {} | ||
|
||
get uiMode() { | ||
return !this.serve && this.mode === 'UI' | ||
} | ||
|
||
get apiMode() { | ||
return !this.serve && this.mode === 'API' | ||
} | ||
|
||
get fullMode() { | ||
return this.serve || this.mode === 'FULL' | ||
} | ||
|
||
get uri() { | ||
return `${this.host}:${this.port}` | ||
} | ||
} | ||
|
||
export class ArgumentParser { | ||
static parse(args?: string[]): ParsedArgument { | ||
const parsed = parseArgs({ | ||
args: args ?? process.argv.slice(1), | ||
allowPositionals: true, | ||
options: { | ||
serve: { | ||
type: 'boolean', | ||
}, | ||
mode: { | ||
type: 'string', | ||
}, | ||
host: { | ||
type: 'string', | ||
}, | ||
port: { | ||
type: 'string', | ||
}, | ||
}, | ||
}) | ||
|
||
const serve = parsed.values.serve ?? false | ||
const mode: ApplicationMode = | ||
parsed.values.mode === 'ui' ? 'UI' | ||
: parsed.values.mode === 'api' ? 'API' | ||
: 'FULL' | ||
const host = parsed.values.host || 'localhost' | ||
const port = parseInt(parsed.values.port || '0') || (serve ? 7000 : 0) | ||
|
||
return new ParsedArgument(serve, mode, host, port) | ||
} | ||
} |
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,60 @@ | ||
import * as fs from 'fs' | ||
import { dirname } from 'path' | ||
|
||
export class LocalStorage<T extends {}> { | ||
private readonly data: T = Object.create(null) | ||
|
||
constructor(private path: string) { | ||
try { | ||
console.info(`loading config file at ${path}`) | ||
|
||
const parsedData = JSON.parse(fs.readFileSync(path, 'utf8')) | ||
|
||
if (typeof parsedData === 'object' && !Array.isArray(parsedData) && parsedData) { | ||
Object.assign(this.data, parsedData) | ||
} | ||
} catch (e) { | ||
console.error(e) | ||
|
||
this.ensureDirectory() | ||
|
||
fs.writeFileSync(path, '{}', { mode: 0o666 }) | ||
} | ||
} | ||
|
||
get<K extends keyof T>(key: K): T[K] | undefined { | ||
return this.data[key] | ||
} | ||
|
||
set<K extends keyof T>(key: K, value: T[K]) { | ||
this.data[key] = value | ||
} | ||
|
||
delete<K extends keyof T>(key: K) { | ||
if (this.has(key)) { | ||
delete this.data[key] | ||
} | ||
} | ||
|
||
has<K extends keyof T>(key: K | string) { | ||
return key in this.data | ||
} | ||
|
||
save() { | ||
try { | ||
this.ensureDirectory() | ||
fs.writeFileSync(this.path, JSON.stringify(this.data)) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
|
||
private ensureDirectory(): void { | ||
const dir = dirname(this.path) | ||
|
||
if (!fs.existsSync(dir)) { | ||
// Ensure the directory exists as it could have been deleted in the meantime. | ||
fs.mkdirSync(dir, { recursive: true }) | ||
} | ||
} | ||
} |
Oops, something went wrong.