project-cli/configuration/configuration.ts
jank fb23f6fd8b
All checks were successful
Release / Release (push) Successful in 32s
feat: Add basic project creation
2025-06-19 13:48:55 +02:00

60 lines
1.5 KiB
TypeScript

import fs from "fs";
import os from "os";
import * as toml from "@std/toml";
export const configPath = os.homedir() + "/.config/project-cli/";
export interface Project {
name: string;
}
export interface Configuration {
projectsDirectory: string;
cloningCommand: string;
}
export function getProjects(): Project[] {
const projectsString = fs
.readFileSync(configPath + "projects.json")
.toString();
return JSON.parse(projectsString) as Project[];
}
export function getConfiguration(): Configuration {
return toml.parse(
fs.readFileSync(configPath + "config.toml").toString(),
) as unknown as Configuration;
}
export function addProject(project: Project) {
let projects = getProjects();
if (projects.length == undefined) {
saveProjects([project]);
} else {
projects.push(project);
saveProjects(projects);
}
}
export function generateDefaultConfig() {
fs.mkdirSync(configPath, { recursive: true });
fs.mkdirSync(configPath + "templates/", { recursive: true });
const defaultConfig: Configuration = {
projectsDirectory: os.homedir() + "/projects",
cloningCommand: "git clone %s %n",
};
const configString = toml.stringify(
defaultConfig as unknown as Record<string, unknown>,
);
fs.writeFileSync(configPath + "config.toml", configString);
const projects: Project[] = [];
saveProjects(projects);
}
function saveProjects(projects: Project[]) {
const projectsString = JSON.stringify(projects);
fs.writeFileSync(configPath + "projects.json", projectsString);
}