CLI tool to sync your Markdown to Leaflet
leafletpub atproto cli markdown

Allow loading config in different JS formats and JSON

sharpmars.nekoweb.org 7a5732bd b8baa10d

Waiting for spindle ...
+34 -18
+1 -1
.gitignore
··· 34 34 .DS_Store 35 35 36 36 testing 37 - leaflet-md.config.ts 37 + leaflet-md.config.* 38 38 leaflet-md.map.json
+3 -6
src/commands/config-cmd.ts
··· 1 1 import { defineCommand } from "citty"; 2 - import { join } from "node:path"; 3 2 import { consola } from "consola"; 4 3 import { colorize, leftAlign, type ColorName } from "consola/utils"; 5 - import type { Config } from "../config"; 6 4 import type { Did } from "@atcute/lexicons"; 7 - import { exists, jiti } from "../utils.ts"; 5 + import { exists, loadConfig } from "../utils.ts"; 8 6 9 7 export const configCmd = defineCommand({ 10 8 async run(context) { 11 - const configPath = join(process.cwd(), "/leaflet-md.config.ts"); 9 + const config = await loadConfig(); 12 10 13 - if (!exists(configPath)) { 11 + if (!config) { 14 12 throw new Error("Could not find config"); 15 13 } 16 14 ··· 32 30 ? consola.success(colorize("green", "BSKY_PASSW provided")) 33 31 : consola.fail(colorize("red", "BSKY_PASSW not provided")); 34 32 35 - const config = (await jiti.import(configPath, { default: true })) as Config; 36 33 printConfigLine("Glob pattern", config.glob.pattern); 37 34 if (config.glob.base) printConfigLine("Search folder", config.glob.base); 38 35 if (config.publicationUri) {
+3 -6
src/commands/files-cmd.ts
··· 1 1 import { defineCommand } from "citty"; 2 2 import { join, normalize } from "node:path"; 3 3 import { glob } from "node:fs/promises"; 4 - import type { Config } from "../config"; 5 4 import { consola } from "consola"; 6 5 import { colorize } from "consola/utils"; 7 - import { readJson, exists, jiti } from "../utils"; 6 + import { readJson, exists, loadConfig } from "../utils"; 8 7 9 8 const ListTypeValues = ["all", "uploaded", "to-upload", "not-found"] as const; 10 9 type ListTypes = (typeof ListTypeValues)[number]; ··· 20 19 listType = context.args.list as ListTypes; 21 20 } 22 21 23 - const configPath = join(process.cwd(), "/leaflet-md.config.ts"); 22 + const config = await loadConfig(); 24 23 const mapPath = join(process.cwd(), "/leaflet-md.map.json"); 25 24 26 - if (!exists(configPath)) { 25 + if (!config) { 27 26 throw new Error("Could not find config"); 28 27 } 29 - 30 - const config = (await jiti.import(configPath, { default: true })) as Config; 31 28 32 29 const uploadedDocs: string[] = []; 33 30
+7 -5
src/commands/sync-cmd.ts
··· 16 16 import { RepoReader } from "@atcute/car/v4"; 17 17 import * as CID from "@atcute/cid"; 18 18 import * as CBOR from "@atcute/cbor"; 19 - import type { Config } from "../config.ts"; 20 19 import { generateDoc } from "../doc.ts"; 21 20 import yoctoSpinner from "yocto-spinner"; 22 21 import { consola } from "consola"; 23 - import { exists, readJson, jiti } from "../utils.ts"; 22 + import { exists, readJson, loadConfig } from "../utils.ts"; 24 23 import { parse } from "yaml"; 25 24 26 25 export const syncCmd = defineCommand({ 27 26 async run({ cmd, args }) { 28 - const configPath = join(process.cwd(), "/leaflet-md.config.ts"); 29 - const config = (await jiti.import(configPath, { default: true })) as Config; 27 + const config = await loadConfig(); 30 28 31 - const mapPath = join(configPath, "../leaflet-md.map.json"); 29 + if (!config) { 30 + throw new Error("Could not find config"); 31 + } 32 + 33 + const mapPath = join(process.cwd(), "/leaflet-md.map.json"); 32 34 33 35 const handler = new CredentialManager({ service: "https://bsky.social" }); 34 36 const client = new Client({ handler: handler });
+20
src/utils.ts
··· 1 1 import { readFile, access } from "node:fs/promises"; 2 2 import { createJiti } from "jiti"; 3 + import { join } from "node:path"; 4 + import type { Config } from "./config"; 3 5 4 6 export const jiti = createJiti(import.meta.url); 5 7 ··· 17 19 18 20 return JSON.parse((await readFile(path)).toString()); 19 21 } 22 + 23 + export async function loadConfig() { 24 + const extensions = [".js", ".ts", ".mjs", ".mts", ".json"]; 25 + 26 + let selectedExt: string | undefined = undefined; 27 + for (const ext of extensions) { 28 + if (await exists(join(process.cwd(), `/leaflet-md.config${ext}`))) { 29 + selectedExt = ext; 30 + break; 31 + } 32 + } 33 + 34 + if (!selectedExt) return undefined; 35 + 36 + const configPath = join(process.cwd(), `/leaflet-md.config${selectedExt}`); 37 + const config = (await jiti.import(configPath, { default: true })) as Config; 38 + return config; 39 + }