import { IExtension , IExtensionPlugin , IDriverExtensionApi } from '@sqltools/types' ; import { ExtensionContext , extensions } from 'vscode' ; import { DRIVER_ALIASES } from './constants' ; const { publisher , name } = require ( '../package.json' ) ; const driverName = 'PostgreSQL/Redshift' ; export async function activate ( extContext ) { const sqltools = extensions . getExtension < IExtension > ( 'mtxr.sqltools' ) ; if ( ! sqltools ) { throw new Error ( 'SQLTools not installed' ) ; } await sqltools . activate ( ) ; const api = sqltools . exports ; const extensionId = `${ publisher }.${ name }` ; const plugin = { extensionId , name : `${ driverName } Plugin` , type : 'driver' , async register ( extension ) { extension . resourcesMap ( ) . set ( `driver/${ DRIVER_ALIASES [ 0 ] . value }/icons` , { active : extContext . asAbsolutePath ( 'icons/pg/active.png' ) , default : extContext . asAbsolutePath ( 'icons/pg/default.png' ) , inactive : extContext . asAbsolutePath ( 'icons/pg/inactive.png' ) , } ) ; extension . resourcesMap ( ) . set ( `driver/${ DRIVER_ALIASES [ 1 ] . value }/icons` , { active : extContext . asAbsolutePath ( 'icons/redshift/active.png' ) , default : extContext . asAbsolutePath ( 'icons/redshift/default.png' ) , inactive : extContext . asAbsolutePath ( 'icons/redshift/inactive.png' ) , } ) ; extension . resourcesMap ( ) . set ( `driver/${ DRIVER_ALIASES [ 2 ] . value }/icons` , { active : extContext . asAbsolutePath ( 'icons/cockroach/active.png' ) , default : extContext . asAbsolutePath ( 'icons/cockroach/default.png' ) , inactive : extContext . asAbsolutePath ( 'icons/cockroach/inactive.png' ) , } ) ; DRIVER_ALIASES . forEach ( ( { value } ) => { extension . resourcesMap ( ) . set ( `driver/${ value }/extension-id` , extensionId ) ; extension . resourcesMap ( ) . set ( `driver/${ value }/connection-schema` , extContext . asAbsolutePath ( 'connection.schema.json' ) ) ; extension . resourcesMap ( ) . set ( `driver/${ value }/ui-schema` , extContext . asAbsolutePath ( 'ui.schema.json' ) ) ; } ) ; await extension . client . sendRequest ( 'ls/RegisterPlugin' , { path : extContext . asAbsolutePath ( 'out/ls/plugin.js' ) } ) ; } } ; api . registerPlugin ( plugin ) ; return { driverName , parseBeforeSaveConnection : ( { connInfo } ) => { const propsToRemove = [ 'connectionMethod' , 'id' , 'usePassword' ] ; if ( connInfo . usePassword ) { if ( connInfo . usePassword . toString ( ) . toLowerCase ( ) . includes ( 'ask' ) ) { propsToRemove . push ( 'password' ) ; } else if ( connInfo . usePassword . toString ( ) . toLowerCase ( ) . includes ( 'empty' ) ) { connInfo . password = '' ; propsToRemove . push ( 'askForPassword' ) ; } else if ( connInfo . usePassword . toString ( ) . toLowerCase ( ) . includes ( 'save' ) ) { propsToRemove . push ( 'askForPassword' ) ; } } propsToRemove . forEach ( p => delete connInfo [ p ] ) ; connInfo . pgOptions = connInfo . pgOptions || { } ; if ( connInfo . pgOptions . enableSsl === 'Enabled' ) { if ( typeof connInfo . pgOptions . ssl === 'object' && Object . keys ( connInfo . pgOptions . ssl ) . length === 0 ) { connInfo . pgOptions . ssl = true ; } } else if ( connInfo . pgOptions . enableSsl === 'Disabled' ) { delete connInfo . pgOptions . ssl ; } delete connInfo . pgOptions . enableSsl ; if ( Object . keys ( connInfo . pgOptions ) . length === 0 ) { delete connInfo . pgOptions ; } return connInfo ; } , parseBeforeEditConnection : ( { connInfo } ) => { const formData = { ... connInfo , connectionMethod : 'Server and Port' , } ; if ( connInfo . socketPath ) { formData . connectionMethod = 'Socket File' ; } else if ( connInfo . connectString ) { formData . connectionMethod = 'Connection String' ; } if ( connInfo . askForPassword ) { formData . usePassword = 'Ask on connect' ; delete formData . password ; } else if ( typeof connInfo . password === 'string' ) { delete formData . askForPassword ; formData . usePassword = connInfo . password ? 'Save password' : 'Use empty password' ; } formData . pgOptions = formData . pgOptions || { } ; if ( formData . pgOptions . ssl ) { formData . pgOptions . enableSsl = 'Enabled' ; if ( typeof formData . pgOptions . ssl === 'boolean' ) { formData . pgOptions . ssl = { } ; } } else { formData . pgOptions . enableSsl = 'Disabled' ; } return formData ; } , driverAliases : DRIVER_ALIASES , } ; } export function deactivate ( ) { }