-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·88 lines (82 loc) · 2.86 KB
/
index.js
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
#! /usr/bin/env node
'use strict';
const readline = require('readline');
const fs = require('fs');
const recursiveReaddir = require('recursive-readdir');
//const program = require('commander');
const cli = require('cli');
const properties = require('properties');
const options = cli.parse({
file: ['F', 'The properties file', 'file'],
baseDir: ['D', 'The base directory to search', 'file'],
extraProps: ['p', 'Comma-separated list of any extra properties files used for finding undefined properties',
'string'],
ignoreUndefined: ['i', 'Ignore undefined properties']
});
const foundKeys = [];
const suspectedUndefinedKeys = [];
const propertiesPaths = options.extraProps && options.extraProps.split(',') || [];
let propertiesString = '';
for (let i = 0; i < propertiesPaths.length; i++) {
propertiesString += fs.readFileSync(propertiesPaths[i], 'utf8');
propertiesString += '\n';
}
properties.parse(options.file, {path: true}, (error, obj) => {
properties.parse(propertiesString, (otherError, otherObj) => {
if (error) {
cli.error(error);
}
else {
const keys = Object.keys(obj);
const done = function() {
if (keys.length === 0) {
console.info('All keys are in use!');
}
else {
console.log('Found ' + keys.length + ' unused keys:');
console.info(keys);
}
if (suspectedUndefinedKeys.length > 0 && !options.ignoreUndefined) {
console.log('Found ' + suspectedUndefinedKeys.length +
' suspected keys that aren\'t defined in the properties file');
console.log(suspectedUndefinedKeys);
}
};
recursiveReaddir(options.baseDir, ['.git', 'gen', 'node_modules', '*.properties'], (error, files) => {
if (!error) {
for (let i = 0; i < files.length; i++) {
if (keys.length === 0) {
done();
}
const lineReader = readline.createInterface({
input: fs.createReadStream(files[i])
});
lineReader.on('line', (line) => {
for (let j = keys.length - 1; j >= 0; j--) {
const key = keys[j];
let match;
if (key && line.match(new RegExp('[\'"]' + key + '[\'"]'))) {
keys.splice(j, 1);
}
else if (match = line.match(/['"](rw\..+?)["']/)) {
const property = match[1];
if (suspectedUndefinedKeys.indexOf(property) < 0 && !obj[property] && !otherObj[property]) {
suspectedUndefinedKeys.push(property);
}
}
}
});
lineReader.on('close', () => {
if (i === files.length - 1) {
done();
}
})
}
}
else {
cli.error(error);
}
});
}
});
});