@clawject/di
Version:
<p align="center"> <a href="https://clawject.com/" target="_blank"><img src="https://clawject.com/img/logo.svg" align="center" alt="Clawject Logo" width="120" height="120" /></a> </p>
44 lines (43 loc) • 1.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginCompiler = void 0;
const tslib_1 = require("tslib");
const typescript_1 = tslib_1.__importDefault(require("typescript"));
const Dirty_1 = require("./Dirty");
const index_1 = tslib_1.__importDefault(require("@clawject/core/transformer/index"));
class PluginCompiler {
constructor(programStorage) {
this.programStorage = programStorage;
this.printer = typescript_1.default.createPrinter();
this.filesToCompile = new Set();
this.transformDirtyMarker = new Dirty_1.Dirty(5, async () => {
await this.programStorage.refreshProgram();
const compiled = await this.compile();
this.transformPromise = null;
this.filesToCompile.clear();
return compiled;
});
this.transformPromise = null;
}
async transform(fileName, content) {
this.transformDirtyMarker.markDirty();
await this.programStorage.updateFile(fileName, content);
this.filesToCompile.add(fileName);
if (!this.transformPromise) {
this.transformPromise = this.transformDirtyMarker.waitClean();
}
const result = await this.transformPromise;
return result.get(fileName) ?? content;
}
async compile() {
const program = await this.programStorage.getProgram();
const clawjectTransformer = (0, index_1.default)(program, undefined);
const transformationResult = typescript_1.default.transform(Array.from(this.filesToCompile).map(it => program.getSourceFile(it)).filter((it) => Boolean(it)), [clawjectTransformer], program.getCompilerOptions());
const result = new Map();
transformationResult.transformed.forEach((sourceFile) => {
result.set(sourceFile.fileName, this.printer.printFile(sourceFile));
});
return result;
}
}
exports.PluginCompiler = PluginCompiler;