Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

target_script

Marcel Kloubert edited this page Dec 9, 2017 · 8 revisions

Home >> Targets >> script

External Node.js based scripts

Deploys via a JS script.

{
    "deploy.reloaded": {
        "targets": [
            {
                "type": "script",
                "name": "My script",
                "description": "A deploy script",

                "script": "E:/test/deploy.js",
                "options": {
                    "TM": 5979,
                    "MK": "23979"
                }
            }
        ]
    }
}
Name Description
cache Load script from cache or not. Default: (false)
options Optional value for the execution.
script* The script file to exeute. Relative path will use the settings folder (like .vscode) or the home directory as scopes. Default: ./deploy.js

* supports placeholders

Implement own scripts

A script file has the following skeleton:

// Node.js API provided by Visual Studio Code: https://nodejs.org/en/docs
const Path = require('path');

// Visual Studio Code API: https://code.visualstudio.com/docs/extensionAPI/vscode-api
const vscode = require('vscode');


// entry point
exports.execute = function(args) {
    // args: https://mkloubert.github.io/vscode-deploy-reloaded/interfaces/_plugins_script_.scriptarguments.html

    // module shipped with extension: https://github.com/mkloubert/vscode-deploy-reloaded/blob/master/package.json
    const FSExtra = args.require('fs-extra');
    // module of extension: https://github.com/mkloubert/vscode-deploy-reloaded/tree/master/src
    const Helpers = args.require('./helpers');

    if (0 == args.operation)
        // Delete files
        return deleteFiles(args);  // s. below

    if (1 == args.operation)
        // Deploy / upload
        return deployFiles(args);  // s. below

    if (2 == args.operation)
        // list directory
        return listDirectory(args);  // s. below

    if (3 == args.operation)
        // Pull / download
        return pullFiles(args);  // s. below

    throw new Error(args.operation + ' operation is not supported!');
};


// DELETE
function deleteFiles(args) {
    return new Promise((resolve, reject) => {
        try {
            for (let file of args.files) {
                // file: https://mkloubert.github.io/vscode-deploy-reloaded/interfaces/_plugins_.filetodelete.html

                if (args.isCancelling)
                    break;  // user wants to cancel

                try {
                    file.onBeforeDelete();  // tell that we are going to start the
                                            // delete operation for this file now
                                            // 
                                            // you can submit an optional string that
                                            // is displayed as 'destination' in the GUI

                    // do the delete operation here
                    throw new Error('Not implemented!');

                    file.onDeleteCompleted();  // tell that anything worked fine
                }
                catch (e) {
                    file.onDeleteCompleted(e);  // submit the error
                }
            }

            resolve();
        }
        catch (e) {
            reject( e );
        }
    });
}

// DEPLOY / UPLOAD
function deployFiles(args) {
    return new Promise((resolve, reject) => {
        try {
            for (let file of args.files) {
                // file: https://mkloubert.github.io/vscode-deploy-reloaded/interfaces/_plugins_.filetoupload.html

                if (args.isCancelling)
                    break;  // user wants to cancel

                try {
                    file.onBeforeUpload();  // tell that we are going to start the
                                            // deploy operation for this file now
                                            // 
                                            // you can submit an optional string that
                                            // is displayed as 'destination' in the GUI

                    // do the deploy operation here
                    
                    // reads the content of this file async
                    // and returns a Promise with the buffer
                    // of the data to deploy
                    // 
                    // let contentToDeploy = await file.read();

                    throw new Error('Not implemented!');

                    file.onUploadCompleted();  // tell that anything worked fine
                }
                catch (e) {
                    file.onUploadCompleted(e);  // submit the error
                }
            }

            resolve();
        }
        catch (e) {
            reject( e );
        }
    });
}

// LIST DIRECTORY
function listDirectory(args) {
    return new Promise((resolve, reject) => {
        try {
            let result = {
                dirs: [],   // DirectoryInfo: https://mkloubert.github.io/vscode-deploy-reloaded/interfaces/_files_.directoryinfo.html
                files: [],  // FileInfo: https://mkloubert.github.io/vscode-deploy-reloaded/interfaces/_files_.fileinfo.html
                others: [],  // other FileSystemInfo objects: https://mkloubert.github.io/vscode-deploy-reloaded/interfaces/_files_.filesysteminfo.html
                target: args.target
            };

            // the directory to list is stored in
            // 'args.dir'

            // args.isCancelling provides if
            // user wants to cancel or not

            throw new Error('Not implemented!');

            resolve( result );
        }
        catch (e) {
            reject( e );
        }
    });
}

// PULL / DOWNLOAD
function pullFiles(args) {
    return new Promise((resolve, reject) => {
        try {
            for (let file of args.files) {
                // file: https://mkloubert.github.io/vscode-deploy-reloaded/interfaces/_plugins_.filetodownload.html

                if (args.isCancelling)
                    break;  // user wants to cancel

                try {
                    file.onBeforeDownload();  // tell that we are going to start the
                                              // pull operation for this file now
                                              // 
                                              // you can submit an optional string that
                                              // is displayed as 'source' in the GUI

                    // do the pull operation here
                    // 
                    // we store the data in 'downloadedData' var
                    // for this example
                    // 
                    // recommended is to load the data as buffer
                    // or readable NodeJS stream
                    throw new Error('Not implemented!');

                    // tell that anything worked fine
                    // and submit the data to write
                    file.onDownloadCompleted(null, downloadedData);
                }
                catch (e) {
                    file.onDownloadCompleted(e);  // submit the error
                }
            }

            resolve();
        }
        catch (e) {
            reject( e );
        }
    });
}
Clone this wiki locally