import * as fs from 'fs' ; import * as path from 'path' ; export class Importer { public static importUserProjectFiles ( codeRootPath ) { Importer . getImportFiles ( codeRootPath ) . forEach ( Importer . importWithoutExtension ) ; } private static importWithoutExtension ( file ) { require ( Importer . removeDevExtension ( file ) ) ; } private static getImportFiles ( codeRootPath ) { return Importer . walkDir ( codeRootPath ) . filter ( Importer . isJavaScriptFile ) . filter ( Importer . isNotIndexJs ) ; } private static walkDir ( dir ) { const files = [ ] ; Importer . listDirectory ( dir ) . forEach ( ( file ) : void => { const fileName = path . join ( dir , file ) ; if ( Importer . isDirectory ( fileName ) ) { const filesInDirectory = Importer . walkDir ( fileName ) ; files . push ( ... filesInDirectory ) ; } else { files . push ( fileName ) ; } } ) ; return files ; } private static isJavaScriptFile ( file ) { return file . match ( /.*\.js$/ ) != null ; } private static isNotIndexJs ( file ) { return file . match ( /index.js$/ ) == null ; } private static removeDevExtension ( file ) { return path . join ( file . replace ( /(\.d)?(\.ts|\.js)/ , '' ) ) ; } private static listDirectory ( dir ) { return fs . readdirSync ( dir ) ; } private static isDirectory ( fileName ) { return fs . statSync ( fileName ) . isDirectory ( ) ; } }