From 0997db20d47479e7bc63c3e5c6e6b7f7ecbb802a Mon Sep 17 00:00:00 2001 From: Evgenii Korolevskii Date: Wed, 31 Aug 2022 15:41:46 +0200 Subject: [PATCH] refactoring --- src/authutil.ts | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/src/authutil.ts b/src/authutil.ts index 9599979..d88c347 100644 --- a/src/authutil.ts +++ b/src/authutil.ts @@ -67,41 +67,30 @@ function writeFeedToFile( if (fs.existsSync(existingFileLocation)) { // get key from existing NuGet.config so NuGet/dotnet can match credentials const curContents: string = fs.readFileSync(existingFileLocation, 'utf8'); - var json = xmlParser.parse(curContents, {ignoreAttributes: false}); + const json = xmlParser.parse(curContents, {ignoreAttributes: false}); - if (typeof json.configuration == 'undefined') { + if (typeof json.configuration === 'undefined') { throw new Error(`The provided NuGet.config seems invalid.`); } - if (typeof json.configuration.packageSources != 'undefined') { - if (typeof json.configuration.packageSources.add != 'undefined') { - // file has at least one - if (typeof json.configuration.packageSources.add[0] == 'undefined') { - // file has only one - if ( - json.configuration.packageSources.add['@_value'] - .toLowerCase() - .includes(feedUrl.toLowerCase()) - ) { - let key = json.configuration.packageSources.add['@_key']; + + if (json.configuration?.packageSources?.add) { + const packageSources = json.configuration.packageSources.add; + + if (Array.isArray(packageSources)) { + packageSources.forEach((source) => { + const value = source["@_value"]; + core.debug(`source '${value}'`); + if (value.toLowerCase().includes(feedUrl.toLowerCase())) { + const key = source["@_key"]; sourceKeys.push(key); core.debug(`Found a URL with key ${key}`); } - } else { - // file has 2+ - for ( - let i = 0; - i < json.configuration.packageSources.add.length; - i++ - ) { - const source = json.configuration.packageSources.add[i]; - const value = source['@_value']; - core.debug(`source '${value}'`); - if (value.toLowerCase().includes(feedUrl.toLowerCase())) { - let key = source['@_key']; - sourceKeys.push(key); - core.debug(`Found a URL with key ${key}`); - } - } + }); + } else { + if (packageSources["@_value"].toLowerCase().includes(feedUrl.toLowerCase())) { + const key = packageSources["@_key"]; + sourceKeys.push(key); + core.debug(`Found a URL with key ${key}`); } } }