<commit_before>import * as fs from 'fs';
import * as path from 'path';
import * as Q from 'q';
import * as url from 'url';

import * as tl from 'vsts-task-lib/task';
import * as vsts from 'vso-node-api/WebApi';

import { INpmRegistry, NpmRegistry } from './npmregistry';
import * as NpmrcParser from './npmrcparser';

export function appendToNpmrc(npmrc: string, data: string): void {
    tl.writeFile(npmrc, data, {
        flag: 'a'
    } as tl.FsOptions);
}

export async function getLocalRegistries(npmrc: string): Promise<string[]> {
    let localRegistries: string[] = [];
    let registries = NpmrcParser.GetRegistries(npmrc);
    let collectionUrl = url.parse(await getPackagingCollectionUrl());

    for (let registry of registries) {
        let registryUrl = url.parse(registry);
        if (registryUrl.host.toLowerCase() === collectionUrl.host.toLowerCase()) {
            localRegistries.push(registry);
        }
    }

    tl.debug(tl.loc('FoundLocalRegistries', localRegistries.length));
    return localRegistries;
}

export function getFeedIdFromRegistry(registry: string) {
    let registryUrl = url.parse(registry);
    let registryPathname = registryUrl.pathname.toLowerCase();
    let startingToken = '/_packaging/';
    let startingIndex = registryPathname.indexOf(startingToken);
    let endingIndex = registryPathname.indexOf('/npm/registry');

    return registryUrl.pathname.substring(startingIndex + startingToken.length, endingIndex);
}

export async function getLocalNpmRegistries(workingDir: string): Promise<INpmRegistry[]> {
    let localNpmRegistries: INpmRegistry[] = [];
    let npmrcPath = path.join(workingDir, '.npmrc');

    if (tl.exist(npmrcPath)) {
        for (let registry of await getLocalRegistries(npmrcPath)) {
            localNpmRegistries.push(await NpmRegistry.FromFeedId(getFeedIdFromRegistry(registry), true));
        }
    return localNpmRegistries;
    }

    return localNpmRegistries;
}

export async function getPackagingCollectionUrl(): Promise<string> {
    let forcedUrl = tl.getVariable('Npm.PackagingCollectionUrl');
    if (forcedUrl) {
        let testUrl = url.parse(forcedUrl);
        tl.debug(tl.loc('ForcePackagingUrl', forcedUrl));
        return Q(url.format(testUrl));
    }

    let collectionUrl = url.parse(tl.getVariable('System.TeamFoundationCollectionUri'));

    if (collectionUrl.hostname.toUpperCase().endsWith('.VISUALSTUDIO.COM')) {
        let hostParts = collectionUrl.hostname.split('.');
        let packagingHostName = hostParts[0] + '.pkgs.visualstudio.com';
        collectionUrl.hostname = packagingHostName;
        // remove the host property so it doesn't override the hostname property for url.format
        delete collectionUrl.host;
    }

    return Q(url.format(collectionUrl));<commit_msg>fixed double return statement<commit_after>import * as fs from 'fs';
import * as path from 'path';
import * as Q from 'q';
import * as url from 'url';

import * as tl from 'vsts-task-lib/task';
import * as vsts from 'vso-node-api/WebApi';

import { INpmRegistry, NpmRegistry } from './npmregistry';
import * as NpmrcParser from './npmrcparser';

export function appendToNpmrc(npmrc: string, data: string): void {
    tl.writeFile(npmrc, data, {
        flag: 'a'
    } as tl.FsOptions);
}

export async function getLocalRegistries(npmrc: string): Promise<string[]> {
    let localRegistries: string[] = [];
    let registries = NpmrcParser.GetRegistries(npmrc);
    let collectionUrl = url.parse(await getPackagingCollectionUrl());

    for (let registry of registries) {
        let registryUrl = url.parse(registry);
        if (registryUrl.host.toLowerCase() === collectionUrl.host.toLowerCase()) {
            localRegistries.push(registry);
        }
    }

    tl.debug(tl.loc('FoundLocalRegistries', localRegistries.length));
    return localRegistries;
}

export function getFeedIdFromRegistry(registry: string) {
    let registryUrl = url.parse(registry);
    let registryPathname = registryUrl.pathname.toLowerCase();
    let startingToken = '/_packaging/';
    let startingIndex = registryPathname.indexOf(startingToken);
    let endingIndex = registryPathname.indexOf('/npm/registry');

    return registryUrl.pathname.substring(startingIndex + startingToken.length, endingIndex);
}

export async function getLocalNpmRegistries(workingDir: string): Promise<INpmRegistry[]> {
    let localNpmRegistries: INpmRegistry[] = [];
    let npmrcPath = path.join(workingDir, '.npmrc');

    if (tl.exist(npmrcPath)) {
        for (let registry of await getLocalRegistries(npmrcPath)) {
            localNpmRegistries.push(await NpmRegistry.FromFeedId(getFeedIdFromRegistry(registry), true));
        }
    }

    return localNpmRegistries;
}

export async function getPackagingCollectionUrl(): Promise<string> {
    let forcedUrl = tl.getVariable('Npm.PackagingCollectionUrl');
    if (forcedUrl) {
        let testUrl = url.parse(forcedUrl);
        tl.debug(tl.loc('ForcePackagingUrl', forcedUrl));
        return Q(url.format(testUrl));
    }

    let collectionUrl = url.parse(tl.getVariable('System.TeamFoundationCollectionUri'));

    if (collectionUrl.hostname.toUpperCase().endsWith('.VISUALSTUDIO.COM')) {
        let hostParts = collectionUrl.hostname.split('.');
        let packagingHostName = hostParts[0] + '.pkgs.visualstudio.com';
        collectionUrl.hostname = packagingHostName;
        // remove the host property so it doesn't override the hostname property for url.format
        delete collectionUrl.host;
    }

    return Q(url.format(collectionUrl));
{'fraction_non_alphanumeric': 0.11820627802690582, 'fraction_numerical': 0.00035874439461883406, 'mean_word_length': 3.9257950530035335, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7344764', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1643, 'n_words': 368}