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

Commit

Permalink
upload event script for SFTP and FTP
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Mar 21, 2018
1 parent 3eb1bea commit a54baa9
Show file tree
Hide file tree
Showing 9 changed files with 992 additions and 28 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

[![Share via Facebook](https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/share/Facebook.png)](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded&quote=vscode-deploy-reloaded) [![Share via Twitter](https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/share/Twitter.png)](https://twitter.com/intent/tweet?source=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded&text=vscode-deploy-reloaded:%20https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded&via=mjkloubert) [![Share via Google+](https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/share/Google+.png)](https://plus.google.com/share?url=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded) [![Share via Pinterest](https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/share/Pinterest.png)](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded&media=https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/demo1.gif&description=Recoded%20version%20of%20Visual%20Studio%20Code%20extension%20%27vs-deploy%27%2C%20which%20provides%20commands%20to%20deploy%20files%20to%20one%20or%20more%20destinations.) [![Share via Reddit](https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/share/Reddit.png)](https://www.reddit.com/submit?url=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded&title=vscode-deploy-reloaded) [![Share via LinkedIn](https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/share/LinkedIn.png)](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded&title=vscode-deploy-reloaded&summary=Recoded%20version%20of%20Visual%20Studio%20Code%20extension%20%27vs-deploy%27%2C%20which%20provides%20commands%20to%20deploy%20files%20to%20one%20or%20more%20destinations.&source=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded) [![Share via Wordpress](https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/share/Wordpress.png)](https://wordpress.com/press-this.php?u=https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded&quote=vscode-deploy-reloaded&s=Recoded%20version%20of%20Visual%20Studio%20Code%20extension%20%27vs-deploy%27%2C%20which%20provides%20commands%20to%20deploy%20files%20to%20one%20or%20more%20destinations.&i=https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/demo1.gif) [![Share via Email](https://raw.githubusercontent.com/mkloubert/vscode-deploy-reloaded/master/img/share/Email.png)](mailto:?subject=vscode-deploy-reloaded&body=Recoded%20version%20of%20Visual%20Studio%20Code%20extension%20'vs-deploy'%2C%20which%20provides%20commands%20to%20deploy%20files%20to%20one%20or%20more%20destinations.:%20https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dmkloubert.vscode-deploy-reloaded)

## 0.65.0 (March 21st, 2018; upload events for SFTP and FTP targets)

* added `beforeUpload` and `uploaded` settings for [SFTP](https://github.com/mkloubert/vscode-deploy-reloaded/wiki/target_sftp) targets, which store paths to [scripts](https://github.com/mkloubert/vscode-deploy-reloaded/wiki/target_sftp#event-scripts), that are invoked before a file is going to be uploaded or has (tried to be) uploaded ... s. [issue #51](https://github.com/mkloubert/vscode-deploy-reloaded/issues/51)
* added `beforeUpload` and `uploaded` settings for [FTP](https://github.com/mkloubert/vscode-deploy-reloaded/wiki/target_ftp) targets, which store paths to [scripts](https://github.com/mkloubert/vscode-deploy-reloaded/wiki/target_ftp#event-scripts), that are invoked before a file is going to be uploaded or has (tried to be) uploaded ... s. [issue #51](https://github.com/mkloubert/vscode-deploy-reloaded/issues/51)
* code cleanups and improvements

## 0.64.0 (March 20th, 2018; bugfixes)

* fixed bug with automatic password prompts ... s [issue #48](https://github.com/mkloubert/vscode-deploy-reloaded/issues/48#issuecomment-374321041)
Expand Down
Binary file modified img/screenshot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-deploy-reloaded",
"displayName": "Deploy (Reloaded)",
"description": "Deploys files of a workspace to a destination.",
"version": "0.64.0",
"version": "0.65.0",
"publisher": "mkloubert",
"engines": {
"vscode": "^1.21.0"
Expand Down Expand Up @@ -24638,6 +24638,13 @@
]
}
},
"beforeUpload": {
"type": "string",
"description": "The path to an (event) script, which is executed BEFORE a file is going to be uploaded."
},
"beforeUploadOptions": {
"description": "Options for the script defined in 'beforeUpload'."
},
"checkBeforeDeploy": {
"description": "Check for newer files before a deploy operation starts or not.",
"type": "boolean",
Expand Down Expand Up @@ -26846,6 +26853,13 @@
"type": "string",
"description": "Deploys to a FTP server."
},
"uploaded": {
"type": "string",
"description": "The path to an (event) script, which is executed AFTER a file has been uploaded or tried to be uploaded."
},
"uploadedOptions": {
"description": "Options for the script defined in 'uploaded'."
},
"user": {
"type": "string",
"description": "The username.",
Expand Down Expand Up @@ -53901,6 +53915,13 @@
]
}
},
"beforeUpload": {
"type": "string",
"description": "The path to an (event) script, which is executed BEFORE a file is going to be uploaded."
},
"beforeUploadOptions": {
"description": "Options for the script defined in 'beforeUpload'."
},
"checkBeforeDeploy": {
"description": "Check for newer files before a deploy operation starts or not.",
"type": "boolean",
Expand Down Expand Up @@ -56160,6 +56181,13 @@
"type": "string",
"description": "Deploys to a SFTP server."
},
"uploaded": {
"type": "string",
"description": "The path to an (event) script, which is executed AFTER a file has been uploaded or tried to be uploaded."
},
"uploadedOptions": {
"description": "Options for the script defined in 'uploaded'."
},
"user": {
"type": "string",
"description": "The username."
Expand Down
173 changes: 172 additions & 1 deletion src/clients/ftp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,46 @@ const ParseListening = require("parse-listing");
import * as Path from 'path';


/**
* An action that is invoked BEFORE an upload process starts.
*
* @param {FTPBeforeUploadArguments} args The arguments.
*
* @return {FTPBeforeUploadResult|PromiseLike<FTPBeforeUploadResult>} The result.
*/
export type FTPBeforeUpload = (args: FTPBeforeUploadArguments) => FTPBeforeUploadResult | PromiseLike<FTPBeforeUploadResult>;

/**
* Arguments for an action that is invoked BEFORE an upload process starts.
*/
export interface FTPBeforeUploadArguments {
/**
* The underlying (raw) connection.
*/
readonly connection: any;
/**
* The data to upload.
*/
data: Buffer;
/**
* The path of the remote file.
*/
readonly file: string;
}

/**
* A possible file for an action that is invoked BEFORE an upload process starts.
*/
export type FTPBeforeUploadResult = void | false;

/**
* Options for a FTP connection.
*/
export interface FTPConnectionOptions {
/**
* Is invoked BEFORE an upload process starts.
*/
readonly beforeUpload?: FTPBeforeUpload;
/**
* The engine.
*/
Expand All @@ -47,12 +83,56 @@ export interface FTPConnectionOptions {
* The TCP port.
*/
readonly port?: number;
/**
* Is invoked AFTER an upload process.
*/
readonly uploadCompleted?: FTPUploadCompleted;
/**
* The username.
*/
readonly user?: string;
}

/**
* An action that is invoked AFTER an upload process.
*
* @param {FTPUploadCompletedArguments} args The arguments.
*
* @return {FTPUploadCompletedResult|PromiseLike<FTPUploadCompletedResult>} The result.
*/
export type FTPUploadCompleted = (args: FTPUploadCompletedArguments) => FTPUploadCompletedResult | PromiseLike<FTPUploadCompletedResult>;

/**
* Arguments for an action that is invoked AFTER an upload process.
*/
export interface FTPUploadCompletedArguments {
/**
* The underlying (raw) connection.
*/
readonly connection: any;
/**
* The data.
*/
readonly data: Buffer;
/**
* The error (if occurred).
*/
readonly error?: any;
/**
* The path of the remote file.
*/
readonly file: string;
/**
* Indicates if file has been uploaded or not.
*/
readonly hasBeenUploaded: boolean;
}

/**
* A possible file for an action that is invoked AFTER an upload process.
*/
export type FTPUploadCompletedResult = void | boolean;


/**
* The default value for a host address.
Expand Down Expand Up @@ -194,13 +274,77 @@ export abstract class FTPClientBase extends deploy_clients.AsyncFileListBase {
*/
public abstract mkdir(dir: string): Promise<void>;

/**
* Invokes the event for an 'before upload' operation.
*
* @param {string} path The path of the remote file.
* @param {Buffer} data The data to upload.
*
* @param {Promise<Buffer|false>} The promise with the data to upload or (false)
* if the file should NOT be uploaded.
*/
protected async onBeforeUpload(path: string, data: Buffer): Promise<Buffer | false> {
let doUpload = true;

const BEFORE_UPLOAD_ARGS: FTPBeforeUploadArguments = {
connection: this.connection,
data: data,
file: path,
};

const BEFORE_UPLOAD = this.options.beforeUpload;
if (BEFORE_UPLOAD) {
const BEFORE_UPLOAD_RESULT = deploy_helpers.toBooleanSafe(await Promise.resolve(
BEFORE_UPLOAD( BEFORE_UPLOAD_ARGS )
), true);

doUpload = false !== BEFORE_UPLOAD_RESULT;
}

return doUpload ? BEFORE_UPLOAD_ARGS.data
: false;
}

/** @inheritdoc */
protected onDispose() {
this.end().then(() => {
}).catch((err) => {
});
}

/**
* Invokes the event for an 'upload completed' operation.
*
* @param {any} err The error (if occurred).
* @param {string} path The path of the remote file.
* @param {Buffer} data The uploaded data.
* @param {boolean} hasBeenUploaded Indicates if file has been uploaded or not.
*/
protected async onUploadCompleted(err: any, path: string, data: Buffer, hasBeenUploaded: boolean) {
let errorHandled = false;

const UPLOAD_COMPLETED_ARGS: FTPUploadCompletedArguments = {
connection: this.connection,
data: data,
error: err,
file: path,
hasBeenUploaded: hasBeenUploaded,
};

const UPLOAD_COMPLETED = this.options.uploadCompleted;
if (UPLOAD_COMPLETED) {
const UPLOAD_COMPLETED_RESULT = deploy_helpers.toBooleanSafe(await Promise.resolve(
UPLOAD_COMPLETED( UPLOAD_COMPLETED_ARGS )
));

errorHandled = false !== UPLOAD_COMPLETED_RESULT;
}

if (err && !errorHandled) {
throw err;
}
}

/**
* Gets the underlying options.
*/
Expand Down Expand Up @@ -228,7 +372,34 @@ export abstract class FTPClientBase extends deploy_clients.AsyncFileListBase {

/** @inheritdoc */
public async uploadFile(path: string, data: Buffer): Promise<void> {
await this.put(path, data);
const BEFORE_UPLOAD_RESULT = await await this.onBeforeUpload(path, data);

let hasBeenUploaded = false;
let uploadError: any;
try {
if (false === BEFORE_UPLOAD_RESULT) {
return;
}

data = await deploy_helpers.asBuffer( BEFORE_UPLOAD_RESULT );
if (!data) {
data = Buffer.alloc(0);
}

await this.put(path, data);

hasBeenUploaded = true;
}
catch (e) {
uploadError = e;
}
finally {
await this.onUploadCompleted(
uploadError,
path, data,
hasBeenUploaded,
);
}
}
}

Expand Down
Loading

0 comments on commit a54baa9

Please sign in to comment.