2018-01-28 19:17:13 +02:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
const fs = require('fs');
|
2018-01-28 18:54:00 +02:00
|
|
|
const path = require('path');
|
|
|
|
const shell = require('shelljs');
|
|
|
|
|
2018-01-29 00:57:51 +02:00
|
|
|
function getCommand(watch) {
|
2018-01-28 18:54:00 +02:00
|
|
|
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__/',
|
2019-03-19 17:43:12 +01:00
|
|
|
'**/*.test.*',
|
2018-12-28 01:39:01 +01:00
|
|
|
'**/stories/',
|
2019-03-19 17:43:12 +01:00
|
|
|
'**/*.story.*',
|
|
|
|
'**/*.stories.*',
|
2018-12-28 01:39:01 +01:00
|
|
|
'**/__snapshots__',
|
|
|
|
'**/*.d.ts',
|
|
|
|
];
|
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
const args = [
|
2018-12-28 01:39:01 +01:00
|
|
|
'./src',
|
|
|
|
'--out-dir ./dist',
|
2018-11-19 17:21:53 +01:00
|
|
|
`--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('","')}"`,
|
2018-01-28 18:54:00 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if (watch) {
|
|
|
|
args.push('-w');
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:57:51 +02:00
|
|
|
return `${babel} ${args.join(' ')}`;
|
|
|
|
}
|
2018-01-28 18:54:00 +02:00
|
|
|
|
2018-01-29 00:57:51 +02:00
|
|
|
function handleExit(code, errorCallback) {
|
2018-01-28 18:54:00 +02:00
|
|
|
if (code !== 0) {
|
2018-01-28 19:09:40 +02:00
|
|
|
if (errorCallback && typeof errorCallback === 'function') {
|
|
|
|
errorCallback();
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
shell.exit(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 00:57:51 +02:00
|
|
|
function babelify(options = {}) {
|
2019-01-04 14:13:23 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
module.exports = {
|
|
|
|
babelify,
|
|
|
|
};
|