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 = / \. ( m d | m d x ) $ / 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