All files / src/utils FilesHelper.ts

62.96% Statements 17/27
45% Branches 9/20
100% Functions 3/3
62.96% Lines 17/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 581x 1x   1x   1x     9x     9x 9x     9x         9x       9x       9x 9x       9x       24x 24x       24x 24x                            
import * as path from 'path';
import * as fs from 'fs';
 
import { UrlHelper } from './UrlHelper';
 
export class FilesHelper {
 
  public static getUserDataFolder(): string {
    let platform = process.platform;
    let homepath: string;
 
    Eif (platform.lastIndexOf('win') === 0) {
      homepath = process.env.APPDATA || process.env.LOCALAPPDATA;
    }
 
    Iif (platform === 'darwin') {
      homepath = process.env.HOME;
      homepath = path.join(homepath, 'Library', 'Preferences');
    }
 
    Iif (platform === 'linux') {
      homepath = process.env.HOME;
    }
 
    Iif (!homepath) {
      throw new Error('Couldn\'t find the base application data folder');
    }
 
    let dataPath = path.join(homepath, 'spauth');
    Iif (!fs.existsSync(dataPath)) {
      fs.mkdirSync(dataPath);
    }
 
    return dataPath;
  }
 
  public static resolveFileName(siteUrl: string): string {
    let url = FilesHelper.resolveSiteUrl(siteUrl);
    return url.replace(/[\:/\s]/g, '_');
  }
 
  private static resolveSiteUrl(siteUrl: string): string {
    Eif (siteUrl.indexOf('/_') === -1 && siteUrl.indexOf('/vti_') === -1) {
      return UrlHelper.removeTrailingSlash(siteUrl);
    }
 
    if (siteUrl.indexOf('/_') !== -1) {
      return siteUrl.slice(0, siteUrl.indexOf('/_'));
    }
 
    if (siteUrl.indexOf('/vti_') !== -1) {
      return siteUrl.slice(0, siteUrl.indexOf('/vti_'));
    }
 
    throw new Error('Unable to resolve web site url from full request url');
  }
}