forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.js
88 lines (75 loc) · 2.51 KB
/
prepare.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* eslint-disable no-console */
const path = require('path');
const shell = require('shelljs');
const chalk = require('chalk');
const fs = require('fs');
const log = require('npmlog');
const { babelify } = require('./utils/compile-babel');
const { tscfy } = require('./utils/compile-tsc');
function getPackageJson(modulePath) {
// eslint-disable-next-line global-require,import/no-dynamic-require
return require(path.join(modulePath, 'package.json'));
}
function removeDist() {
shell.rm('-rf', 'dist');
}
const ignore = [
'__mocks__',
'__snapshots__',
'__testfixtures__',
'__tests__',
'/tests/',
/.+\.test\..+/,
];
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'))) {
const isInStorybookCLIPackage = process.cwd().includes(path.join('lib', 'cli'));
const filesToRemove = shell.find('dist').filter((filePath) => {
// Do not remove folder
// And do not clean anything for:
// - @storybook/cli/dist/(esm|cjs)/generators/**/template*
// - @storybook/cli/dist/(esm|cjs)/frameworks/*
// because these are the template files that will be copied to init SB on users' projects
if (
fs.lstatSync(filePath).isDirectory() ||
(isInStorybookCLIPackage &&
/\/(esm|cjs)\/(generators\/.+\/template|frameworks).*/.test(filePath))
) {
return false;
}
// 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 (filesToRemove.length) {
shell.rm('-f', ...filesToRemove);
}
}
}
function logError(type, packageJson, errorLogs) {
log.error(`FAILED (${type}) : ${errorLogs}`);
log.error(
`FAILED to compile ${type}: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`
);
}
const modulePath = path.resolve('./');
const packageJson = getPackageJson(modulePath);
const modules = true;
async function prepare() {
removeDist();
await babelify({
modules,
errorCallback: (errorLogs) => logError('js', packageJson, errorLogs),
});
tscfy({ errorCallback: (errorLogs) => logError('ts', packageJson, errorLogs) });
cleanup();
console.log(chalk.gray(`Built: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`));
}
prepare();