Skip to content

Instantly share code, notes, and snippets.

Avatar
😃

David Wells DavidWells

😃
View GitHub Profile
View async-forEach.js
var waitFor = (ms) => new Promise(r => setTimeout(r, ms))
async function asyncForEach(array, callback) { 
for (let index = 0; index < array.length; index++) {   
await callback(array[index], index, array) 
}
}
var start = async () => { 
let state = 0 
await asyncForEach([1, 2, 3], async (num) => {   
View get-global-context.js
function getGlobalThis() {
if (typeof globalThis !== 'undefined') return globalThis
if (typeof global !== 'undefined') return global
if (typeof self !== 'undefined') return self /* eslint-disable-line no-restricted-globals */
if (typeof window !== 'undefined') return window
if (typeof this !== 'undefined') return this
return {} // should never happen
}
View generate.js
// From https://github.com/sw-yx/swyxdotio/blob/60b088cea0439d3e2536a78dc922af3146ba40fd/screenshot-plugin/screenshot.js
const puppeteer = require('puppeteer')
const fs = require('fs')
const path = require('path')
module.exports = screenshot
async function screenshot(PostArray) {
const headless = true
// const headless = false // for debug
const browser = await puppeteer.launch({ headless })
@DavidWells
DavidWells / install-all-dependancies.js
Created Jul 1, 2020
Install dependancies in every folder with package.json file
View install-all-dependancies.js
const path = require('path')
const fs = require('fs')
const globby = require('globby')
const cp = require("child_process")
function installDeps(functionDir, cb) {
cp.exec("npm i", { cwd: functionDir }, cb)
}
async function installAllDeps(additionalDirs = []) {
View git-info-regex.js
const pattern = /^(?:\S+:\/\/)?(?:\S+@)?(?'host'\S+?)[:\/](?'owner'\S+?)[\/](?'repository'\S+)$/gm
/*
https://github.com/rawkode/dotfiles
https://github.com/rawkode/dotfiles/2
git@github.com:rawkode/dotfiles
git@github.com:rawkode/dotfiles/micro/1/2
ssh://git@github.com/rawkode/dotfiles
@DavidWells
DavidWells / rescursive-lowercase.js
Created May 12, 2020
rescursively lowercase an object values
View rescursive-lowercase.js
function recursiveLowerCase(object) {
if (Array.isArray(object)) {
return object.map((value) => recursiveLowerCase(value))
}
if (object !== null && object.constructor === Object) {
return Object.keys(object).reduce((result, key) => (
Object.assign(Object.assign({}, result), {
[key]: recursiveLowerCase(object[key].toLowerCase())
})
@DavidWells
DavidWells / add-user-to-private-repo.js
Created Apr 26, 2020
Add GitHub user to private GitHub repo utility function
View add-user-to-private-repo.js
const axios = require('axios')
const GITHUB_REPO = process.env.GITHUB_REPO
const GITHUB_API_TOKEN = process.env.GITHUB_API_TOKEN
const GITHUB_USERNAME = process.env.GITHUB_USERNAME
module.exports = function addUserToGithubRepo(username) {
const githubEndpoint = `https://api.github.com/repos/${GITHUB_REPO}/collaborators/${username}`
const config = {
'headers': {
'User-Agent': GITHUB_USERNAME,
@DavidWells
DavidWells / auto-install-npm-deps.js
Created Apr 24, 2020
Automatically install deps for any serverless project found
View auto-install-npm-deps.js
const path = require('path')
const globby = require('globby')
const execSync = require('child_process').execSync
globby(['*/serverless.yml']).then(paths => {
// Now run npm install
const command = `npm install`
paths.forEach((p) => {
var t = path.join(__dirname, p)
execSync(command, {
View avoid-null-when-possible.js
function test() {
// simple return or return void 0 to return undefined
return void 0
}
var x = test()
if (!x) {
console.log('no x! great check') // good
}
@DavidWells
DavidWells / queries.md
Created Apr 1, 2020
Cloudwatch insights queries
View queries.md

Cloudwatch insights queries

Find function timeouts

Get all the functions having pesky timeouts

fields @timestamp, @message
| filter @message like 'Task timed out after'
| sort @timestamp desc