author : Austin Ruby, Alex Chao, Ed Greenberg
 *  function changelog changelog changelog changelog import * as vscode from 'vscode' ; const fs = require ( 'fs' ) ; const debounce = require ( './modules/client/debounce' ) ; const readFileSendReqAndWriteResponse = require ( './modules/client/readFileSendReqAndWriteResponse' ) ; const serverOn = require ( './modules/server/serverOn' ) ; const serverOff = require ( './modules/server/serverOff' ) ; const checkForRunningServer = require ( './modules/server/checkForRunningServer' ) ; const findRootDirectory = require ( './modules/client/findRootDirectory' ) ; const parseConfigFile = require ( './modules/client/parseConfigFile' ) ; const showGraphqlSchema = require ( './modules/client/showGraphqlSchema' ) ; export function activate ( context ) { const gqChannel = vscode . window . createOutputChannel ( 'GraphQuill' ) ; let isOnToggle = false ; let saveListener ; const rootPath = findRootDirectory ( ) ; let entryPoint ; let allowServerTimeoutConfigSetting ; let portNumber ; let serverTurnedOnByGraphQuill = false ; /** **********************************************************************************************
     * * The command must be defined in package.json under contributes/commands AND activation events
     * Now provide the implementation of the command with registerCommand
     * The commandId parameter must match the command field in package.json
     * * This is the first GraphQuill option in the command palette for activating GraphQuill
    *********************************************************************************************** */ const disposableActivateGraphQuill = vscode . commands . registerCommand ( 'extension.activateGraphQuill' , async ( ) => { if ( isOnToggle ) { vscode . window . showInformationMessage ( 'GraphQuill is already active' ) ; return null ; } gqChannel . show ( true ) ; gqChannel . clear ( ) ; let parseResult = parseConfigFile ( rootPath ) ; entryPoint = parseResult . entryPoint ; allowServerTimeoutConfigSetting = parseResult . allowServerTimeoutConfigSetting ; portNumber = parseResult . portNumber ; if ( ! entryPoint || ! portNumber ) { gqChannel . append ( 'The config file was not found or had an error, please use the Create GraphQuill Config File Command to make one.' ) ; return null ; } const serverOnFromUser = await checkForRunningServer ( portNumber , true ) ; const externalURL = entryPoint . slice ( 0 , 4 ) === 'http' ; if ( ! serverOnFromUser && ! externalURL ) { serverOn ( entryPoint , gqChannel ) ; gqChannel . clear ( ) ; gqChannel . append ( 'The server is starting up...\n' ) ; serverTurnedOnByGraphQuill = await checkForRunningServer ( portNumber , false , allowServerTimeoutConfigSetting ) ; if ( ! serverTurnedOnByGraphQuill ) { gqChannel . clear ( ) ; gqChannel . append ( `The server is taking too long to startup (>${ ( allowServerTimeoutConfigSetting || 3000 ) / 1000 } seconds).\nTo increase this time, update the "serverStartupTimeAllowed" setting in the graphquill.config.js file.` ) ; return setTimeout ( ( ) => serverOff ( portNumber ) , 5000 ) ; } } if ( serverOnFromUser || serverTurnedOnByGraphQuill || externalURL ) { let url ; if ( externalURL ) url = entryPoint ; isOnToggle = true ; const currOpenEditorPath = vscode . window . activeTextEditor ! . document . fileName ; readFileSendReqAndWriteResponse ( currOpenEditorPath , gqChannel , portNumber , rootPath , url ) ; const debouncedRFSRWR = debounce ( readFileSendReqAndWriteResponse , 200 , false ) ; saveListener = vscode . workspace . onDidSaveTextDocument ( ( event ) => { gqChannel . clear ( ) ; parseResult = parseConfigFile ( rootPath ) ; entryPoint = parseResult . entryPoint ; allowServerTimeoutConfigSetting = parseResult . allowServerTimeoutConfigSetting ; portNumber = parseResult . portNumber ; if ( ! entryPoint ) { gqChannel . append ( 'The config file was not found, please use the Create GraphQuill Config File Command to make one.' ) ; return null ; } debouncedRFSRWR ( event . fileName , gqChannel , portNumber , rootPath , url ) ; return null ; } ) ; } return null ; } ) ; context . subscriptions . push ( disposableActivateGraphQuill ) ; /** **************************************************************************
     * * Second GraphQuill option in the command palette (Cmd Shift P) for deactivating graphquill
    ************************************************************************** */ const disposableDisableGraphQuill = vscode . commands . registerCommand ( 'extension.deactivateGraphQuill' , ( ) => { if ( ! isOnToggle ) { vscode . window . showInformationMessage ( 'GraphQuill is already off' ) ; return null ; } isOnToggle = false ; if ( saveListener ) saveListener . dispose ( ) ; gqChannel . hide ( ) ; gqChannel . clear ( ) ; return setTimeout ( ( ) => ( serverTurnedOnByGraphQuill && serverOff ( portNumber ) ) , 1 ) ; } ) ; context . subscriptions . push ( disposableDisableGraphQuill ) ; /** **************************************************************************
     * * Third GraphQuill option in command palette to toggle graphquill extension
     ************************************************************************** */ const disposableToggleGraphQuill = vscode . commands . registerCommand ( 'extension.toggleGraphQuill' , ( ) => { if ( ! isOnToggle ) { vscode . commands . executeCommand ( 'extension.activateGraphQuill' ) ; } else { vscode . commands . executeCommand ( 'extension.deactivateGraphQuill' ) ; } return null ; } ) ; context . subscriptions . push ( disposableToggleGraphQuill ) ; /** **************************************************************************
     * * Fourth GraphQuill option in command palette to CREATE A CONFIG FILE
     ************************************************************************** */ const disposableCreateConfigFile = vscode . commands . registerCommand ( 'extension.createConfigFile' , ( ) => { const graphQuillConfigPath = `${ rootPath }/graphquill.config.js` ; if ( fs . existsSync ( graphQuillConfigPath ) ) { vscode . window . showInformationMessage ( `A GraphQuill configuration file already exists at ${ graphQuillConfigPath }` ) ; return null ; } fs . writeFileSync ( graphQuillConfigPath , 'module.exports = {\n  // change "./server/index.js" to the relative path from the root directory to\n  // the file that starts your server.\n  // if you\'re connecting to an external server,\n  // change "./server/index.js" to its URL in the following format:\n  // "https://yourserverurl.com"\n  entry: \'./server/index.js\',\n\n  // change 3000 to the port number that your server runs on\n  portNumber: 3000,\n\n  // to increase the amount of time allowed for the server to startup, add a time\n  // in milliseconds (integer) to the "serverStartupTimeAllowed"\n  // serverStartupTimeAllowed: 5000,\n};\n' , 'utf-8' ) ; vscode . workspace . openTextDocument ( graphQuillConfigPath ) . then ( ( doc ) => { vscode . window . showTextDocument ( doc ) ; } ) ; return null ; } ) ; context . subscriptions . push ( disposableCreateConfigFile ) ; /** **************************************************************************
     * * Fifth GraphQuill option in command palette to SHOW THE SCHEMA
     ************************************************************************** */ const disposableShowGraphQLSchema = vscode . commands . registerCommand ( 'extension.showGraphQLSchema' , async ( ) => { gqChannel . show ( true ) ; gqChannel . clear ( ) ; const parseResult = parseConfigFile ( rootPath ) ; entryPoint = parseResult . entryPoint ; allowServerTimeoutConfigSetting = parseResult . allowServerTimeoutConfigSetting ; portNumber = parseResult . portNumber ; if ( ! entryPoint || ! portNumber ) { gqChannel . append ( 'The config file was not found or had an error, please use the Create GraphQuill Config File Command to make one.' ) ; return null ; } const serverOnAlready = await checkForRunningServer ( portNumber , true ) ; const externalURL = entryPoint . slice ( 0 , 4 ) === 'http' ; let serverTurnedOnBySchemaOutputter = false ; if ( ! serverOnAlready && ! externalURL ) { serverOn ( entryPoint , gqChannel ) ; gqChannel . clear ( ) ; gqChannel . append ( 'The server is starting up...\n' ) ; serverTurnedOnBySchemaOutputter = await checkForRunningServer ( portNumber , false , allowServerTimeoutConfigSetting ) ; if ( ! serverTurnedOnBySchemaOutputter ) { gqChannel . clear ( ) ; gqChannel . append ( `The server is taking too long to startup (>${ ( allowServerTimeoutConfigSetting || 3000 ) / 1000 } seconds).\nTo increase this time, update the "serverStartupTimeAllowed" setting in the graphquill.config.js file.` ) ; return setTimeout ( ( ) => serverOff ( portNumber ) , 5000 ) ; } } let url ; if ( externalURL ) url = entryPoint ; gqChannel . clear ( ) ; showGraphqlSchema ( serverOnAlready , serverTurnedOnBySchemaOutputter , gqChannel , portNumber , url ) ; return setTimeout ( ( ) => ( serverTurnedOnBySchemaOutputter && serverOff ( portNumber ) ) , 1 ) ; } ) ; context . subscriptions . push ( disposableShowGraphQLSchema ) ; } export function deactivate ( ) { vscode . commands . executeCommand ( 'extension.deactivateGraphQuill' ) ; }