// Menu: Optical Character Recognition// Description: Extract text from images// Author: Kent C. Dodds// Twitter: @kentcdoddsimport '@johnlindquist/kit'import Tesseract from 'tesseract.js'const clipboardImage = await clipboard.readImage()if (clipboardImage.byteLength) {const {data} = await Tesseract.recognize(clipboardImage, 'eng', {logger: m => console.log(m),})clipboard.writeText(data.text)} else {let selectedFiles = await getSelectedFile()let filePaths: Array<string>if (selectedFiles) {filePaths = selectedFiles.split('\n')} else {let droppedFiles = await drop({placeholder: 'Drop images to compress'})filePaths = droppedFiles.map(file => file.path)}for (const filePath of filePaths) {const {data} = await Tesseract.recognize(filePath, 'eng', {logger: m => console.log(m),})clipboard.writeText(data.text)}}notify({title: 'OCR finished',message: `Copied text to your clipboard`,})
// Menu: Cloudinary upload
// Description: Upload an image to cloudinary
// Shortcut: command option control c
// Author: Kent C. Dodds
// Twitter: @kentcdodds
import path from 'path'
const cloudinaryCloudName = await env('CLOUDINARY_CLOUD_NAME')
const cloudinaryKey = await env('CLOUDINARY_API_KEY')
const cloudinarySecret = await env('CLOUDINARY_API_SECRET')
const cloudiaryConsoleId = await env('CLOUDINARY_CONSOLE_ID')
await npm('cloudinary')
import cloudinary from 'cloudinary'
const cacheDb = await db('cloudinary-cache', {lastChoice: '', folders: {}})
await cacheDb.read()
cloudinary.config({
  cloud_name: cloudinaryCloudName,
  api_key: cloudinaryKey,
  api_secret: cloudinarySecret,
  secure: true,
})
const actions = {
  CREATE_NEW: 'creating new folder',
  REFRESH_CACHE: 'refreshing cache',
  OPEN_DIR: 'opening directory',
}
let chosenDirectory = await cacheDb.data.lastChoice
let lastSelection
while (true) {
  // if the last action was to create a new directory then we know the chosen
  // directory is new and has no folders otherwise we have to wait a few seconds
  // for the API to be prepared for us to make a request for the contents.
  const directories =
    lastSelection === actions.CREATE_NEW
      ? []
      : await getFolders(chosenDirectory)
  lastSelection = await arg(
    `Select directory in ${chosenDirectory || '/'}`,
    [
      {name: '.', value: '.', description: '✅ Choose this directory'},
      !chosenDirectory
        ? null
        : {name: '..', value: '..', description: '⤴️ Go up a directory'},
      ...directories.map(folder => ({
        name: folder.name,
        value: folder.path,
        description: '⤵️ Select directory',
      })),
      {
        name: 'Open directory',
        value: actions.OPEN_DIR,
        description: '🌐 Open this directory in the browser',
      },
      {
        name: 'Refresh cache',
        value: actions.REFRESH_CACHE,
        description: '🔄 Refresh the cache for this directory',
      },
      {
        name: 'Create new directory',
        value: actions.CREATE_NEW,
        description: '➕ Create a new directory here',
      },
    ].filter(Boolean),
  )
  if (lastSelection === '..') {
    chosenDirectory = chosenDirectory.split('/').slice(0, -1).join('/')
  } else if (lastSelection === '.') {
    break
  } else if (lastSelection === actions.CREATE_NEW) {
    const newFolderName = await arg(`What's the new folder name?`)
    const newDirectory = `${chosenDirectory}/${newFolderName}`
    await cloudinary.v2.api.create_folder(newDirectory)
    delete cacheDb.data.folders[chosenDirectory]
    chosenDirectory = newDirectory
  } else if (lastSelection === actions.REFRESH_CACHE) {
    delete cacheDb.data.folders[chosenDirectory]
  } else if (lastSelection === actions.OPEN_DIR) {
    await openFolder(chosenDirectory)
  } else {
    chosenDirectory = lastSelection
  }
}
cacheDb.data.lastChoice = chosenDirectory
await cacheDb.write()
const images = await drop('Drop the image(s) you want to upload')
let renameSome = true
if (images.length > 1) {
  const renameChoice = await arg('Do you want to rename any of these?', [
    'yes',
    'no',
  ])
  renameSome = renameChoice === 'yes'
}
for (const image of images) {
  const defaultName = path.parse(image.path).name
  const name = renameSome
    ? (await arg({
        placeholder: `Name of this image?`,
        hint: `Default is: "${defaultName}"`,
      })) || defaultName
    : defaultName
  setPlaceholder(`Uploading ${name}`)
  const uploadedImage = await cloudinary.v2.uploader.upload(image.path, {
    public_id: name,
    overwrite: false,
    folder: chosenDirectory,
  })
  // If you have multiple files then this isn't really useful unless you have
  // clipbloard history (which I recommend you get!)
  await copy(uploadedImage.secure_url)
}
await openFolder(chosenDirectory)
function openFolder(folder) {
  const encodedFolder = encodeURIComponent(folder)
  console.log('opening')
  return exec(
    `open "https://cloudinary.com/console/${cloudiaryConsoleId}/media_library/folders/${encodedFolder}"`,
  )
}
async function getFolders(directory) {
  const cachedDirectories = cacheDb.data.folders[directory]
  if (cachedDirectories) {
    return cachedDirectories
  }
  try {
    const {folders: directories} = !directory
      ? await cloudinary.v2.api.root_folders()
      : await cloudinary.v2.api.sub_folders(directory)
    cacheDb.data.folders[directory] = directories
    await cacheDb.write()
    return directories
  } catch (error) {
    console.error('error with the directory')
    return []
  }
}
// Menu: ConvertKit > Lookup
// Description: Query convertkit
// Author: Kent C. Dodds
// Twitter: @kentcdodds
const CONVERT_KIT_API_SECRET = await env('CONVERT_KIT_API_SECRET')
const CONVERT_KIT_API_KEY = await env('CONVERT_KIT_API_KEY')
const query = await arg('query')
let url
if (query.includes('@')) {
  const sub = await getConvertKitSubscriber(query)
  if (sub?.id) {
    url = `https://app.convertkit.com/subscribers/${sub.id}`
  }
}
if (!url) {
  url = `https://app.convertkit.com/subscribers?utf8=%E2%9C%93&q=${query}&status=all`
}
exec(`open "${url}"`)
async function getConvertKitSubscriber(email) {
  const url = new URL('https://api.convertkit.com/v3/subscribers')
  url.searchParams.set('api_secret', CONVERT_KIT_API_SECRET)
  url.searchParams.set('email_address', email)
  const resp = await fetch(url.toString())
  const json = await resp.json()
  const {subscribers: [subscriber] = []} = json
  return subscriber
}
// Menu: Daily Story
// Description: Write a quick story
// Author: Kent C. Dodds
// Shortcut: command option control o
// Twitter: @kentcdodds
const dateFns = await npm('date-fns')
const filenamify = await npm('filenamify')
const prettier = await npm('prettier')
const storyDir = await env(
  'DAILY_STORY_DIRECTORY',
  `Where do you want daily stories to be saved?`,
)
const story = await textarea({placeholder: 'Write your story here'})
const today = dateFns.format(new Date(), 'yyyy-MM-dd')
const date = await arg({
  input: today,
  hint: 'When did this happen?',
})
const title = await arg({
  placeholder: 'What do you want to call this story?',
  hint: 'Title',
})
const md = `---
title: ${title}
date: ${date}
written: ${today}
---
${story}
`
// prettify the markdown
const prettyMd = await prettier.format(md, {
  parser: 'markdown',
  arrowParens: 'avoid',
  bracketSpacing: false,
  embeddedLanguageFormatting: 'auto',
  htmlWhitespaceSensitivity: 'css',
  insertPragma: false,
  jsxBracketSameLine: false,
  jsxSingleQuote: false,
  printWidth: 80,
  proseWrap: 'always',
  quoteProps: 'as-needed',
  requirePragma: false,
  semi: false,
  singleQuote: true,
  tabWidth: 2,
  trailingComma: 'all',
  useTabs: false,
  vueIndentScriptAndStyle: false,
})
const filename = filenamify(
  `${date}-${title.toLowerCase().replace(/ /g, '-')}.md`,
  {replacement: '-'},
)
await writeFile(path.join(storyDir, filename), prettyMd)
await cli("install", "~/.kit")
// Menu: New Post
// Description: Create a new blog post
// Author: Kent C. Dodds
// Shortcut: command option control p
// Twitter: @kentcdodds
const dateFns = await npm('date-fns')
const prettier = await npm('prettier')
const YAML = await npm('yaml')
const slugify = await npm('@sindresorhus/slugify')
const {format: formatDate} = await npm('date-fns')
const makeMetascraper = await npm('metascraper')
const {$filter, toRule} = await npm('@metascraper/helpers')
const unsplashTitleToAlt = toRule(str => str.replace(/ photo – .*$/, ''))
const unsplashOGTitleToAuthor = toRule(str =>
  str.replace(/Photo by (.*?) on Unsplash/, '$1'),
)
const unsplashImageToPhotoId = toRule(str =>
  new URL(str).pathname.replace('/', ''),
)
const metascraper = makeMetascraper([
  {
    unsplashPhotoId: [
      unsplashImageToPhotoId($ =>
        $('meta[property="og:image"]').attr('content'),
      ),
    ],
  },
  {
    author: [
      unsplashOGTitleToAuthor($ =>
        $('meta[property="og:title"]').attr('content'),
      ),
    ],
  },
  {alt: [unsplashTitleToAlt($ => $('title').text())]},
])
async function getMetadata(url) {
  const html = await fetch(url).then(res => res.text())
  return metascraper({html, url})
}
const blogDir = await env(
  'KCD_BLOG_CONTENT_DIR',
  `What's the path to the blog content directory on this machine?`,
)
const title = await arg({
  placeholder: `What's the title of this post?`,
  hint: 'Title',
  ignoreBlur: true,
})
const description = await arg({
  placeholder: `What's the description of this post?`,
  hint: 'Description',
  input: 'TODO: add a description',
  ignoreBlur: true,
})
const categories = (
  await arg({
    placeholder: `What are the categories of this post?`,
    hint: 'Categories (comma separated)',
    ignoreBlur: true,
  })
)
  .split(',')
  .map(c => c.trim())
  .filter(Boolean)
const keywords = (
  await arg({
    placeholder: `What are the keywords of this post?`,
    hint: 'Keywords (comma separated)',
    ignoreBlur: true,
  })
)
  .split(',')
  .map(c => c.trim())
  .filter(Boolean)
const filename = slugify(title, {decamelize: false})
await exec(`open https://unsplash.com/s/photos/${filename}`)
const unsplashPhotoInput = await arg({
  placeholder: `What's the unsplash photo?`,
  hint: 'Unsplash Photo',
  ignoreBlur: true,
})
const unsplashPhotoUrl = unsplashPhotoInput.startsWith('http')
  ? unsplashPhotoInput
  : `https://unsplash.com/photos/${unsplashPhotoInput}`
const metadata = await getMetadata(unsplashPhotoUrl)
const frontmatter = YAML.stringify({
  title,
  date: dateFns.format(new Date(), 'yyyy-MM-dd'),
  description,
  categories,
  meta: {keywords},
  bannerCloudinaryId: `unsplash/${metadata.unsplashPhotoId}`,
  bannerAlt: metadata.alt,
  bannerCredit: `Photo by [${metadata.author}](${unsplashPhotoUrl})`,
})
const md = `---
${frontmatter}
---
Be excellent to each other.
`
// prettify the markdown
const prettyMd = await prettier.format(md, {
  parser: 'markdown',
  arrowParens: 'avoid',
  bracketSpacing: false,
  embeddedLanguageFormatting: 'auto',
  htmlWhitespaceSensitivity: 'css',
  insertPragma: false,
  jsxBracketSameLine: false,
  jsxSingleQuote: false,
  printWidth: 80,
  proseWrap: 'always',
  quoteProps: 'as-needed',
  requirePragma: false,
  semi: false,
  singleQuote: true,
  tabWidth: 2,
  trailingComma: 'all',
  useTabs: false,
  vueIndentScriptAndStyle: false,
})
const newFile = path.join(blogDir, `${filename}.mdx`)
await writeFile(newFile, prettyMd)
await edit(newFile)
// Menu: Open EpicReact Repos
// Description: Open all the EpicReact Repos in VSCode
const repos = [
  'advanced-react-hooks',
  'advanced-react-patterns',
  'bookshelf',
  'react-fundamentals',
  'react-hooks',
  'react-performance',
  'react-suspense',
  'testing-react-apps',
]
for (const repo of repos) {
  edit(`~/code/epic-react/${repo}`)
}
// Menu: Open Project
// Description: Opens a project in code
// Shortcut: cmd shift .
import path from 'path'
import fs from 'fs'
import os from 'os'
const isDirectory = async filePath => {
  try {
    const stat = await fs.promises.stat(filePath)
    return stat.isDirectory()
  } catch (e) {
    return false
  }
}
const isFile = async filePath => {
  try {
    const stat = await fs.promises.stat(filePath)
    return stat.isFile()
  } catch (e) {
    return false
  }
}
async function getProjects(parentDir) {
  const codeDir = (await ls(parentDir)).stdout.split('\n').filter(Boolean)
  const choices = []
  for (const dir of codeDir) {
    let fullPath = dir
    if (!path.isAbsolute(dir)) {
      fullPath = path.join(parentDir, dir)
    }
    if (fullPath.includes('/node_modules/')) continue
    if (fullPath.includes('/build/')) continue
    if (fullPath.includes('/dist/')) continue
    if (fullPath.includes('/coverage/')) continue
    const pkgjson = path.join(fullPath, 'package.json')
    if (await isFile(pkgjson)) {
      choices.push({
        name: dir,
        value: fullPath,
        description: fullPath,
      })
    } else if (await isDirectory(fullPath)) {
      choices.push(...(await getProjects(fullPath)))
    }
  }
  return choices
}
const choice = await arg('Which project?', async () => {
  const choices = [
    ...(await getProjects(path.join(os.homedir(), 'code'))),
    ...(await getProjects(path.join(os.homedir(), 'Desktop'))),
  ]
  return choices
})
await edit(choice)
// Menu: Shorten
// Description: Shorten a given URL with a given short name via netlify-shortener
// Shortcut: command option control s
// Author: Kent C. Dodds
// Twitter: @kentcdodds
const dir = await env(
  'SHORTEN_REPO_DIRECTORY',
  'Where is your netlify-shortener repo directory?',
)
const longURL = await arg(`What's the full URL?`)
// TODO: figure out how to make this optional
const shortName = await arg(`What's the short name?`)
const netlifyShortenerPath = path.join(
  dir,
  'node_modules/netlify-shortener/dist/index.js',
)
const {baseUrl} = JSON.parse(await readFile(path.join(dir, 'package.json')))
setPlaceholder(`Creating redirect: ${baseUrl}/${shortName} -> ${longURL}`)
const result = exec(
  `node "${netlifyShortenerPath}" "${longURL}" "${shortName}"`,
)
const {stderr, stdout} = result
if (result.code === 0) {
  const lastLine = stdout.split('\n').filter(Boolean).slice(-1)[0]
  notify({
    title: '✅ Short URL created',
    message: lastLine,
  })
} else {
  const getErr = str => str.match(/Error: (.+)\n/)?.[1]
  const error = getErr(stderr) ?? getErr(stdout) ?? 'Unknown error'
  console.error({stderr, stdout})
  notify({
    title: '❌ Short URL not created',
    message: error,
  })
}
// Menu: Twimage Download
// Description: Download twitter images and set their exif info based on the tweet metadata
// Shortcut: command option control t
// Author: Kent C. Dodds
// Twitter: @kentcdodds
import fs from 'fs'
import {fileURLToPath, URL} from 'url'
const exiftool = await npm('node-exiftool')
const exiftoolBin = await npm('dist-exiftool')
const fsExtra = await npm('fs-extra')
const baseOut = home('Pictures/twimages')
const token = await env('TWITTER_BEARER_TOKEN')
const twitterUrl = await arg('Twitter URL')
console.log(`Starting with ${twitterUrl}`)
const tweetId = new URL(twitterUrl).pathname.split('/').slice(-1)[0]
const response = await get(
  `https://api.twitter.com/1.1/statuses/show/${tweetId}.json?include_entities=true`,
  {
    headers: {
      authorization: `Bearer ${token}`,
    },
  },
)
const tweet = response.data
console.log({tweet})
const {
  geo,
  id,
  text,
  created_at,
  extended_entities: {media: medias} = {
    media: [
      {
        type: 'photo',
        media_url_https: await arg({
          ignoreBlur: true,
          input: `Can't find media. What's the URL for the media?`,
          hint: `Media URL`,
        }),
      },
    ],
  },
} = tweet
const [latitude, longitude] = geo?.coordinates || []
const ep = new exiftool.ExiftoolProcess(exiftoolBin)
await ep.open()
for (const media of medias) {
  let url
  if (media.type === 'photo') {
    url = media.media_url_https
  } else if (media.type === 'video') {
    let best = {bitrate: 0}
    for (const variant of media.video_info.variants) {
      if (variant.bitrate > best.bitrate) best = variant
    }
    url = best.url
  } else {
    throw new Error(`Unknown media type for ${twitterUrl}: ${media.type}`)
  }
  if (!url) throw new Error(`Huh... no media url found for ${twitterUrl}`)
  const formattedDate = formatDate(created_at)
  const colonDate = formattedDate.replace(/-/g, ':')
  const formattedTimestamp = formatTimestamp(created_at)
  const filename = new URL(url).pathname.split('/').slice(-1)[0]
  const filepath = path.join(
    baseOut,
    formattedDate.split('-').slice(0, 2).join('-'),
    /\..+$/.test(filename) ? filename : `${filename}.jpg`,
  )
  await download(url, filepath)
  await ep.writeMetadata(
    filepath,
    {
      ImageDescription: `${text} – ${twitterUrl}`,
      Keywords: 'photos from tweets',
      DateTimeOriginal: formattedTimestamp,
      FileModifyDate: formattedTimestamp,
      ModifyDate: formattedTimestamp,
      CreateDate: formattedTimestamp,
      ...(geo
        ? {
            GPSLatitudeRef: latitude > 0 ? 'North' : 'South',
            GPSLongitudeRef: longitude > 0 ? 'East' : 'West',
            GPSLatitude: latitude,
            GPSLongitude: longitude,
            GPSDateStamp: colonDate,
            GPSDateTime: formattedTimestamp,
          }
        : null),
    },
    ['overwrite_original'],
  )
}
await ep.close()
notify(`All done with ${twitterUrl}`)
function formatDate(t) {
  const d = new Date(t)
  return `${d.getFullYear()}-${padZero(d.getMonth() + 1)}-${padZero(
    d.getDate(),
  )}`
}
function formatTimestamp(t) {
  const d = new Date(t)
  const formattedDate = formatDate(t)
  return `${formatDate(t)} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`
}
function padZero(n) {
  return String(n).padStart(2, '0')
}
async function getGeoCoords(placeId) {
  const response = await get(
    `https://api.twitter.com/1.1/geo/id/${placeId}.json`,
    {
      headers: {
        authorization: `Bearer ${token}`,
      },
    },
  )
  const [longitude, latitude] = response.data.centroid
  return {latitude, longitude}
}
async function download(url, out) {
  console.log(`downloading ${url} to ${out}`)
  await fsExtra.ensureDir(path.dirname(out))
  const writer = fs.createWriteStream(out)
  const response = await get(url, {responseType: 'stream'})
  response.data.pipe(writer)
  return new Promise((resolve, reject) => {
    writer.on('finish', () => resolve(out))
    writer.on('error', reject)
  })
}
// Menu: Update EpicReact deps
// Description: Update all the dependencies in the epicreact workshop repos
const repos = [
  'advanced-react-hooks',
  'advanced-react-patterns',
  'bookshelf',
  'react-fundamentals',
  'react-hooks',
  'react-performance',
  'react-suspense',
  'testing-react-apps',
]
const script = `git add -A && git stash && git checkout main && git pull && ./scripts/update-deps && git commit -am "update all deps" --no-verify && git push && git status`
for (const repo of repos) {
  const scriptString = JSON.stringify(
    `cd ~/code/epic-react/${repo} && ${script}`,
  )
  exec(
    `osascript -e 'tell application "Terminal" to activate' -e 'tell application "Terminal" to do script ${scriptString}'`,
  )
}