Skip to content

Commit

Permalink
Add schema validations for options
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Oct 4, 2020
1 parent 7c00f57 commit fec1785
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 82 deletions.
124 changes: 105 additions & 19 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@
"cpy": "^8.1.1",
"make-dir": "^3.1.0",
"mv": "^2.1.1",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"schema-utils": "^2.7.1"
},
"devDependencies": {
"@ava/babel": "^1.0.1",
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"@babel/register": "^7.11.5",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-commonjs": "^15.1.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"ava": "^3.13.0",
"glob": "^7.1.6",
Expand Down
4 changes: 4 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { babel } from '@rollup/plugin-babel';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';

export default {
input: 'src/index.js',
plugins: [
commonjs(),
json(),
babel({
babelHelpers: 'bundled',
}),
Expand Down
9 changes: 0 additions & 9 deletions src/actions/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ function archiveAction(command, options) {

return () =>
new Promise((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();
}

const fileRegex = /(\*|\{+|\}+)/g;
const matches = fileRegex.exec(command.source);

Expand Down
9 changes: 0 additions & 9 deletions src/actions/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ const cpFile = require('cp-file');
function copyAction(command, options) {
const { verbose, context } = options;

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 () =>
new Promise((resolve, reject) => {
// if source is a file, just copyFile()
Expand Down
7 changes: 0 additions & 7 deletions src/actions/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ function deleteAction(command, options) {
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();
}

const source = path.resolve(context, command.source);

rimraf(source, {}, (response) => {
Expand Down
7 changes: 0 additions & 7 deletions src/actions/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ function mkdirAction(command, options) {
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;
}

const source = path.resolve(context, command.source);
return makeDir(source);
};
Expand Down
9 changes: 0 additions & 9 deletions src/actions/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ const mv = require('mv');
function moveAction(command, options) {
const { verbose } = options;

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.existsSync(command.source)) {
return () =>
new Promise((resolve, reject) => {
Expand Down
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import validateOptions from 'schema-utils';

import { copyAction, moveAction, mkdirAction, archiveAction, deleteAction } from './actions';

import schema from './options-schema';

const PLUGIN_NAME = 'FileManagerPlugin';

class FileManagerPlugin {
constructor(options) {
validateOptions(schema, options, {
name: PLUGIN_NAME,
baseDataPath: 'options',
});

this.options = this.setOptions(options);
}

Expand Down Expand Up @@ -147,8 +158,8 @@ class FileManagerPlugin {
cb();
};

compiler.hooks.compilation.tap('compilation', comp);
compiler.hooks.afterEmit.tapAsync('afterEmit', afterEmit);
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, comp);
compiler.hooks.afterEmit.tapAsync(PLUGIN_NAME, afterEmit);
}
}

Expand Down
Loading

0 comments on commit fec1785

Please sign in to comment.