diff --git a/README.md b/README.md index bc9bccce..d99de091 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/js-yaml b/bin/js-yaml new file mode 120000 index 00000000..05f5ee9e --- /dev/null +++ b/bin/js-yaml @@ -0,0 +1 @@ +js-yaml.js \ No newline at end of file diff --git a/bin/js-yaml.js b/bin/js-yaml.js new file mode 100755 index 00000000..a3fc9666 --- /dev/null +++ b/bin/js-yaml.js @@ -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); +}); diff --git a/package.json b/package.json index 11b989cb..2d5aaee7 100644 --- a/package.json +++ b/package.json @@ -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" } }