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-01-28 18:54:00 +02:00
|
|
|
const args = [
|
2018-07-05 21:22:42 +03:00
|
|
|
'--ignore **/__mocks__/,**/tests/*,**/__tests__/,**/**.test.js,**/stories/,**/**.story.js,**/**.stories.js,**/__snapshots__',
|
2018-01-28 18:54:00 +02:00
|
|
|
'./src --out-dir ./dist',
|
2018-07-05 21:22:42 +03:00
|
|
|
'--copy-files',
|
2018-11-19 17:21:53 +01:00
|
|
|
`--config-file ${path.resolve(__dirname, '../.babelrc.js')}`,
|
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 = {}) {
|
|
|
|
const { watch = false, silent = true, errorCallback } = options;
|
|
|
|
|
|
|
|
if (!fs.existsSync('src')) {
|
|
|
|
if (!silent) console.log('No src dir');
|
|
|
|
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,
|
|
|
|
};
|