UNPKG

just-scripts

Version:
97 lines 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tscWatchTask = exports.tscTask = void 0; const just_task_1 = require("just-task"); const utils_1 = require("../utils"); const fs = require("fs"); /** * Returns a task that runs the TSC CLI. */ function tscTask(options = {}) { const tscCmd = (0, just_task_1.resolve)('typescript/lib/tsc.js'); if (!tscCmd) { throw new Error('cannot find tsc'); } return function tsc() { // Read from options argument, if not there try the tsConfigFile found in root, if not then skip and use no config options = { ...options, ...getProjectOrBuildOptions(options) }; if (isValidProject(options)) { just_task_1.logger.info(`Running ${tscCmd} with ${options.project || options.build}`); const args = argsFromOptions(tscCmd, options); const cmd = (0, utils_1.encodeArgs)([process.execPath, ...args]).join(' '); just_task_1.logger.info(`Executing: ${cmd}`); return (0, utils_1.exec)(cmd); } return Promise.resolve(); }; } exports.tscTask = tscTask; /** * Returns a task that runs the TSC CLI in watch mode. */ function tscWatchTask(options = {}) { const tscCmd = (0, just_task_1.resolve)('typescript/lib/tsc.js'); if (!tscCmd) { throw new Error('cannot find tsc'); } return function tscWatch() { options = { ...options, ...getProjectOrBuildOptions(options) }; if (isValidProject(options)) { just_task_1.logger.info(`Running ${tscCmd} with ${options.project || options.build} in watch mode`); const args = argsFromOptions(tscCmd, options); const cmd = [...args, '--watch']; just_task_1.logger.info((0, utils_1.encodeArgs)(cmd).join(' ')); return (0, utils_1.spawn)(process.execPath, cmd, { stdio: 'inherit' }); } return Promise.resolve(); }; } exports.tscWatchTask = tscWatchTask; /** * Determine `project` or `build` options. `build` option takes precedence. */ function getProjectOrBuildOptions(options) { const tsConfigFile = (0, just_task_1.resolveCwd)('./tsconfig.json') || 'tsconfig.json'; const result = {}; if (options.build) { result.build = typeof options.build === 'string' || Array.isArray(options.build) ? options.build : tsConfigFile; } else { result.project = typeof options.project === 'string' ? options.project : tsConfigFile; } return result; } /** * Returns true if the `project` or `build` setting refers to an existing `tsconfig.json` file. */ function isValidProject(options) { return ((typeof options.project === 'string' && fs.existsSync(options.project)) || (typeof options.build === 'string' && fs.existsSync(options.build)) || (Array.isArray(options.build) && options.build.reduce((currentIsValid, buildPath) => { return currentIsValid && typeof buildPath === 'string' && fs.existsSync(buildPath); }, true))); } /** * Returns an array of CLI arguments for TSC given the `options`. */ function argsFromOptions(tscCmd, options) { const { nodeArgs, ...rest } = options; return [ ...(nodeArgs ? nodeArgs : []), ...Object.keys(rest).reduce((currentArgs, option) => { const optionValue = options[option]; if (typeof optionValue === 'string') { return currentArgs.concat(['--' + option, optionValue]); } else if (typeof optionValue === 'boolean' && optionValue) { return currentArgs.concat(['--' + option]); } else if (Array.isArray(optionValue)) { return currentArgs.concat(['--' + option, ...optionValue]); } return currentArgs; }, [tscCmd]), ]; } //# sourceMappingURL=tscTask.js.map