diff --git a/.gitignore b/.gitignore index 58a45545939..9a513646e9c 100644 --- a/.gitignore +++ b/.gitignore @@ -28,7 +28,7 @@ tests/services/browser/typescriptServices.js scripts/configureNightly.js scripts/processDiagnosticMessages.d.ts scripts/processDiagnosticMessages.js -scripts/importDefinitelyTypedTests.js +scripts/importDefinitelyTypedTests/importDefinitelyTypedTests.js src/harness/*.js rwc-report.html *.swp @@ -45,6 +45,7 @@ scripts/run.bat scripts/word2md.js scripts/ior.js scripts/*.js.map +scripts/typings/ coverage/ internal/ **/.DS_Store diff --git a/Jakefile.js b/Jakefile.js index be94b60b388..c84713c18bc 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -206,6 +206,9 @@ function concatenateFiles(destinationFile, sourceFiles) { var useDebugMode = true; var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node"); var compilerFilename = "tsc.js"; +var LKGCompiler = path.join(LKGDirectory, compilerFilename); +var builtLocalCompiler = path.join(builtLocalDirectory, compilerFilename); + /* Compiles a file from a list of sources * @param outFile: the target file name * @param sources: an array of the names of the source files @@ -220,7 +223,7 @@ var compilerFilename = "tsc.js"; */ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOutFile, generateDeclarations, outDir, preserveConstEnums, keepComments, noResolve, stripInternal, callback) { file(outFile, prereqs, function() { - var dir = useBuiltCompiler ? builtLocalDirectory : LKGDirectory; + var compilerPath = useBuiltCompiler ? builtLocalCompiler : LKGCompiler; var options = "--module commonjs --noImplicitAny --noEmitOnError"; // Keep comments when specifically requested @@ -257,7 +260,7 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOu options += " --stripInternal" } - var cmd = host + " " + dir + compilerFilename + " " + options + " "; + var cmd = host + " " + compilerPath + " " + options + " "; cmd = cmd + sources.join(" "); console.log(cmd + "\n"); @@ -328,7 +331,7 @@ compileFile(processDiagnosticMessagesJs, // The generated diagnostics map; built for the compiler and for the 'generate-diagnostics' task file(diagnosticInfoMapTs, [processDiagnosticMessagesJs, diagnosticMessagesJson], function () { - var cmd = "node " + processDiagnosticMessagesJs + " " + diagnosticMessagesJson; + var cmd = host + " " + processDiagnosticMessagesJs + " " + diagnosticMessagesJson; console.log(cmd); var ex = jake.createExec([cmd]); // Add listeners for output and error @@ -374,7 +377,7 @@ task("setDebugMode", function() { }); task("configure-nightly", [configureNightlyJs], function() { - var cmd = "node " + configureNightlyJs + " " + packageJson + " " + programTs; + var cmd = host + " " + configureNightlyJs + " " + packageJson + " " + programTs; console.log(cmd); exec(cmd); }, { async: true }); @@ -386,6 +389,32 @@ task("publish-nightly", ["configure-nightly", "LKG", "clean", "setDebugMode", "r exec(cmd); }); +var scriptsTsdJson = path.join(scriptsDirectory, "tsd.json"); +file(scriptsTsdJson); + +task("tsd-scripts", [scriptsTsdJson], function () { + var cmd = "tsd --config " + scriptsTsdJson + " install"; + console.log(cmd) + exec(cmd); +}, { async: true }) + +var importDefinitelyTypedTestsDirectory = path.join(scriptsDirectory, "importDefinitelyTypedTests"); +var importDefinitelyTypedTestsJs = path.join(importDefinitelyTypedTestsDirectory, "importDefinitelyTypedTests.js"); +var importDefinitelyTypedTestsTs = path.join(importDefinitelyTypedTestsDirectory, "importDefinitelyTypedTests.ts"); + +file(importDefinitelyTypedTestsTs); +file(importDefinitelyTypedTestsJs, ["tsd-scripts", importDefinitelyTypedTestsTs], function () { + var cmd = host + " " + LKGCompiler + " -p " + importDefinitelyTypedTestsDirectory; + console.log(cmd); + exec(cmd); +}, { async: true }); + +task("importDefinitelyTypedTests", [importDefinitelyTypedTestsJs], function () { + var cmd = host + " " + importDefinitelyTypedTestsJs + " ./ ../DefinitelyTyped"; + console.log(cmd); + exec(cmd); +}, { async: true }); + // Local target to build the compiler and services var tscFile = path.join(builtLocalDirectory, compilerFilename); compileFile(tscFile, compilerSources, [builtLocalDirectory, copyright].concat(compilerSources), [copyright], /*useBuiltCompiler:*/ false); diff --git a/package.json b/package.json index b743b04838d..ba8edd188e4 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "browserify": "latest", "istanbul": "latest", "mocha-fivemat-progress-reporter": "latest", - "tslint": "latest" + "tslint": "latest", + "tsd": "latest" }, "scripts": { "pretest": "jake tests", diff --git a/scripts/importDefinitelyTypedTests.ts b/scripts/importDefinitelyTypedTests.ts deleted file mode 100644 index f33ba1dd525..00000000000 --- a/scripts/importDefinitelyTypedTests.ts +++ /dev/null @@ -1,125 +0,0 @@ -declare var require: any, process: any; -declare var __dirname: any; - -var fs = require("fs"); -var path = require("path"); -var child_process = require('child_process'); - -var tscRoot = path.join(__dirname, "..\\"); -var tscPath = path.join(tscRoot, "built", "instrumented", "tsc.js"); -var rwcTestPath = path.join(tscRoot, "tests", "cases", "rwc", "dt"); -var definitelyTypedRoot = process.argv[2]; - -function fileExtensionIs(path: string, extension: string): boolean { - var pathLen = path.length; - var extLen = extension.length; - return pathLen > extLen && path.substr(pathLen - extLen, extLen).toLocaleLowerCase() === extension.toLocaleLowerCase(); -} - -function copyFileSync(source, destination) { - var text = fs.readFileSync(source); - fs.writeFileSync(destination, text); -} - -function importDefinitelyTypedTest(testCaseName: string, testFiles: string[], responseFile: string ) { - var cmd = "node " + tscPath + " --module commonjs " + testFiles.join(" "); - if (responseFile) cmd += " @" + responseFile; - - var testDirectoryName = testCaseName + "_" + Math.floor((Math.random() * 10000) + 1); - var testDirectoryPath = path.join(process.env["temp"], testDirectoryName); - if (fs.existsSync(testDirectoryPath)) { - throw new Error("Could not create test directory"); - } - fs.mkdirSync(testDirectoryPath); - - child_process.exec(cmd, { - 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 - var outputFilePath = path.join(testDirectoryPath, "iocapture0.json"); - var 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', function (error) { - console.log("==> error " + JSON.stringify(error)); - console.log("\r\n"); - }); -} - -function importDefinitelyTypedTests(definitelyTypedRoot: string): void { - fs.readdir(definitelyTypedRoot, (err, subDirectorys) => { - if (err) throw err; - - subDirectorys - .filter(d => ["_infrastructure", "node_modules", ".git"].indexOf(d) < 0) - .filter(i => i.indexOf("sipml") >=0 ) - .filter(i => fs.statSync(path.join(definitelyTypedRoot, i)).isDirectory()) - .forEach(d => { - var directoryPath = path.join(definitelyTypedRoot, d); - fs.readdir(directoryPath, function (err, files) { - if (err) throw err; - - var tsFiles = []; - var testFiles = []; - var paramFile; - - files - .map(f => path.join(directoryPath, f)) - .forEach(f => { - if (fileExtensionIs(f, ".ts")) tsFiles.push(f); - else if (fileExtensionIs(f, ".tscparams")) paramFile = f; - - if (fileExtensionIs(f, "-tests.ts")) testFiles.push(f); - }); - - if (testFiles.length === 0) { - // no test files but multiple d.ts's, e.g. winjs - var regexp = new RegExp(d + "(([-][0-9])|([\.]d[\.]ts))"); - if (tsFiles.length > 1 && tsFiles.every(t => fileExtensionIs(t, ".d.ts") && regexp.test(t))) { - tsFiles.forEach(filename => { - importDefinitelyTypedTest(path.basename(filename, ".d.ts"), [filename], paramFile); - }); - } - else { - importDefinitelyTypedTest(d, tsFiles, paramFile); - } - } - else { - testFiles.forEach(filename => { - importDefinitelyTypedTest(path.basename(filename, "-tests.ts"), [filename], paramFile); - }); - } - }); - }) - }); -} - -importDefinitelyTypedTests(definitelyTypedRoot); \ No newline at end of file diff --git a/scripts/importDefinitelyTypedTests/importDefinitelyTypedTests.ts b/scripts/importDefinitelyTypedTests/importDefinitelyTypedTests.ts new file mode 100644 index 00000000000..1ae3725fff2 --- /dev/null +++ b/scripts/importDefinitelyTypedTests/importDefinitelyTypedTests.ts @@ -0,0 +1,161 @@ +import * as fs from "fs"; +import * as path from "path"; +import * as child_process from "child_process"; + + +interface Map { + [key: string]: T; +} + +declare var process: { + argv: string[]; + env: Map; + exit(exitCode?: number): void; +} + +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; + } + + const tscPath = path.resolve(tscRoot, "built", "local", "tsc.js"); + const rwcTestPath = path.resolve(tscRoot, "internal", "cases", "rwc"); + const resolvedDefinitelyTypedRoot = path.resolve(definitelyTypedRoot); + + 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); +} + +function filePathEndsWith(path: string, endingString: string): boolean { + const pathLen = path.length; + const extLen = endingString.length; + return pathLen > extLen && path.substr(pathLen - extLen, extLen).toLocaleLowerCase() === endingString.toLocaleLowerCase(); +} + +function copyFileSync(source: string, destination: string) { + let text = fs.readFileSync(source); + fs.writeFileSync(destination, text); +} + +function importDefinitelyTypedTest(tscPath: string, rwcTestPath: string, testCaseName: string, testFiles: string[], responseFile: string ) { + let cmd = "node " + tscPath + " --module commonjs " + testFiles.join(" "); + if (responseFile) { + cmd += " @" + responseFile; + } + + let testDirectoryName = testCaseName + "_" + Math.floor((Math.random() * 10000) + 1); + let testDirectoryPath = path.join(process.env["temp"], testDirectoryName); + if (fs.existsSync(testDirectoryPath)) { + throw new Error("Could not create test directory"); + } + fs.mkdirSync(testDirectoryPath); + + child_process.exec(cmd, { + 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 + let outputFilePath = path.join(testDirectoryPath, "iocapture0.json"); + let 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; + } + + // 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 + .filter(d => ["_infrastructure", "node_modules", ".git"].indexOf(d) < 0) + .filter(i => fs.statSync(path.join(definitelyTypedRoot, i)).isDirectory()) + .forEach(d => { + const directoryPath = path.join(definitelyTypedRoot, d); + fs.readdir(directoryPath, function (err, files) { + if (err) { + throw err; + } + + let tsFiles: string[] = []; + let testFiles: string[] = []; + let paramFile: string; + + for (const filePath of files.map(f => path.join(directoryPath, f))) { + if (filePathEndsWith(filePath, ".ts")) { + tsFiles.push(filePath); + + 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 + const regexp = new RegExp(d + "(([-][0-9])|([\.]d[\.]ts))"); + 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); + } + } + else { + importDefinitelyTypedTest(tscPath, rwcTestPath, d, tsFiles, paramFile); + } + } + else { + for (const fileName of tsFiles) { + importDefinitelyTypedTest(tscPath, rwcTestPath, path.basename(fileName, "-tests.ts"), [fileName], paramFile); + } + } + }); + }) + }); +} \ No newline at end of file diff --git a/scripts/importDefinitelyTypedTests/tsconfig.json b/scripts/importDefinitelyTypedTests/tsconfig.json new file mode 100644 index 00000000000..dede4572531 --- /dev/null +++ b/scripts/importDefinitelyTypedTests/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "ES5", + "noImplicitAny": true, + "outDir": "./", + "rootDir": ".", + "sourceMap": false + }, + "files": [ + "../typings/node/node.d.ts", + "importDefinitelyTypedTests.ts" + ], + "exclude": [ + ] +} \ No newline at end of file diff --git a/scripts/tsd.json b/scripts/tsd.json new file mode 100644 index 00000000000..c2fc88a0b0f --- /dev/null +++ b/scripts/tsd.json @@ -0,0 +1,12 @@ +{ + "version": "v4", + "repo": "borisyankov/DefinitelyTyped", + "ref": "master", + "path": "typings", + "bundle": "typings/tsd.d.ts", + "installed": { + "node/node.d.ts": { + "commit": "5f480287834a2615274eea31574b713e64decf17" + } + } +}