Created
August 1, 2024 14:51
-
-
Save ejfox/c40348c4b18c71fc33b06d2bbe2aff11 to your computer and use it in GitHub Desktop.
Organize a folder of markdown files into folders by year
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs-extra'); | |
const path = require('path'); | |
const matter = require('gray-matter'); | |
const { Command } = require('commander'); | |
const inquirer = require('inquirer'); | |
const { parseISO, isValid, getYear } = require('date-fns'); | |
// Define the root directory of your markdown files | |
let rootDir = 'ejfox'; | |
// Set up the commander program | |
const program = new Command(); | |
program | |
.version('1.0.0') | |
.description('Organize markdown files based on date metadata') | |
.option('-d, --directory <dir>', 'Directory to organize') | |
.option('-h, --help', 'Display help') | |
.parse(process.argv); | |
const options = program.opts(); | |
async function organizeMarkdownFiles() { | |
try { | |
const files = await fs.readdir(rootDir); | |
for (const file of files) { | |
if (path.extname(file) === '.md') { | |
const filePath = path.join(rootDir, file); | |
const fileContent = await fs.readFile(filePath, 'utf8'); | |
const { data } = matter(fileContent); | |
const date = data.date || data.modified; | |
if (!date) { | |
console.log(`🚫 No date found in ${file}, skipping.`); | |
continue; | |
} | |
let year; | |
if (typeof date === 'string') { | |
// Try to parse the date string using date-fns | |
const parsedDate = parseISO(date); | |
if (isValid(parsedDate)) { | |
year = getYear(parsedDate); | |
} else { | |
console.log(`❌ Invalid date format in ${file}: ${date}`); | |
continue; | |
} | |
} else if (date instanceof Date) { | |
// If date is a Date object, get the year from it | |
year = getYear(date); | |
} else { | |
console.log(`❌ Invalid date format in ${file}: ${date}`); | |
continue; | |
} | |
const targetDir = path.join(rootDir, year.toString()); | |
// Create the target directory if it does not exist | |
if (!fs.existsSync(targetDir)) { | |
await fs.mkdir(targetDir); | |
} | |
// Move the file to the appropriate directory | |
await fs.move(filePath, path.join(targetDir, file)); | |
console.log(`📁 Moved ${file} to ${targetDir}/`); | |
} | |
} | |
} catch (err) { | |
console.error(`❌ Error: ${err.message}`); | |
} | |
} | |
async function main() { | |
if (options.help) { | |
program.help(); | |
return; | |
} | |
if (options.directory) { | |
rootDir = options.directory; | |
} else { | |
const answers = await inquirer.prompt([ | |
{ | |
type: 'input', | |
name: 'directory', | |
message: 'Enter the directory to organize:', | |
default: 'ejfox' | |
} | |
]); | |
rootDir = answers.directory; | |
} | |
console.log(`🚀 Organizing files in directory: ${rootDir}`); | |
await organizeMarkdownFiles(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment