2018-01-28 18:35:39 +02:00
|
|
|
/* eslint-disable no-console */
|
2019-04-04 13:30:35 +02:00
|
|
|
/* tslint:disable:no-console */
|
2017-04-16 18:11:44 +07:00
|
|
|
const path = require('path');
|
|
|
|
const shell = require('shelljs');
|
|
|
|
const chalk = require('chalk');
|
2019-01-04 14:13:23 +01:00
|
|
|
const fs = require('fs');
|
2018-01-28 19:09:40 +02:00
|
|
|
const log = require('npmlog');
|
2019-03-03 19:36:15 +01:00
|
|
|
const { babelify } = require('./compile-babel');
|
|
|
|
const { tscfy } = require('./compile-tsc');
|
2017-04-16 18:11:44 +07:00
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
function getPackageJson() {
|
|
|
|
const modulePath = path.resolve('./');
|
2017-05-18 09:14:29 +02:00
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
// eslint-disable-next-line global-require,import/no-dynamic-require
|
|
|
|
return require(path.join(modulePath, 'package.json'));
|
2018-01-27 15:42:33 +02:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
function removeDist() {
|
|
|
|
shell.rm('-rf', 'dist');
|
2018-01-27 15:42:33 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 12:38:58 +08:00
|
|
|
const ignore = [
|
|
|
|
'__mocks__',
|
|
|
|
'__snapshots__',
|
|
|
|
'__testfixtures__',
|
|
|
|
'__tests__',
|
|
|
|
'/tests/',
|
|
|
|
/.+\.test\..+/,
|
|
|
|
];
|
2019-07-09 13:23:30 +02:00
|
|
|
|
2018-12-28 01:39:01 +01:00
|
|
|
function cleanup() {
|
2019-07-09 13:23:30 +02:00
|
|
|
// remove files after babel --copy-files output
|
2018-01-28 20:11:01 +02:00
|
|
|
// --copy-files option doesn't work with --ignore
|
2019-07-09 13:23:30 +02:00
|
|
|
// https://github.com/babel/babel/issues/6226
|
2019-01-04 14:13:23 +01:00
|
|
|
if (fs.existsSync(path.join(process.cwd(), 'dist'))) {
|
2020-03-27 20:04:50 +01:00
|
|
|
const files = shell.find('dist').filter((filePath) => {
|
2020-02-10 20:24:46 +01:00
|
|
|
// Do not remove folder
|
|
|
|
// And do not clean anything for @storybook/cli/dist/generators/**/template* because these are the template files
|
|
|
|
// that will be copied to init SB on users' projects
|
|
|
|
if (fs.lstatSync(filePath).isDirectory() || /generators\/.+\/template.*/.test(filePath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-26 22:00:09 +01:00
|
|
|
// Remove all copied TS files (but not the .d.ts)
|
|
|
|
if (/\.tsx?$/.test(filePath) && !/\.d\.ts$/.test(filePath)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-10 20:24:46 +01:00
|
|
|
|
2019-07-09 13:23:30 +02:00
|
|
|
return ignore.reduce((acc, pattern) => {
|
|
|
|
return acc || !!filePath.match(pattern);
|
|
|
|
}, false);
|
|
|
|
});
|
2019-01-04 14:13:23 +01:00
|
|
|
if (files.length) {
|
2019-07-09 13:23:30 +02:00
|
|
|
shell.rm('-f', ...files);
|
2019-01-04 14:13:23 +01:00
|
|
|
}
|
2018-01-28 20:11:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 13:30:35 +02:00
|
|
|
function logError(type, packageJson, errorLogs) {
|
|
|
|
log.error(`FAILED (${type}) : ${errorLogs}`);
|
2018-01-28 19:09:40 +02:00
|
|
|
log.error(
|
2019-03-24 00:13:41 +01:00
|
|
|
`FAILED to compile ${type}: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`
|
2018-01-28 19:09:40 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:54:00 +02:00
|
|
|
const packageJson = getPackageJson();
|
|
|
|
|
2018-01-27 15:42:33 +02:00
|
|
|
removeDist();
|
2019-03-24 00:13:41 +01:00
|
|
|
|
2020-03-27 20:04:50 +01:00
|
|
|
babelify({ errorCallback: (errorLogs) => logError('js', packageJson, errorLogs) });
|
|
|
|
tscfy({ errorCallback: (errorLogs) => logError('ts', packageJson, errorLogs) });
|
2017-09-29 23:00:47 +03:00
|
|
|
|
2019-07-09 13:23:30 +02:00
|
|
|
cleanup();
|
|
|
|
|
2017-09-29 23:00:47 +03:00
|
|
|
console.log(chalk.gray(`Built: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`));
|