mirror of
https://github.com/morten-olsen/mini-loader.git
synced 2026-02-08 01:36:26 +01:00
feat: switched from worker API to fs based
This commit is contained in:
50
packages/cli/src/context/context.ts
Normal file
50
packages/cli/src/context/context.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import envPaths from 'env-paths';
|
||||
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
||||
import { mkdir } from 'fs/promises';
|
||||
import { dirname } from 'path';
|
||||
|
||||
type ContextValues = {
|
||||
host: string;
|
||||
token: string;
|
||||
};
|
||||
|
||||
class Context {
|
||||
#location: string;
|
||||
#config?: ContextValues;
|
||||
|
||||
constructor() {
|
||||
const paths = envPaths('dws');
|
||||
this.#location = paths.config;
|
||||
if (existsSync(this.#location)) {
|
||||
this.#config = JSON.parse(readFileSync(this.#location, 'utf-8'));
|
||||
}
|
||||
}
|
||||
|
||||
public get host() {
|
||||
return this.#config?.host;
|
||||
}
|
||||
|
||||
public get token() {
|
||||
return this.#config?.token;
|
||||
}
|
||||
|
||||
public saveLogin = (host: string, token: string) => {
|
||||
this.#config = {
|
||||
...(this.#config || {}),
|
||||
host,
|
||||
token,
|
||||
};
|
||||
this.save();
|
||||
};
|
||||
|
||||
public save = async () => {
|
||||
if (!this.#config) {
|
||||
return;
|
||||
}
|
||||
const json = JSON.stringify(this.#config);
|
||||
mkdir(dirname(this.#location), { recursive: true });
|
||||
writeFileSync(this.#location, json);
|
||||
};
|
||||
}
|
||||
|
||||
export { Context };
|
||||
Reference in New Issue
Block a user