Skip to content

Instantly share code, notes, and snippets.

Avatar
👨‍💻
Coding

Doug Ayers douglascayers

👨‍💻
Coding
View GitHub Profile
@douglascayers
douglascayers / graph.ts
Created Aug 31, 2021
Graph class that implements depth-first search algorithm.
View graph.ts
/**
* References:
* https://adelachao.medium.com/graph-topological-sort-javascript-implementation-1cc04e10f181
* http://peterknolle.com/page/51/
* https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
*/
class Graph {
private vertices: Record<string, Array<string>>;
@douglascayers
douglascayers / async-poller.ts
Created Dec 23, 2019
Typescript async polling function. Simple proof-of-concept before I adopted the p-retry npm module.
View async-poller.ts
/**
* The function you pass to `asyncPoll` should return a promise
* that resolves with object that satisfies this interface.
*
* The `done` property indicates to the async poller whether to
* continue polling or not.
*
* When done is `true` that means you've got what you need
* and the poller will resolve with `data`.
*
@douglascayers
douglascayers / JWT.cls
Last active Apr 8, 2021
Sign a JWT token with only a private key
View JWT.cls
/**
* Inspired by the JWT repo by Salesforce Identity
* https://github.com/salesforceidentity/jwt/
*
* Inspired by the JWT repo by Auth0
* https://github.com/auth0/java-jwt
*
* Learn more about JWT at https://jwt.io
*/
public inherited sharing class JWT {
@douglascayers
douglascayers / create-package-version.sh
Last active Feb 22, 2021
Bash script to create a new managed package version from source control and no maintenance of sfdx-project.json
View create-package-version.sh
#!/usr/bin/env bash
# =========================================================================== #
# USAGE
# -----
# create-package-version.sh [branchName]
#
# The $1 argument (optional) indicates the git branch to use.
# If not set then script defaults to 'develop' branch.
# =========================================================================== #
@douglascayers
douglascayers / delete-local-branches.sh
Last active Mar 10, 2020
Delete local and remote branches
View delete-local-branches.sh
# You'll need to keep one branch.
# Specify that branch's name here.
BRANCH_TO_KEEP=master
# Switch to the branch you're keeping
git checkout ${BRANCH_TO_KEEP}
# Delete all other local branches
git branch -D $(git branch | grep -v -e ${BRANCH_TO_KEEP})
@douglascayers
douglascayers / delete-org.sh
Created Aug 30, 2019
Deletes a scratch org when `force:org:delete` fails for any reason
View delete-org.sh
org_list_json=$(sfdx force:org:list --json)
scratch_org_username=$1
devhub_username=$2
# if no scratch org username given, use default
if [ -z "$1" ]; then
scratch_org_username=$(echo $org_list_json | jq -r '.result.scratchOrgs[] | select(.isDefaultUsername) | .signupUsername')
else
# else check if argument is an org alias and get signup username from that
scratch_org_username=$(echo $org_list_json | jq -r --arg ALIAS "$1" '.result.scratchOrgs[] | select(.alias==$ALIAS) | .signupUsername')
@douglascayers
douglascayers / rename.sh
Created Aug 18, 2019
Rename files in all subdirectories.
View rename.sh
# Finds all files in the current directory and subdirectories
# whose filenames match the expression "*.js" (ends with .js)
# then renames the files, changing their extension to ".ts".
# https://stackoverflow.com/questions/7450818/rename-all-files-in-directory-from-filename-h-to-filename-half
for file in $(find . -type f -name "*.js")
do
mv "$file" "${file/.js/.ts}"
done
@douglascayers
douglascayers / load_properties.sh
Created Aug 17, 2019
Bash: Read a properties file and set variables
View load_properties.sh
# Read a property file line by line and
# set key=value pairs as bash variables
# https://stackoverflow.com/a/28831442/470818
FILEPATH=$1
while IFS='=' read -r k v; do
eval ${k}=${v}
done < $FILEPATH
@douglascayers
douglascayers / github-copy-labels.sh
Last active Mar 26, 2022
Export and import GitHub labels between projects by running bash script with jq and curl. Uses GitHub REST API. Requires personal access token.
View github-copy-labels.sh
# This script uses the GitHub Labels REST API
# https://developer.github.com/v3/issues/labels/
# Provide a personal access token that can
# access the source and target repositories.
# This is how you authorize with the GitHub API.
# https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line
GH_TOKEN="YOUR_TOKEN"
# If you use GitHub Enterprise, change this to "https://<your_domain>/api/v3"
@douglascayers
douglascayers / github-export-labels.js
Last active May 16, 2022
Export and import GitHub labels between projects by running JavaScript in the browser console to automate clicks.
View github-export-labels.js
/**
* Inspired by @MoOx original script: https://gist.github.com/MoOx/93c2853fee760f42d97f
* Adds file download per @micalevisk https://gist.github.com/MoOx/93c2853fee760f42d97f#gistcomment-2660220
*
* Changes include:
* - Get the description from the `title` attribute instead of `aria-label` (doesn't exist anymore)
* - Use style.backgroundColor and parse the rgb(...) to hex (rather than regex parsing of 'style' string)
* - Downloads labels to a JSON file named after the webpage to know which GitHub repo they came from.
*
* Last tested 2019-July-27: