Skip to content

Commit

Permalink
Merge pull request #45 from angrykoala/dev
Browse files Browse the repository at this point in the history
Yerbamate 2.0.1
  • Loading branch information
angrykoala committed Jul 15, 2017
2 parents 4e54d06 + efcaec4 commit fd8649c
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 207 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
2.0.1 / 2017-07-15
==================

* Minor general refactor
* Added package-lock file
* README file code examples updated

2.0.0 / 2017-07-08
==================

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Expand Up @@ -37,6 +37,7 @@ Want to fix a bug, implement a feature or help with the tests / docs? Read this
- Make sure no one else is assigned or working on the issue. It's a good idea to leave a comment stating
that you're going to implement it before sending in your PR. This way no two devs are working on the same
thing.
- If required, update the CHANGELOG.md file with a small summary of your changes in the latest version (add a new section if needed)



Expand Down
21 changes: 11 additions & 10 deletions README.md
Expand Up @@ -20,25 +20,26 @@ To install yerbamate, simply execute `npm install --save-dev yerbamate` in your
With _yerbamate_ you can easily test commands from your favorite Javascript testing framework simply importing it with `require('yerbamate')` and calling `yerbamate.run`.

```js
var yerbamate=require('yerbamate');
const yerbamate = require('yerbamate');

yerbamate.run("cat my_file.md", function(code, out, errs){
if(!yerbamate.successCode(code)) console.log("Error: " + errs[0]);
else console.log("Success - " + out);
yerbamate.run("cat my_file.md", (code, out, errs) => {
if (!yerbamate.successCode(code)) console.log("Error: " + errs[0]);
else console.log("Success - " + out);
});
```

_Yerbamate_ also provides easy access to you package.json defined scripts and commands, so you can test your module easily with `yerbamate.loadPackage`.

```js
var yerbamate=require('yerbamate');

var pkg=yerbamate.loadPackage(module);
const yerbamate = require('yerbamate');
const pkg = yerbamate.loadPackage(module);

//Test the package.json start script
yerbamate.run(pkg.start, pkg.dir, {args: "[my arguments]"} function(code, out, errs){
if(!yerbamate.successCode(code)) console.log("Process exited with error code");
if(errs.length > 0) console.log("Errors in process:" + errs.length);
yerbamate.run(pkg.start, pkg.dir, {
args: "[my arguments]"
}, function(code, out, errs) {
if (!yerbamate.successCode(code)) console.log("Process exited with error code");
if (errs.length > 0) console.log("Errors in process:" + errs.length);
console.log("Output: " + out[0]);
});
```
Expand Down
87 changes: 42 additions & 45 deletions app/loader.js
Expand Up @@ -7,62 +7,59 @@ Loads all the scripts and binaries data from a module package.json
const path = require('path');
const fs = require('fs');

class Loader {
static getContents(content, dir) {
return {
dir: dir,
main: content.main ? "node " + content.main : undefined,
start: content.scripts ? content.scripts.start : undefined,
bin: content.bin || {},
scripts: content.scripts || {}
};
}

function getContents(content, dir) {
return {
dir: dir,
main: content.main ? "node " + content.main : undefined,
start: content.scripts ? content.scripts.start : undefined,
bin: content.bin || {},
scripts: content.scripts || {}
};
}

function moduleLoader(pmodule, dir) {
dir = processDir(pmodule, dir);
validateDir(pmodule, dir);

let contents;
try {
contents = require(dir + '/package.json');
} catch (error) {}

if (contents) return getContents(contents, dir);
static moduleLoader(pmodule, dir) {
dir = Loader.processDir(pmodule, dir);
Loader.validateDir(pmodule, dir);

else return moduleLoader(pmodule, path.dirname(dir));
}
let contents;
try {
contents = require(dir + '/package.json');
} catch (error) {}

function processDir(pmodule, dir) {
if (!dir) {
dir = path.dirname(pmodule.filename || pmodule.id);
return (contents) ? Loader.getContents(contents, dir) : Loader.moduleLoader(pmodule, path.dirname(dir));
}

static processDir(pmodule, dir) {
if (!dir) {
dir = path.dirname(pmodule.filename || pmodule.id);
}
return dir;
}

return dir;
}

function validateDir(pmodule, dir) {
if (dir === '/') {
throw new Error('Could not find package.json up from ' + (pmodule.filename || pmodule.id));
} else if (!dir || dir === '.') {
throw new Error('Cannot find package.json from unspecified directory');
static validateDir(pmodule, dir) {
if (dir === '/') {
throw new Error('Could not find package.json up from ' + (pmodule.filename || pmodule.id));
} else if (!dir || dir === '.') {
throw new Error('Cannot find package.json from unspecified directory');
}
}
}


function fileLoader(filepath) {
filepath = path.resolve(filepath);
let contents;
try {
contents = fs.readFileSync(filepath, 'utf-8');
} catch (error) {
throw error;
static fileLoader(filepath) {
filepath = path.resolve(filepath);
let contents;
try {
contents = fs.readFileSync(filepath, 'utf-8');
} catch (error) {
throw error;
}
return Loader.getContents(JSON.parse(contents), path.dirname(filepath));
}
return getContents(JSON.parse(contents), path.dirname(filepath));
}

module.exports = function(pmodule) {

module.exports = (pmodule) => {
if (!pmodule) throw new Error("yerbamate loader - Not module found");
if (typeof pmodule === "string") return fileLoader(pmodule);
else return moduleLoader(pmodule);
return (typeof pmodule === "string") ? Loader.fileLoader(pmodule) : Loader.moduleLoader(pmodule);
};
102 changes: 52 additions & 50 deletions app/runner.js
Expand Up @@ -4,67 +4,69 @@ const path = require('path');
const childProcess = require('child_process');
const untildify = require('untildify');

function filterOutput(out) {
return out.split('\n').filter(Boolean);
}

function processPath(dir) {
dir = untildify(dir).trim();
return path.resolve(dir);
}
class Runner {

function runProcess(command, dir, options, done) {
const execOptions = {
shell: true
};
let args = [];
if (dir) execOptions.cwd = processPath(dir);

if (options.args) {
if (options.args.constructor === Array) {
args = options.args;
} else args = options.args.split(" ");
static filterOutput(out) {
return out.split('\n').filter(Boolean);
}

if (options.env) {
execOptions.env = Object.assign(options.env, process.env);
static processPath(dir) {
dir = untildify(dir).trim();
return path.resolve(dir);
}

const arr = command.split(" ").concat(args);
let proc;
try {
proc = childProcess.spawn(arr.shift(), arr, execOptions);
} catch (e) {
done(1, [], [e]);
return null;
}
static runProcess(command, dir, options, done) {
const execOptions = {
shell: true
};
let args = [];
if (dir) execOptions.cwd = Runner.processPath(dir);

let outs = "";
let errs = "";
if (options.args) {
if (options.args.constructor === Array) {
args = options.args;
} else args = options.args.split(" ");
}

proc.stdout.on('data', (data) => {
outs += data;
if (options.stdout) options.stdout(data.toString());
});
if (options.env) {
execOptions.env = Object.assign(options.env, process.env);
}

proc.stderr.on('data', (data) => {
errs += data;
if (options.stderr) options.stderr(data.toString());
});
const arr = command.split(" ").concat(args);
let proc;
try {
proc = childProcess.spawn(arr.shift(), arr, execOptions);
} catch (e) {
done(1, [], [e]);
return null;
}

proc.on('error', (err) => {
errs += err;
});
let outs = "";
let errs = "";

proc.on('close', (code, signal) => {
if (signal === "SIGTERM" && code === null) code = 143;
if (done) done(code, filterOutput(outs), filterOutput(errs));
});
return proc;
}
proc.stdout.on('data', (data) => {
outs += data;
if (options.stdout) options.stdout(data.toString());
});

proc.stderr.on('data', (data) => {
errs += data;
if (options.stderr) options.stderr(data.toString());
});

proc.on('error', (err) => {
errs += err;
});

module.exports = function(command, dir, options, done) {
proc.on('close', (code, signal) => {
if (signal === "SIGTERM" && code === null) code = 143;
if (done) done(code, Runner.filterOutput(outs), Runner.filterOutput(errs));
});
return proc;
}

}
module.exports = (command, dir, options, done) => {
if (!done && typeof options === 'function') {
done = options;
options = null;
Expand All @@ -80,5 +82,5 @@ module.exports = function(command, dir, options, done) {
}
if (!options) options = {};

return runProcess(command, dir, options, done);
return Runner.runProcess(command, dir, options, done);
};
11 changes: 3 additions & 8 deletions index.js
Expand Up @@ -4,18 +4,13 @@
// by @angrykoala
// The js testing library for command-line interfaces.

const kill = require('tree-kill');
const runner = require('./app/runner');
const loader = require('./app/loader');

const kill = require('tree-kill');

module.exports = {
run: runner,
stop: (proc, cb) => {
kill(proc.pid, 'SIGKILL', cb); //This sends SIGTERM
},
loadPackage: loader,
successCode: (code) => {
return code === 0;
}
stop: (proc, cb) => kill(proc.pid, 'SIGKILL', cb),
successCode: (code) => code === 0
};

0 comments on commit fd8649c

Please sign in to comment.