Skip to content

Instantly share code, notes, and snippets.

View khalidx's full-sized avatar
💭
Engineering for the ☁️

Khalid Zoabi khalidx

💭
Engineering for the ☁️
View GitHub Profile
@khalidx
khalidx / index.ts
Last active July 29, 2020 09:25
Using lowdb with TypeScript; instantly instantiates a typed db with defaults and compile-time-checked queries
import low from 'lowdb'
import FileSync from 'lowdb/adapters/FileSync'
export const database = <T> (defaults: T) => {
const adapter = new FileSync<T>('db.json')
const db = low(adapter)
db.defaults(defaults).write()
return db
}
@khalidx
khalidx / status.sh
Created April 11, 2020 01:46
Check the exit status code of a shell command
whoami # run some command
echo $? # show the exit status code result of the command
@khalidx
khalidx / event.ts
Created May 10, 2020 13:20
A typed event emitter for TypeScript
// This lets you emit and listen for events in a type-safe manner
// Awesome!
import { EventEmitter } from 'events'
type Type<Name> = { type: Name }
type Value<Type> = { value: Type }
export type TypedValue<T, V> = Type<T> & Value<V>
@khalidx
khalidx / vpc.tf
Created June 10, 2020 02:09
Simple, quick, and complete VPC setup for AWS
locals {
region = "us-east-1"
namespace = "infrastructure"
}
provider "aws" {
region = local.region
}
module "vpc" {
@khalidx
khalidx / typescript-config.md
Created June 18, 2020 03:08
Add a TypeScript config to your project easily

typescript config

Make sure to install the following dependencies for the best experience.

npm install --save tslib
npm install --save-dev typescript
npm install --save-dev ts-node
@khalidx
khalidx / banner.md
Created June 25, 2020 16:34
Shows a banner on the CLI using a cool font

banner

Shows a banner on the CLI using a cool font.

For NodeJs & TypeScript. Depends on the amazing figlet and chalk libraries.

import figlet from 'figlet'
import chalk from 'chalk'
@khalidx
khalidx / api.ts
Last active May 19, 2021 11:52
A quick api boilerplate file for express with hot-reloading and typescript support
#!/usr/bin/env node
/* The API routes and middlewares */
const server = () => import('express').then(({ default: express }) => {
const app = express()
app.get('/hello', (req, res) => {
res.json({ message: 'Hey there!' })
})
return app
})
@khalidx
khalidx / script-directory-name.sh
Created October 18, 2020 04:06
For cd'ing into the same directory as the script file, instead of running at cwd by default.
# https://stackoverflow.com/questions/3349105/how-to-set-current-working-directory-to-the-directory-of-the-script-in-bash
cd "${0%/*}"
@khalidx
khalidx / unzip-gz-file.md
Created November 12, 2020 04:57
For unzipping gz files (like for CloudFront logs stored in S3).

unzipping a .gz file

On Linux and MacOS, a .gz file can be decompressed using the gzip command.

For example:

gzip -d log.gz
@khalidx
khalidx / message.js
Last active August 20, 2023 22:27
Quickly send an SMS message using the Twilio API.
// make sure you `npm install twilio` before you run
const config = {
// get your credentials from the twilio console: www.twilio.com/console
// can also be configured as environment variables instead
accountSid: '<twilio account sid>',
authToken: '<twilio auth token>',
// on a twilio trial account, you can only send from and to confirmed or purchased numbers
// upgrade your account to send to any number
from: '+10000000000',