idimitrov.dev/new.ts

37 lines
728 B
TypeScript
Raw Normal View History

2023-11-19 07:18:30 +01:00
import { baseDir, getAllContent } from "$lib/content";
2023-11-20 19:29:43 +01:00
import fs from "fs";
2023-11-11 18:54:06 +01:00
2023-11-20 19:29:43 +01:00
const args = process.argv.slice(2);
2023-11-11 18:54:06 +01:00
2023-11-20 19:29:43 +01:00
const path = args[0];
2023-11-11 18:54:06 +01:00
if (!path) {
2023-11-20 19:29:43 +01:00
throw new Error("Path is needed!");
2023-11-11 18:54:06 +01:00
}
const slug = path.split("/");
2023-11-20 19:29:43 +01:00
const t = slug[slug.length - 1];
2023-11-11 18:54:06 +01:00
2023-11-20 19:29:43 +01:00
const nextZ =
Math.max.apply(
Math,
getAllContent().map(c => Number(c.data.z)),
) + 1;
2023-11-11 18:54:06 +01:00
const meta = (title: string = t, goal: string = "", role: string = "", date: string = "", z: number = nextZ) => `---
title: ${title}
goal: ${goal}
role: ${role}
date: ${date}
z: ${z}
draft: true
---
2023-11-20 19:29:43 +01:00
`;
2023-11-11 18:54:06 +01:00
2023-11-20 19:29:43 +01:00
const filePath = `${baseDir}${path}.md`;
2023-11-11 18:56:51 +01:00
if (fs.existsSync(filePath)) {
2023-11-20 19:29:43 +01:00
throw new Error("File already exists!");
2023-11-11 18:56:51 +01:00
}
2023-11-20 19:29:43 +01:00
fs.writeFileSync(filePath, meta(), { flag: "w+" });