Files
azuredatastudio/extensions/sql-database-projects/src/common/utils.ts
Sakshi Sharma 9a55b0275d Import project from database (#10326)
* Initial changes for Import database as new project

* Functionally complete code

* Initial changes for Import database as new project

* Functionally complete code

* Resolved conflicts with latest changes. Also did some code refactoring.

* Addressed comments. Added unit tests.

* Addressed comments

* Moved ExtractTarget enum from azdata to mssql

* Addressed comments

* Fixed indentation in project templates
2020-05-26 16:08:24 -07:00

98 lines
2.9 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as os from 'os';
import { promises as fs } from 'fs';
/**
* Consolidates on the error message string
*/
export function getErrorMessage(error: any): string {
return (error instanceof Error)
? (typeof error.message === 'string' ? error.message : '')
: typeof error === 'string' ? error : `${JSON.stringify(error, undefined, '\t')}`;
}
/**
* removes any leading portion shared between the two URIs from outerUri.
* e.g. [@param innerUri: 'this\is'; @param outerUri: '\this\is\my\path'] => 'my\path'
* @param innerUri the URI that will be cut away from the outer URI
* @param outerUri the URI that will have any shared beginning portion removed
*/
export function trimUri(innerUri: vscode.Uri, outerUri: vscode.Uri): string {
let innerParts = innerUri.path.split('/');
let outerParts = outerUri.path.split('/');
while (innerParts.length > 0 && outerParts.length > 0 && innerParts[0].toLocaleLowerCase() === outerParts[0].toLocaleLowerCase()) {
innerParts = innerParts.slice(1);
outerParts = outerParts.slice(1);
}
return outerParts.join('/');
}
/**
* Trims any character contained in @param chars from both the beginning and end of @param input
*/
export function trimChars(input: string, chars: string): string {
let output = input;
let i = 0;
while (chars.includes(output[i])) { i++; }
output = output.substr(i);
i = 0;
while (chars.includes(output[output.length - i - 1])) { i++; }
output = output.substring(0, output.length - i);
return output;
}
/**
* Checks if the folder or file exists @param path path of the folder/file
*/
export async function exists(path: string): Promise<boolean> {
try {
await fs.access(path);
return true;
} catch (e) {
return false;
}
}
/**
* Convert camelCase input to PascalCase
*/
export function toPascalCase(input: string): string {
return input.charAt(0).toUpperCase() + input.substr(1);
}
/**
* get quoted path to be used in any commandline argument
* @param filePath
*/
export function getSafePath(filePath: string): string {
return (os.platform() === 'win32') ?
getSafeWindowsPath(filePath) :
getSafeNonWindowsPath(filePath);
}
/**
* ensure that path with spaces are handles correctly
*/
export function getSafeWindowsPath(filePath: string): string {
filePath = filePath.split('\\').join('\\\\').split('"').join('');
return '"' + filePath + '"';
}
/**
* ensure that path with spaces are handles correctly
*/
export function getSafeNonWindowsPath(filePath: string): string {
filePath = filePath.split('\\').join('/').split('"').join('');
return '"' + filePath + '"';
}