runBuble.js
3.06 KB
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
var fs = require( 'fs' );
var path = require( 'path' );
var buble = require( '../dist/buble.deps.js' );
var handleError = require( './handleError.js' );
var EOL = require('os').EOL;
function compile ( from, to, command, options ) {
try {
var stats = fs.statSync( from );
if ( stats.isDirectory() ) {
compileDir( from, to, command, options );
} else {
compileFile( from, to, command, options );
}
} catch ( err ) {
handleError( err );
}
}
function compileDir ( from, to, command, options ) {
if ( !command.output ) handleError({ code: 'MISSING_OUTPUT_DIR' });
try {
fs.mkdirSync( to )
} catch ( e ) {
if ( e.code !== 'EEXIST' ) throw e
}
fs.readdirSync( from ).forEach( function ( file ) {
compile( path.resolve( from, file ), path.resolve( to, file ), command, options );
});
}
function compileFile ( from, to, command, options ) {
var ext = path.extname( from );
if ( ext !== '.js' && ext !== '.jsm' && ext !== '.es6' ) return;
if ( to ) to = to.slice( 0, -ext.length ) + '.js';
var source = fs.readFileSync( from, 'utf-8' );
var result = buble.transform( source, {
target: options.target,
transforms: options.transforms,
source: from,
file: to,
jsx: options.jsx
});
write( result, to, command );
}
function write ( result, to, command ) {
if ( command.sourcemap === 'inline' ) {
result.code += EOL + '//# sourceMappingURL=' + result.map.toUrl();
} else if ( command.sourcemap ) {
if ( !to ) {
handleError({ code: 'MISSING_OUTPUT_FILE' });
}
result.code += EOL + '//# sourceMappingURL=' + path.basename( to ) + '.map';
fs.writeFileSync( to + '.map', result.map.toString() );
}
if ( to ) {
fs.writeFileSync( to, result.code );
} else {
console.log( result.code ); // eslint-disable-line no-console
}
}
module.exports = function ( command ) {
if ( command._.length > 1 ) {
handleError({ code: 'ONE_AT_A_TIME' });
}
if ( command._.length === 1 ) {
if ( command.input ) {
handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' });
}
command.input = command._[0];
}
var options = {
target: {},
transforms: {},
jsx: command.jsx
};
if ( command.target ) {
if ( !/^(?:(\w+):([\d\.]+),)*(\w+):([\d\.]+)$/.test( command.target ) ) {
handleError({ code: 'BAD_TARGET' });
}
command.target.split( ',' )
.map( function ( target ) {
return target.split( ':' );
})
.forEach( function ( pair ) {
options.target[ pair[0] ] = pair[1];
});
}
if ( command.yes ) {
command.yes.split( ',' ).forEach( function ( transform ) {
options.transforms[ transform ] = true;
});
}
if ( command.no ) {
command.no.split( ',' ).forEach( function ( transform ) {
options.transforms[ transform ] = false;
});
}
if ( command.input ) {
compile( command.input, command.output, command, options );
}
else {
process.stdin.resume();
process.stdin.setEncoding( 'utf8' );
var source = '';
process.stdin.on( 'data', function ( chunk ) {
source += chunk;
});
process.stdin.on( 'end', function () {
var result = buble.transform( source, options );
write( result, null, command );
});
}
};