forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile-babel.js
68 lines (56 loc) · 1.55 KB
/
compile-babel.js
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
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
function getCommand(watch) {
// Compile angular with tsc
if (process.cwd().includes(path.join('app', 'angular'))) {
return '';
}
const babel = path.join(__dirname, '..', 'node_modules', '.bin', 'babel');
const args = [
'./src',
'--out-dir ./dist',
`--config-file ${path.resolve(__dirname, '../.babelrc.js')}`,
`--copy-files`,
];
/*
* angular needs to be compiled with tsc; a compilation with babel is possible but throws
* runtime errors because of the the babel decorators plugin
* Only transpile .js and let tsc do the job for .ts files
*/
if (process.cwd().includes(path.join('addons', 'storyshots'))) {
args.push(`--extensions ".js"`);
} else {
args.push(`--extensions ".js,.jsx,.ts,.tsx"`);
}
if (watch) {
args.push('-w');
}
return `${babel} ${args.join(' ')}`;
}
function handleExit(code, stderr, errorCallback) {
if (code !== 0) {
if (errorCallback && typeof errorCallback === 'function') {
errorCallback(stderr);
}
shell.exit(code);
}
}
function babelify(options = {}) {
const { watch = false, silent = true, errorCallback } = options;
if (!fs.existsSync('src')) {
if (!silent) {
console.log('No src dir');
}
return;
}
const command = getCommand(watch);
if (command !== '') {
const { code, stderr } = shell.exec(command, { silent });
handleExit(code, stderr, errorCallback);
}
}
module.exports = {
babelify,
};