Command line selector to publish file from drafts

I always found it annoying to copy files from the _drafts to _post file.

  • I would start writing a post on a given day
  • Then I wouldn’t finish writing it on that day
  • When I finally wanted to publish instead of just hitting git push, I’d have to mess with filenames and dates
  • Today my pain ended:
const { default: select, Separator } = require("@inquirer/select");
const fs = require("fs");

(async () => {
  // load all files in _drafts folder
  const files = await fs.promises.readdir("_drafts");
  const answer = await select({
    message: "Select article to publish",
    choices: Array.from(files).map((file) => ({
      name: file,
      value: file,
      description: `publish ${file} to _posts`,
    })),
  });
  // move file from _drafts to _posts
  const date = new Date().toISOString().split("T")[0];
  await fs.promises.rename(`_drafts/${answer}`, `_posts/${date}-${answer}`);
  console.log(`moved _drafts/${answer} to _posts/${date}-${answer}`);
})();

I think I am not the first to write this but I hope it’s useful to someone.

It’s my first time creating a UI for the command line by the way, super fun, I’m going to use that a lot.