From 418cced40785cd88c3435fda82bcff646bd82cb0 Mon Sep 17 00:00:00 2001 From: jank Date: Thu, 19 Jun 2025 07:45:02 +0200 Subject: [PATCH] feat: Add config generation --- .npmrc | 1 + bun.lock | 5 +++++ configuration/configuration.ts | 23 +++++++++++++++++++++++ index.ts | 23 ++++++++++++++++++++++- package.json | 1 + 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 .npmrc create mode 100644 configuration/configuration.ts diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..41583e3 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +@jsr:registry=https://npm.jsr.io diff --git a/bun.lock b/bun.lock index d4d0128..4acf08a 100644 --- a/bun.lock +++ b/bun.lock @@ -4,6 +4,7 @@ "": { "name": "project-cli", "dependencies": { + "@std/toml": "npm:@jsr/std__toml", "@types/commander": "^2.12.5", "@types/inquirer": "^9.0.8", "commander": "^14.0.0", @@ -46,6 +47,10 @@ "@inquirer/type": ["@inquirer/type@3.0.7", "", { "peerDependencies": { "@types/node": ">=18" }, "optionalPeers": ["@types/node"] }, "sha512-PfunHQcjwnju84L+ycmcMKB/pTPIngjUJvfnRhKY6FKPuYXlM4aQCb/nIdTFR6BEhMjFvngzvng/vBAJMZpLSA=="], + "@jsr/std__collections": ["@jsr/std__collections@1.1.1", "https://npm.jsr.io/~/11/@jsr/std__collections/1.1.1.tgz", {}, "sha512-bS4Y5f80IUeDUf+0BgVVWValgBeGcJ/UwFYTC5PBIJtrFqwEmU8IohDBwFLJILyUtiAB1jbJewl0K+REEQ9kQQ=="], + + "@std/toml": ["@jsr/std__toml@1.0.8", "https://npm.jsr.io/~/11/@jsr/std__toml/1.0.8.tgz", { "dependencies": { "@jsr/std__collections": "^1.1.1" } }, "sha512-rnMoiySl30TrsdnfKcroYEWIBlmPvdNoA8fn/Njy3rsZR9C8k5nUeo9kWP/D/PhxYlUaYPtmQ31YhNqGpmEZ3Q=="], + "@types/bun": ["@types/bun@1.2.16", "", { "dependencies": { "bun-types": "1.2.16" } }, "sha512-1aCZJ/6nSiViw339RsaNhkNoEloLaPzZhxMOYEa7OzRzO41IGg5n/7I43/ZIAW/c+Q6cT12Vf7fOZOoVIzb5BQ=="], "@types/commander": ["@types/commander@2.12.5", "", { "dependencies": { "commander": "*" } }, "sha512-YXGZ/rz+s57VbzcvEV9fUoXeJlBt5HaKu5iUheiIWNsJs23bz6AnRuRiZBRVBLYyPnixNvVnuzM5pSaxr8Yp/g=="], diff --git a/configuration/configuration.ts b/configuration/configuration.ts new file mode 100644 index 0000000..2c35e86 --- /dev/null +++ b/configuration/configuration.ts @@ -0,0 +1,23 @@ +import fs from "fs"; +import os from "os"; +import * as toml from "@std/toml"; + +export const configPath = os.homedir() + "/.config/project-cli/"; + +export interface Configuration { + projectsDirectory: string; +} + +export function generateDefaultConfig() { + fs.mkdirSync(configPath, { recursive: true }); + fs.mkdirSync(configPath + "templates/", { recursive: true }); + + const defaultConfig: Configuration = { + projectsDirectory: os.homedir() + "/projects", + }; + + const configString = toml.stringify( + defaultConfig as unknown as Record, + ); + fs.writeFileSync(configPath + "config.toml", configString); +} diff --git a/index.ts b/index.ts index d12aa38..62c0d0d 100644 --- a/index.ts +++ b/index.ts @@ -1,2 +1,23 @@ -console.log("Hello via Bun!"); +import fs from "fs"; +import { confirm } from "@inquirer/prompts"; +import { exit } from "process"; +import { + configPath, + generateDefaultConfig, +} from "./configuration/configuration"; + +if (!fs.existsSync(configPath + "config.toml")) { + const createConfig = await confirm({ + message: "No cofig has been found. Would you like to generate a new one?", + }); + + if (createConfig) { + generateDefaultConfig(); + } else { + console.log( + "This project can not run without the config. Either create it yourself or generate the default.", + ); + exit(1); + } +} diff --git a/package.json b/package.json index 8982789..39b0257 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "typescript": "^5.0.0" }, "dependencies": { + "@std/toml": "npm:@jsr/std__toml", "@types/commander": "^2.12.5", "@types/inquirer": "^9.0.8", "commander": "^14.0.0",