Scripts - egodigital/vscode-powertools GitHub Wiki

Scripts can be used to realize any kind of custom logic for a workspace, based on Node.js.

Press F1, select Power Tools: Scripts and choose one of the following commands:

Name Description
New Script ... Opens an editor with a new script.
Open Script ... Opens an existing script file in an editor.
Run Script ... Runs a script for the currently opened text editor.

Demo

demo-scripts.gif

Other languages

Instead of executing JavaScript code, you are also able to compile from:

Constants

$cancel

Stores the current CancellationToken object, to check if the user wants to cancel the script.

if ($cancel.isCancellationRequested) {
    // user wants to cancel
}

$html

An instance of AllHtmlEntities class of html-entities module.

$writeHtml(`
This <span style="color: red;">text is unparsed</span>
and the following one is ${ $html.encode('<span style="color: yellow;">parsed</span>') }.
`);

$workspaces

Provides a grouped list of WorkspaceInfo objects.

for (let workspaceName in $workspaces) {
    const WS = $workspaces[ workspaceName ];

    // 'WS' is a 'WorkspaceInfo' instance
    // or an array of it, if there are at least 2
    // workspaces with the same name
    // 
    // s. https://egodigital.github.io/vscode-powertools/api/interfaces/_contracts_.workspaceinfo.html
}

Functions

$alert( msg )

Shows a (warning) popup.

await $alert('Hello, TM!');

$clear( )

Clears the console.

$clear();

$writeLine('Hello, TM!');

$dump( val )

Dumps a value or object and writes it to the console.

$dump({
    'TM': '1979-09-05',
    'MK': 19790923
});

$getWorkspaces( )

Returns a sorted list of all known workspaces.

for (let ws of $getWorkspaces()) {
    // for 'ws', s. $workspaces constant
}

$require( module )

Includes a module from the extension context, what means, that you can use any module, which is shipped with the extension.

const moment = $require('moment');

const UTC_NOW = moment.utc();

$sleep( sec )

Waits for a specific number of seconds.

// waits 5 seconds
await $sleep(5);
// you also can use floating numbers
await $sleep(23.979);

$withProgress( task )

Short version of withProgress() function.

const RESULT = await $withProgress(async (progress, cancelToken) => {
    let sum = 0;
    for (let i = 0; i < 20; i++) {
        if (cancelToken.isCancellationRequested) {
            return false;
        }

        progress.report({
            message: `Processing item ${ i + 1 } ...`,
            increment: 1 / 20 * 100.0,
        });

        sum += i;

        await $sleep(1);
    }

    return sum;
});

$write( val )

Writes something as text to the console without adding a line break.

$write('Hello, ');
$write(' TM!');

$writeHtml( html )

Writes something as unparsed HTML code to the console.

$writeHtml(`
This is a <strong style="text-decoration: underline;">HTML message</strong>
with some <span style="color: red;">red text</span> and an image:<br /><br />
<img src="https://www.e-go-mobile.com/site/assets/files/1040/ego_life_website_rot-880x550.jpg">
`);

$writeLine( val )

Writes something as text to the console and adds a line break.

$writeLine('Hello, TM!');

$writeMarkdown( markdown )

Writes Markdown to the console.

$writeMarkdown(`
# Header 1

## Header 2

![e.GO Life](https://www.e-go-mobile.com/site/assets/files/1040/ego_life_website_rot-880x550.jpg)
`);

Modules

Name Description
_ lodash
$fs fs-extra
$helpers vscode-helpers
$moment Moment.js
$path Node.js path module
$vscode Visual Studio Code API
⚠️ **GitHub.com Fallback** ⚠️