2018-01-28 19:17:13 +02:00
|
|
|
/* eslint-disable no-console */
|
2022-01-26 14:12:34 +01:00
|
|
|
const fs = require('fs-extra');
|
2018-01-28 18:54:00 +02:00
|
|
|
const path = require('path');
|
2022-07-21 15:42:01 +02:00
|
|
|
const { join } = require('path');
|
2018-01-28 18:54:00 +02:00
|
|
|
|
2020-11-26 10:50:46 +01:00
|
|
|
function getCommand(watch, dir) {
|
2019-07-22 06:33:57 +00:00
|
|
|
// Compile angular with tsc
|
2022-05-12 09:31:48 +08:00
|
|
|
if (process.cwd().includes(path.join('frameworks', 'angular'))) {
|
2019-05-20 14:05:15 +02:00
|
|
|
return '';
|
|
|
|
}
|
2020-12-09 09:42:03 +01:00
|
|
|
if (process.cwd().includes(path.join('addons', 'storyshots'))) {
|
|
|
|
return '';
|
|
|
|
}
|
2019-05-20 14:05:15 +02:00
|
|
|
|
2019-03-23 23:09:01 +01:00
|
|
|
const args = [
|
2022-07-23 17:15:13 +02:00
|
|
|
join(process.cwd(), 'src'),
|
2022-01-26 14:12:34 +01:00
|
|
|
`--out-dir=${dir}`,
|
2022-07-21 15:42:01 +02:00
|
|
|
`--config-file=${join(__dirname, '..', '.babelrc.js')}`,
|
2018-01-28 18:54:00 +02:00
|
|
|
];
|
|
|
|
|
2022-01-26 14:12:34 +01:00
|
|
|
// babel copying over files it did not parse is a anti-pattern
|
|
|
|
// but in the case of the CLI, it houses generators are are templates
|
|
|
|
// moving all these is a lot of work. We should make different choices when we eventually refactor / rebuild the CLI
|
|
|
|
if (process.cwd().includes(path.join('lib', 'cli'))) {
|
|
|
|
args.push('--copy-files');
|
|
|
|
}
|
|
|
|
|
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'))) {
|
2022-01-26 14:12:34 +01:00
|
|
|
args.push(`--extensions=".js"`);
|
2019-03-24 00:13:41 +01:00
|
|
|
} else {
|
2022-01-26 14:12:34 +01:00
|
|
|
args.push(`--extensions=.js,.jsx,.ts,.tsx`);
|
2019-03-24 00:13:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
if (watch) {
|
|
|
|
args.push('-w');
|
|
|
|
}
|
|
|
|
|
2021-04-23 18:04:48 +02:00
|
|
|
return `yarn run -T babel ${args.join(' ')}`;
|
2018-01-29 00:57:51 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-01-26 14:12:34 +01:00
|
|
|
process.exit(code);
|
2018-01-28 18:54:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 10:50:46 +01:00
|
|
|
async function run({ watch, dir, silent, errorCallback }) {
|
2022-11-09 14:27:19 +11:00
|
|
|
const execa = await import('execa');
|
|
|
|
|
2020-11-26 10:50:46 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const command = getCommand(watch, dir);
|
|
|
|
|
|
|
|
if (command !== '') {
|
2022-11-09 14:27:19 +11:00
|
|
|
const child = execa.execaCommand(command, {
|
2022-07-21 15:42:01 +02:00
|
|
|
cwd: join(__dirname, '..'),
|
2022-01-26 14:12:34 +01:00
|
|
|
buffer: false,
|
|
|
|
env: { BABEL_MODE: path.basename(dir) },
|
2020-11-26 10:50:46 +01:00
|
|
|
});
|
|
|
|
|
2022-01-26 14:12:34 +01:00
|
|
|
let stderr = '';
|
2020-11-26 10:50:46 +01:00
|
|
|
|
2022-01-26 14:12:34 +01:00
|
|
|
if (watch) {
|
|
|
|
child.stdout.pipe(process.stdout);
|
|
|
|
child.stderr.pipe(process.stderr);
|
|
|
|
} else {
|
|
|
|
child.stderr.on('data', (data) => {
|
|
|
|
stderr += data.toString();
|
|
|
|
});
|
|
|
|
child.stdout.on('data', (data) => {
|
|
|
|
stderr += data.toString();
|
|
|
|
});
|
|
|
|
}
|
2020-11-26 10:50:46 +01:00
|
|
|
|
|
|
|
child.on('exit', (code) => {
|
|
|
|
resolve();
|
|
|
|
handleExit(code, stderr, errorCallback);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function babelify(options = {}) {
|
2021-05-17 18:09:33 +08:00
|
|
|
const { watch = false, silent = true, errorCallback } = options;
|
2018-01-29 00:57:51 +02:00
|
|
|
|
2022-01-26 14:12:34 +01:00
|
|
|
if (!(await fs.pathExists('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;
|
|
|
|
}
|
|
|
|
|
2022-04-21 15:52:53 +02:00
|
|
|
const runners = [
|
2022-07-21 15:42:01 +02:00
|
|
|
run({ watch, dir: join(process.cwd(), 'dist/cjs'), silent, errorCallback }),
|
|
|
|
run({ watch, dir: join(process.cwd(), 'dist/esm'), silent, errorCallback }),
|
2022-04-21 15:52:53 +02:00
|
|
|
];
|
2021-05-17 18:09:33 +08:00
|
|
|
|
|
|
|
await Promise.all(runners);
|
2018-01-29 00:57:51 +02:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
module.exports = {
|
|
|
|
babelify,
|
|
|
|
};
|