Skip to content

Instantly share code, notes, and snippets.

@joshgrib
Created December 22, 2022 22:44
Show Gist options
  • Save joshgrib/63322971ed604d1fe7e0bc8b403c1bfe to your computer and use it in GitHub Desktop.
Save joshgrib/63322971ed604d1fe7e0bc8b403c1bfe to your computer and use it in GitHub Desktop.
Daily task list script
/**
* 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)
}
}
let [dayOffset] = arguments;
dayOffset = dayOffset || 0;
// get date variables for today
let today = new Date()
today.setDate(today.getDate() + parseInt(dayOffset))
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!')
})

Daily task list

This is what I use for keeping notes at work as a simple solution that I can use inside my code editor. I have it set up with a yarn/npm script that just runs this JS file with no arguments as yarn daily. Then I can optionally supply a day offset if I want to want to plan ahead, e.g. yarn daily 3 if it's a Friday and I want to plan for Monday.

// package.json
{
  ...
  "scripts": {
    "daily": "node create-daily-task-list.js",
    ...
  },
}

The script copies the Today section from the previous notes file, puts it under a Yesterday heading, and sets up a new area for the current day.

I've gotten a lot of good use from this basic strategy of "carry along yesterday for reference, then plan out today", so I wanted to set up this gist for reference

@joshgrib
Copy link
Author

This still has a bug along certain date cutoffs, I think it's just for new years but might be months, I haven't fixed it yet because it hasn't been a big issue, but now that I posted this I'll probably update next time it comes up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment