Skip to content

Commit 759f59c

Browse files
committed
feat: add move scripts
1 parent 4319a31 commit 759f59c

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ lang: jp # 仅当文章语言与 `config.ts` 中的网站语言不同时需
5555
| `pnpm build` | 构建网站至 `./dist/` |
5656
| `pnpm preview` | 本地预览已构建的网站 |
5757
| `pnpm new-post <filename>` | 创建新文章 |
58+
| `pnpm move <filename> <targetDir>`| 将文章移动至 `./src/content/posts/<targetDir>` |
5859
| `pnpm astro ...` | 执行 `astro add`, `astro check` 等指令 |
5960
| `pnpm astro --help` | 显示 Astro CLI 帮助 |

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"astro": "astro",
1111
"type-check": "tsc --noEmit --isolatedDeclarations",
1212
"new-post": "node scripts/new-post.js",
13+
"move": "node scripts/move-post.js",
1314
"format": "biome format --write ./src",
1415
"lint": "biome check --write ./src",
1516
"preinstall": "npx only-allow pnpm"

scripts/move-post.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* This is a script to move a post markdown file to a target directory */
2+
3+
import fs from "fs"
4+
import path from "path"
5+
6+
const args = process.argv.slice(2)
7+
8+
if (args.length !== 2) {
9+
console.error(`Error: Incorrect number of arguments
10+
Usage: npm run move -- <source_file> <target_directory>`)
11+
process.exit(1)
12+
}
13+
14+
const sourceFile = args[0]
15+
const targetDir = args[1]
16+
17+
// Add .md extension if not present
18+
const fileExtensionRegex = /\.(md|mdx)$/i
19+
const fileName = fileExtensionRegex.test(sourceFile) ? sourceFile : sourceFile + ".md"
20+
21+
// Ensure source file exists
22+
const sourcePath = path.resolve("./src/content/posts/", fileName)
23+
if (!fs.existsSync(sourcePath)) {
24+
console.error(`Error: Source file ${sourcePath} does not exist`)
25+
process.exit(1)
26+
}
27+
28+
// Create target directory if it doesn't exist
29+
const targetPath = path.resolve("./src/content/posts/", targetDir)
30+
if (!fs.existsSync(targetPath)) {
31+
fs.mkdirSync(targetPath, { recursive: true })
32+
}
33+
34+
// Construct the new file path
35+
const newFilePath = path.join(targetPath, path.basename(fileName))
36+
37+
// Check if target file already exists
38+
if (fs.existsSync(newFilePath)) {
39+
console.error(`Error: Target file ${newFilePath} already exists`)
40+
process.exit(1)
41+
}
42+
43+
// Move the file
44+
try {
45+
fs.renameSync(sourcePath, newFilePath)
46+
console.log(`Successfully moved ${sourcePath} to ${newFilePath}`)
47+
} catch (error) {
48+
console.error(`Error moving file: ${error.message}`)
49+
process.exit(1)
50+
}

0 commit comments

Comments
 (0)