Skip to content

Instantly share code, notes, and snippets.

@AutoSponge
Created October 1, 2015 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AutoSponge/aa9c02c25b56abe6dfd1 to your computer and use it in GitHub Desktop.
Save AutoSponge/aa9c02c25b56abe6dfd1 to your computer and use it in GitHub Desktop.
Babel npm scripts
#! /usr/bin/env babel-node
import dotenv from 'dotenv';
import program from 'commander';
import jwt from 'jsonwebtoken';
program
.usage('[options]')
.option('-u, --user <user id>', 'user id')
.option('-E, --env [value]', 'environment, defaults to development', 'development')
.parse(process.argv);
const {user, env} = program;
dotenv.config({path: `./${program.env}.env`});
console.log(`creating ${env} token: ${JSON.stringify({user})}...`);
console.log(jwt.sign({user}, process.env.JWT_SECRET));
#! /usr/bin/env babel-node
import {spawn} from 'child_process';
import dotenv from 'dotenv';
const [a, b, envName = 'development'] = process.argv;
dotenv.config({path: `./${envName}.env`});
const log = console.log.bind(console);
const stdio = 'inherit';
const commands = [
spawn('redis-server', {stdio}),
spawn('./node_modules/.bin/babel-node', ['src/server'], {stdio})
];
commands.forEach(child => ['error', 'close'].forEach(e => child.on(e, log)));
process.on('SIGINT', () => commands.forEach(child => child.kill('SIGHUP')));
#! /usr/bin/env babel-node
import {spawn} from 'child_process';
import dotenv from 'dotenv';
const [a, b, coverage] = process.argv;
dotenv.config({path: `./test.env`});
const log = console.log.bind(console);
const stdio = 'inherit';
const mocha = './node_modules/.bin/_mocha';
const babel = './node_modules/.bin/babel-node';
const mochaParams = ['--compilers', 'js:babel/register'];
const mochaReport = ['--reporter', 'spec'];
const covParams = ['node_modules/.bin/isparta', 'cover', '--report', 'text', '--report', 'html'];
const commands = [];
if (!process.env.CIRCLECI) {
commands.push(spawn('redis-server', {stdio}));
}
if (coverage) {
commands.push(spawn(babel, [...covParams, mocha, ...mochaParams], {stdio}));
} else {
commands.push(spawn(mocha, [...mochaParams, ...mochaReport], {stdio}));
}
commands.forEach(child => child.on('error', log));
if (!process.env.CIRCLECI) {
commands[1].on('close', () => commands[0].kill('SIGINT'));
}
process.on('SIGINT', () => commands.forEach(child => child.kill('SIGHUP')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment