1+ import * as fs from "fs" ;
2+ import * as path from "path" ;
3+ import * as child_process from "child_process" ;
4+
5+
6+ interface Map < T > {
7+ [ key : string ] : T ;
8+ }
9+
10+ declare var process : {
11+ argv : string [ ] ;
12+ env : Map < string > ;
13+ exit ( exitCode ?: number ) : void ;
14+ }
15+
16+ main ( ) ;
17+ function main ( ) {
18+ const [ , progName , tscRoot , definitelyTypedRoot ] = process . argv ;
19+ if ( process . argv . length !== 4 ) {
20+ if ( process . argv . length < 2 ) {
21+ throw "Expected at least 2 argv elements."
22+ }
23+ console . log ( "Usage:" )
24+ console . log ( ` node ${ path . relative ( __dirname , progName ) } [TypeScript Repo Root] [DefinitelyTyped Repo Root]` ) ;
25+ return ;
26+ }
27+
28+ const tscPath = path . resolve ( tscRoot , "built" , "local" , "tsc.js" ) ;
29+ const rwcTestPath = path . resolve ( tscRoot , "internal" , "cases" , "rwc" ) ;
30+ const resolvedDefinitelyTypedRoot = path . resolve ( definitelyTypedRoot ) ;
31+
32+ console . log ( `Resolved TypeScript Compiler Path: '${ tscPath } '.` ) ;
33+ console . log ( `Resolved TypeScript RWC Path: '${ rwcTestPath } '.` ) ;
34+ console . log ( `Resolved DefinitelyTyped Repo Root: '${ resolvedDefinitelyTypedRoot } '.` ) ;
35+ importDefinitelyTypedTests ( tscPath , rwcTestPath , resolvedDefinitelyTypedRoot ) ;
36+ }
37+
38+ function filePathEndsWith ( path : string , endingString : string ) : boolean {
39+ const pathLen = path . length ;
40+ const extLen = endingString . length ;
41+ return pathLen > extLen && path . substr ( pathLen - extLen , extLen ) . toLocaleLowerCase ( ) === endingString . toLocaleLowerCase ( ) ;
42+ }
43+
44+ function copyFileSync ( source : string , destination : string ) {
45+ let text = fs . readFileSync ( source ) ;
46+ fs . writeFileSync ( destination , text ) ;
47+ }
48+
49+ function importDefinitelyTypedTest ( tscPath : string , rwcTestPath : string , testCaseName : string , testFiles : string [ ] , responseFile : string ) {
50+ let cmd = "node " + tscPath + " --module commonjs " + testFiles . join ( " " ) ;
51+ if ( responseFile ) {
52+ cmd += " @" + responseFile ;
53+ }
54+
55+ let testDirectoryName = testCaseName + "_" + Math . floor ( ( Math . random ( ) * 10000 ) + 1 ) ;
56+ let testDirectoryPath = path . join ( process . env [ "temp" ] , testDirectoryName ) ;
57+ if ( fs . existsSync ( testDirectoryPath ) ) {
58+ throw new Error ( "Could not create test directory" ) ;
59+ }
60+ fs . mkdirSync ( testDirectoryPath ) ;
61+
62+ child_process . exec ( cmd , {
63+ maxBuffer : 1 * 1024 * 1024 ,
64+ cwd : testDirectoryPath
65+ } , ( error , stdout , stderr ) => {
66+ console . log ( "importing " + testCaseName + " ..." ) ;
67+ console . log ( cmd ) ;
68+
69+ if ( error ) {
70+ console . log ( "importing " + testCaseName + " ..." ) ;
71+ console . log ( cmd ) ;
72+ console . log ( "==> error " + JSON . stringify ( error ) ) ;
73+ console . log ( "==> stdout " + String ( stdout ) ) ;
74+ console . log ( "==> stderr " + String ( stderr ) ) ;
75+ console . log ( "\r\n" ) ;
76+ return ;
77+ }
78+
79+ // copy generated file to output location
80+ let outputFilePath = path . join ( testDirectoryPath , "iocapture0.json" ) ;
81+ let testCasePath = path . join ( rwcTestPath , "DefinitelyTyped_" + testCaseName + ".json" ) ;
82+ copyFileSync ( outputFilePath , testCasePath ) ;
83+
84+ //console.log("output generated at: " + outputFilePath);
85+
86+ if ( ! fs . existsSync ( testCasePath ) ) {
87+ throw new Error ( "could not find test case at: " + testCasePath ) ;
88+ }
89+ else {
90+ fs . unlinkSync ( outputFilePath ) ;
91+ fs . rmdirSync ( testDirectoryPath ) ;
92+ //console.log("testcase generated at: " + testCasePath);
93+ //console.log("Done.");
94+ }
95+ //console.log("\r\n");
96+
97+ } )
98+ . on ( 'error' , ( error : any ) => {
99+ console . log ( "==> error " + JSON . stringify ( error ) ) ;
100+ console . log ( "\r\n" ) ;
101+ } ) ;
102+ }
103+
104+ function importDefinitelyTypedTests ( tscPath : string , rwcTestPath : string , definitelyTypedRoot : string ) : void {
105+ fs . readdir ( definitelyTypedRoot , ( err , subDirectories ) => {
106+ if ( err ) {
107+ throw err ;
108+ }
109+
110+ // When you just want to test the script out on one or two files,
111+ // just add a line like the following:
112+ //
113+ // .filter(d => d.indexOf("sipml") >= 0 )
114+ subDirectories
115+ . filter ( d => [ "_infrastructure" , "node_modules" , ".git" ] . indexOf ( d ) < 0 )
116+ . filter ( i => fs . statSync ( path . join ( definitelyTypedRoot , i ) ) . isDirectory ( ) )
117+ . forEach ( d => {
118+ const directoryPath = path . join ( definitelyTypedRoot , d ) ;
119+ fs . readdir ( directoryPath , function ( err , files ) {
120+ if ( err ) {
121+ throw err ;
122+ }
123+
124+ let tsFiles : string [ ] = [ ] ;
125+ let testFiles : string [ ] = [ ] ;
126+ let paramFile : string ;
127+
128+ for ( const filePath of files . map ( f => path . join ( directoryPath , f ) ) ) {
129+ if ( filePathEndsWith ( filePath , ".ts" ) ) {
130+ tsFiles . push ( filePath ) ;
131+
132+ if ( filePathEndsWith ( filePath , "-tests.ts" ) ) {
133+ testFiles . push ( filePath ) ;
134+ }
135+ }
136+ else if ( filePathEndsWith ( filePath , ".tscparams" ) ) {
137+ paramFile = filePath ;
138+ }
139+ }
140+
141+ if ( testFiles . length === 0 ) {
142+ // no test files but multiple d.ts's, e.g. winjs
143+ const regexp = new RegExp ( d + "(([-][0-9])|([\.]d[\.]ts))" ) ;
144+ if ( tsFiles . length > 1 && tsFiles . every ( t => filePathEndsWith ( t , ".d.ts" ) && regexp . test ( t ) ) ) {
145+ for ( const fileName of tsFiles ) {
146+ importDefinitelyTypedTest ( tscPath , rwcTestPath , path . basename ( fileName , ".d.ts" ) , [ fileName ] , paramFile ) ;
147+ }
148+ }
149+ else {
150+ importDefinitelyTypedTest ( tscPath , rwcTestPath , d , tsFiles , paramFile ) ;
151+ }
152+ }
153+ else {
154+ for ( const fileName of tsFiles ) {
155+ importDefinitelyTypedTest ( tscPath , rwcTestPath , path . basename ( fileName , "-tests.ts" ) , [ fileName ] , paramFile ) ;
156+ }
157+ }
158+ } ) ;
159+ } )
160+ } ) ;
161+ }
0 commit comments