Skip to content

Instantly share code, notes, and snippets.

@dansalias
dansalias / promise-queue.ts
Last active September 12, 2023 19:30
Waiting for an empty queue
const queue: Set<Promise<unknown>> = new Set()
const queueEmpty = async (): Promise<void> => {
await Promise.all(queue)
if (queue.size) {
await queueEmpty()
}
}
@dansalias
dansalias / script.sh
Created March 16, 2023 19:20
Bash script with flags
#!/bin/bash
# usage:
# ./script.sh -a <value for a> -b <value for b>
while getopts a:b: opt
do
case $opt in
a) VAR_A=$opt;;
b) VAR_B=$opt;;
esac
@dansalias
dansalias / compile.ts
Created December 11, 2022 20:35
Rewrite TypeScript imports at compile time
import ts from 'typescript'
const unpackImportPath = (node: ts.ImportDeclaration): string => {
const moduleSpecifier = node.moduleSpecifier as ts.StringLiteral
return moduleSpecifier.text
}
const transformImport = (
factory: ts.NodeFactory,
node: ts.ImportDeclaration,
@dansalias
dansalias / reporter.ts
Created April 10, 2022 12:11
Deeply observe object changes
const reporter = (path: string[] = []) => ({
get(target, property) {
if (typeof target[property] === 'object') {
return new Proxy(target[property], reporter(path.concat(property)))
} else {
return Reflect.get(target, property)
}
},
set(target, property, value) {
console.log('setting ', path.concat(property).join('.'))
@dansalias
dansalias / CMCIMPORT.gs
Last active October 8, 2021 13:15
Import coinmarketcap.com crypto prices into Google Sheets
/**
* - Create a coinmarketcap.com account and paste your API key below.
* - In Google Sheets go to Tools -> Script editor and paste this code in a new CMCIMPORT.gs script file
* - Use the function in your sheet, e.g. =CMCIMPORT("ADA")
*/
function CMCIMPORT(symbol) {
const cache = CacheService.getDocumentCache()
if (cache.get('prices') == null) {
const response = UrlFetchApp.fetch('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=500&convert=USD',{
headers: {
@dansalias
dansalias / invalidate-cf.sh
Last active October 6, 2023 11:32
Invalidate an AWS CloudFront Distribution by domain name
#!/bin/bash
# get the CloudFront Id
CF_DISTRIBUTION_ID=$(aws cloudfront list-distributions \
--query "DistributionList.Items[?contains(Aliases.Items[0], "example.com")] | [0].Id" \
--output text
)
# invalidate all paths
aws cloudfront create-invalidation --distribution-id ${CF_DISTRIBUTION_ID} --paths "/*"
@dansalias
dansalias / package.json
Last active July 3, 2022 15:00
Squash npm version bumps into previous commit
{
"scripts": {
"postversion": "git reset --soft HEAD~1 && git commit --amend -C HEAD",
...
}
...
}
}
@dansalias
dansalias / htmlElementWithAttributes.js
Created January 14, 2020 15:15
Create DOM elements with attributes.
const myScript = Object.assign(document.createElement('script'), {
src: '/some-script.js',
type: 'text/javascript',
async: true,
});
document.head.appendChild(myScript);
@dansalias
dansalias / .zshrc
Created December 9, 2019 19:14
Switch node versions without nvm
# node-switch alias
alias ns=$HOME/scripts/node-switch.sh
# switch node version based on .nvmrc
default_node_version=12.13.1
chpwd() {
if [[ -f .nvmrc && -r .nvmrc ]]; then
ns switch "$(< .nvmrc)"
elif [[ $(node --version) != v$default_node_version ]]; then
ns switch $default_node_version
@dansalias
dansalias / node-switch.sh
Created December 9, 2019 18:25
Switch node versions without nvm
#!/bin/bash
system=linux-x64
directory=$HOME/.node-versions
# create directory
prepare() {
mkdir -p $directory
}
# install specified version