TypeScript/src/instrumenter/instrumenter.ts
Nathan Shively-Sanders 92e7fb521f
Update tsc-instrumented for project build (#45383)
* Update tsc-instrumented for project build

loggedIO has a weird build that never got updated for the project build
system. This PR just adds a project for it in a straightforward way. It
might be less efficient than the old way, but that's not a big concern
for recording RWC test cases.

However, I may have done things wrong. If anybody knows
tsc-instrumented, please comment.

* Create a second loggedIO tsconfig for tsc-instrumented

The normal tsconfig should not have `prepend`; the standalone one for
tsc-instrumented should.

* fix semicolon lint
2021-08-10 14:53:17 -07:00

44 lines
1.7 KiB
TypeScript

import fs = require("fs");
import path = require("path");
function instrumentForRecording(fn: string, tscPath: string) {
instrument(tscPath, `
ts.sys = Playback.wrapSystem(ts.sys);
ts.sys.startRecord("${ fn }");`, `ts.sys.endRecord();`);
}
function instrumentForReplay(logFilename: string, tscPath: string) {
instrument(tscPath, `
ts.sys = Playback.wrapSystem(ts.sys);
ts.sys.startReplay("${ logFilename }");`);
}
function instrument(tscPath: string, prepareCode: string, cleanupCode = "") {
const bak = `${tscPath}.bak`;
const filename = fs.existsSync(bak) ? bak : tscPath;
const tscContent = fs.readFileSync(filename, "utf-8");
fs.writeFileSync(bak, tscContent);
const loggerContent = fs.readFileSync(path.resolve(path.dirname(tscPath) + "/loggedIO.js"), "utf-8");
const invocationLine = "ts.executeCommandLine(ts.sys, ts.noop, ts.sys.args);";
const index1 = tscContent.indexOf(invocationLine);
if (index1 < 0) {
throw new Error(`Could not find ${invocationLine}`);
}
const index2 = index1 + invocationLine.length;
const newContent = tscContent.substr(0, index1) + loggerContent + prepareCode + invocationLine + cleanupCode + tscContent.substr(index2) + "\r\n";
fs.writeFileSync(tscPath, newContent);
}
const isJson = (arg: string) => arg.indexOf(".json") > 0;
const record = process.argv.indexOf("record");
const tscPath = process.argv[process.argv.length - 1];
if (record >= 0) {
console.log(`Instrumenting ${tscPath} for recording`);
instrumentForRecording(process.argv[record + 1], tscPath);
}
else if (process.argv.some(isJson)) {
const filename = process.argv.filter(isJson)[0];
instrumentForReplay(filename, tscPath);
}