diff options
author | Stéphane Bachelier <stephane.bachelier@gmail.com> | 2015-05-27 19:08:14 +0200 |
---|---|---|
committer | Stéphane Bachelier <stephane.bachelier@gmail.com> | 2015-05-27 19:38:08 +0200 |
commit | 3422a8c3db65b55b8b2a96d29e2e1e71825e54bd (patch) | |
tree | 232eb2f1b7d9564989647508269bc67b796278d6 /cli | |
parent | 16fdadb25b3a0ddda9d507995561a88be29874cb (diff) | |
download | yamljs-3422a8c3db65b55b8b2a96d29e2e1e71825e54bd.tar.gz |
Add support for output directory option
Diffstat (limited to 'cli')
-rw-r--r-- | cli/json2yaml.js | 25 | ||||
-rw-r--r-- | cli/yaml2json.js | 27 |
2 files changed, 39 insertions, 13 deletions
diff --git a/cli/json2yaml.js b/cli/json2yaml.js index 4c849b2..4498e1a 100644 --- a/cli/json2yaml.js +++ b/cli/json2yaml.js @@ -4,6 +4,7 @@ */ var YAML = require('../lib/Yaml.js'); +var mkdirp = require('mkdirp'); var ArgumentParser = require('argparse').ArgumentParser; var cli = new ArgumentParser({ @@ -47,6 +48,15 @@ cli.addArgument( ); cli.addArgument( + ['-o', '--output'], + { + help: 'The output directory.', + type: 'string', + action: 'store' + } +); + +cli.addArgument( ['-w', '--watch'], { help: 'Watch for changes.', @@ -108,7 +118,7 @@ try { }; // Convert to JSON - var convertToYAML = function(input, inline, save, spaces, str) { + var convertToYAML = function(input, inline, save, spaces, str, directory) { var yaml; if (inline == null) inline = 2; if (spaces == null) spaces = 2; @@ -130,12 +140,15 @@ try { else { output = input + '.yaml'; } - + + var outputPath = path.join(directory, path.relative(process.cwd(), output)); + mkdirp.sync(path.dirname(outputPath)); + // Write file - var file = fs.openSync(output, 'w+'); + var file = fs.openSync(outputPath, 'w+'); fs.writeSync(file, yaml); fs.closeSync(file); - process.stdout.write("saved "+output+"\n"); + process.stdout.write("saved "+outputPath+"\n"); } }; @@ -154,7 +167,7 @@ try { if (!stat.isDirectory()) { if (!mtimes[file] || mtimes[file] < time) { mtimes[file] = time; - convertToYAML(file, options.depth, options.save, options.indentation); + convertToYAML(file, options.depth, options.save, options.indentation, null, options.output); } } } @@ -166,7 +179,7 @@ try { data += chunk; }); stdin.on('end', function() { - convertToYAML(null, options.depth, options.save, options.indentation, data); + convertToYAML(null, options.depth, options.save, options.indentation, data, options.output); }); } } catch (e) { diff --git a/cli/yaml2json.js b/cli/yaml2json.js index 662201c..280e38f 100644 --- a/cli/yaml2json.js +++ b/cli/yaml2json.js @@ -2,8 +2,9 @@ /** * yaml2json cli program */ - + var YAML = require('../lib/Yaml.js'); +var mkdirp = require('mkdirp'); var ArgumentParser = require('argparse').ArgumentParser; var cli = new ArgumentParser({ @@ -46,6 +47,15 @@ cli.addArgument( ); cli.addArgument( + ['-o', '--output'], + { + help: 'The output directory.', + type: 'string', + action: 'store' + } +); + +cli.addArgument( ['-w', '--watch'], { help: 'Watch for changes.', @@ -109,7 +119,7 @@ try { }; // Convert to JSON - var convertToJSON = function(input, pretty, save, spaces, str) { + var convertToJSON = function(input, pretty, save, spaces, str, directory) { var json; if (spaces == null) spaces = 2; if (str != null) { @@ -143,12 +153,15 @@ try { else { output = input + '.json'; } - + + var outputPath = path.join(directory, path.relative(process.cwd(), output)); + mkdirp.sync(path.dirname(outputPath)); + // Write file - var file = fs.openSync(output, 'w+'); + var file = fs.openSync(outputPath, 'w+'); fs.writeSync(file, json); fs.closeSync(file); - process.stdout.write("saved "+output+"\n"); + process.stdout.write("saved "+outputPath+"\n"); } }; @@ -168,7 +181,7 @@ try { if (!stat.isDirectory()) { if (!mtimes[file] || mtimes[file] < time) { mtimes[file] = time; - convertToJSON(file, options.pretty, options.save, options.indentation); + convertToJSON(file, options.pretty, options.save, options.indentation, null, options.output); } } } @@ -180,7 +193,7 @@ try { data += chunk; }); stdin.on('end', function() { - convertToJSON(null, options.pretty, options.save, options.indentation, data); + convertToJSON(null, options.pretty, options.save, options.indentation, data, options.output); }); } } catch (e) { |