Skip to content

Commit

Permalink
Add bin file
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Jul 1, 2012
1 parent 13870f0 commit 100ed32
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ npm install js-yaml
```

If you want to inspect your YAML files from CLI,
install [js-yaml.bin](https://github.com/nodeca/js-yaml.bin).
install js-yaml globally:

```
npm install js-yaml -g
```


### bundled YAML library for browser
Expand Down
1 change: 1 addition & 0 deletions bin/js-yaml
93 changes: 93 additions & 0 deletions bin/js-yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env node


'use strict';


// stdlib
var fs = require('fs');
var util = require('util');


// 3rd-party
var ArgumentParser = require('argparse').ArgumentParser;


// internal
var yaml = require('..');


////////////////////////////////////////////////////////////////////////////////


var cli = new ArgumentParser({
prog: 'js-yaml',
version: require('../package.json').version,
addHelp: true
});


cli.addArgument(['-c', '--compact'], {
help: 'Display errors in compact mode',
action: 'storeTrue'
});


cli.addArgument(['-j', '--to-json'], {
help: 'Output a non-funky boring JSON',
dest: 'json',
action: 'storeTrue'
});


cli.addArgument(['-t', '--trace'], {
help: 'Show stack trace on error',
action: 'storeTrue'
});


cli.addArgument(['file'], {
help: 'File with YAML document(s)'
});


////////////////////////////////////////////////////////////////////////////////


var options = cli.parseArgs();


////////////////////////////////////////////////////////////////////////////////


fs.readFile(options.file, 'utf8', function (err, str) {
var docs = [], out;

if (err) {
if ('ENOENT' === err.code) {
console.error('File not found: ' + options.file);
process.exit(2);
}

// Fatal fuckup
console.error(options.trace && err.stack || err.message || String(err));
process.exit(1);
}

try {
// try load all documents from the file
yaml.loadAll(str, function (doc) { docs.push(doc); });
out = (1 >= docs.length) ? (docs.shift() || null) : docs;
} catch (err) {
console.error(options.trace && err.stack || err.message || err.toString(options.compact));
process.exit(1);
}

if (options.json) {
console.log(JSON.stringify(out));
process.exit(0);
}

console.log("\n" + util.inspect(out, false, 10, true) + "\n");
process.exit(0);
});
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
"repository" : { "type": "git", "url": "git://github.com/nodeca/js-yaml.git" },

"main" : "./index.js",
"scripts" : {
"test" : "make test"
},

"bin" : { "js-yaml": "bin/js-yaml.js" },
"scripts" : { "test": "make test" },

"dependencies" : { "argparse": "~ 0.1.3" },
"devDependencies" : { "vows": "~ 0.6.0" },
"engines" : { "node": "> 0.4.11" }
"engines" : { "node": ">= 0.6.0" }
}

0 comments on commit 100ed32

Please sign in to comment.