1 | #!/usr/bin/env node
|
2 |
|
3 | /**
|
4 | * yaml2json cli program
|
5 | */
|
6 |
|
7 | var YAML = require('./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.'
|
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 | 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 | var isDirectory = fs.statSync(input).isDirectory();
|
89 | var files = [];
|
90 |
|
91 | if (!isDirectory) {
|
92 | files.push(input);
|
93 | }
|
94 | else {
|
95 | if (options.recursive) {
|
96 | files = files.concat(glob.sync(input+'/**/*.yml'));
|
97 | files = files.concat(glob.sync(input+'/**/*.yaml'));
|
98 | }
|
99 | else {
|
100 | files = files.concat(glob.sync(input+'/*.yml'));
|
101 | files = files.concat(glob.sync(input+'/*.yaml'));
|
102 | }
|
103 | }
|
104 |
|
105 | return files;
|
106 | };
|
107 |
|
108 | // Convert to JSON
|
109 | var convertToJSON = function(input, pretty, save, spaces) {
|
110 | var json;
|
111 | if (spaces == null) spaces = 2;
|
112 | if (pretty) {
|
113 | json = JSON.stringify(YAML.load(input), null, spaces);
|
114 | }
|
115 | else {
|
116 | json = JSON.stringify(YAML.load(input));
|
117 | }
|
118 |
|
119 | if (!save) {
|
120 | // Ouput result
|
121 | process.stdout.write(json+"\n");
|
122 | }
|
123 | else {
|
124 | var output;
|
125 | if (input.substring(input.length-4) == '.yml') {
|
126 | output = input.substr(0, input.length-4) + '.json';
|
127 | }
|
128 | else if (input.substring(input.length-5) == '.yaml') {
|
129 | output = input.substr(0, input.length-5) + '.json';
|
130 | }
|
131 | else {
|
132 | output = input + '.json';
|
133 | }
|
134 |
|
135 | // Write file
|
136 | var file = fs.openSync(output, 'w+');
|
137 | fs.writeSync(file, json);
|
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 | var len = files.length;
|
150 |
|
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 | convertToJSON(file, options.pretty, options.save, options.indentation);
|
159 | }
|
160 | }
|
161 | }
|
162 | } catch (e) {
|
163 | process.stderr.write((e.message ? e.message : e)+"\n");
|
164 | }
|
165 | };
|
166 |
|
167 | if (!options.watch) {
|
168 | runCommand();
|
169 | } else {
|
170 | runCommand();
|
171 | setInterval(runCommand, 1000);
|
172 | }
|
173 | } catch (e) {
|
174 | process.stderr.write((e.message ? e.message : e)+"\n");
|
175 | }
|