@markdown-vue/mdv
Version:
Markdown-Vue (MDV) lets you write Vue-style components directly inside Markdown files.
63 lines (55 loc) • 2.42 kB
text/typescript
import { Command } from "commander";
import { Compiler } from "@mdv/compiler";
import path from "path";
import fs from "fs";
import { pathToFileURL } from "url";
export default async function loadConfig(configPath?: string) {
// load mdv.config.ts if path is not passed but file exists
let resolvedConfig: any = {};
const fullPath = configPath
? path.resolve(process.cwd(), configPath)
: path.resolve(process.cwd(), "mdv.config.ts");
if (fs.existsSync(fullPath)) {
const fileUrl = pathToFileURL(fullPath).href;
const mod = await import(fileUrl);
resolvedConfig = mod?.default ?? {};
}
return resolvedConfig;
}
const program = new Command();
program.name("mdv").description("Markdown-Vue CLI").version("0.1.0");
program
.command("build")
.option("-c, --config <path>", "Path to config file")
.option("--srcRoot <path>", "Source root for .v.md files")
.option("--cacheDir <path>", "Cache directory")
.option("--skipCleanup", "Skip cleanup of cache files")
.action(async (options) => {
const fileConfig = await loadConfig(options.config);
const config = {
srcRoot: options.srcRoot || fileConfig.srcRoot || "src",
cacheDir: options.cacheDir || fileConfig.cacheDir || ".mdv",
};
const { compileAllMDVFiles, writeComponentsDTS, copyComponentsDir } = Compiler(config);
await compileAllMDVFiles(config.srcRoot);
await copyComponentsDir(path.resolve(__dirname, '../../src'));
await writeComponentsDTS(config.srcRoot);
});
program
.command("watch")
.option("-c, --config <path>", "Path to config file")
.option("--srcRoot <path>", "Source root for .v.md files")
.option("--cacheDir <path>", "Cache directory")
.action(async (options) => {
const fileConfig = await loadConfig(options.config);
const config = {
srcRoot: options.srcRoot || fileConfig.srcRoot || "src",
cacheDir: options.cacheDir || fileConfig.cacheDir || ".mdv",
};
const { watchAll, writeComponentsDTS, copyComponentsDir } = Compiler(config);
await watchAll(config.srcRoot);
await copyComponentsDir(path.resolve(__dirname, '../../src'));
await writeComponentsDTS(config.srcRoot);
});
program.parse();