Skip to content

Commit 8bb6313

Browse files
Use explicit types, var -> let/const, misspellings, style.
1 parent a47b7b8 commit 8bb6313

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

scripts/importDefinitelyTypedTests/importDefinitelyTypedTests.ts

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
1-
var fs = require("fs");
2-
var path = require("path");
3-
var child_process = require('child_process');
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
import * as child_process from "child_process";
44

5-
var tscRoot = path.join(__dirname, "..\\");
6-
var tscPath = path.join(tscRoot, "built", "instrumented", "tsc.js");
7-
var rwcTestPath = path.join(tscRoot, "tests", "cases", "rwc", "dt");
8-
var definitelyTypedRoot = process.argv[2];
5+
interface Map<T> {
6+
[key: string]: T;
7+
}
8+
9+
declare const __dirname: string;
10+
declare const process: {
11+
argv: string[];
12+
env: Map<string>
13+
}
14+
15+
const tscRoot = path.join(__dirname, "..\\");
16+
const tscPath = path.join(tscRoot, "built", "instrumented", "tsc.js");
17+
const rwcTestPath = path.join(tscRoot, "tests", "cases", "rwc", "dt");
18+
const definitelyTypedRoot = process.argv[2];
919

1020
function fileExtensionIs(path: string, extension: string): boolean {
11-
var pathLen = path.length;
12-
var extLen = extension.length;
21+
const pathLen = path.length;
22+
const extLen = extension.length;
1323
return pathLen > extLen && path.substr(pathLen - extLen, extLen).toLocaleLowerCase() === extension.toLocaleLowerCase();
1424
}
1525

16-
function copyFileSync(source, destination) {
17-
var text = fs.readFileSync(source);
26+
function copyFileSync(source: string, destination: string) {
27+
let text = fs.readFileSync(source);
1828
fs.writeFileSync(destination, text);
1929
}
2030

2131
function importDefinitelyTypedTest(testCaseName: string, testFiles: string[], responseFile: string ) {
22-
var cmd = "node " + tscPath + " --module commonjs " + testFiles.join(" ");
23-
if (responseFile) cmd += " @" + responseFile;
32+
let cmd = "node " + tscPath + " --module commonjs " + testFiles.join(" ");
33+
if (responseFile) {
34+
cmd += " @" + responseFile;
35+
}
2436

25-
var testDirectoryName = testCaseName + "_" + Math.floor((Math.random() * 10000) + 1);
26-
var testDirectoryPath = path.join(process.env["temp"], testDirectoryName);
37+
let testDirectoryName = testCaseName + "_" + Math.floor((Math.random() * 10000) + 1);
38+
let testDirectoryPath = path.join(process.env["temp"], testDirectoryName);
2739
if (fs.existsSync(testDirectoryPath)) {
2840
throw new Error("Could not create test directory");
2941
}
@@ -47,8 +59,8 @@ function importDefinitelyTypedTest(testCaseName: string, testFiles: string[], re
4759
}
4860

4961
// copy generated file to output location
50-
var outputFilePath = path.join(testDirectoryPath, "iocapture0.json");
51-
var testCasePath = path.join(rwcTestPath, "DefinitelyTyped_" + testCaseName + ".json");
62+
let outputFilePath = path.join(testDirectoryPath, "iocapture0.json");
63+
let testCasePath = path.join(rwcTestPath, "DefinitelyTyped_" + testCaseName + ".json");
5264
copyFileSync(outputFilePath, testCasePath);
5365

5466
//console.log("output generated at: " + outputFilePath);
@@ -65,28 +77,32 @@ function importDefinitelyTypedTest(testCaseName: string, testFiles: string[], re
6577
//console.log("\r\n");
6678

6779
})
68-
.on('error', function (error) {
80+
.on('error', (error: any) => {
6981
console.log("==> error " + JSON.stringify(error));
7082
console.log("\r\n");
7183
});
7284
}
7385

7486
function importDefinitelyTypedTests(definitelyTypedRoot: string): void {
75-
fs.readdir(definitelyTypedRoot, (err, subDirectorys) => {
76-
if (err) throw err;
87+
fs.readdir(definitelyTypedRoot, (err, subDirectories) => {
88+
if (err) {
89+
throw err;
90+
}
7791

78-
subDirectorys
92+
subDirectories
7993
.filter(d => ["_infrastructure", "node_modules", ".git"].indexOf(d) < 0)
8094
.filter(i => i.indexOf("sipml") >=0 )
8195
.filter(i => fs.statSync(path.join(definitelyTypedRoot, i)).isDirectory())
8296
.forEach(d => {
83-
var directoryPath = path.join(definitelyTypedRoot, d);
97+
let directoryPath = path.join(definitelyTypedRoot, d);
8498
fs.readdir(directoryPath, function (err, files) {
85-
if (err) throw err;
99+
if (err) {
100+
throw err;
101+
}
86102

87-
var tsFiles = [];
88-
var testFiles = [];
89-
var paramFile;
103+
let tsFiles: string[] = [];
104+
let testFiles: string[] = [];
105+
let paramFile: string;
90106

91107
files
92108
.map(f => path.join(directoryPath, f))
@@ -99,7 +115,7 @@ function importDefinitelyTypedTests(definitelyTypedRoot: string): void {
99115

100116
if (testFiles.length === 0) {
101117
// no test files but multiple d.ts's, e.g. winjs
102-
var regexp = new RegExp(d + "(([-][0-9])|([\.]d[\.]ts))");
118+
let regexp = new RegExp(d + "(([-][0-9])|([\.]d[\.]ts))");
103119
if (tsFiles.length > 1 && tsFiles.every(t => fileExtensionIs(t, ".d.ts") && regexp.test(t))) {
104120
tsFiles.forEach(filename => {
105121
importDefinitelyTypedTest(path.basename(filename, ".d.ts"), [filename], paramFile);

0 commit comments

Comments
 (0)