storybook/scripts/utils/compile-tsc.js

96 lines
2.3 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-console */
const fs = require('fs-extra');
const path = require('path');
const execa = require('execa');
2018-01-29 00:57:51 +02:00
function getCommand(watch) {
const args = [
'--outDir ./dist/ts3.9',
'--listEmittedFiles false',
'--declaration true',
'--noErrorTruncation',
'--pretty',
];
2019-03-22 18:34:00 +01:00
2019-04-01 19:16:40 +02:00
/**
* Only emit declarations if it does not need to be compiled with tsc
* Currently, angular and storyshots (that contains an angular component) need to be compiled
* with tsc. (see comments in compile-babel.js)
*/
const isAngular = process.cwd().includes(path.join('app', 'angular'));
const isStoryshots = process.cwd().includes(path.join('addons', 'storyshots'));
if (!isAngular && !isStoryshots) {
args.push('--emitDeclarationOnly');
2020-12-09 09:42:03 +01:00
}
2018-01-29 00:57:51 +02:00
if (watch) {
2020-06-12 09:50:36 -07:00
args.push('-w', '--preserveWatchOutput');
2018-01-29 00:57:51 +02:00
}
2019-03-23 16:33:55 +01:00
return `yarn run -T tsc ${args.join(' ')}`;
2018-01-29 00:57:51 +02:00
}
function handleExit(code, stderr, errorCallback) {
2018-01-29 00:57:51 +02:00
if (code !== 0) {
if (errorCallback && typeof errorCallback === 'function') {
errorCallback(stderr);
2018-01-29 00:57:51 +02:00
}
2020-09-16 16:45:51 -07:00
process.exit(code);
2018-01-29 00:57:51 +02:00
}
}
async function run({ watch, silent, errorCallback }) {
return new Promise((resolve, reject) => {
const command = getCommand(watch);
const child = execa.command(command, {
buffer: false,
});
let stderr = '';
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();
});
}
child.on('exit', (code) => {
resolve();
handleExit(code, stderr, errorCallback);
});
});
}
async function tscfy(options = {}) {
2018-12-10 20:58:09 +01:00
const { watch = false, silent = false, errorCallback } = options;
const tsConfigFile = 'tsconfig.json';
if (!(await fs.pathExists(tsConfigFile))) {
2019-03-13 11:13:17 +01:00
if (!silent) {
2019-03-22 18:34:00 +01:00
console.log(`No ${tsConfigFile}`);
2019-03-13 11:13:17 +01:00
}
return;
}
const tsConfig = await fs.readJSON(tsConfigFile);
if (!(tsConfig && tsConfig.lerna && tsConfig.lerna.disabled === true)) {
await run({ watch, silent, errorCallback });
}
if (!watch) {
await execa.command('yarn run -T downlevel-dts dist/ts3.9 dist/ts3.4');
}
}
module.exports = {
tscfy,
};