Skip to content

Commit

Permalink
feat: added debug with flag (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
amritk committed Nov 10, 2023
1 parent 05990e7 commit b3b36ba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const cmd = process.argv[2];
process.env.CORE_D_TITLE = 'your_d';
process.env.CORE_D_DOTFILE = '.your_d';
process.env.CORE_D_SERVICE = require.resolve('./your-service');
// optional to get logging from the child process
process.env.CORE_D_DEBUG = 'true';

const core_d = require('core_d');

Expand Down
44 changes: 38 additions & 6 deletions lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,44 @@ function launch(callback) {
// eslint-disable-next-line node/global-require
const { spawn } = require('child_process');
const daemon = require.resolve('./daemon');
const child = spawn('node', [daemon], {
detached: true,
env,
stdio: ['ignore', 'ignore', 'ignore']
});
child.unref();

// Debug is enabled
if (process.env.CORE_D_DEBUG) {
let scriptOutput = '';
const child = spawn('node', [daemon], {
detached: true,
env
});
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
//Here is where the output goes
console.log(`stdout: ${data}`);
data = data.toString();
scriptOutput += data;
});

child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
//Here is where the error output goes
console.log(`stderr: ${data}`);
data = data.toString();
scriptOutput += data;
});

child.on('close', (code) => {
//Here you can get the exit code of the script
console.log(`closing code: ${code}`);
console.log('Full output of script: ', scriptOutput);
});
child.unref();
} else {
const child = spawn('node', [daemon], {
detached: true,
env,
stdio: ['ignore', 'ignore', 'ignore']
});
child.unref();
}
setTimeout(() => {
wait(callback);
}, 100);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b3b36ba

Please sign in to comment.