49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import os from 'os';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
|
|
export const envisionPath = path.join(os.homedir(), '.config', 'envision');
|
|
const configPath = path.join(os.homedir(), '.config', 'envision', 'config.json');
|
|
|
|
export const pidFilePath = path.join(envisionPath, 'app.pid');
|
|
export const dbPath = path.join(envisionPath, 'db.sqlite');
|
|
const envisionPidDir = path.join(envisionPath);
|
|
export const getPidList = () => {
|
|
const files = fs.readdirSync(envisionPidDir);
|
|
const pidFiles = files.filter((file) => file.endsWith('.pid'));
|
|
return pidFiles.map((file) => {
|
|
const pid = fs.readFileSync(path.join(envisionPidDir, file), 'utf-8');
|
|
return { pid, file: path.join(envisionPidDir, file) };
|
|
});
|
|
};
|
|
export const writeVitePid = (pid: number) => {
|
|
fs.writeFileSync(path.join(envisionPath, `vite-${pid}.pid`), pid.toString());
|
|
};
|
|
export const checkFileExists = (filePath: string) => {
|
|
try {
|
|
fs.accessSync(filePath, fs.constants.F_OK);
|
|
return true;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
};
|
|
export const getConfig = () => {
|
|
if (!checkFileExists(envisionPath)) {
|
|
fs.mkdirSync(envisionPath, { recursive: true });
|
|
}
|
|
if (checkFileExists(configPath)) {
|
|
const config = fs.readFileSync(configPath, 'utf-8');
|
|
try {
|
|
return JSON.parse(config);
|
|
} catch (e) {
|
|
return {};
|
|
}
|
|
}
|
|
return {};
|
|
};
|
|
|
|
export const writeConfig = (config: Record<string, any>) => {
|
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
return config;
|
|
};
|