Skip to content

Commit

Permalink
[desktop]: Refactor main.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Jun 19, 2024
1 parent 134b4fa commit f01e80d
Show file tree
Hide file tree
Showing 27 changed files with 819 additions and 994 deletions.
1 change: 0 additions & 1 deletion desktop/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
main.js
src/**/*.js
app/**/*.js
!app/**/preload.js
*.js.map

node_modules
Expand Down
1 change: 0 additions & 1 deletion desktop/app/.gitignore

This file was deleted.

61 changes: 61 additions & 0 deletions desktop/app/argument.parser.ts
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)
}
}
60 changes: 60 additions & 0 deletions desktop/app/local.storage.ts
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 })
}
}
}
Loading

0 comments on commit f01e80d

Please sign in to comment.