storybook/scripts/compile-tsc.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
2018-01-29 00:57:51 +02:00
function getCommand(watch) {
const tsc = path.join(__dirname, '..', 'node_modules', '.bin', 'tsc');
const downlevelDts = path.join(__dirname, '..', 'node_modules', '.bin', 'downlevel-dts');
const args = ['--outDir ./dist', '--listEmittedFiles true'];
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 --declaration true');
}
2018-01-29 00:57:51 +02:00
if (isAngular) {
args.push('--declaration true');
}
2018-01-29 00:57:51 +02:00
if (watch) {
args.push('-w');
}
2019-03-23 16:33:55 +01:00
return `${tsc} ${args.join(' ')} && ${downlevelDts} dist ts3.5/dist`;
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
}
shell.exit(code);
}
}
function tscfy(options = {}) {
2018-12-10 20:58:09 +01:00
const { watch = false, silent = false, errorCallback } = options;
const tsConfigFile = 'tsconfig.json';
if (!fs.existsSync(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 content = fs.readFileSync(tsConfigFile);
const tsConfig = JSON.parse(content);
if (tsConfig && tsConfig.lerna && tsConfig.lerna.disabled === true) {
2019-03-13 11:13:17 +01:00
if (!silent) {
2019-03-22 18:34:00 +01:00
console.log('Lerna disabled');
2019-03-13 11:13:17 +01:00
}
return;
}
2018-01-29 00:57:51 +02:00
const command = getCommand(watch);
const { code, stderr } = shell.exec(command, { silent });
handleExit(code, stderr, errorCallback);
}
module.exports = {
tscfy,
};