storybook/scripts/compile-babel.js

66 lines
1.3 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) {
const babel = path.join(__dirname, '..', 'node_modules', '.bin', 'babel');
2018-01-29 00:57:51 +02:00
2018-12-28 01:39:01 +01:00
const ignore = [
'**/__mocks__/',
'**/tests/',
'**/__tests__/',
'**/*.test.*',
2018-12-28 01:39:01 +01:00
'**/stories/',
'**/*.story.*',
'**/*.stories.*',
2018-12-28 01:39:01 +01:00
'**/__snapshots__',
'**/*.d.ts',
];
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
`--extensions ".js,.jsx,.ts,.tsx"`,
`--copy-files`,
`--ignore "${ignore.join('","')}"`,
];
if (watch) {
args.push('-w');
}
2018-01-29 00:57:51 +02:00
return `${babel} ${args.join(' ')}`;
}
2018-01-29 00:57:51 +02:00
function handleExit(code, errorCallback) {
if (code !== 0) {
2018-01-28 19:09:40 +02:00
if (errorCallback && typeof errorCallback === 'function') {
errorCallback();
}
shell.exit(code);
}
}
2018-01-29 00:57:51 +02:00
function babelify(options = {}) {
const { watch = false, silent = false, 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);
const { code } = shell.exec(command, { silent });
handleExit(code, errorCallback);
}
module.exports = {
babelify,
};