Skip to content

Instantly share code, notes, and snippets.

View eschwartz's full-sized avatar

Edan Schwartz eschwartz

View GitHub Profile
@eschwartz
eschwartz / Dockerfile
Last active November 2, 2023 15:11
npm install from private repo, in docker build
# Add these lines to your dockerfile, before `npm install`
# Copy the bitbucket private key to your docker image
COPY ./bitbucket_ssh_key /opt/my-app
# Copy the ssh script to your docker image
COPY ./ssh-bitbucket.sh /opt/my-app
# Tell git to use your `ssh-bitbucket.sh` script
ENV GIT_SSH="/opt/map-project-tile-server/ssh-bitbucket.sh"
@eschwartz
eschwartz / assert-file-exists.ts
Last active December 21, 2017 20:08
Assert a file does not exist (node)
import * as fs from 'fs';
import * as assert from 'assert';
import * as _ from 'lodash';
function assertFileExists(filePath:string, msg?:string) {
try {
fs.statSync(filePath);
}
catch (err) {
if (_.includes(['ENOENT', 'ENOTDIR'], err.code)) {
@eschwartz
eschwartz / demo.js
Last active April 12, 2016 15:28
Using deferred to test async services
function mockReadFile(mockPath, content) {
// see https://gist.github.com/eschwartz/955a3f8925e24270550cb635dd49c12b
const deferred = Deferred();
sinon.stub(fs, 'readFile', _.wrap(fs.readFile, (origFn, path, encoding, cb) => {
if (path !== mockPath) {
return origFn.readFile(path, encoding, cb);
}
// Only resolve `fs.readFile` when deferred is resolved
@eschwartz
eschwartz / poll.js
Last active January 9, 2017 17:22
Poll
/**
* eg.
*
* poll(
* () => Promise.resolve(Math.random()),
* val => val > 0.5
* )
* .then(val => console.log(`You won, with ${val}!`));
*
* @param {():Promise<T>} run
@eschwartz
eschwartz / deferred.js
Last active November 21, 2016 14:23
Deferred
function Deferred() {
var deferred = {
resolve: null,
reject: null
};
deferred.promise = new Promise((resolve, reject) => {
// Save resolve/reject to deferred, for later usage.
deferred.resolve = resolve;
deferred.reject = reject;
});
function intent({DOM}) {
return {
addTodo: DOM.get('input', 'change').
map(evt => evt.target.value).
filter(val => val.trim().length),
removeTodo: DOM.get('button', 'click').
// Map the remove button click to the item text
map(evt => evt.target.previousElementSibling.innerText.trim())
};
}
@eschwartz
eschwartz / assert-file-exists.js
Last active December 18, 2017 18:30
Assert that a file exists
const fs = require('fs');
const assert = require('assert');
const _ = require('lodash');
function fileExists(filePath, msg) {
try {
fs.statSync(filePath);
}
catch (err) {
if (_.includes(['ENOENT', 'ENOTDIR'], err.code)) {
@eschwartz
eschwartz / assert-throws-async
Created March 29, 2016 19:36
Assertion that a promise-fn fails
const assert = require('assert');
const _ = require('lodash');
function throwsAsync(block, expectedError, message) {
return block()
.then(res => {
assert.fail(res, expectedError, message, 'is');
}, err => {
if (_.isFunction(expectedError)) {
// note that assert.throws doesn't work with custom Error constructors
@eschwartz
eschwartz / cluster.js
Last active May 2, 2016 17:10
Node cluster
#!/usr/bin/env node
/**
* Usage:
*
* node cluster.js ./worker-script.js
*/
const cluster = require('cluster');
const path = require('path');
function startCluster(runWorker, opts) {
@eschwartz
eschwartz / promise-series
Created February 23, 2016 15:47
Promise Series: run an async fn in series an arbitrary number of times
const _ = require('lodash');
function series(count, asyncFn) {
return _.range(0, count)
.reduce((lastRun_, i) => {
return lastRun_.then(() => asyncFn(i))
}, Promise.resolve());
}
series(10, (i) => {