Skip to content

Instantly share code, notes, and snippets.

@TheYarin
TheYarin / AsyncLock.ts
Last active February 3, 2022 17:11
A cute typescript async lock class I scribbled down
// You can either use lock() and get a function that releases the lock, or you could use lockAndRun() to wrap around a function! Amazing!
class AsyncLock {
private promises = new Map<string, Promise<void>>();
public async lock(key: string): Promise<VoidFunction> {
if (this.promises.has(key)) {
await this.promises.get(key);
}
let release: VoidFunction;
const lockPromise = new Promise<void>(resolve => {
@TheYarin
TheYarin / yarn-download.sh
Last active July 18, 2020 18:12
Using yarn for an offline environment setup, including a script that uses yarn to download packages and their dependencies as tgz archives. Compatible with git bash on windows.
#!/bin/bash
set -e
if [[ $# -eq 0 ]]; then
echo "Usage: $(basename $0) [packages ...]"
exit
fi
if ! command -v yarn &> /dev/null; then
@TheYarin
TheYarin / typescriptreact.json
Last active May 2, 2020 17:43
A useful VSCode snippet that generates a React class component with all the Material-UI styling boilerplate built in.
{
// Place your snippets for typescriptreact here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
@TheYarin
TheYarin / tunnel.sh
Created January 16, 2020 22:46
ssh tunneling
ssh -fN -L 0.0.0.0:8000:192.168.99.100:80 user@target
@TheYarin
TheYarin / download-npm.sh
Created January 13, 2019 18:02
Download an npm package, and compress it to a zip archive. No unnecessary files left behind.
# Usage: download-npm.sh <package-name>
#!/bin/bash
set -e
set -u
NC='\033[0m'
LIGHT_GREEN='\033[1;32m'
libName="$1"
@TheYarin
TheYarin / clear-folder-on-creation.sh
Created January 13, 2019 17:27
Clear a folder when it is "created". For example, when a flash drive is connected and mounted to /path/to/containing/folder, clear the folder inside of it named FOLDER_NAME
inotifywait -mq --format '%w%f/FOLDER_NAME/' --event=CREATE /path/to/containing/folder | while read DIR_TO_CLEAR
do
echo "$DIR_TO_CLEAR"
sleep 1
find "$DIR_TO_CLEAR" -delete
mkdir -p "$DIR_TO_CLEAR"
done
@TheYarin
TheYarin / IPRegex.cs
Created January 13, 2019 17:04
A good IPv4 regex in C#. Should work for most languages with some tweaks.
const string ZERO_TO_255 = "(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])";
static readonly string IP_REGEX_PATTERN = $@"({ZERO_TO_255}\.){{3}}{ZERO_TO_255}";
static readonly Regex FindIPRegex = new Regex(IP_REGEX_PATTERN, RegexOptions.Compiled);
static readonly Regex ValidateIPRegex = new Regex($"^{IP_REGEX_PATTERN}$", RegexOptions.Compiled);
@TheYarin
TheYarin / check-internet.sh
Last active June 10, 2019 06:16
Log internet connection failures
#!/bin/bash
# Hit Ctrl+C a lot of times to stop
# Ping google's 8.8.8.8 DNS server
while true; do
timeout 1 ping -q -c 1 8.8.8.8 > /dev/null && echo $(date +%Y/%m/%d_%H:%M:%S) GOOD && sleep 1 || echo $(date +%Y/%m/%d_%H:%M:%S) NO INTERNET;
done