'use strict' ; import * as path from 'path' ; import * as fs from 'fs-extra' ; import * as vscode from 'vscode' ; import { Uri } from 'vscode' ; import { IConfig } from './../spgo' ; import { Logger } from '../util/logger' ; import { UrlHelper } from '../util/urlHelper' ; import { FileHelper } from '../util/fileHelper' ; export class ConfigurationDao { static initializeConfiguration ( contextPath ) { return new Promise ( ( resolve , reject ) => { let config ; try { if ( ! vscode . workspace . workspaceFolders ) { throw { message : 'In order to use SPGo, please create a local folder to use as the solution workspace. See https://www.chrishasz.com/spgo/general/getting-started-with-spgo for more info.' } ; } let configFilePath = contextPath . fsPath ; let rootPath = vscode . workspace . getWorkspaceFolder ( contextPath ) . uri ; fs . stat ( configFilePath , ( err ) => { try { if ( err == null ) { config = { ... config || { } , ... fs . readJsonSync ( configFilePath ) } ; } else { Logger . outputMessage ( "Config file does not exist, creating now.\r" + err ) ; fs . ensureFileSync ( configFilePath ) ; config = config || { } ; } if ( typeof config === 'object' && ! config . sourceDirectory ) { config . sourceDirectory = 'src' ; } config . sourceDirectory = FileHelper . ensureCorrectPathSeparator ( config . sourceDirectory ) ; if ( config . sharePointSiteUrl !== undefined ) { config . sharePointSiteUrl = UrlHelper . removeTrailingSlash ( config . sharePointSiteUrl ) ; } config . sharePointSiteUrl = decodeURI ( config . sharePointSiteUrl ) ; config . workspaceRoot = `${ rootPath . fsPath }` ; config . sourceRoot = `${ config . workspaceRoot }${ path . sep }${ config . sourceDirectory }` ; if ( config . publishWorkspaceGlobPattern ) { Logger . outputMessage ( 'The publishWorkspaceGlobPattern property has been deprecated. Please use publishWorkspaceOptions instead: https://www.chrishasz.com/spgo/general/config-options#publishWorkspaceOptions' ) ; } if ( ! config . publishWorkspaceOptions ) { config . publishWorkspaceOptions = { destinationFolder : '/' , globPattern : config . publishWorkspaceGlobPattern ? config . sourceRoot + UrlHelper . ensureLeadingSlash ( config . publishWorkspaceGlobPattern ) : config . sourceRoot + UrlHelper . osAwareGlobStar ( ) , localRoot : config . sourceRoot } ; } else { config . publishWorkspaceOptions . destinationFolder = config . publishWorkspaceOptions . destinationFolder || '/' ; config . publishWorkspaceOptions . localRoot = this . calculateLocalRoot ( config ) ; config . publishWorkspaceOptions . globPattern = this . calculateGlobPattern ( config ) ; } resolve ( config ) ; } catch ( fileErr ) { Logger . outputError ( fileErr ) ; config = { } ; config . sourceDirectory = 'src' ; resolve ( config ) ; } } ) ; } catch ( err ) { Logger . showError ( err . message , err ) ; config = { } ; reject ( config ) ; } } ) ; } static calculateLocalRoot ( config ) { let localRoot = config . publishWorkspaceOptions . localRoot ; if ( localRoot ) { if ( ! path . isAbsolute ( localRoot ) ) { localRoot = config . workspaceRoot + path . sep + localRoot ; } } else { localRoot = config . sourceRoot ; } return localRoot ; } static calculateGlobPattern ( config ) { let globPattern = config . publishWorkspaceOptions . globPattern ; if ( globPattern ) { if ( ! path . isAbsolute ( globPattern ) ) { globPattern = config . publishWorkspaceOptions . localRoot + UrlHelper . ensureLeadingWebSlash ( globPattern ) ; } } else { globPattern = config . publishWorkspaceOptions . localRoot + UrlHelper . osAwareGlobStar ( ) ; } return globPattern ; } }