import { expect } from "chai" ; import * as dts from "dts-bundle" ; import { existsSync , readFile , readFileSync } from "fs" ; import * as glob from "glob" ; const NODEJS_DEPENDENCY_EXCEPTIONS = [ "node-fetch" , "abort-controller" , "safe-buffer" , "@xmldom/xmldom" , "tmp" ] ; const EXTERNAL_EXCEPTIONS = [ "preact/hooks" ] ; function calcExternals ( main = "types/index.d.ts" , out = "dist/index.d.ts" ) { const bundleInfo = dts . bundle ( { name : "__dummy__" , baseDir : "." , main , out , outputAsModuleFolder : true } ) ; const externals = [ ] ; for ( const key in bundleInfo . fileMap ) { for ( const external of bundleInfo . fileMap [ key ] . externalImports ) { if ( externals . indexOf ( external ) < 0 ) { externals . push ( external ) ; } } } return externals ; } describe ( "Types" , function ( ) { it ( "tsc 2.9.x import issue" , function ( done ) { glob ( "../../packages/*/types/*.d.ts" , { } , function ( er , files ) { Promise . all ( files . map ( ( f ) => { return new Promise < void > ( ( resolve , reject ) => { readFile ( f , "utf8" , function ( err , data ) { if ( err ) throw err ; expect ( data . indexOf ( 'import("' ) , f ) . to . equal ( - 1 ) ; resolve ( ) ; } ) ; } ) ; } ) ) . then ( ( ) => { done ( ) ; } ) ; } ) ; } ) ; it ( "dependencies" , function ( done ) { glob ( "../../packages/*/" , { } , function ( er , folders ) { Promise . all ( folders . filter ( ( folder ) => folder . indexOf ( "codemirror-shim" ) < 0 ) . map ( ( folder ) => { return new Promise < void > ( ( resolve , reject ) => { const pkg = JSON . parse ( readFileSync ( `${ folder }package.json` , "utf8" ) ) ; if ( pkg . module && pkg . module . indexOf ( "lib-es6/" ) === 0 ) { resolve ( ) ; } else { glob ( `${ folder }types/**/*.d.ts` , { } , function ( err , files ) { if ( err ) throw err ; let typePath = `${ folder }types/index.node.d.ts` ; if ( ! existsSync ( typePath ) ) { typePath = `${ folder }types/index.d.ts` ; } if ( existsSync ( typePath ) ) { const externals = calcExternals ( typePath , `tmp/${ pkg . name }` ) ; externals . filter ( external => EXTERNAL_EXCEPTIONS . indexOf ( external ) < 0 ) . forEach ( external => { if ( pkg . name . indexOf ( "-shim" ) < 0 && ( ! pkg . dependencies || ( ! pkg . dependencies [ external ] && ! pkg . dependencies [ "@types/" + external ] ) ) ) { expect ( false , `${ pkg . name }:${ folder } missing dependency:  ${ external }` ) . to . be . true ; } } ) ; for ( const key in pkg . dependencies ) { const deps = key . indexOf ( "@types/" ) === 0 ? key . substr ( 7 ) : key ; if ( NODEJS_DEPENDENCY_EXCEPTIONS . indexOf ( deps ) < 0 && key . indexOf ( "@hpcc-js" ) < 0 && externals . indexOf ( deps ) < 0 ) { expect ( false , `${ pkg . name }:${ folder } extraneous dependency:  ${ deps }` ) . to . be . true ; } } } resolve ( ) ; } ) ; } } ) ; } ) ) . then ( ( ) => { done ( ) ; } ) ; } ) ; } ) ; } ) ;