1
+ #!/usr/bin/env node
2
+ /* js-codepage (C) 2014-present SheetJS -- http://sheetjs.com */
3
+ /* vim: set ts=2 ft=javascript: */
4
+ /* eslint-env node */
5
+ var codepage = require ( 'codepage' ) ;
6
+ require ( 'exit-on-epipe' ) ;
7
+ var fs = require ( 'fs' ) , program /*:any*/ = ( require ( 'commander' ) /*:any*/ ) ;
8
+ function run ( ) {
9
+ program
10
+ . version ( codepage . version )
11
+ . usage ( '[options] <file>' )
12
+ . option ( '-f, --from-code <code>' , 'codepage of input (default 65001 utf8)' )
13
+ . option ( '-t, --to-code <code>' , 'codepage of output (default 65001 utf8)' )
14
+ . option ( '-o, --output <file>' , 'output file (<file>.<to> if specified)' )
15
+ . option ( '-B, --bom' , 'write BOM (for unicode codepages)' )
16
+ . option ( '-F, --force' , 'force writing to stdout for non-utf8 codepages' )
17
+ . option ( '-l, --list' , 'List supported codepages' ) ;
18
+
19
+ program . on ( '--help' , function ( ) {
20
+ console . log ( ' Codepage descriptions can be found in the README' ) ;
21
+ console . log ( ' http://oss.sheetjs.com/js-codepage/README.md' ) ;
22
+ console . log ( ' Support email: dev.codepage@sheetjs.com' ) ;
23
+ } ) ;
24
+
25
+ program . parse ( process . argv ) ;
26
+
27
+ if ( program . list ) {
28
+ var l /*:Array<number>*/ = [ ] ;
29
+ Object . keys ( codepage ) . forEach ( function ( x ) { if ( parseInt ( x , 10 ) == + x ) l . push ( + x ) ; } ) ;
30
+ Object . keys ( codepage . utils . magic ) . forEach ( function ( x ) { if ( parseInt ( x , 10 ) == + x && + x != 16969 ) l . push ( + x ) ; } ) ;
31
+ l . sort ( function ( a , b ) { return a - b ; } ) . forEach ( function ( x ) { console . log ( x ) ; } ) ;
32
+ process . exit ( ) ;
33
+ }
34
+
35
+ var fr = + program . fromCode || 65001 ;
36
+ var to = + program . toCode || 65001 ;
37
+ var f = program . args [ 0 ] ;
38
+ var o = program . output ;
39
+
40
+ if ( ! process . stdin . isTTY ) f = f || "-" ;
41
+
42
+ if ( f !== "-" && ! fs . existsSync ( f ) ) {
43
+ console . error ( 'codepage: must specify a filename' ) ;
44
+ process . exit ( 13 ) ;
45
+ }
46
+
47
+ function concat ( func ) {
48
+ // $FlowIgnore
49
+ var writable = require ( 'stream' ) . Writable ( ) ;
50
+ var buf = [ ] ;
51
+ writable . _write = function ( chunk , e , cb ) { buf . push ( chunk ) ; cb ( ) ; } ;
52
+ writable . _writev = function ( chunks , cb ) { chunks . forEach ( function ( c ) { buf . push ( c . chunk ) ; cb ( ) ; } ) ; } ;
53
+ writable . on ( 'finish' , function ( ) { func ( Buffer . concat ( buf ) ) ; } ) ;
54
+ return writable ;
55
+ }
56
+
57
+ if ( f === "-" ) process . stdin . pipe ( concat ( process_text ) ) ;
58
+ else process_text ( fs . readFileSync ( f ) ) ;
59
+
60
+ function process_text ( text /*:Buffer*/ ) {
61
+ var dec /*:Buffer*/ = ( codepage . utils . decode ( fr , text ) /*:any*/ ) ;
62
+
63
+ var bom /*:Array<Buffer>*/ = [ ] ;
64
+ bom [ 1200 ] = new Buffer ( [ 0xFF , 0xFE ] ) ;
65
+ bom [ 1201 ] = new Buffer ( [ 0xFE , 0xFF ] ) ;
66
+ bom [ 12000 ] = new Buffer ( [ 0xFF , 0xFE , 0x00 , 0x00 ] ) ;
67
+ bom [ 12001 ] = new Buffer ( [ 0x00 , 0x00 , 0xFE , 0xFF ] ) ;
68
+ bom [ 16969 ] = new Buffer ( [ 0x69 , 0x69 ] ) ;
69
+ bom [ 65000 ] = new Buffer ( [ 0x2B , 0x2F , 0x76 , 0x2B ] ) ;
70
+ bom [ 65001 ] = new Buffer ( [ 0xEF , 0xBB , 0xBF ] ) ;
71
+
72
+ var mybom = ( program . bom && bom [ to ] ? bom [ to ] : "" ) ;
73
+ var out /*:any*/ = to === 65001 ? dec . toString ( 'utf8' ) : codepage . utils . encode ( to , dec ) ;
74
+
75
+ /* if output file is specified */
76
+ if ( o ) writefile ( o , out , mybom ) ;
77
+ /* utf8 -> print to stdout */
78
+ else if ( to === 65001 ) logit ( out , mybom ) ;
79
+ /* stdout piped to process -> print */
80
+ else if ( ! process . stdout . isTTY ) logit ( out , mybom ) ;
81
+ /* forced */
82
+ else if ( program . force ) logit ( out , mybom ) ;
83
+ /* input file specified -> write to file */
84
+ else if ( f !== "-" ) writefile ( f + "." + to , out , mybom ) ;
85
+ else {
86
+ console . error ( 'codepage: use force (-F, --force) to print ' + to + ' codes' ) ;
87
+ process . exit ( 14 ) ;
88
+ }
89
+ }
90
+
91
+ function logit ( out /*:Buffer*/ , bom ) {
92
+ process . stdout . write ( bom ) ;
93
+ process . stdout . write ( out ) ;
94
+ }
95
+
96
+ function writefile ( o , out /*:Buffer*/ , bom ) {
97
+ fs . writeFileSync ( o , bom ) ;
98
+ fs . appendFileSync ( o , out ) ;
99
+ }
100
+ }
101
+
102
+ module . exports = run ;
0 commit comments