2024-04-02 20:58:16 +03:00
|
|
|
import { createRequire } from "module";
|
2023-03-15 01:27:07 +03:00
|
|
|
import {
|
|
|
|
__importDefault,
|
|
|
|
__importStar,
|
|
|
|
} from "tslib";
|
2024-04-02 20:58:16 +03:00
|
|
|
import { pathToFileURL } from "url";
|
2023-03-15 01:27:07 +03:00
|
|
|
|
|
|
|
// This script tests that TypeScript's CJS API is structured
|
|
|
|
// as expected. It calls "require" as though it were in CWD,
|
|
|
|
// so it can be tested on a separate install of TypeScript.
|
|
|
|
|
|
|
|
const require = createRequire(process.cwd() + "/index.js");
|
2024-03-05 00:06:41 +03:00
|
|
|
const typescript = process.argv[2];
|
|
|
|
const resolvedTypeScript = pathToFileURL(require.resolve(typescript)).toString();
|
2023-03-15 01:27:07 +03:00
|
|
|
|
2024-03-05 00:06:41 +03:00
|
|
|
console.log(`Testing ${typescript}...`);
|
2023-03-15 01:27:07 +03:00
|
|
|
|
|
|
|
// See: https://github.com/microsoft/TypeScript/pull/51474#issuecomment-1310871623
|
2024-03-05 00:06:41 +03:00
|
|
|
/** @type {[fn: (() => Promise<any>), shouldSucceed: boolean][]} */
|
2023-03-15 01:27:07 +03:00
|
|
|
const fns = [
|
2024-03-05 00:06:41 +03:00
|
|
|
[() => require(typescript).version, true],
|
|
|
|
[() => require(typescript).default.version, false],
|
|
|
|
[() => __importDefault(require(typescript)).version, false],
|
|
|
|
[() => __importDefault(require(typescript)).default.version, true],
|
|
|
|
[() => __importStar(require(typescript)).version, true],
|
|
|
|
[() => __importStar(require(typescript)).default.version, true],
|
|
|
|
[async () => (await import(resolvedTypeScript)).version, true],
|
|
|
|
[async () => (await import(resolvedTypeScript)).default.version, true],
|
2023-03-15 01:27:07 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const [fn, shouldSucceed] of fns) {
|
|
|
|
let success = false;
|
|
|
|
try {
|
2024-03-05 00:06:41 +03:00
|
|
|
success = !!(await fn());
|
2023-03-15 01:27:07 +03:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
const status = success ? "succeeded" : "failed";
|
|
|
|
if (success === shouldSucceed) {
|
|
|
|
console.log(`${fn.toString()} ${status} as expected.`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(`${fn.toString()} unexpectedly ${status}.`);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 00:06:41 +03:00
|
|
|
|
|
|
|
if (process.exitCode) {
|
|
|
|
console.log("fail");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("ok");
|
|
|
|
}
|