Skip to content

Instantly share code, notes, and snippets.

@aabril
Created May 24, 2020 00:40
Show Gist options
  • Save aabril/23096f36565e8b46054f92e76331b024 to your computer and use it in GitHub Desktop.
Save aabril/23096f36565e8b46054f92e76331b024 to your computer and use it in GitHub Desktop.
journaling system
const fs = require('fs');
const path = require('path');
const baseDir = '/Users/albert/Desktop'
const months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
function getDatePath(ctime){
const d = ctime
const day = String(d.getUTCDate()).padStart(2, "0")
const monthStr = String(d.getMonth()).padStart(2, "0")
const month = months[d.getMonth()]
const year = d.getFullYear()
const dayName = `${day}${month}${year}`
return `${year}/${monthStr}/${dayName}`
}
function mkdirp(newDest){
if (!fs.existsSync(newDest)) fs.mkdirSync(newDest, {recursive: true }, '0777', true);
}
function mv(oldPath, newPath) {
fs.rename(oldPath, newPath, function (err) {
if (err) throw err
console.log('Successfully renamed - AKA moved!')
})
}
function cp(fromPath, destPath) {
fs.copyFile(fromPath, destPath, (err) => {
if (err) throw err;
console.log(`${fromPath} was copied to ${destPath}`);
});
}
function findInDir (dir, filter, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const fileStat = fs.lstatSync(filePath);
if (fileStat.isDirectory()) {
findInDir(filePath, filter, fileList);
} else if (filter.test(filePath)) {
const datePath = getDatePath(fileStat.ctime)
const dir = `${baseDir}/Journal/${datePath}`
mkdirp(dir)
const destFilePath = `${dir}/${filePath.split('/').pop()}`
cp(filePath, destFilePath)
// console.log(destFilePath)
fileList.push(filePath);
}
});
return fileList;
}
// Usage
const files = findInDir(baseDir, /\.png$/);
// console.log(files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment