refactoring

This commit is contained in:
Evgenii Korolevskii 2022-08-31 15:41:46 +02:00
parent f7747565a1
commit 0997db20d4
1 changed files with 18 additions and 29 deletions

View File

@ -67,41 +67,30 @@ function writeFeedToFile(
if (fs.existsSync(existingFileLocation)) { if (fs.existsSync(existingFileLocation)) {
// get key from existing NuGet.config so NuGet/dotnet can match credentials // get key from existing NuGet.config so NuGet/dotnet can match credentials
const curContents: string = fs.readFileSync(existingFileLocation, 'utf8'); 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.`); throw new Error(`The provided NuGet.config seems invalid.`);
} }
if (typeof json.configuration.packageSources != 'undefined') {
if (typeof json.configuration.packageSources.add != 'undefined') { if (json.configuration?.packageSources?.add) {
// file has at least one <add> const packageSources = json.configuration.packageSources.add;
if (typeof json.configuration.packageSources.add[0] == 'undefined') {
// file has only one <add> if (Array.isArray(packageSources)) {
if ( packageSources.forEach((source) => {
json.configuration.packageSources.add['@_value'] const value = source["@_value"];
.toLowerCase() core.debug(`source '${value}'`);
.includes(feedUrl.toLowerCase()) if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
) { const key = source["@_key"];
let key = json.configuration.packageSources.add['@_key'];
sourceKeys.push(key); sourceKeys.push(key);
core.debug(`Found a URL with key ${key}`); core.debug(`Found a URL with key ${key}`);
} }
} else { });
// file has 2+ <add> } else {
for ( if (packageSources["@_value"].toLowerCase().includes(feedUrl.toLowerCase())) {
let i = 0; const key = packageSources["@_key"];
i < json.configuration.packageSources.add.length; sourceKeys.push(key);
i++ core.debug(`Found a URL with key ${key}`);
) {
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}`);
}
}
} }
} }
} }