Skip to content

Commit

Permalink
Intercept calls to console.{warn,error,log}
Browse files Browse the repository at this point in the history
This clears the tests output and will allow smarter debug later
  • Loading branch information
geowarin committed Jun 25, 2016
1 parent ad31710 commit 809cca3
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 31 deletions.
6 changes: 3 additions & 3 deletions lib/bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const addBuiltInCommands = require('../commands/addBuiltInCommands');
const loadUserConfig = require('./loadUserConfig');
const addConfiguredPlugins = require('../plugins/addConfiguredPlugins');
const Commands = require('../commands/Commands');
const debug = require('../utils/debug');

function tarec(projectDir, args) {
const rootDir = path.join(__dirname, '../..');
Expand Down Expand Up @@ -46,7 +47,6 @@ function tarec(projectDir, args) {
const commandName = args[0];
const commandArgs = minimist(args.slice(1));

console.log(commandName);
if (commandName !== 'init') {
context.webpackBabelConfig = createBabelConfig(context, true);
context.babelConfig = createBabelConfig(context, false);
Expand All @@ -55,8 +55,8 @@ function tarec(projectDir, args) {
try {
commands.runCommand(commandName, context, commandArgs);
} catch (e) {
console.error(chalk.red(e.message));
console.error(e.stack);
debug.error(chalk.red(e.message));
debug.error(e.stack);
showHelp(commands, tarecPkg);
process.exit(1);
}
Expand Down
13 changes: 7 additions & 6 deletions lib/bin/showHelp.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
'use strict';

const chalk = require('chalk');
const debug = require('../utils/debug');

module.exports = function showHelp (commands, tarecPkg) {

const version = tarecPkg.version;
console.log(`Tarec ${chalk.blue(version)}`);
console.log("\nAvailable commands:");
debug.log(`Tarec ${chalk.blue(version)}`);
debug.log("\nAvailable commands:");
const availableCommands = commands.commands;
Object.keys(availableCommands).forEach(commandName => {
console.log(" * " + chalk.blue(commandName));
debug.log(" * " + chalk.blue(commandName));
const command = availableCommands[commandName];
if (command.help) {
console.log(" " + command.help)
debug.log(" " + command.help)
}
})
console.log(`\nType ${chalk.blue('tarec <command> --help')} for more information on a specific command`);
});
debug.log(`\nType ${chalk.blue('tarec <command> --help')} for more information on a specific command`);
};
1 change: 1 addition & 0 deletions lib/bin/tarec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
'use strict';

const tarec = require('./main');
require('../utils/debug').enable();
tarec(process.cwd(), process.argv.slice(2));
11 changes: 6 additions & 5 deletions lib/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
const deasync = require('deasync');
const webpack = deasync(require('webpack'));
const chalk = require('chalk');
const debug = require('../utils/debug');

module.exports = function build (context, args) {
if (args.help || args.h) {
console.log(`\nCommand: ${chalk.green('tarec build')}`);
console.log(` Generates your bundled application in ${chalk.magenta('/dist')}`);
console.log(` Options:`);
console.log(` -nominify: do not use uglifyjs to compress the javascript code`);
debug.log(`\nCommand: ${chalk.green('tarec build')}`);
debug.log(` Generates your bundled application in ${chalk.magenta('/dist')}`);
debug.log(` Options:`);
debug.log(` -nominify: do not use uglifyjs to compress the javascript code`);
process.exit(0);
}

const stats = webpack(context.webpackConfig);
console.log(stats.toString({
debug.log(stats.toString({
children: false,
chunks: false,
colors: true,
Expand Down
9 changes: 5 additions & 4 deletions lib/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const deasync = require('deasync');
const copy = deasync(require('copy-template-dir'));
const path = require('path');
const chalk = require('chalk');
const debug = require('../utils/debug');

module.exports = function init (context, args) {
if (args.help || args.h) {
console.log(`\nCommand: ${chalk.green('tarec init')}`);
console.log(` Generates a simple application in the current directory`);
console.log(` Options: <None>`);
debug.log(`\nCommand: ${chalk.green('tarec init')}`);
debug.log(` Generates a simple application in the current directory`);
debug.log(` Options: <None>`);
process.exit(0);
}
const minimal = args.minimal;
Expand All @@ -25,5 +26,5 @@ module.exports = function init (context, args) {
const outDir = path.join(context.projectDir);

const createdFiles = copy(inDir, outDir, vars);
createdFiles.forEach(filePath => console.log(`${chalk.green('created')} ${filePath}`))
createdFiles.forEach(filePath => debug.log(`${chalk.green('created')} ${filePath}`))
};
21 changes: 11 additions & 10 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ const chalk = require('chalk');
const history = require('connect-history-api-fallback');
const proxy = require('http-proxy-middleware');
const ngrok = require('ngrok');
const debug = require('../utils/debug');

module.exports = function start (context, args) {

if (args.help || args.h) {
console.log(`\nCommand: ${chalk.green('tarec start')}`);
console.log(` Starts a dev server on port ${chalk.magenta('3000')}`);
console.log(` Options:`);
console.log(` -p, --port <port>: change the server port`);
console.log(` -o, --open: open in your browser`);
console.log(` --ngrok: creates a tunnel to your local server so it is accessible on the outside`);
debug.log(`\nCommand: ${chalk.green('tarec start')}`);
debug.log(` Starts a dev server on port ${chalk.magenta('3000')}`);
debug.log(` Options:`);
debug.log(` -p, --port <port>: change the server port`);
debug.log(` -o, --open: open in your browser`);
debug.log(` --ngrok: creates a tunnel to your local server so it is accessible on the outside`);
process.exit(0);
}
const shouldOpen = args.o || args.open || false;
Expand Down Expand Up @@ -47,18 +48,18 @@ module.exports = function start (context, args) {

app.listen(context.serverPort, 'localhost', (err) => {
if (err) {
console.log(err);
debug.log(err);
return;
}

console.log(`Listening at ${chalk.blue(url)}`);
debug.log(`Listening at ${chalk.blue(url)}`);
if (shouldOpenTunnel) {
ngrok.connect(context.serverPort, (ngrokErr, ngrokUrl) => {
if (ngrokErr) {
console.log(ngrokErr);
debug.log(ngrokErr);
return;
}
console.log(`Tunnel opened at: ${chalk.blue(ngrokUrl)}`);
debug.log(`Tunnel opened at: ${chalk.blue(ngrokUrl)}`);
});

}
Expand Down
5 changes: 3 additions & 2 deletions lib/plugins/addConfiguredPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const resolve = require('resolve');
const chalk = require('chalk');
const fs = require('fs');
const debug = require('../utils/debug');

function resolveOrUndefined(pluginPath) {
let resolved;
Expand Down Expand Up @@ -35,11 +36,11 @@ module.exports = function addConfiguredPlugins (context, commands) {
const plugin = require(resolvedPath);
plugin(context, operations);
} catch (e) {
console.error(e.stack);
debug.error(e.stack);
throw new Error(`Error in plugin ${pluginPath}`);
}
} else {
console.warn(chalk.yellow(`Could not find plugin ${pluginPath}`));
debug.warn(chalk.yellow(`Could not find plugin ${pluginPath}`));
}

})
Expand Down
31 changes: 31 additions & 0 deletions lib/utils/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Debugger {

constructor() {
this.enabled = false;
}

enable() {
this.enabled = true;
}

log(...args) {
if (!this.enabled) {
return;
}
console.log(...args);
}

error(...args) {
// always show errors
console.error(...args);
}

warn(...args) {
if (!this.enabled) {
return;
}
console.warn(...args);
}
}

module.exports = new Debugger();
2 changes: 1 addition & 1 deletion test/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function toHaveFilesMatching (...regexps) {
expect.extend({toHaveFiles, toHaveFilesMatching});

function npmInstall (cwd) {
execSync('npm install --cache-min 99999', {cwd, stdio: [0, 1, 2]})
execSync('npm install --cache-min 99999', {cwd, stdio: ['ignore', 'ignore', 'ignore']})
}


Expand Down

0 comments on commit 809cca3

Please sign in to comment.