Created
September 14, 2021 01:20
-
-
Save joshgrib/edc61b1e68ae4c8bcc7552bb2ef17639 to your computer and use it in GitHub Desktop.
Create a daily task list, importing the tasks from yesterday and creating a new file with the date
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
/** | |
* A script to create a new daily task list and open it | |
* | |
* File is created in ./daily-task-lists/YYYY/MM with the | |
* file name DD-<day letter code>.md | |
* | |
* Imports the tasks from the last task list and puts them | |
* under a `Yesterday` header, then creates a `Today` | |
* heading and opens the file in VSCode for editing | |
* | |
* Main goal is to have the easy reference for standups | |
* and to track what I'm doing during the day if I need | |
* it for reference | |
*/ | |
const fs = require("fs") | |
const { exec } = require("child_process"); | |
const [nodeExePath, currentScriptPath, ...arguments] = process.argv | |
const exit = msg => { | |
console.error(`[ERR]: ${msg}`) | |
process.exit(1) | |
} | |
const ensureDirExists = dir => { | |
if (!fs.existsSync(dir)) { | |
fs.mkdirSync(dir) | |
} | |
} | |
// get date variables for today | |
let today = new Date() | |
const dayCode = ['U', 'M', 'T', 'W', 'R', 'F', 'S'][today.getDay()] | |
today = today.toISOString().split('T')[0] | |
const fulldate = today.replace(/-/g, '') | |
const [year, month, day] = today.split('-') | |
// check that the folders exist and the file doesn't | |
const rootDir = `./daily-task-lists` | |
const yearDir = `${rootDir}/${year}` | |
ensureDirExists(yearDir) | |
const monthDir = `${yearDir}/${month}` | |
ensureDirExists(monthDir) | |
const dayFile = `${monthDir}/${day}-${dayCode}.md` | |
if (fs.existsSync(dayFile)) { | |
exit(`'${dayFile}' already exists!`) | |
} | |
// get the tasks from the previous day | |
let yesterdayTasks = `` | |
const oldFiles = fs.readdirSync(monthDir) | |
if (oldFiles.length === 0) { | |
console.warn('Unable to get tasks from yesterday') | |
//TODO: look at the previous month/year if needed | |
} else { | |
const lastFile = oldFiles.sort().reverse()[0]; | |
yesterdayTasks = fs.readFileSync(`${monthDir}/${lastFile}`) | |
.toString() | |
.split(/^##\sToday$/gm)[1] | |
.split(/\n/g) | |
.filter(v => v !== '\r' && v !== '') | |
.join('\n') | |
} | |
// create the new file | |
const template = `# ${fulldate} | |
## Yesterday | |
${yesterdayTasks} | |
## Today | |
- [ ] Do something cool! | |
` | |
fs.writeFile(dayFile, template, err => { | |
if (err) { | |
exit(`Unable to write file: ${err}`) | |
} else { | |
console.log(`Successfully created file!\n${dayFile}`) | |
} | |
}) | |
// open the file in VSCode | |
exec(`code -r ${dayFile}`, (error, stdout, stderr) => { | |
if (error) { | |
exit(`Unable to open file in VSCode: ${error.message}`); | |
} | |
if (stderr) { | |
exit(`Unable to open file in VSCode: ${error.message}`); | |
} | |
console.log('Success!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use this script in a private repo I have to maintain a daily task list for work and occasionally personal things. Then I have an easy reference for standups and if I need to look back on when I did something