1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var YAML = require('../lib/Yaml.js');
|
7 |
|
8 | var ArgumentParser = require('argparse').ArgumentParser;
|
9 | var cli = new ArgumentParser({
|
10 | prog: "json2yaml",
|
11 | version: require('../package.json').version,
|
12 | addHelp: true
|
13 | });
|
14 |
|
15 | cli.addArgument(
|
16 | ['-d', '--depth'],
|
17 | {
|
18 | action: 'store',
|
19 | type: 'int',
|
20 | help: 'Set minimum level of depth before generating inline YAML (default: 2).'
|
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 (default: 2).',
|
30 | }
|
31 | );
|
32 |
|
33 | cli.addArgument(
|
34 | ['-s', '--save'],
|
35 | {
|
36 | help: 'Save output inside YML 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 JSON 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: 'JSON file or directory containing JSON files or - to read JSON 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 |
|
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+'/**/*.json'));
|
99 | }
|
100 | else {
|
101 | files = files.concat(glob.sync(input+'/*.json'));
|
102 | }
|
103 | }
|
104 |
|
105 | return files;
|
106 | }
|
107 | return null;
|
108 | };
|
109 |
|
110 |
|
111 | var convertToYAML = function(input, inline, save, spaces, str) {
|
112 | var yaml;
|
113 | if (inline == null) inline = 2;
|
114 | if (spaces == null) spaces = 2;
|
115 |
|
116 | if (str == null) {
|
117 | str = ''+fs.readFileSync(input);
|
118 | }
|
119 | yaml = YAML.dump(JSON.parse(str), inline, spaces);
|
120 |
|
121 | if (!save || input == null) {
|
122 |
|
123 | process.stdout.write(yaml);
|
124 | }
|
125 | else {
|
126 | var output;
|
127 | if (input.substring(input.length-5) == '.json') {
|
128 | output = input.substr(0, input.length-5) + '.yaml';
|
129 | }
|
130 | else {
|
131 | output = input + '.yaml';
|
132 | }
|
133 |
|
134 |
|
135 | var file = fs.openSync(output, 'w+');
|
136 | fs.writeSync(file, yaml);
|
137 | fs.closeSync(file);
|
138 | process.stdout.write("saved "+output+"\n");
|
139 | }
|
140 | };
|
141 |
|
142 | var input = parsePath(options.input);
|
143 | var mtimes = [];
|
144 |
|
145 | var runCommand = function() {
|
146 | try {
|
147 | var files = findFiles(input);
|
148 | if (files != null) {
|
149 | var len = files.length;
|
150 | for (var i = 0; i < len; i++) {
|
151 | var file = files[i];
|
152 | var stat = fs.statSync(file);
|
153 | var time = stat.mtime.getTime();
|
154 | if (!stat.isDirectory()) {
|
155 | if (!mtimes[file] || mtimes[file] < time) {
|
156 | mtimes[file] = time;
|
157 | convertToYAML(file, options.depth, options.save, options.indentation);
|
158 | }
|
159 | }
|
160 | }
|
161 | } else {
|
162 |
|
163 | var stdin = process.openStdin();
|
164 | var data = "";
|
165 | stdin.on('data', function(chunk) {
|
166 | data += chunk;
|
167 | });
|
168 | stdin.on('end', function() {
|
169 | convertToYAML(null, options.depth, options.save, options.indentation, data);
|
170 | });
|
171 | }
|
172 | } catch (e) {
|
173 | process.stderr.write((e.message ? e.message : e)+"\n");
|
174 | }
|
175 | };
|
176 |
|
177 | if (!options.watch) {
|
178 | runCommand();
|
179 | } else {
|
180 | runCommand();
|
181 | setInterval(runCommand, 1000);
|
182 | }
|
183 | } catch (e) {
|
184 | process.stderr.write((e.message ? e.message : e)+"\n");
|
185 | }
|