scratch – Blame information for rev 75

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