const xpath = require ( 'xpath' ) ;
const { DOMParser} = require ( 'xmldom' ) ;
const axios = require ( 'axios' ) ;
const fs = require ( 'fs' ) ;
const apiList = [ ] ;
function getEnumList ( ) { return axios. get ( "https://learn.microsoft.com/zh-cn/office/vba/api/word(enumerations)" ) . then ( response => { let data = response. data; let doc = new DOMParser ( ) . parseFromString ( data) ; let nodelistx = xpath. select ( '//main/div[3]/ul[1]/li' , doc) ; nodelistx. forEach ( itemx => { let node = itemx. textContent; let item = node. toLowerCase ( ) ; let apiObj = { } ; apiObj. url = 'https://learn.microsoft.com/zh-cn/office/vba/api/word.' + item; apiObj. params = { } ; apiObj. EnumField= node; apiList. push ( apiObj) ; } ) ; } ) . catch ( error => { console. error ( error) ; } ) ;
}
function fetchApiList ( apiList ) { const promises = apiList. map ( api => { return axios. get ( api. url, { params : api. params, headers : { } } ) . then ( response => { let data = response. data; let doc = new DOMParser ( ) . parseFromString ( data) ; let enumEn = xpath. select ( '//main/div[3]/h1' , doc) . at ( 0 ) ; let enumCh = xpath. select ( '//main/div[3]/p[1]' , doc) . at ( 0 ) ; let enumStr = '<!--' + enumEn. textContent+ '====' + enumCh. textContent+ '-->' ; enumStr+= '<' + api. EnumField+ '>' ; let nodelist = xpath. select ( '//table/tbody/tr' , doc) ; nodelist. forEach ( node => { enumStr+= '<Item name="' + node. childNodes[ 1 ] . textContent+ '" ' + 'value="' + node. childNodes[ 3 ] . textContent+ '" ' + 'description="' + node. childNodes[ 5 ] . textContent+ '"/>' ; } ) ; enumStr+= '</' + api. EnumField+ '>' ; return enumStr; } ) . catch ( error => { console. error ( error) ; } ) ; } ) ; return Promise. all ( promises) ;
} getEnumList ( ) . then ( ( ) => { fetchApiList ( apiList) . then ( ( nodelist ) => { let enumStr = '<?xml version="1.0" encoding="UTF-8"?><WordEnum>' ; nodelist. forEach ( node => { enumStr+= node; } ) ; enumStr+= "</WordEnum>" const filename = 'D:\\tirklee\\QTWork\\InterfacePlatform\\config\\word\\WordEnum.xml' ; const writeStream = fs. createWriteStream ( filename) ; writeStream. write ( enumStr, ( ) => { console. log ( 'File written successfully.' ) ; } ) ; writeStream. end ( ) ; } ) . catch ( error => { console. error ( error) ; } ) ;
} ) ;