build-firewall-plugin 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/build/index.js +29 -33
  2. package/package.json +1 -1
package/build/index.js CHANGED
@@ -17,16 +17,9 @@ const settings = {
17
17
  * empty template of project rules map
18
18
  */
19
19
  const template = {
20
- layers: {
21
- ui: settings,
22
- lib: settings,
23
- hooks: settings,
24
- pages: settings,
25
- models: settings,
26
- widgets: settings,
20
+ layers: [],
21
+ rules: {
27
22
  services: settings,
28
- components: settings,
29
- declarations: settings,
30
23
  },
31
24
  mode: 'warn',
32
25
  };
@@ -37,36 +30,34 @@ const exceptions = ['node_modules', 'assets', '.css', '.scss', 'styles', 'style'
37
30
  /**
38
31
  * @function guard validates received file by path
39
32
  */
40
- const isValidSchema = (value) => {
41
- var _a, _b;
42
- return (!!value &&
43
- typeof value === 'object' &&
44
- 'mode' in value &&
45
- 'layers' in value &&
46
- typeof value.layers === 'object' &&
47
- Object.keys((_a = value.layers) !== null && _a !== void 0 ? _a : {}).length > 0 &&
48
- (value.mode === 'warn' || value.mode === 'error') &&
49
- Object.entries((_b = value.layers) !== null && _b !== void 0 ? _b : {}).every(([_, layer]) => typeof layer === 'object' && 'allow' in layer && 'deny' in layer
50
- && Array.isArray(layer.allow) && Array.isArray(layer.deny)));
33
+ const isValidSchema = (v) => {
34
+ return (!!v && typeof v === 'object' &&
35
+ 'layers' in v && 'rules' in v && 'mode' in v &&
36
+ Array.isArray(v.layers) &&
37
+ v.layers.every((l) => typeof l === 'string') &&
38
+ !!v.rules && typeof v.rules === 'object' &&
39
+ Object.values(v.rules).every((r) => !!r &&
40
+ typeof r === 'object' &&
41
+ 'deny' in r && 'allow' in r &&
42
+ Array.isArray(r.allow) &&
43
+ Array.isArray(r.deny)) && (v.mode === 'warn' || v.mode === 'error'));
51
44
  };
52
45
  /**
53
46
  * @function resolveLayer normilizes module resource
54
47
  */
55
48
  function resolveLayer(resource, layers) {
56
49
  const normalized = resource.replace(/\\/g, '/');
57
- for (const layer of Object.keys(layers)) {
50
+ for (const layer of layers) {
58
51
  if (normalized.includes(`/src/${layer}/`)) {
59
52
  return layer;
60
53
  }
61
54
  }
62
55
  return null;
63
56
  }
64
- ;
65
57
  /**
66
58
  * @function prepareOutput is for object type output
67
59
  */
68
60
  function prepareOutput(params) {
69
- const { total, modules, warnings, layers } = params;
70
61
  return {
71
62
  plugin: 'Build Firewall Plugin',
72
63
  message_type: 'output',
@@ -91,6 +82,7 @@ class BuildFirewallPlugin {
91
82
  mode: template.mode,
92
83
  outputType: 'default',
93
84
  layers: template.layers,
85
+ rules: template.rules,
94
86
  name: 'build-firewall-plugin.rules.json',
95
87
  };
96
88
  this.shouldRunAgain = false;
@@ -101,7 +93,7 @@ class BuildFirewallPlugin {
101
93
  this.shouldRunAgain = false;
102
94
  compiler.hooks.thisCompilation.tap('BuildFirewallPluginThisCompilation', (compilation) => {
103
95
  compilation.hooks.finishModules.tap('BuildFirewallPluginThisCompilationFinishModules', (modules) => {
104
- console.log('[Build Firewall Plugin] Starting... \n');
96
+ console.log('[Build Firewall Plugin] Starting...');
105
97
  /**
106
98
  * Creating or Validating a rules file
107
99
  */
@@ -111,7 +103,7 @@ class BuildFirewallPlugin {
111
103
  fs_1.default.writeFileSync(pathToFile, JSON.stringify(template));
112
104
  this.shouldRunAgain = true;
113
105
  this.config.path = pathToFile;
114
- console.log(`[Build Firewall Plugin] Created a template of ${this.config.name} file successfully \n`);
106
+ console.log(`[Build Firewall Plugin] Created a template of ${this.config.name} file successfully`);
115
107
  console.log(`[Build Firewall Plugin] Check and edit a ${this.config.name} file if it is not passing for your project \n`);
116
108
  }
117
109
  catch (e) {
@@ -145,12 +137,16 @@ class BuildFirewallPlugin {
145
137
  const hasValidSchema = isValidSchema(parsedFile);
146
138
  if (!hasValidSchema) {
147
139
  console.error(`[Build Firewall Plugin] Your ${this.config.name} file value is not valid schema, it has to be like this: \n
148
- {
149
- layers: {
150
- [name]: { allow: [], deny: [] },
151
- mode: 'warn'
152
- }
153
- }
140
+ ${JSON.stringify({
141
+ layers: [],
142
+ rules: {
143
+ layer_name: {
144
+ allow: [],
145
+ deny: [],
146
+ },
147
+ },
148
+ mode: 'warn'
149
+ })}
154
150
  \n`);
155
151
  return;
156
152
  }
@@ -203,7 +199,7 @@ class BuildFirewallPlugin {
203
199
  if (selfImport) {
204
200
  continue;
205
201
  }
206
- const rules = this.config.layers[fromLayer];
202
+ const rules = this.config.rules[fromLayer];
207
203
  if (!rules)
208
204
  continue;
209
205
  const isDenied = rules.deny.includes(toLayer);
@@ -214,7 +210,7 @@ class BuildFirewallPlugin {
214
210
  seen.add(moduleKey);
215
211
  warningsAmount++;
216
212
  if (this.config.outputType === 'default') {
217
- console.log(`\n [Build Firewall Plugin] Module ${toModuleResource} is not allowed to import in ${fromResource} \n`);
213
+ console.log(`\n [Build Firewall Plugin] Module ${fromResource} imports forbidden module ${toModuleResource} \n`);
218
214
  }
219
215
  }
220
216
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "build-firewall-plugin",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "type": "commonjs",
5
5
  "license": "MIT",
6
6
  "main": "build/index.js",