|
/** |
|
* 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!') |
|
}) |
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