scratch – Blame information for rev 75

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