import * as glob from 'glob' ; import * as fs from 'fs-extra' ; import * as path from 'path' ; import Mock from './mock' ; /** Registry represents a group of phases grouped under one name. */ class Processor { static PCWD : string = path . resolve ( __dirname , '..' ) ; static PNMD : string = path . join ( Processor . PCWD , 'node_modules' ) ; static PTD : string = path . join ( Processor . PCWD , 'templates' ) ; static PTID = path . join ( Processor . PTD , 'interface' ) ; param { string } directory returns { Mock [ ] } processMocks ( directory ) { const mocks = [ ] ; glob . sync ( '**/*.json' , { cwd : directory , root : '/' } ) . forEach ( ( file ) => mocks . push ( fs . readJsonSync ( path . join ( directory , file ) ) ) ) ; return mocks ; } param { string } directory param { string } baseUrl generateProtractorMock ( directory , baseUrl ) { const from = path . join ( Processor . PTD , 'protractor.mock.js' ) ; const to = path . join ( directory , 'protractor.mock.js' ) ; if ( baseUrl ) { try { const data = fs . readFileSync ( from , 'utf-8' ) ; const newValue = data . replace ( /const requestUrl = undefined;/gi , 'const requestUrl = \'' + baseUrl + '\';' ) ; fs . writeFileSync ( to , newValue , 'utf-8' ) ; } catch ( error ) { console . error ( 'Unable to rewrite protractor.mock.js' , error ) ; } } else { fs . copySync ( from , to ) ; } } param { string } directory generateMockingInterface ( directory ) { /** Check if the plugin has a different version of angular as the application. */ const anmd = ! fs . existsSync ( path . join ( Processor . PNMD , 'angular' ) ) ? path . join ( process . cwd ( ) , 'node_modules' ) : Processor . PNMD , arnmd = ! fs . existsSync ( path . join ( Processor . PNMD , 'angular-resource' ) ) ? path . join ( process . cwd ( ) , 'node_modules' ) : Processor . PNMD , angularJs = path . join ( anmd , 'angular' , 'angular.min.js' ) , angularResource = path . join ( arnmd , 'angular-resource' , 'angular-resource.min.js' ) ; glob . sync ( '**/*' , { cwd : Processor . PTID , root : '/' } ) . forEach ( ( file ) => fs . copySync ( path . join ( Processor . PTID , file ) , path . join ( directory , file ) ) ) ; fs . copySync ( angularJs , path . join ( directory , 'js' , 'angular.min.js' ) ) ; fs . copySync ( angularResource , path . join ( directory , 'js' , 'angular-resource.min.js' ) ) ; } } export default Processor ;