Skip to content

Commit

Permalink
feat: source code & test app debugger (#685)
Browse files Browse the repository at this point in the history
  • Loading branch information
willviles authored and zacharygolba committed Mar 17, 2017
1 parent 7858af5 commit 61bb4f4
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"flow": "flow check",
"types": "shx rm -rf flow-typed && flow-typed install",
"lint": "remark . && eslint .",
"test": "nyc -i ./lib/babel-hook.js --instrument false --source-map false mocha --opts mocha.opts"
"test": "nyc -i ./lib/babel-hook.js --instrument false --source-map false mocha --opts mocha.opts",
"debugger": "node test/utils/debugger/dist/debug.js",
"build:debugger": "rollup -c test/utils/debugger/rollup.config.js"
},
"author": "Zachary Golba",
"license": "MIT",
Expand Down
38 changes: 38 additions & 0 deletions test/utils/debugger/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import path from 'path';

import json from 'rollup-plugin-json';
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';

export default {
dest: './test/utils/debugger/dist/debug.js',
entry: './test/utils/debugger/src/debug.js',
format: 'cjs',
banner: (
'require(\'source-map-support\').install({\n'
+ ' environment: \'node\'\n'
+ '});\n'
),
onwarn: ({ code, message }) => {
if (code === 'UNUSED_EXTERNAL_IMPORT') {
return;
}
// eslint-disable-next-line no-console
console.warn(message);
},
plugins: [
json(),
babel(),
resolve()
],
external: id => !(
id.startsWith('.')
|| id.startsWith('/') // Absolute path on Unix
|| /^[A-Z]:[\\/]/.test(id) // Absolute path on Windows
|| id.startsWith('src')
|| id.startsWith(path.join(__dirname, 'src'))
|| id === 'babelHelpers'
|| id === '\u0000babelHelpers'
),
sourceMap: true
};
112 changes: 112 additions & 0 deletions test/utils/debugger/src/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import childProcess from 'child_process';

// @const spawn
// Spawn commands from a node child process

const { spawn } = childProcess;

// @const repoDir
// Starting CWD in the repo root

const repoDir = process.cwd();

// @function log
// Shortcut for console.log

function log(...msg) { console.log(...msg); }

// @async runCommand
// Run a terminal command

async function runCommand(command, args = [], [ preMsg = '', successMsg = '' ]) {
log(preMsg);

return new Promise((resolve, reject) => {
const run = spawn(command, args);

run.stderr.on('data', err => {
log(err.toString());
reject(err.toString());
});

run.stdout.on('data', data => log(data.toString()));

run.on('exit', () => resolve(successMsg));

})
.then(msg => log(msg))
.catch(err => log(err.toString()));

}

// @async runInspector
// Run the node inspector
// and open URL in Chrome window

async function runInspector() {
log('Starting node inspector...');

return new Promise((resolve, reject) => {

const inspectArgs = '--inspect=4000 dist/boot.js'.split(' ');
const run = spawn('node', inspectArgs);

let url;

function openURL(url) {
runCommand(
'osascript',
['-e', `tell application "Google Chrome" to open location "${url}"`],
['Opening in Google Chrome (OSX Users only)']
);
}

function parseURL(msg) {
msg = msg.split('chrome-devtools');
return `chrome-devtools${msg[1]}`.replace(/\s+/g, '');
}

run.stderr.on('data', msg => {
if (url) { return; }
msg = msg.toString();
log(msg);
url = parseURL(msg);
openURL(url);

});

run.stdout.on('data', data => log(data.toString()));

run.on('exit', (code) => log(`Child exited with code ${code}`));

})
.then(msg => log(msg))
.catch(err => log(err.toString));

}

// @listener
// Ensure repoDir is returned to when process exits

process.on('exit', () => {
process.chdir(repoDir);
});

// @async
// Run commands

(async function() {

const cleanArgs = 'rm -rf .nyc_output coverage dist test/test-app/dist test-results.xml'.split(' ');

await runCommand('shx', cleanArgs, ['Cleaning Lux repo...', 'Repo cleaned.']);

await runCommand('rollup', ['-c'], ['Building Lux source...', 'Lux source built.']);

process.chdir('./test/test-app');

await runCommand('lux', ['build'], ['Building Lux test-app...', 'Lux test-app built.']);

await runInspector();

}());

0 comments on commit 61bb4f4

Please sign in to comment.