2015-09-17 02:42:32 +02:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
import * as child_process from "child_process";
|
2014-09-03 19:37:03 +02:00
|
|
|
|
2015-09-18 01:37:03 +02:00
|
|
|
|
2015-09-17 02:42:32 +02:00
|
|
|
interface Map<T> {
|
|
|
|
[key: string]: T;
|
|
|
|
}
|
|
|
|
|
2015-09-18 01:37:03 +02:00
|
|
|
declare var process: {
|
2015-09-17 02:42:32 +02:00
|
|
|
argv: string[];
|
2015-09-18 01:37:03 +02:00
|
|
|
env: Map<string>;
|
|
|
|
exit(exitCode?: number): void;
|
2015-09-17 02:42:32 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 01:37:03 +02:00
|
|
|
main();
|
|
|
|
function main() {
|
|
|
|
const [, progName, tscRoot, definitelyTypedRoot] = process.argv;
|
|
|
|
if (process.argv.length !== 4) {
|
|
|
|
if (process.argv.length < 2) {
|
|
|
|
throw "Expected at least 2 argv elements."
|
|
|
|
}
|
|
|
|
console.log("Usage:")
|
|
|
|
console.log(` node ${path.relative(__dirname, progName)} [TypeScript Repo Root] [DefinitelyTyped Repo Root]`);
|
|
|
|
return;
|
|
|
|
}
|
2014-09-03 19:37:03 +02:00
|
|
|
|
2015-09-18 01:37:03 +02:00
|
|
|
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");
|
2015-09-18 01:37:03 +02:00
|
|
|
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}'.`);
|
2015-09-18 01:37:03 +02:00
|
|
|
importDefinitelyTypedTests(tscPath, rwcTestPath, resolvedDefinitelyTypedRoot);
|
|
|
|
}
|
2015-09-17 23:17:23 +02:00
|
|
|
|
|
|
|
function filePathEndsWith(path: string, endingString: string): boolean {
|
2015-09-17 02:42:32 +02:00
|
|
|
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();
|
2014-09-03 19:37:03 +02:00
|
|
|
}
|
|
|
|
|
2015-09-17 02:42:32 +02:00
|
|
|
function copyFileSync(source: string, destination: string) {
|
|
|
|
let text = fs.readFileSync(source);
|
2015-09-18 02:26:10 +02:00
|
|
|
fs.writeFileSync(destination, text);
|
2014-09-03 19:37:03 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 01:37:03 +02:00
|
|
|
function importDefinitelyTypedTest(tscPath: string, rwcTestPath: string, testCaseName: string, testFiles: string[], responseFile: string ) {
|
2015-09-17 02:42:32 +02:00
|
|
|
let cmd = "node " + tscPath + " --module commonjs " + testFiles.join(" ");
|
|
|
|
if (responseFile) {
|
|
|
|
cmd += " @" + responseFile;
|
|
|
|
}
|
2014-09-03 19:37:03 +02:00
|
|
|
|
2015-09-17 02:42:32 +02:00
|
|
|
let testDirectoryName = testCaseName + "_" + Math.floor((Math.random() * 10000) + 1);
|
|
|
|
let testDirectoryPath = path.join(process.env["temp"], testDirectoryName);
|
2014-09-03 19:37:03 +02:00
|
|
|
if (fs.existsSync(testDirectoryPath)) {
|
|
|
|
throw new Error("Could not create test directory");
|
|
|
|
}
|
|
|
|
fs.mkdirSync(testDirectoryPath);
|
|
|
|
|
|
|
|
child_process.exec(cmd, {
|
2014-09-11 18:22:27 +02:00
|
|
|
maxBuffer: 1 * 1024 * 1024,
|
|
|
|
cwd: testDirectoryPath
|
|
|
|
}, (error, stdout, stderr) => {
|
|
|
|
console.log("importing " + testCaseName + " ...");
|
|
|
|
console.log(cmd);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
console.log("importing " + testCaseName + " ...");
|
|
|
|
console.log(cmd);
|
|
|
|
console.log("==> error " + JSON.stringify(error));
|
|
|
|
console.log("==> stdout " + String(stdout));
|
|
|
|
console.log("==> stderr " + String(stderr));
|
|
|
|
console.log("\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy generated file to output location
|
2015-09-17 02:42:32 +02:00
|
|
|
let outputFilePath = path.join(testDirectoryPath, "iocapture0.json");
|
|
|
|
let testCasePath = path.join(rwcTestPath, "DefinitelyTyped_" + testCaseName + ".json");
|
2014-09-11 18:22:27 +02:00
|
|
|
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");
|
|
|
|
|
|
|
|
})
|
2015-09-17 02:42:32 +02:00
|
|
|
.on('error', (error: any) => {
|
2014-09-11 18:22:27 +02:00
|
|
|
console.log("==> error " + JSON.stringify(error));
|
|
|
|
console.log("\r\n");
|
2014-09-03 19:37:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-18 01:37:03 +02:00
|
|
|
function importDefinitelyTypedTests(tscPath: string, rwcTestPath: string, definitelyTypedRoot: string): void {
|
2015-09-17 02:42:32 +02:00
|
|
|
fs.readdir(definitelyTypedRoot, (err, subDirectories) => {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2014-09-03 19:37:03 +02:00
|
|
|
|
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 )
|
2015-09-17 02:42:32 +02:00
|
|
|
subDirectories
|
2014-09-11 18:22:27 +02:00
|
|
|
.filter(d => ["_infrastructure", "node_modules", ".git"].indexOf(d) < 0)
|
2014-09-03 19:37:03 +02:00
|
|
|
.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);
|
2014-09-03 19:37:03 +02:00
|
|
|
fs.readdir(directoryPath, function (err, files) {
|
2015-09-17 02:42:32 +02:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2014-09-03 19:37:03 +02:00
|
|
|
|
2015-09-17 02:42:32 +02:00
|
|
|
let tsFiles: string[] = [];
|
|
|
|
let testFiles: string[] = [];
|
|
|
|
let paramFile: string;
|
2014-09-03 19:37:03 +02:00
|
|
|
|
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);
|
2014-09-03 19:37:03 +02:00
|
|
|
|
2015-09-17 23:17:23 +02:00
|
|
|
if (filePathEndsWith(filePath, "-tests.ts")) {
|
|
|
|
testFiles.push(filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (filePathEndsWith(filePath, ".tscparams")) {
|
|
|
|
paramFile = filePath;
|
|
|
|
}
|
|
|
|
}
|
2014-09-03 19:37:03 +02:00
|
|
|
|
|
|
|
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) {
|
2015-09-18 01:37:03 +02:00
|
|
|
importDefinitelyTypedTest(tscPath, rwcTestPath, path.basename(fileName, ".d.ts"), [fileName], paramFile);
|
2015-09-17 23:17:23 +02:00
|
|
|
}
|
2014-09-03 19:37:03 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-09-18 01:37:03 +02:00
|
|
|
importDefinitelyTypedTest(tscPath, rwcTestPath, d, tsFiles, paramFile);
|
2014-09-03 19:37:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-17 23:17:23 +02:00
|
|
|
for (const fileName of tsFiles) {
|
2015-09-18 01:37:03 +02:00
|
|
|
importDefinitelyTypedTest(tscPath, rwcTestPath, path.basename(fileName, "-tests.ts"), [fileName], paramFile);
|
2015-09-17 23:17:23 +02:00
|
|
|
}
|
2014-09-03 19:37:03 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
});
|
2015-09-17 23:17:23 +02:00
|
|
|
}
|