node-pluginsmanager 2.3.9 → 2.4.0

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 (57) hide show
  1. package/lib/cjs/PluginsManager.d.ts +38 -0
  2. package/lib/cjs/PluginsManager.js +442 -0
  3. package/lib/cjs/checkers/checkAbsoluteDirectory.d.ts +1 -0
  4. package/lib/cjs/checkers/checkAbsoluteDirectory.js +20 -0
  5. package/lib/cjs/checkers/checkDirectory.d.ts +1 -0
  6. package/lib/cjs/checkers/checkDirectory.js +24 -0
  7. package/lib/cjs/checkers/checkFunction.d.ts +1 -0
  8. package/lib/cjs/checkers/checkFunction.js +16 -0
  9. package/lib/cjs/checkers/checkNonEmptyArray.d.ts +1 -0
  10. package/lib/cjs/checkers/checkNonEmptyArray.js +19 -0
  11. package/lib/cjs/checkers/checkNonEmptyString.d.ts +1 -0
  12. package/lib/cjs/checkers/checkNonEmptyString.js +19 -0
  13. package/lib/cjs/checkers/checkOrchestrator.d.ts +1 -0
  14. package/lib/cjs/checkers/checkOrchestrator.js +36 -0
  15. package/lib/cjs/cmd/cmd.d.ts +1 -0
  16. package/lib/cjs/cmd/cmd.js +37 -0
  17. package/lib/cjs/cmd/git/gitInstall.d.ts +1 -0
  18. package/lib/cjs/cmd/git/gitInstall.js +42 -0
  19. package/lib/cjs/cmd/git/gitUpdate.d.ts +1 -0
  20. package/lib/cjs/cmd/git/gitUpdate.js +18 -0
  21. package/lib/cjs/cmd/npm/npmCmd.d.ts +1 -0
  22. package/lib/cjs/cmd/npm/npmCmd.js +18 -0
  23. package/lib/cjs/cmd/npm/npmInstall.d.ts +1 -0
  24. package/lib/cjs/cmd/npm/npmInstall.js +14 -0
  25. package/lib/cjs/cmd/npm/npmUpdate.d.ts +1 -0
  26. package/lib/cjs/cmd/npm/npmUpdate.js +14 -0
  27. package/lib/cjs/cmd/stdToString.d.ts +1 -0
  28. package/lib/cjs/cmd/stdToString.js +16 -0
  29. package/lib/cjs/createPluginByDirectory.d.ts +2 -0
  30. package/lib/cjs/createPluginByDirectory.js +61 -0
  31. package/lib/cjs/extractGithub.d.ts +1 -0
  32. package/lib/cjs/extractGithub.js +20 -0
  33. package/lib/cjs/initSortedPlugins.d.ts +2 -0
  34. package/lib/cjs/initSortedPlugins.js +56 -0
  35. package/lib/cjs/loadSortedPlugins.d.ts +2 -0
  36. package/lib/cjs/loadSortedPlugins.js +74 -0
  37. package/lib/cjs/main.cjs +8 -0
  38. package/lib/cjs/main.d.cts +2 -0
  39. package/package.json +30 -12
  40. package/lib/checkers/isAbsoluteDirectory.js +0 -27
  41. package/lib/checkers/isDirectory.js +0 -34
  42. package/lib/checkers/isFunction.js +0 -18
  43. package/lib/checkers/isNonEmptyArray.js +0 -21
  44. package/lib/checkers/isNonEmptyString.js +0 -21
  45. package/lib/checkers/isOrchestrator.js +0 -41
  46. package/lib/cmd/cmd.js +0 -44
  47. package/lib/cmd/git/install.js +0 -49
  48. package/lib/cmd/git/update.js +0 -23
  49. package/lib/cmd/npm/cmd.js +0 -23
  50. package/lib/cmd/npm/install.js +0 -15
  51. package/lib/cmd/npm/update.js +0 -15
  52. package/lib/cmd/stdToString.js +0 -17
  53. package/lib/createPluginByDirectory.js +0 -70
  54. package/lib/index.d.ts +0 -81
  55. package/lib/initSortedPlugins.js +0 -96
  56. package/lib/loadSortedPlugins.js +0 -132
  57. package/lib/main.js +0 -743
package/lib/main.js DELETED
@@ -1,743 +0,0 @@
1
- /*
2
- eslint-disable max-lines, prefer-destructuring, func-style
3
- */
4
-
5
- "use strict";
6
-
7
- // deps
8
-
9
- // natives
10
- const Events = require("events");
11
- const { join } = require("path");
12
- const { homedir } = require("os");
13
-
14
- // externals
15
- const versionModulesChecker = require("check-version-modules");
16
- const { mkdirp, readdir, remove } = require("fs-extra");
17
-
18
- // locals
19
- const isAbsoluteDirectory = require(join(__dirname, "checkers", "isAbsoluteDirectory.js"));
20
- const isFunction = require(join(__dirname, "checkers", "isFunction.js"));
21
- const isNonEmptyArray = require(join(__dirname, "checkers", "isNonEmptyArray.js"));
22
- const isNonEmptyString = require(join(__dirname, "checkers", "isNonEmptyString.js"));
23
- const isOrchestrator = require(join(__dirname, "checkers", "isOrchestrator.js"));
24
- const createPluginByDirectory = require(join(__dirname, "createPluginByDirectory.js"));
25
-
26
- // git
27
- const gitInstall = require(join(__dirname, "cmd", "git", "install.js"));
28
- const gitUpdate = require(join(__dirname, "cmd", "git", "update.js"));
29
-
30
- // npm
31
- const npmInstall = require(join(__dirname, "cmd", "npm", "install.js"));
32
- const npmUpdate = require(join(__dirname, "cmd", "npm", "update.js"));
33
-
34
- const loadSortedPlugins = require(join(__dirname, "loadSortedPlugins.js"));
35
- const initSortedPlugins = require(join(__dirname, "initSortedPlugins.js"));
36
-
37
- // consts
38
-
39
- const DEFAULT_PLUGINS_DIRECTORY = join(homedir(), "node-pluginsmanager-plugins");
40
- const DEFAULT_RESSOURCES_DIRECTORY = join(homedir(), "node-pluginsmanager-resources");
41
-
42
- // module
43
-
44
- module.exports = class PluginsManager extends Events {
45
-
46
- constructor (options) {
47
-
48
- super(options);
49
-
50
- // protected
51
-
52
- this._beforeLoadAll = null;
53
- this._beforeInitAll = null;
54
-
55
- this._orderedPluginsNames = [];
56
-
57
- this._logger = options && "function" === typeof options.logger ?
58
- options.logger : null;
59
-
60
- // public
61
-
62
- this.plugins = [];
63
-
64
- this.directory = options && "undefined" !== typeof options.directory ?
65
- options.directory : DEFAULT_PLUGINS_DIRECTORY;
66
-
67
- this.externalRessourcesDirectory = options && "undefined" !== typeof options.externalRessourcesDirectory ?
68
- options.externalRessourcesDirectory : DEFAULT_RESSOURCES_DIRECTORY;
69
-
70
- }
71
-
72
- // public
73
-
74
- // getters
75
-
76
- getPluginsNames () {
77
-
78
- return [ ...this.plugins ].map((plugin) => {
79
- return plugin.name;
80
- });
81
-
82
- }
83
-
84
- // setters
85
-
86
- setOrder (pluginsNames) {
87
-
88
- return isNonEmptyArray("setOrder/pluginsNames", pluginsNames).then(() => {
89
-
90
- const errors = [];
91
- for (let i = 0; i < pluginsNames.length; ++i) {
92
-
93
- if ("string" !== typeof pluginsNames[i]) {
94
- errors.push("The directory at index \"" + i + "\" must be a string");
95
- }
96
- else if ("" === pluginsNames[i].trim()) {
97
- errors.push("The directory at index \"" + i + "\" must be not empty");
98
- }
99
- else if (1 < pluginsNames.filter((name) => {
100
- return name === pluginsNames[i];
101
- }).length) {
102
- errors.push("The directory at index \"" + i + "\" is given twice or more");
103
- }
104
-
105
- }
106
-
107
- return !errors.length ? Promise.resolve() : Promise.reject(new Error(errors.join("\r\n")));
108
-
109
- }).then(() => {
110
-
111
- this._orderedPluginsNames = pluginsNames;
112
-
113
- return Promise.resolve();
114
-
115
- });
116
-
117
- }
118
-
119
- getOrder () {
120
-
121
- return [ ...this._orderedPluginsNames ];
122
-
123
- }
124
-
125
- // checkers
126
-
127
- checkAllModules () {
128
-
129
- const _checkPluginsModules = (i = 0) => {
130
-
131
- return i < this.plugins.length ? this.checkModules(this.plugins[i]).then(() => {
132
-
133
- return _checkPluginsModules(i + 1);
134
-
135
- }) : Promise.resolve();
136
-
137
- };
138
-
139
- return _checkPluginsModules();
140
-
141
- }
142
-
143
- checkModules (plugin) {
144
-
145
- return isAbsoluteDirectory("checkModules/directory", this.directory).then(() => {
146
-
147
- return isOrchestrator("checkModules/plugin", plugin);
148
-
149
- }).then(() => {
150
-
151
- return versionModulesChecker(join(this.directory, plugin.name, "package.json"), {
152
- "failAtMajor": true,
153
- "failAtMinor": true,
154
- "failAtPatch": false,
155
- "dev": false,
156
- "console": false
157
- }).then((valid) => {
158
-
159
- return valid ? Promise.resolve() : Promise.reject(new Error("\"" + plugin.name + "\" plugin has obsoletes modules"));
160
-
161
- });
162
-
163
- });
164
-
165
- }
166
-
167
- // network
168
-
169
- appMiddleware (req, res, next) {
170
-
171
- const plugins = this.plugins.filter((plugin) => {
172
- return "function" === typeof plugin.appMiddleware;
173
- });
174
-
175
- if (!plugins.length) {
176
- return next();
177
- }
178
- else {
179
-
180
- const _recursiveNext = (i) => {
181
-
182
- if (i >= plugins.length) {
183
- return next;
184
- }
185
- else {
186
-
187
- return () => {
188
- return plugins[i].appMiddleware(req, res, _recursiveNext(i + 1));
189
- };
190
-
191
- }
192
-
193
- };
194
-
195
- return plugins[0].appMiddleware(req, res, _recursiveNext(1));
196
-
197
- }
198
-
199
- }
200
-
201
- socketMiddleware (server) {
202
-
203
- this.plugins.filter((plugin) => {
204
- return "function" === typeof plugin.socketMiddleware;
205
- }).forEach((plugin) => {
206
- plugin.socketMiddleware(server);
207
- });
208
-
209
- }
210
-
211
- // load / destroy
212
-
213
- beforeLoadAll (callback) {
214
-
215
- return isFunction("beforeLoadAll/callback", callback).then(() => {
216
-
217
- this._beforeLoadAll = callback;
218
-
219
- return Promise.resolve();
220
-
221
- });
222
-
223
- }
224
-
225
- loadAll (data) {
226
-
227
- // create dir if not exist
228
- return isNonEmptyString("initAll/directory", this.directory).then(() => {
229
-
230
- return mkdirp(this.directory).then(() => {
231
- return isAbsoluteDirectory("initAll/directory", this.directory);
232
- });
233
-
234
- // create dir if not exist
235
- }).then(() => {
236
-
237
- return isNonEmptyString("initAll/externalRessourcesDirectory", this.externalRessourcesDirectory).then(() => {
238
-
239
- return mkdirp(this.externalRessourcesDirectory).then(() => {
240
- return isAbsoluteDirectory("initAll/externalRessourcesDirectory", this.externalRessourcesDirectory);
241
- });
242
-
243
- });
244
-
245
- // ensure loop
246
- }).then(() => {
247
-
248
- return new Promise((resolve) => {
249
- setImmediate(resolve);
250
- });
251
-
252
- // execute _beforeLoadAll
253
- }).then(() => {
254
-
255
- return "function" !== typeof this._beforeLoadAll ? Promise.resolve() : new Promise((resolve, reject) => {
256
-
257
- const fn = this._beforeLoadAll(data);
258
-
259
- if (!(fn instanceof Promise)) {
260
- resolve();
261
- }
262
- else {
263
- fn.then(resolve).catch(reject);
264
- }
265
-
266
- });
267
-
268
- // init plugins
269
- }).then(() => {
270
-
271
- return readdir(this.directory);
272
-
273
- // load all
274
- }).then((files) => {
275
-
276
- return loadSortedPlugins(
277
- this.directory, this.externalRessourcesDirectory, files, this.plugins,
278
- this._orderedPluginsNames, this.emit.bind(this), this._logger, data
279
- );
280
-
281
- // sort plugins
282
- }).then(() => {
283
-
284
- this.plugins.sort((a, b) => {
285
-
286
- if (a.name < b.name) {
287
- return -1;
288
- }
289
- else if (a.name > b.name) {
290
- return 1;
291
- }
292
- else {
293
- return 0;
294
- }
295
-
296
- });
297
-
298
- return Promise.resolve();
299
-
300
- // end
301
- }).then(() => {
302
-
303
- this.emit("allloaded", data);
304
-
305
- return Promise.resolve();
306
-
307
- });
308
-
309
- }
310
-
311
- destroyAll (data) {
312
-
313
- return Promise.resolve().then(() => {
314
-
315
- const _destroyPlugin = (i = this.plugins.length - 1) => {
316
-
317
- return -1 < i ? Promise.resolve().then(() => {
318
-
319
- const pluginName = this.plugins[i].name;
320
-
321
- // emit event
322
- return this.plugins[i].destroy(data).then(() => {
323
-
324
- this.emit("destroyed", pluginName, data);
325
-
326
- return Promise.resolve();
327
-
328
- // loop
329
- }).then(() => {
330
-
331
- return _destroyPlugin(i - 1);
332
-
333
- });
334
-
335
- }) : Promise.resolve();
336
-
337
- };
338
-
339
- return _destroyPlugin();
340
-
341
- // end
342
- }).then(() => {
343
-
344
- this.plugins = [];
345
-
346
- this.emit("alldestroyed", data);
347
-
348
- return Promise.resolve();
349
-
350
- // remove all external resources
351
- }).then(() => {
352
-
353
- return remove(this.externalRessourcesDirectory);
354
-
355
- });
356
-
357
- }
358
-
359
- // init / release
360
-
361
- beforeInitAll (callback) {
362
-
363
- return isFunction("beforeInitAll/callback", callback).then(() => {
364
-
365
- this._beforeInitAll = callback;
366
-
367
- return Promise.resolve();
368
-
369
- });
370
-
371
- }
372
-
373
- initAll (data) {
374
-
375
- return isAbsoluteDirectory("initAll/directory", this.directory).then(() => {
376
-
377
- return isAbsoluteDirectory("initAll/externalRessourcesDirectory", this.externalRessourcesDirectory);
378
-
379
- }).then(() => {
380
-
381
- // execute _beforeInitAll
382
- return "function" !== typeof this._beforeInitAll ? Promise.resolve() : new Promise((resolve, reject) => {
383
-
384
- const fn = this._beforeInitAll(data);
385
-
386
- if (!(fn instanceof Promise)) {
387
- resolve();
388
- }
389
- else {
390
- fn.then(resolve).catch(reject);
391
- }
392
-
393
- });
394
-
395
- // init plugins
396
- }).then(() => {
397
-
398
- return initSortedPlugins(
399
- this.plugins, this._orderedPluginsNames, (...subdata) => {
400
- this.emit(...subdata);
401
- }, data
402
- );
403
-
404
- // end
405
- }).then(() => {
406
-
407
- this.emit("allinitialized", data);
408
-
409
- return Promise.resolve();
410
-
411
- });
412
-
413
- }
414
-
415
- releaseAll (data) {
416
-
417
- return Promise.resolve().then(() => {
418
-
419
- const _releasePlugin = (i = this.plugins.length - 1) => {
420
-
421
- return -1 < i ? Promise.resolve().then(() => {
422
-
423
- return this.plugins[i].release(data);
424
-
425
- // emit event
426
- }).then(() => {
427
-
428
- this.emit("released", this.plugins[i], data);
429
-
430
- return Promise.resolve();
431
-
432
- // loop
433
- }).then(() => {
434
-
435
- return _releasePlugin(i - 1);
436
-
437
- }) : Promise.resolve();
438
-
439
- };
440
-
441
- return _releasePlugin();
442
-
443
- // end
444
- }).then(() => {
445
-
446
- this.emit("allreleased", data);
447
-
448
- return Promise.resolve();
449
-
450
- });
451
-
452
- }
453
-
454
- // write
455
-
456
- installViaGithub (user, repo, data) {
457
-
458
- return isAbsoluteDirectory("installViaGithub/directory", this.directory).then(() => {
459
- return isNonEmptyString("installViaGithub/user", user);
460
- }).then(() => {
461
- return isNonEmptyString("installViaGithub/repo", repo);
462
- }).then(() => {
463
-
464
- const directory = join(this.directory, repo);
465
-
466
- return new Promise((resolve, reject) => {
467
-
468
- isAbsoluteDirectory("installViaGithub/plugindirectory", directory).then(() => {
469
- reject(new Error("\"" + repo + "\" plugin already exists"));
470
- }).catch(() => {
471
- resolve(directory);
472
- });
473
-
474
- });
475
-
476
- }).then((directory) => {
477
-
478
- return Promise.resolve().then(() => {
479
-
480
- return gitInstall(directory, user, repo);
481
-
482
- }).then(() => {
483
-
484
- // install dependencies & execute install script
485
- return createPluginByDirectory(directory, this.externalRessourcesDirectory, this._logger);
486
-
487
- // check plugin modules versions
488
- }).then((plugin) => {
489
-
490
- return this.checkModules(plugin).then(() => {
491
- return Promise.resolve(plugin);
492
- });
493
-
494
- }).then((plugin) => {
495
-
496
- return Promise.resolve().then(() => {
497
-
498
- return !plugin.dependencies ? Promise.resolve() : npmInstall(directory);
499
-
500
- }).then(() => {
501
-
502
- return plugin.install(data);
503
-
504
- // execute init script
505
- }).then(() => {
506
-
507
- this.emit("installed", plugin, data);
508
-
509
- return plugin.init(data);
510
-
511
- }).then(() => {
512
-
513
- this.emit("initialized", plugin, data);
514
- this.plugins.push(plugin);
515
-
516
- return Promise.resolve(plugin);
517
-
518
- }).catch((err) => {
519
-
520
- return this.uninstall(plugin, data).then(() => {
521
- return Promise.reject(err);
522
- });
523
-
524
- });
525
-
526
- }).catch((err) => {
527
-
528
- return new Promise((resolve, reject) => {
529
-
530
- isAbsoluteDirectory("installViaGithub/plugindirectory", directory).then(() => {
531
-
532
- return remove(directory).then(() => {
533
- reject(err);
534
- });
535
-
536
- }).catch(() => {
537
- reject(err);
538
- });
539
-
540
- });
541
-
542
- });
543
-
544
- });
545
-
546
- }
547
-
548
- updateViaGithub (plugin, data) {
549
-
550
- let directory = "";
551
- let key = -1;
552
-
553
- // check plugin
554
- return Promise.resolve().then(() => {
555
-
556
- return isOrchestrator("updateViaGithub/plugin", plugin).then(() => {
557
-
558
- let github = "";
559
- if ("string" === typeof plugin.github) {
560
- github = plugin.github;
561
- }
562
- else if ("string" === typeof plugin.repository) {
563
- github = plugin.repository;
564
- }
565
- else if (plugin.repository && "string" === typeof plugin.repository.url) {
566
- github = plugin.repository.url;
567
- }
568
-
569
- return isNonEmptyString("updateViaGithub/github", github).catch(() => {
570
-
571
- return Promise.reject(new ReferenceError(
572
- "Plugin \"" + plugin.name + "\" must be linked in the package to a github project to be updated"
573
- ));
574
-
575
- });
576
-
577
- }).then(() => {
578
-
579
- key = this.getPluginsNames().findIndex((pluginName) => {
580
- return pluginName === plugin.name;
581
- });
582
-
583
- return -1 < key ? Promise.resolve() : Promise.reject(new Error("Plugin \"" + plugin.name + "\" is not registered"));
584
-
585
- });
586
-
587
- // check plugin directory
588
- }).then(() => {
589
-
590
- return isAbsoluteDirectory("updateViaGithub/directory", this.directory).then(() => {
591
-
592
- directory = join(this.directory, plugin.name);
593
-
594
- return isAbsoluteDirectory("updateViaGithub/plugindirectory", directory);
595
-
596
- });
597
-
598
- // release plugin
599
- }).then(() => {
600
-
601
- const pluginName = plugin.name;
602
-
603
- return plugin.release(data).then(() => {
604
-
605
- this.emit("released", plugin, data);
606
-
607
- return plugin.destroy(data);
608
-
609
- }).then(() => {
610
-
611
- this.emit("destroyed", pluginName, data);
612
-
613
- this.plugins[key] = null;
614
-
615
- return Promise.resolve();
616
-
617
- });
618
-
619
- // download plugin
620
- }).then(() => {
621
-
622
- return gitUpdate(directory).then(() => {
623
-
624
- return createPluginByDirectory(directory, this.externalRessourcesDirectory, this._logger);
625
-
626
- });
627
-
628
- // check plugin modules versions
629
- }).then((_plugin) => {
630
-
631
- return this.checkModules(_plugin).then(() => {
632
- return Promise.resolve(_plugin);
633
- });
634
-
635
- }).then((_plugin) => {
636
-
637
- // update dependencies & execute update script
638
- return Promise.resolve().then(() => {
639
-
640
- return !_plugin.dependencies ? Promise.resolve() : npmUpdate(directory);
641
-
642
- }).then(() => {
643
-
644
- return _plugin.update(data);
645
-
646
- }).then(() => {
647
-
648
- this.emit("updated", _plugin, data);
649
-
650
- return _plugin.init(data);
651
-
652
- // execute init script
653
- }).then(() => {
654
-
655
- this.emit("initialized", _plugin, data);
656
- this.plugins[key] = _plugin;
657
-
658
- return Promise.resolve(_plugin);
659
-
660
- });
661
-
662
- });
663
-
664
- }
665
-
666
- uninstall (plugin, data) {
667
-
668
- let directory = "";
669
- let key = -1;
670
- let pluginName = "";
671
-
672
- // check plugin
673
- return Promise.resolve().then(() => {
674
-
675
- return isOrchestrator("uninstall/plugin", plugin).then(() => {
676
-
677
- key = this.getPluginsNames().findIndex((name) => {
678
- return name === plugin.name;
679
- });
680
-
681
- return -1 < key ? Promise.resolve() : Promise.reject(new Error("Plugin \"" + plugin.name + "\" is not registered"));
682
-
683
- });
684
-
685
- // check plugin directory
686
- }).then(() => {
687
-
688
- return isAbsoluteDirectory("uninstall/directory", this.directory).then(() => {
689
-
690
- pluginName = plugin.name;
691
- directory = join(this.directory, pluginName);
692
-
693
- return isAbsoluteDirectory("uninstall/plugindirectory", directory);
694
-
695
- });
696
-
697
- // release plugin
698
- }).then(() => {
699
-
700
- return plugin.release(data).then(() => {
701
-
702
- return remove(join(this.externalRessourcesDirectory, pluginName));
703
-
704
- }).then(() => {
705
-
706
- this.emit("released", plugin, data);
707
-
708
- return plugin.destroy(data);
709
-
710
- }).then(() => {
711
-
712
- this.emit("destroyed", pluginName, data);
713
-
714
- this.plugins.splice(key, 1);
715
-
716
- return Promise.resolve();
717
-
718
- });
719
-
720
- // update dependencies & execute update script
721
- }).then(() => {
722
-
723
- return Promise.resolve().then(() => {
724
-
725
- return plugin.uninstall(data);
726
-
727
- }).then(() => {
728
-
729
- this.emit("uninstalled", pluginName, data);
730
-
731
- return remove(directory);
732
-
733
- });
734
-
735
- }).then(() => {
736
-
737
- return Promise.resolve(pluginName);
738
-
739
- });
740
-
741
- }
742
-
743
- };