Events - egodigital/vscode-powertools GitHub Wiki

The extension makes it possible to run tasks, like scripts, on specific events.

Create an events section to the settings.json file in the .vscode sub folder of your workspace and add one or more entry:

{
    "ego.power-tools": {
        "events": [
            {
                "type": "file.changed",
                "files": [ "**/*.txt" ],
                "exclude": [ "/test2.txt" ],
                "action": {
                    "script": "my_event.js",
                    "type": "script"
                }
            }
        ]
    }
}

For that example, create a my_event.js file in your .vscode folder and use the following skeleton:

exports.execute = async (args) => {
    // args => https://egodigital.github.io/vscode-powertools/api/interfaces/_contracts_.filechangeeventactionscriptarguments.html

    const path = require('path');

    // s. https://code.visualstudio.com/api/references/vscode-api
    const vscode = args.require('vscode');

    vscode.window.showInformationMessage(
        `The following file has changed: ${ 
            path.relative(
                __dirname + '/..',
                args.file.fsPath
            )
         }`
    );
};

demo-events.gif

Settings

Name Description Required?
if (JavaScript) Code that checks if event is available or not. s. Conditional Settings no
importValues Defines a list of properties, which uses (external) values for itself. s. Import Settings no
onCreated The (JavaScript) code to executed after event has been created. s. Executable Settings no
onDestroyed The (JavaScript) code to executed after event has been destroyed. s. Executable Settings no
platforms A list of one or more platform IDs, where the event should be available on. s. process.platform no
type The type. Current supports: document.opened, file.changed, file.created, file.deleted, file.saved and file.willsave. Default: all no

type

document.opened

A document has been opened in the editor.

{
    "ego.power-tools": {
        "events": [
            {
                "type": "document.opened",
                "action": {
                    "script": "my_event.js",
                    "type": "script"
                }
            }
        ]
    }
}
exports.execute = async (args) => {
    // args => https://egodigital.github.io/vscode-powertools/api/interfaces/_contracts_.documentopenedeventactionscriptarguments.html

    const path = require('path');

    // s. https://code.visualstudio.com/api/references/vscode-api
    const vscode = args.require('vscode');

    vscode.window.showInformationMessage(
        `The following file has been opened: ${ 
            path.relative(
                __dirname + '/..',
                args.file.fsPath
            )
         }`
    );
};
file.changed

A file has been changed.

{
    "ego.power-tools": {
        "events": [
            {
                "type": "file.changed",
                "files": [ "**/*.txt" ],
                "exclude": [ "/test2.txt" ],
                "action": {
                    "script": "my_event.js",
                    "type": "script"
                }
            }
        ]
    }
}
Name Description Required?
exclude1 One or more glob patterns, that define what files are excluded. no
files1 One or more glob patterns, that define what files are included. Default: ** no

1 supports placeholders

file.created

A file has been created.

{
    "ego.power-tools": {
        "events": [
            {
                "type": "file.created",
                "files": [ "**/*.txt" ],
                "exclude": [ "/test2.txt" ],
                "action": {
                    "script": "my_event.js",
                    "type": "script"
                }
            }
        ]
    }
}
Name Description Required?
exclude1 One or more glob patterns, that define what files are excluded. no
files1 One or more glob patterns, that define what files are included. Default: ** no

1 supports placeholders

file.deleted

A file has been deleted.

{
    "ego.power-tools": {
        "events": [
            {
                "type": "file.deleted",
                "files": [ "**/*.txt" ],
                "exclude": [ "/test2.txt" ],
                "action": {
                    "script": "my_event.js",
                    "type": "script"
                }
            }
        ]
    }
}
Name Description Required?
exclude1 One or more glob patterns, that define what files are excluded. no
files1 One or more glob patterns, that define what files are included. Default: ** no

1 supports placeholders

file.saved

A file has been saved from the editor by user.

{
    "ego.power-tools": {
        "events": [
            {
                "type": "file.saved",
                "files": [ "**/*.txt" ],
                "exclude": [ "/test2.txt" ],
                "action": {
                    "script": "my_event.js",
                    "type": "script"
                }
            }
        ]
    }
}
Name Description Required?
exclude1 One or more glob patterns, that define what files are excluded. no
files1 One or more glob patterns, that define what files are included. Default: ** no

1 supports placeholders

file.willsave

A file is going to be saved.

{
    "ego.power-tools": {
        "events": [
            {
                "type": "file.willsave",
                "action": {
                    "script": "my_event.js",
                    "type": "script"
                }
            }
        ]
    }
}
exports.execute = async (args) => {
    // args => https://egodigital.github.io/vscode-powertools/api/interfaces/_contracts_.filewillsaveeventactionscriptarguments.html

    const path = require('path');

    // s. https://code.visualstudio.com/api/references/vscode-api
    const vscode = args.require('vscode');

    await vscode.window.showInformationMessage(
        `The following file is going to be saved: ${ 
            path.relative(
                __dirname + '/..',
                args.file.fsPath
            )
         }`
    );
};

The result of the script will work the same way, as waitUntil() method.

action

Name Description Required?
type The type. Current supports: script. Default: script no
Scripts

Runs a Node.js based script file.

{
    "ego.power-tools": {
        "events": [
            {
                "type": "file.changed",
                "files": [ "**/*.txt" ],
                "exclude": [ "/test2.txt" ],
                "action": {
                    "script": "my_event.js",
                    "type": "script"
                }
            }
        ]
    }
}
Name Description Required?
options Options for the script. no
script1 The path to the script that should be invoked. Relative paths will be mapped to the .vscode sub folder of the workspace or the .vscode-powertools sub folder inside the current user's home directory. yes

1 supports placeholders

⚠️ **GitHub.com Fallback** ⚠️