[update] update tool scripts
This commit is contained in:
19
lib/date.ts
19
lib/date.ts
@@ -24,3 +24,22 @@ export const normalizeDate = (date: string = "1970-01-01"): string => {
|
||||
};
|
||||
return `${day_num} ${month_en[month_num]}, ${year}`;
|
||||
};
|
||||
|
||||
export const getCurrentTime = (): {
|
||||
year: string;
|
||||
month: string;
|
||||
day: string;
|
||||
hours: string;
|
||||
minutes: string;
|
||||
seconds: string;
|
||||
} => {
|
||||
const today = new Date();
|
||||
return {
|
||||
year: today.getFullYear().toString(),
|
||||
month: String(today.getMonth() + 1).padStart(2, "0"),
|
||||
day: String(today.getDate()).padStart(2, "0"),
|
||||
hours: String(today.getHours()).padStart(2, "0"),
|
||||
minutes: String(today.getMinutes()).padStart(2, "0"),
|
||||
seconds: String(today.getSeconds()).padStart(2, "0"),
|
||||
};
|
||||
};
|
||||
|
||||
25
lib/file.ts
Normal file
25
lib/file.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import * as fs from "fs";
|
||||
|
||||
export function checkAndCreateDirectory(dirPath: string) {
|
||||
try {
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { recursive: true });
|
||||
}
|
||||
fs.accessSync(dirPath, fs.constants.R_OK | fs.constants.W_OK);
|
||||
return true;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export function isDirectoryEmptySync(directory: string) {
|
||||
try {
|
||||
if (!fs.existsSync(directory)) {
|
||||
fs.mkdirSync(directory, { recursive: true });
|
||||
}
|
||||
const files = fs.readdirSync(directory);
|
||||
return files.length === 0;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PostsRootDirectory } from "@/consts/consts";
|
||||
import { PostFilesDirectory } from "@/consts/consts";
|
||||
import { TFrontmatter } from "@/types/frontmatter.type";
|
||||
import { TPostListItem, TPostsByTag } from "@/types/post-list";
|
||||
import fs from "fs";
|
||||
@@ -36,8 +36,8 @@ async function extractFrontmatters(filepath: string): Promise<TFrontmatter> {
|
||||
|
||||
function readPostsDirectory(): string[] {
|
||||
const result: string[] = [];
|
||||
fs.readdirSync(PostsRootDirectory).forEach((fileName) => {
|
||||
const filePath = path.join(PostsRootDirectory, fileName);
|
||||
fs.readdirSync(PostFilesDirectory).forEach((fileName) => {
|
||||
const filePath = path.join(PostFilesDirectory, fileName);
|
||||
const fileStat = fs.statSync(filePath);
|
||||
|
||||
if (fileStat.isFile() && fileName.endsWith(".md")) {
|
||||
@@ -48,7 +48,7 @@ function readPostsDirectory(): string[] {
|
||||
}
|
||||
|
||||
export const getPostFileContent = (postId: string): string | null => {
|
||||
const filePath = path.join(PostsRootDirectory, `${postId}.md`);
|
||||
const filePath = path.join(PostFilesDirectory, `${postId}.md`);
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
return content;
|
||||
|
||||
Reference in New Issue
Block a user