This repository has been archived by the owner on Feb 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
109 lines (92 loc) · 2.9 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Module dependencies/
*/
var fs = require( 'fs' ),
path = require( 'path' ),
Xgettext = require( 'xgettext-js' ),
preProcessXGettextJSMatch = require( './preprocess-xgettextjs-match.js' ),
formatters = require( './formatters' ),
debug = require( 'debug' )( 'glotpress-js' );
module.exports = function( config ) {
var keywords,
data,
matches,
parser,
parserKeywords,
formatter,
textOutput;
keywords = config.keywords || [ 'translate' ];
formatter = ( config.format || 'pot' ).toLowerCase();
if ( ! config.data && ! config.inputPaths ) {
throw new Error( 'Must provide input `data` or `inputPaths`' );
}
parserKeywords = config.parserKeywords || {};
if ( keywords ) {
parserKeywords = keywords.reduce( function( output, currentKeyword ) {
output[ currentKeyword ] = preProcessXGettextJSMatch;
return output;
}, parserKeywords );
}
parser = new Xgettext( {
keywords: parserKeywords,
parseOptions: {
plugins: [
'asyncFunctions',
'classProperties',
'dynamicImport',
'exportDefaultFrom',
'exportExtensions',
'exportNamespaceFrom',
'jsx',
'objectRestSpread',
'trailingFunctionCommas'
],
allowImportExportEverywhere: true
}
} );
function getFileMatches( inputFiles ) {
return inputFiles.map( function( inputFile ) {
console.log( 'Parsing inputFile: ' + inputFile );
var relativeInputFilePath = path.relative( __dirname, inputFile ).replace( /^[\/.]+/, '' );
return parser.getMatches( fs.readFileSync( inputFile, 'utf8' ) ).map( function( match ) {
match.line = relativeInputFilePath + ':' + match.line;
return match;
});
} );
}
if ( config.data ) {
// If data is provided, feed it directly to the parser and call the file <unknown>
matches = [ parser.getMatches( data ).map( function( match ) {
match.location = '<unknown>:' + match.line;
return match;
}) ];
} else {
matches = getFileMatches( config.inputPaths, config.lines );
}
if ( config.extras ) {
matches = matches.concat( getFileMatches( config.extras.map( function( extra ) {
return path.join( __dirname, 'extras', extra + '.js' );
} ) ) );
}
// The matches array now contains the entries for each file in it's own array:
// [ [ 'file1 match1', 'file1 match2' ], [ 'file2 match1' ] ]
// Flatten array, so that it has all entries in just one level.
matches = [].concat.apply( [], matches );
if ( config.lines ) {
matches = matches.filter( function( match ) {
var line = match.line.split(':');
return ( 'undefined' != typeof config.lines[ line[0] ] && -1 != config.lines[ line[0] ].indexOf( line[1] ) )
});
}
if ( 'string' === typeof formatter ) {
formatter = formatters[ formatter ];
}
if ( ! formatter ) {
throw new Error( 'Formatter not found : ' + config.formatter );
}
textOutput = formatter( matches, config );
if ( config.output ) {
fs.writeFileSync( config.output, textOutput, 'utf8' );
}
return textOutput;
};