bootstrapping notes

2021-07-23

 | 

~3 min read

 | 

439 words

A while back, I was working on a project called nom (My October 2020 checking). It didn’t get very far, but I learned a lot.

One of the actually useful pieces of code that I’ve salvaged from the project and use daily is my gen-draft script.

I’ve turned this into a yarn script by adding it to my package.json as "gen".

It seems like a small thing, but having a consistent set of frontmatter means that I can generate with just a few keys means that I can more quickly jot down my ideas and to the actual value - instead of spinning my wheels with boiler plate.

The full script is below.

gen-draft.js
#!/usr/bin/env node

const fs = require("fs")
const path = require("path")
const yargs = require("yargs")
const chalk = require("chalk")
const dayjs = require("dayjs")
const utc = require("dayjs/plugin/utc")
const kebabCase = require("lodash.kebabcase")

dayjs.extend(utc)

const now = dayjs().utc()
const formattedDate = now.format("YYYY-MM-DD")
const defaultTitle = `${formattedDate} - Daily`

const args = yargs(process.argv.slice(2))
  .alias("t", "title")
  .describe("t", "the title of the note")
  .help("help").argv

const { _: dflt, title } = args
const noteTitle = title ? title : dflt[0] ? dflt[0] : defaultTitle
const slug = kebabCase(noteTitle)
const filePath = path.resolve(__dirname, `../notes/${slug}.md`)

try {
  fs.accessSync(filePath)
  console.log(
    chalk.red(`Error: ${noteTitle} already exists. Open it at ${filePath}`),
  )
} catch (e) {
  const body = `---
title: "${noteTitle}"
slug: "${slug}"
date: \"${now.format()}\"
#publish:
stage: draft
private: false
#project: []
#category: []
#tags: []
archived: false
#actions: ''
#social-context: ''
#external-context: ''
#internal-context: ''
---`
  fs.writeFile(filePath, body, { encoding: "utf8" }, (error) => {
    if (error) console.error(`Failed to write file to ${filePath}`)
  })

  const ideasPath = path.resolve(__dirname, `../notes/.ideas.md`)
  const stream = fs.createWriteStream(ideasPath, {
    flags: "a",
    encoding: "utf-8",
  })

  const link = `-   [${noteTitle}](${slug}.md)\n`
  stream.write(link, (error) => {
    if (error) console.error(`Stream error: ${error}`)
  })
  console.log(
    chalk.green(`Success: ${noteTitle} created. Open it at ${filePath}`),
  )
}

Fun fact: as simple as this script is, it turns out Kent C Dodd’s does something similar. He described a bit of it in his Epic React podcast:

Another thing about the automation stuff is I actually automate more than just making my blog posts and short URLs and stuff. It is really easy for me to create short URLs, but also even creating the blog post in the first place, I have a script that I run to automatically create the files that I need. It lets me choose an image for the blog post, and it will compress that image automatically and all sorts of really fun and cool things.

Which is to say, I have more work to do! Time to start building that personal wiki!


Related Posts
  • Building A Personal Wiki


  • Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!