Skip to content

Instantly share code, notes, and snippets.

@cletusw
cletusw / amd-to-common.js
Created August 16, 2017 21:30
A codemod to transform amd style includes into commonjs includes
/**
* Modified from https://github.com/skratchdot/amd-to-commonjs-codemod
*/
const buildRequire = (j, v, r) => {
let code = "";
if (v && v.type === "Identifier" && v.name.length) {
code += `const ${v.name}`;
}
if (r && r.type === "Literal" && r.value.length) {
@cletusw
cletusw / remove-top-use-strict.js
Last active August 16, 2017 21:39
Remove top-level `"use strict";`. It can cause problems with bundlers that concatenate scripts, and so is not a recommended practice.
module.exports = function transformer(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.ExpressionStatement).filter(path => (
path.parentPath.node.type === "Program" &&
path.value.expression.type === 'Literal' &&
path.value.expression.value === 'use strict'
))
.forEach(path => j(path).remove())
@cletusw
cletusw / each-with-this-to-arrows.js
Last active August 17, 2017 18:08
Converts underscore/lodash `.each()` with `this` context to use arrow functions.
/**
* Converts underscore/lodash `.each()` with `this` context to use arrow functions.
*
* Run this with jscodeshift
* Live demo: https://astexplorer.net/#/gist/0a47495d69719449d2afbb0f0c50f8ea/latest
*
* Converts:
* _.each(array, function(item) {
* // ...
* }, this);
@cletusw
cletusw / each-with-context-to-bind.js
Created August 17, 2017 18:13
Converts underscore/lodash `.each()` with context to use Function.prototype.bind()
/**
* Converts underscore/lodash `.each()` with context to use Function.prototype.bind()
*
* Run this with jscodeshift
* Live demo: https://astexplorer.net/#/gist/b4294e95ef898af1d19cd3db19f9e8b0/latest
*
* Converts:
* _.each(array, function(item) {
* // ...
* }, context);
@cletusw
cletusw / git-sync-ship.sh
Created September 14, 2017 20:35
Git aliases for an easier rebase workflow
git config --global alias.sync '!f() { echo "$(tput setaf 4)Syncing this branch with origin master$(tput sgr 0)" && git fetch origin master && git rebase origin/master && echo "$(tput setaf 2)Branch sync successful$(tput sgr 0)"; }; f'
git config --global alias.ship '!f() { BRANCH=$(git symbolic-ref --short HEAD) && MERGE_BASE=$(git merge-base origin/master HEAD) && NUM_COMMITS=$(git rev-list --count $MERGE_BASE..) && git log --oneline --stat $MERGE_BASE.. && read -p "$(tput setaf 4)Are you sure you want to ship $(tput bold)$NUM_COMMITS$(tput sgr 0)$(tput setaf 4) commits to $(tput bold)master$(tput sgr 0)? [Y/n] " response </dev/tty && case $response in [yY][eE][sS]|[yY]|"") echo "$(tput setaf 4)Shipping branch $(tput bold)$BRANCH$(tput sgr 0)" ;; *) echo "$(tput setaf 1)Ship aborted by user$(tput sgr 0)"; return 1 ;; esac && git checkout master && (git merge --ff-only - || (echo "$(tput setaf 1)Could not merge branch into local master\nRun git sync before running this command\nIf this error persists, you ha
@cletusw
cletusw / angular-injection-to-import.js
Last active May 16, 2019 23:46
Converts the given angular injected parameter into an explicit require statement
/*
* Converts the given angular injected parameter into an explicit require statement
*
* Run this with jscodeshift
* @example
* jscodeshift . --specifier='Auth' --source='application/Auth'
*
* Live demo: https://astexplorer.net/#/gist/5492d2b9850a451d8e8d532bc64f21ce/latest
*
* Converts:
@cletusw
cletusw / Flow.scpt
Created October 30, 2015 20:31
Change your background at a specific time (Mac OS X). Use `crontab -e` to edit your crontab:
tell application "Finder"
-- wrapped in a try block for error suppression
try
set mainDisplayPicture to "/Users/claytonwatts/Pictures/Flow.jpg"
-- set the picture for additional monitors, if applicable
tell application "System Events"
@cletusw
cletusw / git-bisect-run
Last active August 13, 2019 19:00
git bisect run
Quick git tip: Most of you know (and love) git's "bisect" command, but how many have used "git bisect run"? Specify a shell script/command, and git will automatically run it on each bisect step. If it exits with a 0, the commit is marked "good". Anything else, and the commit is marked "bad".
For example, want to find the cause of a failing test?
git bisect start <failing commit> <passing commit>
git bisect run sh -c '(cd app && grunt test)'
Voila! Git will automatically find the first commit in the given range that fails the tests.
http://git-scm.com/docs/git-bisect#_bisect_run
@cletusw
cletusw / get-old-chromium-binary.md
Last active December 7, 2020 18:32
Download an old Chromium binary

(source)

Taking [denilson-sá's answer][2] further...

You need revision numbers to grab downloads. So first lookup the full version string from the following URL, adjusting parameters as needed:

https://omahaproxy.appspot.com/history.json?channel=stable&os=mac

For Chrome version 28 the full version string is 28.0.1500.71. Now go to https://omahaproxy.appspot.com and enter the full version string ("28.0.1500.71") into the Position Lookup box. Copy the Base Position number ("209842" in this case).

@cletusw
cletusw / video-recorder.js
Created April 24, 2021 17:02
Video recorder custom element
import { html, css, LitElement } from 'lit';
import { ref, createRef } from 'lit/directives/ref.js';
export class VideoRecorder extends LitElement {
static get styles() {
return css`
video {
background: #222;
--width: 100%;
width: var(--width);