webpack-scp-plugin
Version:
Webpack Plugin to copy assets to remote machine using scp
76 lines (75 loc) • 2.94 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const node_ssh_1 = require("node-ssh");
const ERROR_MESSAGES = {
missingRequiredParam: (param) => `Missing required parameter ${param}`
};
class WebpackScpPlugin {
constructor(options) {
this.srcPath = options.srcPath;
this.destPath = options.destPath;
this.connect = options.connect;
this.sshConnection = null;
this.validateOptions();
}
validateOptions() {
if (!this.destPath) {
throw new Error(ERROR_MESSAGES.missingRequiredParam("destPath"));
}
if (!this.connect) {
throw new Error(ERROR_MESSAGES.missingRequiredParam("connect"));
}
}
getConnection() {
return __awaiter(this, void 0, void 0, function* () {
if (this.sshConnection) {
return new Promise((res) => res(this.sshConnection));
}
else {
const ssh = new node_ssh_1.NodeSSH();
yield ssh.connect(this.connect);
this.sshConnection = ssh;
return this.sshConnection;
}
});
}
closeConnection() {
if (this.sshConnection) {
this.sshConnection.dispose();
this.sshConnection = null;
}
}
uploadSourceMap(fromPath) {
return __awaiter(this, void 0, void 0, function* () {
const sshConnect = yield this.getConnection();
const srcPath = this.srcPath || fromPath;
yield sshConnect.putDirectory(srcPath, this.destPath, {
concurrency: 10
});
this.closeConnection();
});
}
apply(compiler) {
compiler.hooks.afterEmit.tapPromise("WebpackScpPlugin", (compilation) => __awaiter(this, void 0, void 0, function* () {
try {
console.log(`Upload assets to ${this.connect.host}.`);
const { outputPath } = compiler;
yield this.uploadSourceMap(outputPath);
}
catch (err) {
err.message = `WebpackScpPlugin: ${err.message}`;
console.error(err.message);
compilation.errors.push(err);
}
}));
}
}
module.exports = WebpackScpPlugin;
;