storybook/scripts/compile-babel.js

69 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-01-28 19:17:13 +02:00
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
2018-01-29 00:57:51 +02:00
function getCommand(watch) {
2019-07-22 06:33:57 +00:00
// Compile angular with tsc
2019-05-20 14:05:15 +02:00
if (process.cwd().includes(path.join('app', 'angular'))) {
return '';
}
const babel = path.join(__dirname, '..', 'node_modules', '.bin', 'babel');
2018-01-29 00:57:51 +02:00
2019-03-23 23:09:01 +01:00
const args = [
2018-12-28 01:39:01 +01:00
'./src',
'--out-dir ./dist',
`--config-file ${path.resolve(__dirname, '../.babelrc.js')}`,
2018-12-28 01:39:01 +01:00
`--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
*/
2019-05-20 14:05:15 +02:00
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');
}
2018-01-29 00:57:51 +02:00
return `${babel} ${args.join(' ')}`;
}
function handleExit(code, stderr, errorCallback) {
if (code !== 0) {
2018-01-28 19:09:40 +02:00
if (errorCallback && typeof errorCallback === 'function') {
errorCallback(stderr);
2018-01-28 19:09:40 +02:00
}
shell.exit(code);
}
}
2018-01-29 00:57:51 +02:00
function babelify(options = {}) {
2019-04-01 19:16:40 +02:00
const { watch = false, silent = true, errorCallback } = options;
2018-01-29 00:57:51 +02:00
if (!fs.existsSync('src')) {
2019-03-04 11:56:27 +01:00
if (!silent) {
console.log('No src dir');
}
2018-01-29 00:57:51 +02:00
return;
}
const command = getCommand(watch);
2019-05-20 14:05:15 +02:00
if (command !== '') {
const { code, stderr } = shell.exec(command, { silent });
handleExit(code, stderr, errorCallback);
}
2018-01-29 00:57:51 +02:00
}
module.exports = {
babelify,
};