File size: 2,931 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
#!/usr/bin/env node

const fs = require( 'fs' );
const path = require( 'path' );
const program = require( 'commander' );
const globby = require( 'globby' );
const i18n = require( '.' );

function collect( val, memo ) {
	memo.push( val );
	return memo;
}

function list( val ) {
	return val.split( ',' );
}

program
	.version( '0.0.1' )
	.option( '-k, --keywords <keyword,keyword>', 'keywords of the translate function', list )
	.option( '-f, --format <format>', 'format of the output (php or pot)' )
	.option( '-o, --output-file <file>', 'output file for WP-style translation functions' )
	.option(
		'-i, --input-file <filename>',
		'files in which to search for translation methods',
		collect,
		[]
	)
	.option( '-p, --project-name <name>', 'name of the project' )
	.option(
		'-e, --extra <name>',
		'Extra type of strings to add to the generated file (for now only `date` is available)'
	)
	.option(
		'-l, --lines-filter <file>',
		'Json file containing files and line numbers filters. Only included line numbers will be pased.'
	)
	.option(
		'-a, --array-name <name>',
		'name of variable in generated php file that contains array of method calls'
	)
	.usage( '-o outputFile -i inputFile -f format [inputFile ...]' )
	.on( '--help', function () {
		console.log( '  Examples' );
		console.log( '\n    $ i18n-calypso -o ./outputFile.pot -i ./inputFile.js -i ./inputFile2.js' );
		console.log( '' );
	} )
	.parse( process.argv );

const keywords = program.keywords;
const format = program.format;
const outputFile = program.outputFile;
const arrayName = program.arrayName;
const projectName = program.projectName;
const linesFile = program.linesFilter;
/* eslint-disable-next-line no-nested-ternary */
const extras = Array.isArray( program.extra )
	? program.extra
	: program.extra
	? [ program.extra ]
	: null;
const inputFiles = program.inputFile.length ? program.inputFile : program.args;

if ( inputFiles.length === 0 ) {
	throw new Error( 'Error: You must enter the input file. Run `i18n-calypso -h` for examples.' );
}

const inputPaths = globby.sync( inputFiles );

inputPaths.forEach( function ( inputFile ) {
	if ( ! fs.existsSync( inputFile ) ) {
		console.error( 'Error: inputFile, `' + inputFile + '`, does not exist' );
	}
} );

let lines;
if ( linesFile ) {
	if ( ! fs.existsSync( linesFile ) ) {
		console.error( 'Error: linesFile, `' + linesFile + '`, does not exist' );
	}

	lines = JSON.parse( fs.readFileSync( linesFile, 'utf8' ) );
	for ( const line in lines ) {
		lines[ line ] = lines[ line ].map( String );
		const modPath = path.relative( __dirname, line ).replace( /^[/.]+/, '' );
		if ( modPath !== line ) {
			lines[ modPath ] = lines[ line ];
			delete lines[ line ];
		}
	}
}

const result = i18n( {
	keywords,
	output: outputFile,
	phpArrayName: arrayName,
	inputPaths,
	format,
	extras,
	lines,
	projectName,
} );

if ( outputFile ) {
	console.log( 'Done.' );
} else {
	console.log( result );
}