storybook/scripts/prepare.js

51 lines
1.6 KiB
JavaScript
Raw 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
}
2018-12-28 01:39:01 +01:00
function cleanup() {
// add .ts filtering to babel args and remove after babel - 7 is adopted
// --copy-files option doesn't work with --ignore
// https://github.com/babel/babel/issues/5404
if (fs.existsSync(path.join(process.cwd(), 'dist'))) {
2019-03-23 16:33:55 +01:00
// ^(?!.*\.d\.ts$).*\.(ts|tsx)$ => Remove everything except .d.ts files https://regex101.com/r/gEBQ0U/16
const files = shell.find('dist').filter(tsFile => tsFile.match(/^(?!.*\.d\.ts$).*\.(ts|tsx)$/));
if (files.length) {
shell.rm(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();
babelify({ errorCallback: errorLogs => logError('js', packageJson, errorLogs) });
2018-12-28 01:39:01 +01:00
cleanup();
tscfy({ errorCallback: errorLogs => logError('ts', packageJson, errorLogs) });
2017-09-29 23:00:47 +03:00
console.log(chalk.gray(`Built: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`));