Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

filemanager-webpack-plugin

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filemanager-webpack-plugin - npm Package Compare versions

Comparing version 1.0.29 to 2.0.0

629

lib/index.js
'use strict';
var fs = require('fs');
var path = require('path');
var cpx = require('cpx');
var fsExtra = require('fs-extra');
var makeDir = require('make-dir');
/**
* Execute copy action
*
* @param {Object} command - Command data for given action
* @return {Function|null} - Function that returns a promise or null
*/
function copyAction(command, options) {
var verbose = options.verbose;
if (!command.source || !command.destination) {
if (verbose) {
console.log(' - FileManagerPlugin: Warning - copy parameter has to be formated as follows: { source: <string>, destination: <string> }');
}
return null;
}
return function () {
return new Promise(function (resolve, reject) {
// if source is a file, just copyFile()
// if source is a NOT a glob pattern, simply append **/*
var fileRegex = /(\*|\{+|\}+)/g;
var matches = fileRegex.exec(command.source);
if (matches === null) {
fs.lstat(command.source, function (sErr, sStats) {
if (sErr) return reject(sErr);
fs.lstat(command.destination, function (dErr, dStats) {
if (sStats.isFile()) {
var destination = dStats && dStats.isDirectory() ? command.destination + '/' + path.basename(command.source) : command.destination;
if (verbose) {
console.log(' - FileManagerPlugin: Start copy source: ' + command.source + ' to destination: ' + destination);
}
/*
* If the supplied destination is a directory copy inside.
* If the supplied destination is a directory that does not exist yet create it & copy inside
*/
var pathInfo = path.parse(destination);
var execCopy = function execCopy(src, dest) {
fsExtra.copy(src, dest, function (err) {
if (err) reject(err);
resolve();
});
};
if (pathInfo.ext === '') {
makeDir(destination).then(function (mPath) {
execCopy(command.source, destination + '/' + path.basename(command.source));
});
} else {
execCopy(command.source, destination);
}
} else {
var sourceDir = command.source + (command.source.substr(-1) !== '/' ? '/' : '') + '**/*';
copyDirectory(sourceDir, command.destination, resolve, reject, options);
}
});
});
} else {
copyDirectory(command.source, command.destination, resolve, reject, options);
}
});
};
}
/**
* Execute copy directory
*
* @param {string} source - source file path
* @param {string} destination - destination file path
* @param {Function} resolve - function used to resolve a Promise
* @param {Function} reject - function used to reject a Promise
* @return {void}
*/
function copyDirectory(source, destination, resolve, reject, options) {
var _this = this;
var verbose = options.verbose;
/* cpx options */
var cpxOptions = {
clean: false,
includeEmptyDirs: true,
update: false
};
if (verbose) {
console.log(' - FileManagerPlugin: Start copy source file: ' + source + ' to destination file: ' + destination);
}
cpx.copy(source, destination, cpxOptions, function (err) {
if (err && _this.options.verbose) {
console.log(' - FileManagerPlugin: Error - copy failed', err);
reject(err);
}
if (verbose) {
console.log(' - FileManagerPlugin: Finished copy source: ' + source + ' to destination: ' + destination);
}
resolve();
});
}
var fs$1 = require('fs');
var mv = require('mv');
/**
* Execute move action
*
* @param {Object} command - Command data for given action
* @return {Function|null} - Function that returns a promise or null
*/
function moveAction(command, options) {
var verbose = options.verbose;
if (!command.source || !command.destination) {
if (verbose) {
console.log(' - FileManagerPlugin: Warning - move parameter has to be formated as follows: { source: <string>, destination: <string> }');
}
return null;
}
if (fs$1.existsSync(command.source)) {
return function () {
return new Promise(function (resolve, reject) {
if (verbose) {
console.log(' - FileManagerPlugin: Start move source: ' + command.source + ' to destination: ' + command.destination);
}
mv(command.source, command.destination, { mkdirp: false }, function (err) {
if (err) {
if (verbose) {
console.log(' - FileManagerPlugin: Error - move failed', err);
}
reject(err);
}
if (verbose) {
console.log(' - FileManagerPlugin: Finished move source: ' + command.source + ' to destination: ' + command.destination);
}
resolve();
});
});
};
} else {
process.emitWarning(' - FileManagerPlugin: Could not move ' + command.source + ': path does not exist');
return null;
}
}
var fs$2 = require('fs');
var rimraf = require('rimraf');
/**
* Execute delete action
*
* @param {Object} command - Command data for given action
* @return {Function|null} - Function that returns a promise or null
*/
function deleteAction(command, options) {
var verbose = options.verbose;
return function () {
return new Promise(function (resolve, reject) {
if (verbose) {
console.log(' - FileManagerPlugin: Starting delete path ' + command.source);
}
if (typeof command.source !== 'string') {
if (verbose) {
console.log(' - FileManagerPlugin: Warning - delete parameter has to be type of string. Process canceled.');
}
reject();
}
rimraf(command.source, {}, function (response) {
if (verbose && response === null) {
console.log(' - FileManagerPlugin: Finished delete path ' + command.source);
}
resolve();
});
});
};
}
var makeDir$1 = require('make-dir');
/**
* Execute mkdir action
*
* @param {Object} command - Command data for given action
* @return {Function|null} - Function that returns a promise or null
*/
function mkdirAction(command, options) {
var verbose = options.verbose;
return function () {
if (verbose) {
console.log(' - FileManagerPlugin: Creating path ' + command.source);
}
if (typeof command.source !== 'string') {
if (verbose) {
console.log(' - FileManagerPlugin: Warning - mkdir parameter has to be type of string. Process canceled.');
}
return null;
}
return makeDir$1(command.source);
};
}
var fs$3 = require('fs');
var path$1 = require('path');
var archiver = require('archiver');
/**
* Execute mkdir action
*
* @param {Object} command - Command data for given action
* @return {Function|null} - Function that returns a promise or null
*/
function archiveAction(command, options) {
var verbose = options.verbose;
return function () {
return new Promise(function (resolve, reject) {
if (!command.source || !command.destination) {
if (verbose) {
console.log(' - FileManagerPlugin: Warning - archive parameter has to be formated as follows: { source: <string>, destination: <string> }');
}
reject();
}
var fileRegex = /(\*|\{+|\}+)/g;
var matches = fileRegex.exec(command.source);
var isGlob = matches !== null ? true : false;
fs$3.lstat(command.source, function (sErr, sStats) {
var output = fs$3.createWriteStream(command.destination);
var archive = archiver(command.format, command.options);
archive.on('error', function (err) {
return reject(err);
});
archive.pipe(output);
if (isGlob) archive.glob(command.source, command.options.globOptions || {});else if (sStats.isFile()) archive.file(command.source, { name: path$1.basename(command.source) });else if (sStats.isDirectory()) archive.directory(command.source, false);
archive.finalize().then(function () {
return resolve();
});
});
});
};
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {

@@ -43,11 +319,2 @@ return typeof obj;

var cpx = require("cpx");
var fs = require("fs");
var path = require("path");
var fsExtra = require("fs-extra");
var rimraf = require("rimraf");
var mv = require("mv");
var makeDir = require("make-dir");
var archiver = require("archiver");
var FileManagerPlugin = function () {

@@ -58,14 +325,6 @@ function FileManagerPlugin(options) {

this.options = this.setOptions(options);
this.isWin = /^win/.test(process.platform);
/* cpx options */
this.cpxOptions = {
clean: false,
includeEmptyDirs: true,
update: false
};
}
createClass(FileManagerPlugin, [{
key: "setOptions",
key: 'setOptions',
value: function setOptions(userOptions) {

@@ -88,3 +347,3 @@ var defaultOptions = {

}, {
key: "checkOptions",
key: 'checkOptions',
value: function checkOptions(stage) {

@@ -94,3 +353,3 @@ var _this = this;

if (this.options.verbose && Object.keys(this.options[stage]).length) {
console.log("FileManagerPlugin: processing " + stage + " event");
console.log('FileManagerPlugin: processing ' + stage + ' event');
}

@@ -119,300 +378,80 @@

}, {
key: "copyDirectory",
value: function copyDirectory(source, destination, resolve, reject) {
var _this2 = this;
key: 'replaceHash',
value: function replaceHash(filename) {
return filename.replace('[hash]', this.fileHash);
}
}, {
key: 'processAction',
value: function processAction(action, params, commandOrder) {
var result = action(params, this.options);
if (this.options.verbose) {
console.log(" - FileManagerPlugin: Start copy source file: " + source + " to destination file: " + destination);
if (result !== null) {
commandOrder.push(result);
}
cpx.copy(source, destination, this.cpxOptions, function (err) {
if (err && _this2.options.verbose) {
console.log(" - FileManagerPlugin: Error - copy failed", err);
reject(err);
}
if (_this2.options.verbose) {
console.log(" - FileManagerPlugin: Finished copy source: " + source + " to destination: " + destination);
}
resolve();
});
}
}, {
key: "replaceHash",
value: function replaceHash(filename) {
return filename.replace("[hash]", this.fileHash);
}
}, {
key: "parseFileOptions",
key: 'parseFileOptions',
value: function parseFileOptions(options) {
var _this3 = this;
var _this2 = this;
var optKeys = Object.keys(options);
var commandOrder = [];
for (var i = 0; i < optKeys.length; i++) {
var fileAction = optKeys[i];
var fileOptions = options[fileAction];
Object.keys(options).forEach(function (actionType) {
var actionOptions = options[actionType];
var actionParams = null;
switch (fileAction) {
case "copy":
var _loop = function _loop(key) {
var command = {
source: _this3.replaceHash(fileOptions[key].source),
destination: _this3.replaceHash(fileOptions[key].destination)
};
actionOptions.forEach(function (actionItem) {
switch (actionType) {
case 'copy':
actionParams = Object.assign({ source: _this2.replaceHash(actionItem.source) }, actionItem.destination && { destination: actionItem.destination });
if (!command.source || !command.destination) {
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Warning - copy parameter has to be formated as follows: { source: <string>, destination: <string> }");
}
return {
v: void 0
};
}
_this2.processAction(copyAction, actionParams, commandOrder);
commandOrder.push(function () {
return new Promise(function (resolve, reject) {
// if source is a file, just copyFile()
// if source is a NOT a glob pattern, simply append **/*
var fileRegex = /(\*|\{+|\}+)/g;
var matches = fileRegex.exec(command.source);
break;
if (matches === null) {
fs.lstat(command.source, function (sErr, sStats) {
if (sErr) return reject(sErr);
case 'move':
actionParams = Object.assign({ source: _this2.replaceHash(actionItem.source) }, actionItem.destination && { destination: actionItem.destination });
fs.lstat(command.destination, function (dErr, dStats) {
if (sStats.isFile()) {
var destination = dStats && dStats.isDirectory() ? command.destination + "/" + path.basename(command.source) : command.destination;
_this2.processAction(moveAction, actionParams, commandOrder);
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Start copy source: " + command.source + " to destination: " + destination);
}
break;
/*
* If the supplied destination is a directory copy inside.
* If the supplied destination is a directory that does not exist yet create it & copy inside
*/
var pathInfo = path.parse(destination);
var execCopy = function execCopy(src, dest) {
fsExtra.copy(src, dest, function (err) {
if (err) reject(err);
resolve();
});
};
if (pathInfo.ext === "") {
makeDir(destination).then(function (mPath) {
execCopy(command.source, destination + "/" + path.basename(command.source));
});
} else {
execCopy(command.source, destination);
}
} else {
var sourceDir = command.source + (command.source.substr(-1) !== "/" ? "/" : "") + "**/*";
_this3.copyDirectory(sourceDir, command.destination, resolve, reject);
}
});
});
} else {
_this3.copyDirectory(command.source, command.destination, resolve, reject);
}
});
});
};
for (var key in fileOptions) {
var _ret = _loop(key);
if ((typeof _ret === "undefined" ? "undefined" : _typeof(_ret)) === "object") return _ret.v;
}
break;
case "move":
var _loop2 = function _loop2(key) {
var command = {
source: _this3.replaceHash(fileOptions[key].source),
destination: _this3.replaceHash(fileOptions[key].destination)
};
if (!command.source || !command.destination) {
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Warning - move parameter has to be formated as follows: { source: <string>, destination: <string> }");
}
return {
v: void 0
};
case 'delete':
if (!Array.isArray(actionOptions)) {
throw Error(' - FileManagerPlugin: Fail - delete parameters has to be type of \'strings array\' but was \'' + (typeof actionOptions === 'undefined' ? 'undefined' : _typeof(actionOptions)) + '\'. Process canceled.');
}
if (fs.existsSync(command.source)) {
commandOrder.push(function () {
return new Promise(function (resolve, reject) {
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Start move source: " + command.source + " to destination: " + command.destination);
}
actionParams = Object.assign({ source: _this2.replaceHash(actionItem.source) });
_this2.processAction(deleteAction, actionParams, commandOrder);
mv(command.source, command.destination, { mkdirp: _this3.options.moveWithMkdirp }, function (err) {
if (err) {
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Error - move failed", err);
}
reject(err);
}
break;
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Finished move source: " + command.source + " to destination: " + command.destination);
}
case 'mkdir':
actionParams = { source: _this2.replaceHash(actionItem) };
_this2.processAction(mkdirAction, actionParams, commandOrder);
resolve();
});
});
});
} else {
process.emitWarning(" - FileManagerPlugin: Could not move " + command.source + ": path does not exist");
}
};
break;
for (var key in fileOptions) {
var _ret2 = _loop2(key);
if ((typeof _ret2 === "undefined" ? "undefined" : _typeof(_ret2)) === "object") return _ret2.v;
}
break;
case "delete":
if (!Array.isArray(fileOptions)) {
throw Error(" - FileManagerPlugin: Fail - delete parameters has to be type of 'strings array' but was '" + (typeof fileOptions === "undefined" ? "undefined" : _typeof(fileOptions)) + "'. Process canceled.");
}
var _loop3 = function _loop3(key) {
var path = _this3.replaceHash(fileOptions[key]);
commandOrder.push(function () {
return new Promise(function (resolve, reject) {
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Starting delete path " + path);
}
if (typeof path !== "string") {
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Warning - delete parameter has to be type of string. Process canceled.");
}
reject();
}
rimraf(path, {}, function (response) {
if (_this3.options.verbose && response === null) {
console.log(" - FileManagerPlugin: Finished delete path " + path);
}
resolve();
});
});
});
};
for (var key in fileOptions) {
_loop3(key);
}
break;
case "mkdir":
var _loop4 = function _loop4(key) {
var path = _this3.replaceHash(fileOptions[key]);
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Creating path " + path);
}
if (typeof path !== "string") {
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Warning - mkdir parameter has to be type of string. Process canceled.");
}
return {
v: void 0
};
}
commandOrder.push(function () {
return makeDir(path);
});
};
for (var key in fileOptions) {
var _ret4 = _loop4(key);
if ((typeof _ret4 === "undefined" ? "undefined" : _typeof(_ret4)) === "object") return _ret4.v;
}
break;
case "archive":
var _loop5 = function _loop5(key) {
var command = {
source: _this3.replaceHash(fileOptions[key].source),
destination: fileOptions[key].destination,
format: fileOptions[key].format ? fileOptions[key].format : "zip",
options: fileOptions[key].options ? fileOptions[key].options : { zlib: { level: 9 } }
case 'archive':
actionParams = {
source: _this2.replaceHash(actionItem.source),
destination: actionItem.destination,
format: actionItem.format ? actionItem.format : 'zip',
options: actionItem.options ? actionItem.options : { zlib: { level: 9 } }
};
if (!command.source || !command.destination) {
if (_this3.options.verbose) {
console.log(" - FileManagerPlugin: Warning - archive parameter has to be formated as follows: { source: <string>, destination: <string> }");
}
return {
v: void 0
};
}
_this2.processAction(archiveAction, actionParams, commandOrder);
commandOrder.push(function () {
return new Promise(function (resolve, reject) {
var fileRegex = /(\*|\{+|\}+)/g;
var matches = fileRegex.exec(command.source);
break;
var isGlob = matches !== null ? true : false;
default:
break;
}
});
});
fs.lstat(command.source, function (sErr, sStats) {
var output = fs.createWriteStream(command.destination);
var archive = archiver(command.format, command.options);
archive.on("error", function (err) {
reject(err);
});
archive.pipe(output);
if (isGlob) archive.glob(command.source, command.options.globOptions || {});else if (sStats.isFile()) archive.file(command.source, { name: path.basename(command.source) });else if (sStats.isDirectory()) archive.directory(command.source, false);
archive.finalize().then(function () {
return resolve();
});
});
});
});
};
for (var key in fileOptions) {
var _ret5 = _loop5(key);
if ((typeof _ret5 === "undefined" ? "undefined" : _typeof(_ret5)) === "object") return _ret5.v;
}
break;
default:
break;
}
}
return commandOrder;
}
}, {
key: "apply",
key: 'apply',
value: function apply(compiler) {

@@ -423,3 +462,3 @@ var that = this;

try {
that.checkOptions("onStart");
that.checkOptions('onStart');
} catch (error) {

@@ -434,3 +473,3 @@ compilation.errors.push(error);

try {
that.checkOptions("onEnd");
that.checkOptions('onEnd');
} catch (error) {

@@ -444,7 +483,7 @@ compilation.errors.push(error);

if (compiler.hooks) {
compiler.hooks.compilation.tap("compilation", comp);
compiler.hooks.afterEmit.tapAsync("afterEmit", afterEmit);
compiler.hooks.compilation.tap('compilation', comp);
compiler.hooks.afterEmit.tapAsync('afterEmit', afterEmit);
} else {
compiler.plugin("compilation", comp);
compiler.plugin("after-emit", afterEmit);
compiler.plugin('compilation', comp);
compiler.plugin('after-emit', afterEmit);
}

@@ -451,0 +490,0 @@ }

{
"name": "filemanager-webpack-plugin",
"version": "1.0.29",
"version": "2.0.0",
"description": "This Webpack plugin allows you to copy, archive (.zip), move, delete files and directories before and after builds",

@@ -14,3 +14,3 @@ "main": "lib/index.js",

"test-webpack4": "cd tests/webpack-4 && npm install && npm run test",
"prettier": "prettier --write src/index.js",
"prettier": "find src | xargs prettier --write",
"webpack": "webpack --progress",

@@ -35,7 +35,7 @@ "webpack-dev-server": "webpack-dev-server --colors --progress"

"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-core": "^6.26.3",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-env": "^1.7.0",
"delay": "^2.0.0",

@@ -45,8 +45,10 @@ "eslint": "^4.16.0",

"prettier": "^1.10.2",
"rollup": "^0.63.5",
"rollup-plugin-babel": "^3.0.7",
"rollup-plugin-commonjs": "^9.1.4",
"rollup-plugin-includepaths": "^0.2.3",
"rollup-plugin-node-resolve": "^3.0.0",
"webpack": "^4.2.0",
"webpack-cli": "2.0.12",
"webpack-dev-server": "^3.1.1",
"rollup": "^0.57.1",
"rollup-plugin-babel": "^3.0.2",
"rollup-plugin-node-resolve": "^3.0.0"
"webpack-dev-server": "^3.1.1"
},

@@ -53,0 +55,0 @@ "dependencies": {

@@ -115,10 +115,10 @@

```js
new FileManagerPlugin(object)
new FileManagerPlugin(fileEvents, options)
```
#### Event Options
#### File Events
* `onStart`: Commands to execute before Webpack begins the bundling process
* `onEnd`: Commands to execute after Webpack has finished the bundling process
#### File Management Options
#### File Actions

@@ -125,0 +125,0 @@ |Name|Description|Example

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc