storybook/scripts/compile-ts.js

54 lines
1.2 KiB
JavaScript

/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const shell = require('shelljs');
function getCommand(watch) {
const tsc = path.join(__dirname, '..', 'node_modules', '.bin', 'tsc');
const args = ['--outDir ./dist', '-d true', '--listEmittedFiles true'];
if (watch) {
args.push('-w');
}
return `${tsc} ${args.join(' ')}`;
}
function handleExit(code, errorCallback) {
if (code !== 0) {
if (errorCallback && typeof errorCallback === 'function') {
errorCallback();
}
shell.exit(code);
}
}
function tscfy(options = {}) {
const { watch = false, silent = true, errorCallback } = options;
const tsConfigFile = 'tsconfig.json';
if (!fs.existsSync(tsConfigFile)) {
if (!silent) console.log(`No ${tsConfigFile}`);
return;
}
const content = fs.readFileSync(tsConfigFile);
const tsConfig = JSON.parse(content);
if (tsConfig && tsConfig.lerna && tsConfig.lerna.disabled === true) {
if (!silent) console.log('Lerna disabled');
return;
}
const command = getCommand(watch);
const { code } = shell.exec(command, { silent });
handleExit(code, errorCallback);
}
module.exports = {
tscfy,
};