Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
ValentaTomas / getDatabaseURL.js
Created March 21, 2021 15:33
Retrieves and prints the database URL from the Google Secret Manager
#!/usr/bin/env node
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
const secretManager = new SecretManagerServiceClient();
async function getSecret(name) {
const [secretVersion] = await secretManager.accessSecretVersion({
name: `${name}/versions/latest`,
});
@ValentaTomas
ValentaTomas / upload-gcp.sh
Created May 9, 2021 13:15
Upload files in a directory to GCP bucket without caching
#!/bin/sh
DIR=$1
export BUCKET=$2
cd $DIR
ls -l ./ | tr -s ' ' | cut -d' ' -f9 |
tr ' ' '\n' |
xargs -n 1 -I{} gsutil -h "Cache-Control:no-cache, max-age=0" cp {} gs://$BUCKET
@ValentaTomas
ValentaTomas / docker-run.sh
Created June 9, 2021 17:49
Run Docker image with a mounted GCP credentials
#!/bin/sh
docker run -p 8000:80 \
-e GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds/in/container.json \
-v $GOOGLE_APPLICATION_CREDENTIALS:/path/to/creds/in/container.json \
gcr.io/$PROJECT_ID/$IMAGE_ID
@ValentaTomas
ValentaTomas / gcp-compute.yml
Last active June 9, 2021 18:06
GitHub Action to build and deploy Docker container to Google Compute Engine on master branch push
name: Build and Deploy to Google Compute Engine
on:
push:
branches:
- master
env:
PROJECT_ID: ${{ secrets.GCE_PROJECT }}
GCE_IMAGE: ${{ secrets.GCE_IMAGE }}
@ValentaTomas
ValentaTomas / node16.Dockerfile
Last active June 15, 2021 14:57
Node.js 16 two stage build setup
# This stage installs modules
FROM node:16.2.0 as modules
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --production
# This stage builds TypeScript
@ValentaTomas
ValentaTomas / getFileExtension.ts
Created August 24, 2021 19:19
Get the file extension
function getFileExtension(file: string) {
return file.slice((Math.max(0, file.lastIndexOf('.')) || Infinity) + 1)
}
@ValentaTomas
ValentaTomas / getRandomHexColor.ts
Last active August 26, 2021 14:07
Get random bright color in HEX format
function randomDiscreteNumber(max: number) {
return Math.floor(Math.random() * max) + 1
}
export function getRandomColor() {
// Use HSL model to create pastel colors.
// It is easier to do that by randomizing hue while keeping saturation and lightness in reasonable boundaries.
const hue = 6 * randomDiscreteNumber(60)
const saturation = 80 + 10 * Math.random()
const lightness = (60 + 10 * Math.random()) / 100
@ValentaTomas
ValentaTomas / forceUpdate.ts
Created August 29, 2021 14:12
React hooks for forcing component update
const forceUpdate = useReducer(() => ({}), {})[1]
@ValentaTomas
ValentaTomas / .gitpod.yml
Created October 16, 2021 10:48
Basic NPM setup for Gitpod
tasks:
- init: npm i
github:
prebuilds:
branches: true
image:
file: .gitpod.Dockerfile
@ValentaTomas
ValentaTomas / esbuild.js
Last active December 4, 2021 12:56
Esbuild build script for Node.js projects
#!/usr/bin/env node
const esbuild = require('esbuild');
const path = require('path');
const makeAllPackagesExternalPlugin = {
name: 'make-all-packages-external',
setup(build) {
const filter = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/; // Must not start with "/" or "./" or "../"
build.onResolve({ filter }, args => ({ path: args.path, external: true }));