11import { Engine } from 'php-parser' ;
22
3- function parseWpConfig ( content : string , options : any = { } ) {
4- const engine = new Engine ( options ) ;
5- const { children } = engine . parseCode ( content , '' ) ;
6- let database , dbHost = 'localhost' , dbPort = 3306 , dbUser ;
7- for ( const child of children as any [ ] ) {
8- if ( child . expression ?. what ?. name === 'define' ) {
9- const [ name , value ] = child . expression . arguments ;
10- if ( name . kind === 'string' && value . kind === 'string' ) {
11- if ( name . value === 'DB_NAME' ) {
12- database = value . value as string ;
13- }
14- if ( name . value === 'DB_HOST' ) {
15- const [ host , port ] = value . value . split ( ':' ) ;
16- dbHost = host ?? 'localhost' ;
17- if ( port !== undefined ) {
18- dbPort = parseInt ( port ) ;
3+ export default {
4+ parseWpConfig : function ( content : string , options : any = { } ) {
5+ const engine = new Engine ( options ) ;
6+ const { children } = engine . parseCode ( content , '' ) ;
7+ let database , dbHost = 'localhost' , dbPort = 3306 , dbUser ;
8+ for ( const child of children as any [ ] ) {
9+ if ( child . expression ?. what ?. name === 'define' ) {
10+ const [ name , value ] = child . expression . arguments ;
11+ if ( name . kind === 'string' && value . kind === 'string' ) {
12+ if ( name . value === 'DB_NAME' ) {
13+ database = value . value as string ;
14+ }
15+ if ( name . value === 'DB_HOST' ) {
16+ const [ host , port ] = value . value . split ( ':' ) ;
17+ dbHost = host ?? 'localhost' ;
18+ if ( port !== undefined ) {
19+ dbPort = parseInt ( port ) ;
20+ }
21+ }
22+ if ( name . value === 'DB_USER' ) {
23+ dbUser = value . value as string ;
1924 }
20- }
21- if ( name . value === 'DB_USER' ) {
22- dbUser = value . value as string ;
2325 }
2426 }
2527 }
26- }
27- return { database, dbHost, dbPort, dbUser } ;
28- }
29-
30- export default {
31- parseWpConfig,
28+ return { database, dbHost, dbPort, dbUser } ;
29+ } ,
3230}
0 commit comments