kist
Version:
Package Pipeline Processor
97 lines (96 loc) • 4.99 kB
JavaScript
;
// ============================================================================
// Imports
// ============================================================================
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TypeScriptCompilerAction = void 0;
const path_1 = __importDefault(require("path"));
const typescript_1 = __importDefault(require("typescript"));
const Action_1 = require("../../core/pipeline/Action");
// ============================================================================
// Classes
// ============================================================================
/**
* TypeScriptCompilerAction compiles TypeScript files into JavaScript.
*/
class TypeScriptCompilerAction extends Action_1.Action {
/**
* Executes the TypeScript compilation process.
*
* @param options - The options specifying the tsconfig path and additional compiler options.
* @returns A Promise that resolves when compilation is completed successfully.
* @throws {Error} Throws an error if compilation fails.
*/
execute(options) {
return __awaiter(this, void 0, void 0, function* () {
const { tsconfigPath = "tsconfig.json" } = options;
const resolvedTsconfigPath = path_1.default.resolve(tsconfigPath);
this.logInfo(`Compiling TypeScript using configuration: ${resolvedTsconfigPath}`);
try {
// **Properly Parse tsconfig.json**
const parsedConfig = this.loadAndParseTsConfig(resolvedTsconfigPath);
// **Create a TypeScript Program**
const program = typescript_1.default.createProgram(parsedConfig.fileNames, parsedConfig.options);
const emitResult = program.emit();
// **Collect Diagnostics**
const allDiagnostics = typescript_1.default
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
if (allDiagnostics.length > 0) {
allDiagnostics.forEach((diagnostic) => {
const message = typescript_1.default.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
this.logError(`TypeScript Error: ${message}`);
});
throw new Error("TypeScript compilation failed due to errors.");
}
this.logInfo("TypeScript compilation completed successfully.");
}
catch (error) {
this.logError("Error during TypeScript compilation:", error);
throw new Error(`TypeScript compilation failed: ${error.message}`);
}
});
}
/**
* Loads and parses `tsconfig.json` properly before passing it to the compiler.
*
* @param tsconfigPath - The path to the tsconfig.json file.
* @returns Parsed TypeScript compiler options and source files.
*/
loadAndParseTsConfig(tsconfigPath) {
// **Read and Parse tsconfig.json**
const configFile = typescript_1.default.readConfigFile(tsconfigPath, typescript_1.default.sys.readFile);
if (configFile.error) {
throw new Error(`Error reading tsconfig.json: ${typescript_1.default.flattenDiagnosticMessageText(configFile.error.messageText, "\n")}`);
}
// **Parse the configuration content**
const parsedConfig = typescript_1.default.parseJsonConfigFileContent(configFile.config, typescript_1.default.sys, path_1.default.dirname(tsconfigPath));
if (parsedConfig.errors.length > 0) {
throw new Error(`Error parsing tsconfig.json: ${parsedConfig.errors
.map((diag) => typescript_1.default.flattenDiagnosticMessageText(diag.messageText, "\n"))
.join("\n")}`);
}
return parsedConfig;
}
/**
* Provides a description of the action.
*
* @returns A string description of the action.
*/
describe() {
return "Compiles TypeScript files using a given tsconfig.json configuration.";
}
}
exports.TypeScriptCompilerAction = TypeScriptCompilerAction;