TypeScript/scripts/importDefinitelyTypedTests/importDefinitelyTypedTests.ts

161 lines
6.3 KiB
TypeScript
Raw Normal View History

import * as fs from "fs";
import * as path from "path";
2019-06-19 16:30:28 +02:00
import * as childProcess from "child_process";
interface Map<T> {
[key: string]: T;
}
2019-06-20 09:51:29 +02:00
declare let process: {
argv: string[];
env: Map<string>;
exit(exitCode?: number): void;
2019-06-14 08:46:02 +02:00
};
main();
function main() {
const [, progName, tscRoot, definitelyTypedRoot] = process.argv;
if (process.argv.length !== 4) {
if (process.argv.length < 2) {
2019-06-14 09:54:11 +02:00
throw new Error("Expected at least 2 argv elements.");
}
2019-06-14 08:46:02 +02:00
console.log("Usage:");
console.log(` node ${path.relative(__dirname, progName)} [TypeScript Repo Root] [DefinitelyTyped Repo Root]`);
return;
}
const tscPath = path.resolve(tscRoot, "built", "local", "tsc.js");
2015-09-18 02:26:10 +02:00
const rwcTestPath = path.resolve(tscRoot, "internal", "cases", "rwc");
const resolvedDefinitelyTypedRoot = path.resolve(definitelyTypedRoot);
2015-09-18 02:26:10 +02:00
console.log(`Resolved TypeScript Compiler Path: '${tscPath}'.`);
console.log(`Resolved TypeScript RWC Path: '${rwcTestPath}'.`);
console.log(`Resolved DefinitelyTyped Repo Root: '${resolvedDefinitelyTypedRoot}'.`);
importDefinitelyTypedTests(tscPath, rwcTestPath, resolvedDefinitelyTypedRoot);
}
2015-09-17 23:17:23 +02:00
function filePathEndsWith(path: string, endingString: string): boolean {
const pathLen = path.length;
2015-09-17 23:17:23 +02:00
const extLen = endingString.length;
return pathLen > extLen && path.substr(pathLen - extLen, extLen).toLocaleLowerCase() === endingString.toLocaleLowerCase();
}
function copyFileSync(source: string, destination: string) {
2019-06-19 16:58:49 +02:00
const text = fs.readFileSync(source);
2015-09-18 02:26:10 +02:00
fs.writeFileSync(destination, text);
}
2019-06-14 09:33:03 +02:00
function importDefinitelyTypedTest(tscPath: string, rwcTestPath: string, testCaseName: string, testFiles: string[], responseFile: string) {
let cmd = "node " + tscPath + " --module commonjs " + testFiles.join(" ");
if (responseFile) {
cmd += " @" + responseFile;
}
2019-06-19 16:58:49 +02:00
const testDirectoryName = testCaseName + "_" + Math.floor((Math.random() * 10000) + 1);
const testDirectoryPath = path.join(process.env.temp, testDirectoryName);
if (fs.existsSync(testDirectoryPath)) {
throw new Error("Could not create test directory");
}
fs.mkdirSync(testDirectoryPath);
2019-06-19 16:30:28 +02:00
childProcess.exec(cmd, {
2014-09-11 18:22:27 +02:00
maxBuffer: 1 * 1024 * 1024,
cwd: testDirectoryPath
}, (error, stdout, stderr) => {
2019-06-24 13:55:58 +02:00
console.log("importing " + testCaseName + " ...");
console.log(cmd);
if (error) {
2014-09-11 18:22:27 +02:00
console.log("importing " + testCaseName + " ...");
console.log(cmd);
console.log("==> error " + JSON.stringify(error));
2019-06-24 13:55:58 +02:00
console.log("==> stdout " + String(stdout));
console.log("==> stderr " + String(stderr));
2014-09-11 18:22:27 +02:00
console.log("\r\n");
2019-06-24 13:55:58 +02:00
return;
}
// copy generated file to output location
const outputFilePath = path.join(testDirectoryPath, "iocapture0.json");
const testCasePath = path.join(rwcTestPath, "DefinitelyTyped_" + testCaseName + ".json");
copyFileSync(outputFilePath, testCasePath);
//console.log("output generated at: " + outputFilePath);
if (!fs.existsSync(testCasePath)) {
throw new Error("could not find test case at: " + testCasePath);
}
else {
fs.unlinkSync(outputFilePath);
fs.rmdirSync(testDirectoryPath);
//console.log("testcase generated at: " + testCasePath);
//console.log("Done.");
}
//console.log("\r\n");
}).on("error", (error: any) => {
console.log("==> error " + JSON.stringify(error));
console.log("\r\n");
});
}
function importDefinitelyTypedTests(tscPath: string, rwcTestPath: string, definitelyTypedRoot: string): void {
fs.readdir(definitelyTypedRoot, (err, subDirectories) => {
if (err) {
throw err;
}
2015-09-18 22:13:26 +02:00
// When you just want to test the script out on one or two files,
// just add a line like the following:
//
// .filter(d => d.indexOf("sipml") >= 0 )
subDirectories
2014-09-11 18:22:27 +02:00
.filter(d => ["_infrastructure", "node_modules", ".git"].indexOf(d) < 0)
.filter(i => fs.statSync(path.join(definitelyTypedRoot, i)).isDirectory())
.forEach(d => {
2015-09-17 23:17:23 +02:00
const directoryPath = path.join(definitelyTypedRoot, d);
fs.readdir(directoryPath, (err, files) => {
if (err) {
throw err;
}
2019-06-19 16:58:49 +02:00
const tsFiles: string[] = [];
const testFiles: string[] = [];
let paramFile: string;
2015-09-17 23:17:23 +02:00
for (const filePath of files.map(f => path.join(directoryPath, f))) {
if (filePathEndsWith(filePath, ".ts")) {
tsFiles.push(filePath);
2015-09-17 23:17:23 +02:00
if (filePathEndsWith(filePath, "-tests.ts")) {
testFiles.push(filePath);
}
}
else if (filePathEndsWith(filePath, ".tscparams")) {
paramFile = filePath;
}
}
if (testFiles.length === 0) {
// no test files but multiple d.ts's, e.g. winjs
2015-09-18 22:13:26 +02:00
const regexp = new RegExp(d + "(([-][0-9])|([\.]d[\.]ts))");
2015-09-17 23:17:23 +02:00
if (tsFiles.length > 1 && tsFiles.every(t => filePathEndsWith(t, ".d.ts") && regexp.test(t))) {
for (const fileName of tsFiles) {
importDefinitelyTypedTest(tscPath, rwcTestPath, path.basename(fileName, ".d.ts"), [fileName], paramFile);
2015-09-17 23:17:23 +02:00
}
}
else {
2019-06-24 13:55:58 +02:00
importDefinitelyTypedTest(tscPath, rwcTestPath, d, tsFiles, paramFile);
}
}
else {
2015-09-17 23:17:23 +02:00
for (const fileName of tsFiles) {
importDefinitelyTypedTest(tscPath, rwcTestPath, path.basename(fileName, "-tests.ts"), [fileName], paramFile);
2015-09-17 23:17:23 +02:00
}
}
});
2019-06-14 08:46:02 +02:00
});
});
2019-06-14 08:46:02 +02:00
}