1+ declare var require : any , process : any ;
2+ declare var __dirname : any ;
3+
4+ var fs = require ( "fs" ) ;
5+ var path = require ( "path" ) ;
6+ var child_process = require ( 'child_process' ) ;
7+
8+ var tscRoot = path . join ( __dirname , "..\\" ) ;
9+ var tscPath = path . join ( tscRoot , "built" , "instrumented" , "tsc.js" ) ;
10+ var rwcTestPath = path . join ( tscRoot , "tests" , "cases" , "rwc" , "dt" ) ;
11+ var definitelyTypedRoot = process . argv [ 2 ] ;
12+
13+ function fileExtensionIs ( path : string , extension : string ) : boolean {
14+ var pathLen = path . length ;
15+ var extLen = extension . length ;
16+ return pathLen > extLen && path . substr ( pathLen - extLen , extLen ) . toLocaleLowerCase ( ) === extension . toLocaleLowerCase ( ) ;
17+ }
18+
19+ function copyFileSync ( source , destination ) {
20+ var text = fs . readFileSync ( source ) ;
21+ fs . writeFileSync ( destination , text ) ;
22+ }
23+
24+ function importDefinitelyTypedTest ( testCaseName : string , testFiles : string [ ] , responseFile : string ) {
25+ var cmd = "node " + tscPath + " --module commonjs " + testFiles . join ( " " ) ;
26+ if ( responseFile ) cmd += " @" + responseFile ;
27+
28+ var testDirectoryName = testCaseName + "_" + Math . floor ( ( Math . random ( ) * 10000 ) + 1 ) ;
29+ var testDirectoryPath = path . join ( process . env [ "temp" ] , testDirectoryName ) ;
30+ if ( fs . existsSync ( testDirectoryPath ) ) {
31+ throw new Error ( "Could not create test directory" ) ;
32+ }
33+ fs . mkdirSync ( testDirectoryPath ) ;
34+
35+ child_process . exec ( cmd , {
36+ maxBuffer : 1 * 1024 * 1024 ,
37+ cwd : testDirectoryPath
38+ } , ( error , stdout , stderr ) => {
39+ //console.log("importing " + testCaseName + " ...");
40+ //console.log(cmd);
41+
42+ if ( error ) {
43+ console . log ( "importing " + testCaseName + " ..." ) ;
44+ console . log ( cmd ) ;
45+ console . log ( "==> error " + JSON . stringify ( error ) ) ;
46+ console . log ( "==> stdout " + String ( stdout ) ) ;
47+ console . log ( "==> stderr " + String ( stderr ) ) ;
48+ console . log ( "\r\n" ) ;
49+ return ;
50+ }
51+
52+ // copy generated file to output location
53+ var outputFilePath = path . join ( testDirectoryPath , "iocapture0.json" ) ;
54+ var testCasePath = path . join ( rwcTestPath , "DefinitelyTyped_" + testCaseName + ".json" ) ;
55+ copyFileSync ( outputFilePath , testCasePath ) ;
56+
57+ //console.log("output generated at: " + outputFilePath);
58+
59+ if ( ! fs . existsSync ( testCasePath ) ) {
60+ throw new Error ( "could not find test case at: " + testCasePath ) ;
61+ }
62+ else {
63+ fs . unlinkSync ( outputFilePath ) ;
64+ fs . rmdirSync ( testDirectoryPath ) ;
65+ //console.log("testcase generated at: " + testCasePath);
66+ //console.log("Done.");
67+ }
68+ //console.log("\r\n");
69+
70+ } )
71+ . on ( 'error' , function ( error ) {
72+ console . log ( "==> error " + JSON . stringify ( error ) ) ;
73+ console . log ( "\r\n" ) ;
74+ } ) ;
75+ }
76+
77+ function importDefinitelyTypedTests ( definitelyTypedRoot : string ) : void {
78+ fs . readdir ( definitelyTypedRoot , ( err , subDirectorys ) => {
79+ if ( err ) throw err ;
80+
81+ subDirectorys
82+ . filter ( d => [ "_infrastructure" , "node_modules" , ".git" ] . indexOf ( d ) >= 0 )
83+ . filter ( i => fs . statSync ( path . join ( definitelyTypedRoot , i ) ) . isDirectory ( ) )
84+ . forEach ( d => {
85+ var directoryPath = path . join ( definitelyTypedRoot , d ) ;
86+ fs . readdir ( directoryPath , function ( err , files ) {
87+ if ( err ) throw err ;
88+
89+ var tsFiles = [ ] ;
90+ var testFiles = [ ] ;
91+ var paramFile ;
92+
93+ files
94+ . map ( f => path . join ( directoryPath , f ) )
95+ . forEach ( f => {
96+ if ( fileExtensionIs ( f , ".ts" ) ) tsFiles . push ( f ) ;
97+ else if ( fileExtensionIs ( f , ".tscparams" ) ) paramFile = f ;
98+
99+ if ( fileExtensionIs ( f , "-tests.ts" ) ) testFiles . push ( f ) ;
100+ } ) ;
101+
102+ if ( testFiles . length === 0 ) {
103+ // no test files but multiple d.ts's, e.g. winjs
104+ var regexp = new RegExp ( d + "(([-][0-9])|([\.]d[\.]ts))" ) ;
105+ if ( tsFiles . length > 1 && tsFiles . every ( t => fileExtensionIs ( t , ".d.ts" ) && regexp . test ( t ) ) ) {
106+ tsFiles . forEach ( filename => {
107+ importDefinitelyTypedTest ( path . basename ( filename , ".d.ts" ) , [ filename ] , paramFile ) ;
108+ } ) ;
109+ }
110+ else {
111+ importDefinitelyTypedTest ( d , tsFiles , paramFile ) ;
112+ }
113+ }
114+ else {
115+ testFiles . forEach ( filename => {
116+ importDefinitelyTypedTest ( path . basename ( filename , "-tests.ts" ) , [ filename ] , paramFile ) ;
117+ } ) ;
118+ }
119+ } ) ;
120+ } )
121+ } ) ;
122+ }
123+
124+ importDefinitelyTypedTests ( definitelyTypedRoot ) ;
0 commit comments