1 | var fs = require('fs');
|
2 | var path = require('path');
|
3 | var buble = require('..');
|
4 | var handleError = require('./handleError.js');
|
5 | var EOL = require('os').EOL;
|
6 |
|
7 | function compile(from, to, command, options) {
|
8 | try {
|
9 | var stats = fs.statSync(from);
|
10 | if (stats.isDirectory()) {
|
11 | compileDir(from, to, command, options);
|
12 | } else {
|
13 | compileFile(from, to, command, options);
|
14 | }
|
15 | } catch (err) {
|
16 | handleError(err);
|
17 | }
|
18 | }
|
19 |
|
20 | function compileDir(from, to, command, options) {
|
21 | if (!command.output) handleError({ code: 'MISSING_OUTPUT_DIR' });
|
22 |
|
23 | try {
|
24 | fs.mkdirSync(to);
|
25 | } catch (e) {
|
26 | if (e.code !== 'EEXIST') throw e;
|
27 | }
|
28 |
|
29 | fs.readdirSync(from).forEach(function(file) {
|
30 | compile(path.resolve(from, file), path.resolve(to, file), command, options);
|
31 | });
|
32 | }
|
33 |
|
34 | function compileFile(from, to, command, options) {
|
35 | var ext = path.extname(from);
|
36 |
|
37 | if (ext !== '.js' && ext !== '.jsm' && ext !== '.es6' && ext !== '.jsx')
|
38 | return;
|
39 |
|
40 | if (to) {
|
41 | var extTo = path.extname(to);
|
42 | to = to.slice(0, -extTo.length) + '.js';
|
43 | }
|
44 |
|
45 | var source = fs.readFileSync(from, 'utf-8');
|
46 | var result = buble.transform(source, {
|
47 | target: options.target,
|
48 | transforms: options.transforms,
|
49 | source: from,
|
50 | file: to,
|
51 | jsx: options.jsx,
|
52 | objectAssign: options.objectAssign,
|
53 | namedFunctionExpressions: options.namedFunctionExpressions
|
54 | });
|
55 |
|
56 | write(result, to, command);
|
57 | }
|
58 |
|
59 | function write(result, to, command) {
|
60 | if (command.sourcemap === 'inline') {
|
61 | result.code += EOL + '//# sourceMappingURL=' + result.map.toUrl();
|
62 | } else if (command.sourcemap) {
|
63 | if (!to) {
|
64 | handleError({ code: 'MISSING_OUTPUT_FILE' });
|
65 | }
|
66 |
|
67 | result.code += EOL + '//# sourceMappingURL=' + path.basename(to) + '.map';
|
68 | fs.writeFileSync(to + '.map', result.map.toString());
|
69 | }
|
70 |
|
71 | if (to) {
|
72 | fs.writeFileSync(to, result.code);
|
73 | } else {
|
74 | console.log(result.code);
|
75 | }
|
76 | }
|
77 |
|
78 | module.exports = function(command) {
|
79 | if (command._.length > 1) {
|
80 | handleError({ code: 'ONE_AT_A_TIME' });
|
81 | }
|
82 |
|
83 | if (command._.length === 1) {
|
84 | if (command.input) {
|
85 | handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' });
|
86 | }
|
87 |
|
88 | command.input = command._[0];
|
89 | }
|
90 |
|
91 | var options = {
|
92 | target: {},
|
93 | transforms: {},
|
94 | jsx: command.jsx,
|
95 | objectAssign: command.objectAssign,
|
96 | namedFunctionExpressions: command['named-function-expr'] !== false
|
97 | };
|
98 |
|
99 | if (command.target) {
|
100 | if (!/^(?:(\w+):([\d.]+),)*(\w+):([\d.]+)$/.test(command.target)) {
|
101 | handleError({ code: 'BAD_TARGET' });
|
102 | }
|
103 |
|
104 | command.target
|
105 | .split(',')
|
106 | .map(function(target) {
|
107 | return target.split(':');
|
108 | })
|
109 | .forEach(function(pair) {
|
110 | options.target[pair[0]] = pair[1];
|
111 | });
|
112 | }
|
113 |
|
114 | if (command.yes) {
|
115 | command.yes.split(',').forEach(function(transform) {
|
116 | options.transforms[transform] = true;
|
117 | });
|
118 | }
|
119 |
|
120 | if (command.no) {
|
121 | command.no.split(',').forEach(function(transform) {
|
122 | options.transforms[transform] = false;
|
123 | });
|
124 | }
|
125 |
|
126 | if (command.input) {
|
127 | compile(command.input, command.output, command, options);
|
128 | } else {
|
129 | process.stdin.resume();
|
130 | process.stdin.setEncoding('utf8');
|
131 |
|
132 | var source = '';
|
133 |
|
134 | process.stdin.on('data', function(chunk) {
|
135 | source += chunk;
|
136 | });
|
137 |
|
138 | process.stdin.on('end', function() {
|
139 | options.source = command.input = 'stdin';
|
140 | options.file = command.output;
|
141 | try {
|
142 | var result = buble.transform(source, options);
|
143 | write(result, command.output, command);
|
144 | } catch (err) {
|
145 | handleError(err);
|
146 | }
|
147 | });
|
148 | }
|
149 | };
|