UNPKG

just-scripts

Version:
79 lines 3.07 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); (0, utils_1.logNodeCommand)(args); return (0, utils_1.spawn)(process.execPath, args, { stdio: 'inherit' }); } return Promise.resolve(); }; } exports.tscTask = tscTask; /** * Returns a task that runs the TSC CLI in watch mode. */ function tscWatchTask(options = {}) { return tscTask({ ...options, watch: true }); } 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.every(buildPath => typeof buildPath === 'string' && fs.existsSync(buildPath)))); } /** * Returns an array of CLI arguments for TSC given the `options`. */ function argsFromOptions(tscCmd, options) { const { nodeArgs, build, ...rest } = options; const args = [...(nodeArgs || []), tscCmd]; // --build must be the first arg if specified const argEntries = [...(build !== undefined ? [['build', build]] : []), ...Object.entries(rest)]; for (const [option, optionValue] of argEntries) { if (typeof optionValue === 'string') { args.push('--' + option, optionValue); } else if (typeof optionValue === 'boolean' && optionValue) { args.push('--' + option); } else if (Array.isArray(optionValue)) { args.push('--' + option, ...optionValue); } } return args; } //# sourceMappingURL=tscTask.js.map