File size: 3,048 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fs = require( 'fs' );
const path = require( 'path' );
const debug = require( 'debug' )( 'i18n-calypso' );
const Xgettext = require( 'xgettext-js' );
const formatters = require( './formatters' );
const preProcessXGettextJSMatch = require( './preprocess-xgettextjs-match.js' );

module.exports = function i18nCalypso( config ) {
	const keywords = config.keywords || [ 'translate' ];

	if ( ! config.data && ! config.inputPaths ) {
		throw new Error( 'Must provide input `data` or `inputPaths`' );
	}

	let parserKeywords = config.parserKeywords || {};

	if ( keywords ) {
		parserKeywords = keywords.reduce( function ( output, currentKeyword ) {
			output[ currentKeyword ] = preProcessXGettextJSMatch;
			return output;
		}, parserKeywords );
	}

	const parser = new Xgettext( {
		keywords: parserKeywords,
		parseOptions: {
			plugins: [
				'asyncFunctions',
				'classProperties',
				'dynamicImport',
				'exportDefaultFrom',
				'exportExtensions',
				'exportNamespaceFrom',
				'jsx',
				'objectRestSpread',
				'trailingFunctionCommas',
				'typescript',
				'nullishCoalescingOperator',
				'optionalChaining',
			],
			allowImportExportEverywhere: true,
		},
	} );

	function getFileMatches( inputFiles ) {
		return inputFiles.map( ( inputFile ) => {
			debug( 'Parsing inputFile: ' + inputFile );
			const relativeInputFilePath = path.relative( __dirname, inputFile ).replace( /^[/.]+/, '' );
			return parser.getMatches( fs.readFileSync( inputFile, 'utf8' ) ).map( ( match ) => {
				match.line = relativeInputFilePath + ':' + match.line;
				return match;
			} );
		} );
	}

	let matches;
	if ( config.data ) {
		// If data is provided, feed it directly to the parser and call the file <unknown>
		matches = [
			parser.getMatches( config.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 ) {
			const line = match.line.split( ':' );
			return (
				'undefined' !== typeof config.lines[ line[ 0 ] ] &&
				-1 !== config.lines[ line[ 0 ] ].indexOf( line[ 1 ] )
			);
		} );
	}

	let formatter = ( config.format || 'pot' ).toLowerCase();

	if ( 'string' === typeof formatter ) {
		formatter = formatters[ formatter ];
	}

	if ( ! formatter ) {
		throw new Error( 'Formatter not found : ' + config.formatter );
	}

	const textOutput = formatter( matches, config );

	if ( config.output ) {
		fs.writeFileSync( config.output, textOutput, 'utf8' );
	}

	return textOutput;
};