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) {
|
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 '';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-03-23 23:09:01 +01: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
|
|
|
`--copy-files`,
|
2018-01-28 18:54:00 +02:00
|
|
|
];
|
|
|
|
|
2019-03-24 00:13:41 +01:00
|
|
|
/*
|
|
|
|
* 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'))) {
|
2019-03-24 00:13:41 +01:00
|
|
|
args.push(`--extensions ".js"`);
|
|
|
|
} else {
|
|
|
|
args.push(`--extensions ".js,.jsx,.ts,.tsx"`);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-04-04 13:30:35 +02:00
|
|
|
function handleExit(code, stderr, 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') {
|
2019-04-04 13:30:35 +02:00
|
|
|
errorCallback(stderr);
|
2018-01-28 19:09:40 +02:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:54:00 +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
|
|
|
}
|
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
module.exports = {
|
|
|
|
babelify,
|
|
|
|
};
|