-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
cli.js
executable file
·151 lines (119 loc) · 4.15 KB
/
cli.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
var colors = require('colors');
var program = require('commander');
var fs = require('fs');
var path = require('path');
var Readable = require('stream').Readable;
var readdirp = require('readdirp');
var through = require('through2');
var File = require('vinyl');
var mkdirp = require('mkdirp');
var Parser = require('../index');
// Configure the command line
// ==========================
program
.version('0.5.0')
.option( '-r, --recursive' , 'Parse sub directories' )
.option( '-p, --parser <string>' , 'A custom regex to use to parse your code' )
.option( '-o, --output <directory>' , 'The directory to output parsed keys' )
.option( '-f, --functions <list>' , 'The function names to parse in your code' )
.option( '-n, --namespace <string>' , 'The default namespace (translation by default)' )
.option( '-s, --namespace-separator <string>' , 'The default namespace separator(: by default)' )
.option( '-k, --key-separator <string>' , 'The default key separator(. by default)' )
.option( '-l, --locales <list>' , 'The locales in your application' )
.option( '--directoryFilter <list>' , 'Filter directories' )
.option( '--fileFilter <list>' , 'Filter files' )
.option( '--keep-removed' , 'Prevent keys no longer found from being removed' )
.option( '--write-old <string>' , 'Save (or don\'t if false) _old files' )
.parse( process.argv );
// Define the target directory
// ===========================
var firstArgument = process.argv[2];
var input;
var output;
if ( firstArgument && firstArgument.charAt(0) !== '-' ) {
var parts = firstArgument.split(':');
if ( parts.length > 1 ) {
input = path.resolve(process.cwd(), parts[0]);
output = path.resolve(process.cwd(), parts[1]);
}
else {
input = path.resolve(process.cwd(), firstArgument);
}
}
else {
input = process.cwd();
}
output = output || program.output || 'locales';
if ( ! fs.existsSync(input) ) {
console.log( "\n" + "Error: ".red + input + " is not a file or directory\n" );
process.exit( 1 );
}
// Parse passed values
// ===================
program.locales = program.locales && program.locales.split(',');
program.functions = program.functions && program.functions.split(',');
program.writeOld = program.writeOld !== 'false';
program.directoryFilter = program.directoryFilter && program.directoryFilter.split(',');
program.fileFilter = program.fileFilter && program.fileFilter.split(',');
program.output = path.resolve(process.cwd(), output);
// Welcome message
// ===============
var intro = "\n"+
"i18next Parser".yellow + "\n" +
"--------------".yellow + "\n" +
"Input: ".green + input + "\n" +
"Output: ".green + program.output + "\n\n";
console.log(intro);
// Create a stream from the input
// ==============================
var stat = fs.statSync(input);
var stream;
if ( stat.isDirectory() ) {
var args = { root: input };
if( program.directoryFilter ) {
args.directoryFilter = program.directoryFilter;
}
if( program.fileFilter ) {
args.fileFilter = program.fileFilter;
}
if ( ! program.recursive) {
args.depth = 0;
}
stream = readdirp( args );
}
else {
stream = new Readable( { objectMode: true } );
var file = new File({
path: input,
stat: stat
});
stream._read = function() {
stream.push( file );
stream.push( null );
};
}
// Parse the stream
// ================
var parser = Parser(program);
stream
.pipe(through( { objectMode: true }, function (data, encoding, done) {
if ( data instanceof File ) {
this.push( data );
}
else if ( data.fullPath ) {
var file = new File({
path: data.fullPath,
stat: data.stat
});
this.push( file );
}
done();
}))
.pipe(parser.on('reading', function(path) { console.log("[parse] ".green + path) }))
.pipe(through( { objectMode: true }, function (file, encoding, done) {
mkdirp.sync( path.dirname(file.path) );
fs.writeFileSync( file.path, file.contents );
this.push( file );
done();
}));