storybook/scripts/prepare.js

76 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

/* eslint-disable no-console */
/* tslint:disable:no-console */
const path = require('path');
const shell = require('shelljs');
const chalk = require('chalk');
const fs = require('fs');
2018-01-28 19:09:40 +02:00
const log = require('npmlog');
const { babelify } = require('./compile-babel');
const { tscfy } = require('./compile-tsc');
function getPackageJson() {
const modulePath = path.resolve('./');
2017-05-18 09:14:29 +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
}
function removeDist() {
shell.rm('-rf', 'dist');
2018-01-27 15:42:33 +02:00
}
const ignore = [
'__mocks__',
'__snapshots__',
'__testfixtures__',
'__tests__',
'/tests/',
/.+\.test\..+/,
];
2018-12-28 01:39:01 +01:00
function cleanup() {
// remove files after babel --copy-files output
// --copy-files option doesn't work with --ignore
// https://github.com/babel/babel/issues/6226
if (fs.existsSync(path.join(process.cwd(), 'dist'))) {
2020-03-27 20:04:50 +01:00
const files = shell.find('dist').filter((filePath) => {
// 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;
}
return ignore.reduce((acc, pattern) => {
return acc || !!filePath.match(pattern);
}, false);
});
if (files.length) {
shell.rm('-f', ...files);
}
}
}
function logError(type, packageJson, errorLogs) {
log.error(`FAILED (${type}) : ${errorLogs}`);
2018-01-28 19:09:40 +02:00
log.error(
`FAILED to compile ${type}: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`
2018-01-28 19:09:40 +02:00
);
}
const packageJson = getPackageJson();
2018-01-27 15:42:33 +02:00
removeDist();
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
cleanup();
2017-09-29 23:00:47 +03:00
console.log(chalk.gray(`Built: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`));