Skip to content

Instantly share code, notes, and snippets.

@mochja
Last active July 27, 2023 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mochja/8ce404348472fc33a5f3534fc1ca568d to your computer and use it in GitHub Desktop.
Save mochja/8ce404348472fc33a5f3534fc1ca568d to your computer and use it in GitHub Desktop.
Sync your random scripts to gist

sync-gist

I write scripts everyday, and it's hard to keep them sorted. I wrote this simple tool that allows me to sync them to gist whenever I want. This is nice because I can write script in random places and make sure I never loose it.

For now this only works one-way - me pushing to gist.

Usage

sync-gist -f my-script

This script detects a line in your file that stars with # or // and contains gist=[id|url] with optional extra_files=file1,file2 and updates the file in the remote gist matching the file name of the file and optionally syncing other files as well, this is usefull when you script uses external tool, that for example requires an config file.

Requirements:

  • zx
  • github cli

Examples

# my-script
# gist=8ce404348472fc33a5f3534fc1ca568d extra_files=poor.txt
cat poor.txt
# poor.txt
What can I do...
#!/usr/bin/env zx
// gist=https://gist.github.com/8ce404348472fc33a5f3534fc1ca568d
const file = argv.f || argv.file
if (!file) {
echo(`usage sync-gist -f <file>`)
process.exit(1)
}
let content = await fs.readFile(file)
if (!content.includes('gist=')) {
echo('no gist= url found')
process.exit(1)
}
content = content.toString()
const matches = content.match(/^[#\/]{1,2}\s+gist=(\S+)\s*(extra_files=(\S+))?/m)
const url = matches?.[1]?.trim()
const extra_files = matches?.[3]?.trim()?.split(',') ?? []
if (!url) {
echo('no gist= url found')
process.exit(1)
}
const edit_file = file => $`gh gist edit ${url} ${file} -f ${path.basename(file)}`
await edit_file(file)
for (const extra_file of extra_files) {
await edit_file(extra_file)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment