1 | #!/usr/bin/env node
|
2 |
|
3 | /**
|
4 | * yaml2json cli program
|
5 | */
|
6 |
|
7 | var YAML = require('../lib/Yaml.js');
|
8 |
|
9 | var ArgumentParser = require('argparse').ArgumentParser;
|
10 | var cli = new ArgumentParser({
|
11 | prog: "yaml2json",
|
12 | version: require('../package.json').version,
|
13 | addHelp: true
|
14 | });
|
15 |
|
16 | cli.addArgument(
|
17 | ['-p', '--pretty'],
|
18 | {
|
19 | help: 'Output pretty (indented) JSON.',
|
20 | action: 'storeTrue'
|
21 | }
|
22 | );
|
23 |
|
24 | cli.addArgument(
|
25 | ['-i', '--indentation'],
|
26 | {
|
27 | action: 'store',
|
28 | type: 'int',
|
29 | help: 'Number of space characters used to indent code (use with --pretty, default: 2).',
|
30 | }
|
31 | );
|
32 |
|
33 | cli.addArgument(
|
34 | ['-s', '--save'],
|
35 | {
|
36 | help: 'Save output inside JSON file(s) with the same name.',
|
37 | action: 'storeTrue'
|
38 | }
|
39 | );
|
40 |
|
41 | cli.addArgument(
|
42 | ['-r', '--recursive'],
|
43 | {
|
44 | help: 'If the input is a directory, also find YAML files in sub-directories recursively.',
|
45 | action: 'storeTrue'
|
46 | }
|
47 | );
|
48 |
|
49 | cli.addArgument(
|
50 | ['-w', '--watch'],
|
51 | {
|
52 | help: 'Watch for changes.',
|
53 | action: 'storeTrue'
|
54 | }
|
55 | );
|
56 |
|
57 | cli.addArgument(['input'], {
|
58 | help: 'YAML file or directory containing YAML files or - to read YAML from stdin.'
|
59 | });
|
60 |
|
61 | try {
|
62 | var options = cli.parseArgs();
|
63 | var path = require('path');
|
64 | var fs = require('fs');
|
65 | var glob = require('glob');
|
66 |
|
67 | var rootPath = process.cwd();
|
68 | var parsePath = function(input) {
|
69 | if (input == '-') return '-';
|
70 | var output;
|
71 | if (!(input != null)) {
|
72 | return rootPath;
|
73 | }
|
74 | output = path.normalize(input);
|
75 | if (output.length === 0) {
|
76 | return rootPath;
|
77 | }
|
78 | if (output.charAt(0) !== '/') {
|
79 | output = path.normalize(rootPath + '/./' + output);
|
80 | }
|
81 | if (output.length > 1 && output.charAt(output.length - 1) === '/') {
|
82 | return output.substr(0, output.length - 1);
|
83 | }
|
84 | return output;
|
85 | };
|
86 |
|
87 | // Find files
|
88 | var findFiles = function(input) {
|
89 | if (input != '-' && input != null) {
|
90 | var isDirectory = fs.statSync(input).isDirectory();
|
91 | var files = [];
|
92 |
|
93 | if (!isDirectory) {
|
94 | files.push(input);
|
95 | }
|
96 | else {
|
97 | if (options.recursive) {
|
98 | files = files.concat(glob.sync(input+'/**/*.yml'));
|
99 | files = files.concat(glob.sync(input+'/**/*.yaml'));
|
100 | }
|
101 | else {
|
102 | files = files.concat(glob.sync(input+'/*.yml'));
|
103 | files = files.concat(glob.sync(input+'/*.yaml'));
|
104 | }
|
105 | }
|
106 |
|
107 | return files;
|
108 | }
|
109 | return null;
|
110 | };
|
111 |
|
112 | // Convert to JSON
|
113 | var convertToJSON = function(input, pretty, save, spaces, str) {
|
114 | var json;
|
115 | if (spaces == null) spaces = 2;
|
116 | if (str != null) {
|
117 | if (pretty) {
|
118 | json = JSON.stringify(YAML.parse(str), null, spaces);
|
119 | }
|
120 | else {
|
121 | json = JSON.stringify(YAML.parse(str));
|
122 | }
|
123 | } else {
|
124 | if (pretty) {
|
125 | json = JSON.stringify(YAML.parseFile(input), null, spaces);
|
126 | }
|
127 | else {
|
128 | json = JSON.stringify(YAML.parseFile(input));
|
129 | }
|
130 | }
|
131 |
|
132 | if (!save || input == null) {
|
133 | // Ouput result
|
134 | process.stdout.write(json+"\n");
|
135 | }
|
136 | else {
|
137 | var output;
|
138 | if (input.substring(input.length-4) == '.yml') {
|
139 | output = input.substr(0, input.length-4) + '.json';
|
140 | }
|
141 | else if (input.substring(input.length-5) == '.yaml') {
|
142 | output = input.substr(0, input.length-5) + '.json';
|
143 | }
|
144 | else {
|
145 | output = input + '.json';
|
146 | }
|
147 |
|
148 | // Write file
|
149 | var file = fs.openSync(output, 'w+');
|
150 | fs.writeSync(file, json);
|
151 | fs.closeSync(file);
|
152 | process.stdout.write("saved "+output+"\n");
|
153 | }
|
154 | };
|
155 |
|
156 | var input = parsePath(options.input);
|
157 | var mtimes = [];
|
158 |
|
159 | var runCommand = function() {
|
160 | try {
|
161 | var files = findFiles(input);
|
162 | if (files != null) {
|
163 | var len = files.length;
|
164 |
|
165 | for (var i = 0; i < len; i++) {
|
166 | var file = files[i];
|
167 | var stat = fs.statSync(file);
|
168 | var time = stat.mtime.getTime();
|
169 | if (!stat.isDirectory()) {
|
170 | if (!mtimes[file] || mtimes[file] < time) {
|
171 | mtimes[file] = time;
|
172 | convertToJSON(file, options.pretty, options.save, options.indentation);
|
173 | }
|
174 | }
|
175 | }
|
176 | } else {
|
177 | // Read from STDIN
|
178 | var stdin = process.openStdin();
|
179 | var data = "";
|
180 | stdin.on('data', function(chunk) {
|
181 | data += chunk;
|
182 | });
|
183 | stdin.on('end', function() {
|
184 | convertToJSON(null, options.pretty, options.save, options.indentation, data);
|
185 | });
|
186 | }
|
187 | } catch (e) {
|
188 | process.stderr.write((e.message ? e.message : e)+"\n");
|
189 | }
|
190 | };
|
191 |
|
192 | if (!options.watch) {
|
193 | runCommand();
|
194 | } else {
|
195 | runCommand();
|
196 | setInterval(runCommand, 1000);
|
197 | }
|
198 | } catch (e) {
|
199 | process.stderr.write((e.message ? e.message : e)+"\n");
|
200 | }
|