WXL
9 天以前 2895b4ea66e09cb355aeb4e030ca0de297bf8ce3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
// vim: set filetype=javascript:
'use strict';
 
var glob = require('glob');
var fs = require('fs');
 
/**
 *
 *  dotob <pattern> -f require -t dependencies.npm
 *
 */
var dotob = require('../index.js');
var program = require('commander');
var pkg = require('../package.json');
 
program
  .version(pkg.version)
  .usage('[options]')
  .option('-p, --pattern [pattern]', 'Files pattern to match or just the file')
  .option('-f, --from [path,..]', 'From path')
  .option('-t, --to [path,..]', 'To path (number of replacements must match --to values)')
  .option('-m, --merge', 'Merge into target')
  .option('-r, --remove [path,..]', 'Remove property')
  .option('-c, --dot', 'Convert object to dotted-key/value pair')
  .option('-s, --show', 'show all dotted paths')
  .option('-v, --verbose', 'Be verbose')
  .option('-d, --dry', 'Dry run do not modify files')
  .parse(process.argv);
 
function must(program, option) {
  if(!program.hasOwnProperty(option)) {
    console.log([
      'The', option, 'is required'
    ].join(' '));
    process.exit(1);
  }
}
 
must(program, 'pattern');
 
if (!program.remove && !program.dot && !program.show) {
  must(program, 'from');
  must(program, 'to');
}
 
var g = glob(program.pattern);
 
function finish(program, file, orig, json) {
 
  return function(err) {
    if (err) {
      throw err;
    } else {
      if (program.verbose) {
        if (orig !== JSON.stringify(json)) {
          console.log(file + ': updated.');
        } else {
          console.log(file + ': no matches.');
        }
      }
    }
  };
}
 
function splim(path) {
  return path.split(',')
    .map(function(val) { return val.trim();  });
}
 
function processFile(file) {
 
  fs.readFile(file, 'utf8', function(err, contents) {
    var json;
 
    if (err) {
      console.log(err);
      return;
    }
 
    try {
      json = JSON.parse(contents);
 
      if(program.show) {
        json = console.log(Object.keys(dotob.dot(json)).join('\n'));
        process.exit()
      } else if(program.dot) {
        console.log(dotob.dot(json));
        process.exit()
      }
      
      json = Array.isArray(json) ? json : [json];
      
      if(program.remove) {
        // support comma seperate list of removals
        splim(program.remove)
          .forEach(function(path) {
            for (var j = 0; j < json.length; j++) {
              dotob.remove(path, json[j]);
            }
          });
      } else {
        var from = splim(program.from);
        var to = splim(program.to);
        if (from.length === to.length) {
          for (var i = 0; i < from.length; i++) {
            for (var j = 0; j < json.length; j++) {
              dotob.move(
                from[i], to[i], json[j], program.merge
              );
            }
          }
        } else {
          console.error('--from and --to parameters are not of equal length');
        }
      }
 
      if(program.dry) {
        console.log(json);
        finish(program, file, contents, json)();
      } else {
        fs.writeFile(file, JSON.stringify(json, null, 2), finish(
          program, file, contents, json
        ));
      }
    } catch (e) {
      console.log(file + ': ');
      throw(e);
    }
  });
}
 
g.on('match', processFile);