Skip to content

Instantly share code, notes, and snippets.

View agwells's full-sized avatar

Aaron Wells agwells

View GitHub Profile
@agwells
agwells / gist:bd3f237879a39e76f12a0cb42f1c2fcf
Last active August 1, 2021 22:29
Manually setting up an npm auth token (without using `npm login`)

I was annoyed to find tha the NPM website lets you generate auth tokens manually, but does not provide any instructions on how to configure them in your local workspace. The only instructions are to give your username and password to npm login, which will then configure our workspace.

But all npm login does is pop the token into some annoyingly undocumented config fields in ~.npmrc. You can just as easily do this manually, if you would rather not type your password into a terminal script.

  1. Log in to the NPM website and generate an auth token.
  2. Put it in your ~/.npmrc file like so:
//registry.npmjs.org/:_authToken=YOUR-TOKEN-WOULD-GO-HERE
@agwells
agwells / webpack.cra-ssr.js
Created February 19, 2020 10:35
Webpack config to compile Create React App code to execute in Node
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const NodemonPlugin = require('nodemon-webpack-plugin');
// Directory the compiled SSR server and assets will go in
const SSR_BUILD_PATH = './ssr-build'
const originalNodeEnv = process.env.NODE_ENV;
// This must be "production" for the CRA webpack config to be correct.
@agwells
agwells / cracli.js
Last active September 30, 2019 21:17
Executing code written for Create React App, as a CLI
/**
* This file is the "Launch" script for running Create React App code at the command
* line. It calls "@babel/register", which is a Babel utility that hijacks
* the normal node "require()" function and lets Babel pre-compile imported
* files into Node-compatible JS before they're executed.
*
* This bootstraps up the fancy CRA TypeScript/React configuration (using the
* Babel config imported from "babel-preset-react-app"). Then it simply "require()"s
* some other JS/TS/JSX/TSX file, and executes it.
*
@agwells
agwells / jestFailOnConsoleMessages.js
Created December 24, 2018 00:02
Make Jest tests fail if tests emit any console messages
// Fail the test if it prints ANYTHING to the console!
//
// Warning & log messages in test output are an anti-pattern. Also, testing
// for console messages is the only way to check for PropTypes failures
// (because all PropTypes does is print a console warning)
const realConsole = global.console;
// TODO: It's possible that some of the functions under global.console may
// have legitimate uses during test runs. If so, we'll need to be more picky.
global.console = Object.keys(realConsole).reduce(
@agwells
agwells / jestExpectAlwaysFail.js
Last active January 9, 2022 18:45
Programmatically fail a Jest test
/**
* Sometimes you want to programmatically force a Jest test failure. For example, mocking "console.warn()" to
* make tests fail if they print warning messages to the console. Throwing an error isn't sufficient, because
* the calling code may be in a try/catch block.
*
* Jest has no "fail()" method** to automatically trigger a test failure, but you can do it with an "expect()" matcher
* that will always fail, such as "expect(true).toBe(false)". This will fail the test, but the output isn't very
* self-explanatory. You can improve on it by providing a message string as one of the arguments:
* "expect(null).toBe('Automatic Failure!')". This is better, but the output is still a little confusing, saying
* it expected "null" and received "Automatic Failure!"
@agwells
agwells / update-global-packages.sh
Last active November 10, 2018 22:19
Multi-package-management-system updater
#!/bin/bash
####
# A script to run updates or check for updates, in many different package management systems
# on Ubuntu.
#
# When I was installing everything I use through apt alone, it was easy to keep all my
# packages up to date with "apt-get update && apt-get upgrade", or the GUI update manager.
# But now every programming language has its own separate package management system, and
# I often use these to install packages at a "global" level on my computer. It's a pain
@agwells
agwells / maketasks.sh
Created May 27, 2018 22:52
List the available targets in a makefile
#!/bin/bash
# A utility to list all the tasks in the current directory's makefile.
#
# Idea taken from https://stackoverflow.com/a/24453375/468642
source /usr/share/bash-completion/completions/make
export _make_target_extract_script
sed -nrf <(_make_target_extract_script --) ./Makefile
@agwells
agwells / test-php-basic-auth.php
Last active January 19, 2020 20:56 — forked from rchrd2/test-php-basic-auth.php
Ways to make a browser clear its cached HTTP basic auth credentials
<?php
function require_auth() {
$AUTH_USER = 'admin';
$AUTH_PASS = 'admin';
header('Cache-Control: no-cache, must-revalidate, max-age=0');
$has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW']));
$is_not_authenticated = (
!$has_supplied_credentials ||
$_SERVER['PHP_AUTH_USER'] != $AUTH_USER ||
$_SERVER['PHP_AUTH_PW'] != $AUTH_PASS
@agwells
agwells / R-pager.sh
Last active November 14, 2017 23:54
My additions to my sitewide Rprofile file.
#!/bin/bash
# My customized pager script. Everything below this comment block is identical to
# the R pager script installed by the R debian package at /usr/lib/R/bin/pager,
# except that I've replaced ${PAGER} with /bin/less explicitly, and added
# the -I and -X flags.
#
# The "-I" flag makes searches in the pager case-insensitive by default.
# The "-X" flag tells the pager not to clear the screen when it closes. That
# allows me to continue to see the help content at the same time as having
# the prompt available; which is helpful because I have the memory of a
@agwells
agwells / r-random-grades.R
Last active November 1, 2017 09:05
Generating a skewed bell curve of random data in R
minVal <- 0
maxVal <- 100
medVal <- 70
numRows <- 1000000
adjMax <- maxVal - minVal
adjMed <- medVal - minVal
# Generate a vector that sets how likel
# each value is to be chosen.