<commit_before>import { SPRest, SPHttpClient } from '@pnp/sp';

import { IGulpConfigs } from '../interfaces';

interface IHashedValue {
  value?: string;
  needReplacement: boolean;
  get: () => Promise<string>;
}

interface IHash {
  [key: string]: IHashedValue;
}

interface IHashesProps {
  sp: SPRest;
  configs: IGulpConfigs;
}

export class Hashes {

  private hashes: IHash = {
    '~publishroot/': { get: this.getPublishRootUrl.bind(this), needReplacement: true },
    '~site/': { get: this.getSiteUrl.bind(this), needReplacement: false },
    '~sitecollection/': { get: this.getSiteCollectionUrl.bind(this), needReplacement: false },
    '~hubsite/': { get: this.getHubSiteUrl.bind(this), needReplacement: true }
  };

  private sp: SPRest;
  private client: SPHttpClient;
  private configs: IGulpConfigs;

  public constructor(props: IHashesProps) {
    this.sp = props.sp;
    this.configs = props.configs;
    this.client = new SPHttpClient();
  }

  public isHashedUrl(uri: string): boolean {
    const str = uri.toLowerCase();
    return Object.keys(this.hashes)
      .filter(key => str.indexOf(key) !== -1)
      .length > 0;
  }

  public async replaceHashedUrl(uri: string): Promise<string> {
    let str = uri.toLowerCase();
    const rules = Object.keys(this.hashes).filter(key => str.indexOf(key) !== -1);
    for (const key of rules) {
      const rule = this.hashes[key];
      if (rule.needReplacement) {
        if (typeof rule.value === 'undefined') {
          await rule.get()
          .then(res => rule.value = res)
          .catch(error => {
            console.log(error);
            rule.value = null;
          });
        }
        if (rule.value === null) {
          str = null;
        }
        if (str !== null) {
          str = str.replace(new RegExp(key.replace(/\//g, ''), 'ig'), rule.value);
        }
      }
    }
    return str;
  }

  private async getHubSiteUrl(): Promise<string> {
    const { Url: siteAbsoluteUrl } = await this.sp.site.select('Url').get();
    const endpoint = `${siteAbsoluteUrl}/_api/web/hubsitedata(false)`;

    const siteHubData = await this.client.get(endpoint)
      .then(r => r.json())
      .then(data => {
        if (typeof data['odata.error'] !== 'undefined') {
          throw Error(data['odata.error'].message.value);
        }
        if (data.value === null) {
          return null;
        }
        return JSON.parse(data.value);
      });

    if (siteHubData) {
      const hubRelativeUrl = '/' + siteHubData.url.split('/').splice(3,100).join('/').replace(/\/\//g, '/');
      return hubRelativeUrl;
    } else {
      return null;
    }
  }

  private async getSiteUrl(): Promise<string> {
    const { ServerRelativeUrl } = await this.sp.web.select('ServerRelativeUrl').get();
    return ServerRelativeUrl;
  }

  private async getSiteCollectionUrl(): Promise<string> {
    const { ServerRelativeUrl } = await this.sp.site.select('ServerRelativeUrl').get();
    return ServerRelativeUrl;
  }

  private async getPublishRootUrl(): Promise<string> {
    const publishRootUrl = '/' + (this.configs.privateConf.siteUrl + '/' + this.configs.appConfig.spFolder)
      .split('/').splice(3,100).join('/').replace(/\/\//g, '/');
    return publishRootUrl;
  }

}
<commit_msg>HubSiteData<commit_after>import { SPRest, SPHttpClient } from '@pnp/sp';

import { IGulpConfigs } from '../interfaces';

interface IHashedValue {
  value?: string;
  needReplacement: boolean;
  get: () => Promise<string>;
}

interface IHash {
  [key: string]: IHashedValue;
}

interface IHashesProps {
  sp: SPRest;
  configs: IGulpConfigs;
}

export class Hashes {

  private hashes: IHash = {
    '~publishroot/': { get: this.getPublishRootUrl.bind(this), needReplacement: true },
    '~site/': { get: this.getSiteUrl.bind(this), needReplacement: false },
    '~sitecollection/': { get: this.getSiteCollectionUrl.bind(this), needReplacement: false },
    '~hubsite/': { get: this.getHubSiteUrl.bind(this), needReplacement: true }
  };

  private sp: SPRest;
  private client: SPHttpClient;
  private configs: IGulpConfigs;

  public constructor(props: IHashesProps) {
    this.sp = props.sp;
    this.configs = props.configs;
    this.client = new SPHttpClient();
  }

  public isHashedUrl(uri: string): boolean {
    const str = uri.toLowerCase();
    return Object.keys(this.hashes)
      .filter(key => str.indexOf(key) !== -1)
      .length > 0;
  }

  public async replaceHashedUrl(uri: string): Promise<string> {
    let str = uri.toLowerCase();
    const rules = Object.keys(this.hashes).filter(key => str.indexOf(key) !== -1);
    for (const key of rules) {
      const rule = this.hashes[key];
      if (rule.needReplacement) {
        if (typeof rule.value === 'undefined') {
          await rule.get()
          .then(res => rule.value = res)
          .catch(error => {
            console.log(error);
            rule.value = null;
          });
        }
        if (rule.value === null) {
          str = null;
        }
        if (str !== null) {
          str = str.replace(new RegExp(key.replace(/\//g, ''), 'ig'), rule.value);
        }
      }
    }
    return str;
  }

  private async getHubSiteUrl(): Promise<string> {
    const { Url: siteAbsoluteUrl } = await this.sp.site.select('Url').get();
    const endpoint = `${siteAbsoluteUrl}/_api/web/hubsitedata(false)`;

    const siteHubData = await this.client.get(endpoint)
      .then(r => r.json())
      .then(data => {
        if (typeof data['odata.error'] !== 'undefined') {
          throw Error(data['odata.error'].message.value);
        }
        if (data.value === null) {
          return null;
        }
        return JSON.parse(data.value ? data.value : data.d.HubSiteData);
      });

    if (siteHubData) {
      const hubRelativeUrl = '/' + siteHubData.url.split('/').splice(3,100).join('/').replace(/\/\//g, '/');
      return hubRelativeUrl;
    } else {
      return null;
    }
  }

  private async getSiteUrl(): Promise<string> {
    const { ServerRelativeUrl } = await this.sp.web.select('ServerRelativeUrl').get();
    return ServerRelativeUrl;
  }

  private async getSiteCollectionUrl(): Promise<string> {
    const { ServerRelativeUrl } = await this.sp.site.select('ServerRelativeUrl').get();
    return ServerRelativeUrl;
  }

  private async getPublishRootUrl(): Promise<string> {
    const publishRootUrl = '/' + (this.configs.privateConf.siteUrl + '/' + this.configs.appConfig.spFolder)
      .split('/').splice(3,100).join('/').replace(/\/\//g, '/');
    return publishRootUrl;
  }

}

{'fraction_non_alphanumeric': 0.16061622940817571, 'fraction_numerical': 0.003355704697986577, 'mean_word_length': 2.7554410080183276, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 31, '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': '6497099', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2106, 'n_words': 424}