0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00
astro/packages/telemetry/src/config.ts
Emanuele Stoppa 2dd00a0024
chore: import sort source code, exception for the astro package (#10242)
* chore: import sort source code, exception for the `astro` package

* fix import sorting bug

* Update packages/integrations/lit/server.js

Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>

---------

Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
2024-02-27 11:15:27 +00:00

87 lines
2 KiB
TypeScript

import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import dget from 'dlv';
import { dset } from 'dset';
export interface ConfigOptions {
name: string;
}
// Adapted from https://github.com/sindresorhus/env-paths
function getConfigDir(name: string) {
const homedir = os.homedir();
const macos = () => path.join(homedir, 'Library', 'Preferences', name);
const win = () => {
const { APPDATA = path.join(homedir, 'AppData', 'Roaming') } = process.env;
return path.join(APPDATA, name, 'Config');
};
const linux = () => {
const { XDG_CONFIG_HOME = path.join(homedir, '.config') } = process.env;
return path.join(XDG_CONFIG_HOME, name);
};
switch (process.platform) {
case 'darwin':
return macos();
case 'win32':
return win();
default:
return linux();
}
}
export class GlobalConfig {
private dir: string;
private file: string;
constructor(private project: ConfigOptions) {
this.dir = getConfigDir(this.project.name);
this.file = path.join(this.dir, 'config.json');
}
private _store?: Record<string, any>;
private get store(): Record<string, any> {
if (this._store) return this._store;
this.ensureDir();
if (fs.existsSync(this.file)) {
try {
this._store = JSON.parse(fs.readFileSync(this.file).toString());
} catch {}
}
if (!this._store) {
this._store = {};
this.write();
}
return this._store;
}
private set store(value: Record<string, any>) {
this._store = value;
this.write();
}
private ensureDir() {
fs.mkdirSync(this.dir, { recursive: true });
}
write() {
fs.writeFileSync(this.file, JSON.stringify(this.store, null, '\t'));
}
clear(): void {
this.store = {};
fs.rmSync(this.file, { recursive: true });
}
delete(key: string): boolean {
dset(this.store, key, undefined);
this.write();
return true;
}
get(key: string): any {
return dget(this.store, key);
}
has(key: string): boolean {
return typeof this.get(key) !== 'undefined';
}
set(key: string, value: any): void {
dset(this.store, key, value);
this.write();
}
}