@@ -43,26 +43,82 @@ export async function getVersion() {
4343 }
4444}
4545
46+ /**
47+ * Get btp CLI info
48+ * @returns {Promise<Object> }
49+ */
50+ export async function getInfo ( ) {
51+ base . debug ( 'getInfo' )
52+ try {
53+ const exec = promisify ( child_process . exec )
54+ let script = `btp --info`
55+
56+ const { stdout } = await exec ( script )
57+ if ( stdout ) {
58+ try {
59+ let infoOut = { }
60+ let result = stdout . split ( "\n" )
61+ if ( result [ 6 ] ) {
62+ let config = result [ 6 ]
63+ let configOut = config . split ( / : ( .* ) / s)
64+ infoOut . Configuration = configOut [ 1 ] . trim ( )
65+ }
66+
67+ if ( result [ 4 ] ) {
68+ let url = result [ 4 ]
69+ let urlOut = url . split ( / : ( .* ) / s)
70+ infoOut . serverURL = urlOut [ 1 ] . trim ( )
71+ }
72+
73+ if ( result [ 5 ] ) {
74+ let user = result [ 5 ]
75+ let userOut = user . split ( / : ( .* ) / s)
76+ infoOut . user = userOut [ 1 ] . trim ( )
77+ }
78+
79+ return infoOut
80+ } catch ( e ) {
81+ return
82+ }
83+ }
84+ return
85+
86+ } catch ( error ) {
87+ base . debug ( error )
88+ throw ( error )
89+ }
90+ }
91+
4692/**
4793 * Read central configuration file for BTP CLI
4894 * @returns {Promise<object> }
4995 */
5096export async function getBTPConfig ( ) {
5197 base . debug ( 'getBTPConfig' )
52- try {
53- let localDir = process . env . BTP_CLIENTCONFIG
54- base . debug ( localDir )
55- if ( ! localDir ) {
56- if ( process . env . APPDATA ) {
57- localDir = `${ process . env . APPDATA } /SAP/btp/config.json`
58- } else if ( process . platform == 'darwin' ) {
59- localDir = `${ process . env . HOME } /Library/Preferences/SAP/btp/config.json`
60- } else {
61- localDir = `${ process . env . HOME } /.config/.btp/config.json`
62- }
98+
99+ let localDir = process . env . BTP_CLIENTCONFIG
100+ if ( ! localDir ) {
101+ let info = await getInfo ( )
102+ if ( info . Configuration ) {
103+ localDir = info . Configuration
104+ }
105+ }
106+ if ( ! localDir || ! fs . existsSync ( localDir ) ) {
107+ if ( process . env . APPDATA ) {
108+ localDir = `${ process . env . APPDATA } /SAP/btp/config.json`
109+ } else if ( process . platform == 'darwin' ) {
110+ localDir = `${ process . env . HOME } /Library/Preferences/SAP/btp/config.json`
111+ } else {
112+ localDir = `${ process . env . HOME } /.config/.btp/config.json`
63113 }
64- base . debug ( localDir )
65- const data = fs . readFileSync ( localDir ,
114+ }
115+ //MacOS fallback location
116+ if ( ! fs . existsSync ( localDir ) && process . platform == 'darwin' ) {
117+ localDir = `${ process . env . HOME } /Library/Application Support/.btp/config.json`
118+ }
119+ base . debug ( localDir )
120+ try {
121+ let data = fs . readFileSync ( localDir ,
66122 { encoding : 'utf8' , flag : 'r' } )
67123 const object = JSON . parse ( data )
68124 return object
@@ -84,9 +140,7 @@ export async function getBTPTarget() {
84140 return config . TargetHierarchy
85141 } else {
86142 throw new Error ( bundle . getText ( "err.BTPNoTarget" ) )
87-
88143 }
89-
90144}
91145
92146/**
0 commit comments