[update] update tool scripts
This commit is contained in:
@@ -1,37 +1,14 @@
|
||||
import { UserDataDirectory } from "@/consts/consts";
|
||||
import { getCurrentTime } from "@/lib/date";
|
||||
import { checkAndCreateDirectory, isDirectoryEmptySync } from "@/lib/file";
|
||||
import archiver from "archiver";
|
||||
import Color from "colors";
|
||||
import fs from "fs";
|
||||
import * as fs from "fs";
|
||||
import inquirer from "inquirer";
|
||||
import path from "path";
|
||||
import tar from "tar";
|
||||
import * as path from "path";
|
||||
import * as tar from "tar";
|
||||
|
||||
const UserDataDirectory = "./data";
|
||||
|
||||
function checkAndCreateDirectory(dirPath) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
function isDirectoryEmptySync(directory) {
|
||||
try {
|
||||
if (!fs.existsSync(directory)) {
|
||||
fs.mkdirSync(directory, { recursive: true });
|
||||
}
|
||||
const files = fs.readdirSync(directory);
|
||||
return files.length === 0;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
function packageDirectory(sourceDir, outputFilePath) {
|
||||
function packageDirectory(sourceDir: string, outputFilePath: string) {
|
||||
const output = fs.createWriteStream(outputFilePath);
|
||||
const archive = archiver("tar", {
|
||||
gzip: true,
|
||||
@@ -51,7 +28,7 @@ function packageDirectory(sourceDir, outputFilePath) {
|
||||
archive.finalize();
|
||||
}
|
||||
|
||||
function extractTarGz(tarGzPath, targetDir) {
|
||||
function extractTarGz(tarGzPath: string, targetDir: string) {
|
||||
tar
|
||||
.x({
|
||||
file: tarGzPath,
|
||||
@@ -83,16 +60,13 @@ async function main() {
|
||||
})
|
||||
.then((answers) => {
|
||||
const { OutputDirPath } = answers;
|
||||
const date = new Date();
|
||||
const filename = `archive-${date.getFullYear()}-${
|
||||
date.getMonth() + 1
|
||||
}-${date.getDate()}-${date.getHours()}-${date.getMinutes()}-${date.getSeconds()}.tar.gz`;
|
||||
const { year, month, day, hours, minutes, seconds } = getCurrentTime();
|
||||
const filename = `archive-${year}-${month}-${day}-${hours}-${minutes}-${seconds}.tar.gz`;
|
||||
const outFilePath = path.join(OutputDirPath, filename);
|
||||
if (checkAndCreateDirectory(OutputDirPath)) {
|
||||
packageDirectory(UserDataDirectory, outFilePath);
|
||||
}
|
||||
checkAndCreateDirectory(OutputDirPath) && packageDirectory(UserDataDirectory, outFilePath);
|
||||
});
|
||||
break;
|
||||
|
||||
case "Unpack and restore user data":
|
||||
if (!isDirectoryEmptySync(UserDataDirectory)) {
|
||||
const { confirm } = await inquirer.prompt([
|
||||
@@ -106,25 +80,25 @@ async function main() {
|
||||
),
|
||||
},
|
||||
]);
|
||||
if (!confirm) {
|
||||
console.log("Operation canceled.");
|
||||
if (confirm) {
|
||||
inquirer
|
||||
.prompt({
|
||||
type: "input",
|
||||
name: "ArchivedPackagePath",
|
||||
message: "The archive package's path:",
|
||||
})
|
||||
.then((answers) => {
|
||||
const { ArchivedPackagePath } = answers;
|
||||
if (fs.existsSync(ArchivedPackagePath)) {
|
||||
extractTarGz(ArchivedPackagePath, UserDataDirectory);
|
||||
} else {
|
||||
console.log(Color.red("The archive package does not exist."));
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
inquirer
|
||||
.prompt({
|
||||
type: "input",
|
||||
name: "ArchiveFilePath",
|
||||
message: "The archive package's path:",
|
||||
})
|
||||
.then((answers) => {
|
||||
const { ArchiveFilePath } = answers;
|
||||
if (fs.existsSync(ArchiveFilePath)) {
|
||||
extractTarGz(ArchiveFilePath, UserDataDirectory);
|
||||
} else {
|
||||
console.log(Color.red("The archive package does not exist."));
|
||||
}
|
||||
});
|
||||
console.log("Operation canceled.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
import { spawn } from "child_process";
|
||||
import Color from "colors";
|
||||
import fs from "fs";
|
||||
import inquirer from "inquirer";
|
||||
import path from "path";
|
||||
import process from "process";
|
||||
|
||||
let today = new Date();
|
||||
|
||||
let year = today.getFullYear();
|
||||
let month = today.getMonth() + 1;
|
||||
let day = today.getDate();
|
||||
|
||||
month = month < 10 ? "0" + month : month;
|
||||
day = day < 10 ? "0" + day : day;
|
||||
|
||||
let formattedDate = year + "-" + month + "-" + day;
|
||||
|
||||
const questions = [
|
||||
{
|
||||
type: "input",
|
||||
name: "title",
|
||||
message: "What's the title?",
|
||||
validate: function (input) {
|
||||
if (input.trim() === "") {
|
||||
return "Please enter a title.";
|
||||
}
|
||||
return true;
|
||||
},
|
||||
},
|
||||
{
|
||||
type: "input",
|
||||
name: "subtitle",
|
||||
message: "What's the subtitle?",
|
||||
},
|
||||
{
|
||||
type: "tags",
|
||||
name: "tags",
|
||||
message: "Assign tags for the posts and separate them with commas.",
|
||||
},
|
||||
{
|
||||
type: "confirm",
|
||||
name: "noPrompt",
|
||||
message: "Do NOT prompt this post? (D:false)",
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
type: "confirm",
|
||||
name: "pin",
|
||||
message: "Do you want to pin this post? (D:false)",
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
type: "confirm",
|
||||
name: "allowShare",
|
||||
message: "Do you allow everybody share this post? (D:true)",
|
||||
default: true,
|
||||
},
|
||||
];
|
||||
|
||||
const template = (title, subtitle, tags, noPrompt, pin, allowShare) => `---
|
||||
title: ${JSON.stringify(title.toLowerCase())}
|
||||
subtitle: ${JSON.stringify(subtitle)}
|
||||
summary: ""
|
||||
coverURL: ""
|
||||
time: "${formattedDate}"
|
||||
tags: ${JSON.stringify(tags)}
|
||||
noPrompt: ${noPrompt}
|
||||
pin: ${pin}
|
||||
allowShare: ${allowShare}
|
||||
closed: false
|
||||
---
|
||||
`;
|
||||
|
||||
inquirer.prompt(questions).then((answers) => {
|
||||
const tags = answers.tags
|
||||
.split(",")
|
||||
.map((tag) => tag.trim())
|
||||
.filter((tag) => tag !== "");
|
||||
const content = template(answers.title, answers.subtitle, tags, answers.noPrompt, answers.pin, answers.allowShare);
|
||||
const sluggedTitle = answers.title.replace(/\s/g, "-");
|
||||
const postFileName = `${formattedDate}-${sluggedTitle}.md`;
|
||||
const postFilePath = path.resolve(path.join("./data/posts", postFileName));
|
||||
fs.writeFile(postFilePath, content, "utf-8", (err) => {
|
||||
if (err) console.log(err);
|
||||
console.log(Color.green(Color.bold("Create Post Succeed.")));
|
||||
console.log(`Open the file ${Color.cyan(postFilePath)} to write your blog now.`);
|
||||
console.log("Some fields, such as summary, need to be filled in by yourself after opening the file.");
|
||||
if (process.platform === "win32") {
|
||||
spawn("cmd", ["/c", "start", postFilePath]);
|
||||
return;
|
||||
}
|
||||
if (["darwin", "linux", "freebsd"].includes(process.platform)) {
|
||||
spawn("open", [postFilePath]);
|
||||
return;
|
||||
}
|
||||
});
|
||||
});
|
||||
92
scripts/newpost.ts
Normal file
92
scripts/newpost.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { PostFilesDirectory } from "@/consts/consts";
|
||||
import { getCurrentTime } from "@/lib/date";
|
||||
import { ChildProcessWithoutNullStreams, SpawnSyncReturns, spawn, spawnSync } from "child_process";
|
||||
import colors from "colors";
|
||||
import fs from "fs";
|
||||
import inquirer, { QuestionCollection } from "inquirer";
|
||||
import _ from "lodash";
|
||||
import path from "path";
|
||||
import { titleCase } from "title-case";
|
||||
|
||||
type TAnswer = {
|
||||
title: string;
|
||||
subtitle: string;
|
||||
tags: string;
|
||||
noPrompt: boolean;
|
||||
pin: boolean;
|
||||
allowShare: boolean;
|
||||
};
|
||||
|
||||
const questions: QuestionCollection<TAnswer> = [
|
||||
{
|
||||
type: "input",
|
||||
name: "title",
|
||||
message: "What's the title?",
|
||||
validate: (input: string) => (input.trim() === "" ? "Please enter a title." : true),
|
||||
},
|
||||
{ type: "input", name: "subtitle", message: "What's the subtitle?" },
|
||||
{ type: "input", name: "tags", message: "Assign tags for the posts and separate them with commas." },
|
||||
{ type: "confirm", name: "noPrompt", message: "Do NOT prompt this post? (D:false)", default: false },
|
||||
{ type: "confirm", name: "pin", message: "Do you want to pin this post? (D:false)", default: false },
|
||||
{ type: "confirm", name: "allowShare", message: "Do you allow everybody share this post? (D:true)", default: true },
|
||||
];
|
||||
|
||||
const createTemplate = (answers: TAnswer): string => {
|
||||
const { year, month, day } = getCurrentTime();
|
||||
const tags = JSON.stringify(
|
||||
answers.tags
|
||||
.split(",")
|
||||
.map((tag) => tag.trim())
|
||||
.filter((tag) => tag !== ""),
|
||||
);
|
||||
|
||||
return `---
|
||||
title: "${titleCase(answers.title)}"
|
||||
subtitle: "${answers.subtitle}"
|
||||
summary: ""
|
||||
author: ""
|
||||
coverURL: ""
|
||||
time: "${year}-${month}-${day}"
|
||||
tags: ${tags}
|
||||
noPrompt: ${answers.noPrompt}
|
||||
pin: ${answers.pin}
|
||||
allowShare: ${answers.allowShare}
|
||||
closed: false
|
||||
---`;
|
||||
};
|
||||
|
||||
const writePostFile = (filePath: string, content: string): void => {
|
||||
fs.writeFile(filePath, content, "utf-8", (err) => {
|
||||
if (err) {
|
||||
console.error("Error writing file:", err);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(colors.green(colors.bold("Create Post Succeed.")));
|
||||
console.log(`Open the file ${colors.cyan(filePath)} to write your blog now.`);
|
||||
console.log("Some fields, such as summary, need to be filled in by yourself after opening the file.");
|
||||
|
||||
const openFileCommand: Record<string, () => ChildProcessWithoutNullStreams | SpawnSyncReturns<Buffer>> = {
|
||||
win32: () => spawn("cmd", ["/c", "start", filePath]),
|
||||
darwin: () => spawnSync("open", [filePath]),
|
||||
linux: () => spawnSync("xdg-open", [filePath]),
|
||||
freebsd: () => spawnSync("xdg-open", [filePath]),
|
||||
};
|
||||
|
||||
const command = openFileCommand[process.platform];
|
||||
if (command) command();
|
||||
else {
|
||||
console.log("Unsupported platform. Open the file manually please.");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
inquirer.prompt<TAnswer>(questions).then((answers) => {
|
||||
const { year, month, day } = getCurrentTime();
|
||||
const content = createTemplate(answers);
|
||||
const sluggedTitle = _.kebabCase(answers.title);
|
||||
const postFileName = `${year}-${month}-${day}-${sluggedTitle}.md`;
|
||||
const postFilePath = path.resolve(path.join(PostFilesDirectory, postFileName));
|
||||
|
||||
writePostFile(postFilePath, content);
|
||||
});
|
||||
Reference in New Issue
Block a user