pigallery2-extension-kit 2.0.3-edge6 → 2.5.0-edge

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.
package/README.md CHANGED
@@ -7,7 +7,7 @@ See sample extension at https://github.com/bpatrik/pigallery2-sample-extension.
7
7
  # Extension Usage
8
8
 
9
9
  Extension folder can be set through config. For the docker-ised version,
10
- they live under the `config/extension` folder in their own subdirectory.
10
+ they live under the `config/extension` folder in their own subdirectory.
11
11
 
12
12
  # Extension development
13
13
 
@@ -21,16 +21,20 @@ You need at least a `server.js` in your extension folder that exports a `init(ex
21
21
  <path to the extension fodler>/myextension/package.js <- this is optional. You can add extra npm packages here
22
22
  <path to the extension fodler>/myextension/server.js <- this is needed
23
23
  ```
24
+
24
25
  Where `<path to the extension fodler>` is what you set in the config and `myextension` is the name of your extension.
25
- Note: you do not need to add your `node_modules` folder. The app will call `npm intall` when initializing your extension.
26
+ Note: you do not need to add your `node_modules` folder. The app will call `npm install` when initializing your extension.
27
+
26
28
  ## Extension environment
27
29
 
28
30
  The app runs the extension the following way:
29
- * It reads all extensions in `<path to the extension fodler>/**` folder
30
- * Checks if `package.js` is present. If yes installs the packages
31
- * Checks if `server.js` is present. If yes, calls the `init` function.
31
+
32
+ - It reads all extensions in `<path to the extension fodler>/**` folder
33
+ - Checks if `package.js` is present. If yes installs the packages
34
+ - Checks if `server.js` is present. If yes, calls the `init` function.
32
35
 
33
36
  ### Init and cleanup lifecycle
37
+
34
38
  There is also a `cleanUp` function that you can implement in your extension.
35
39
  The app can call your `init` and `cleanUp` functions any time.
36
40
  Always calls the `init` first then `cleanUp` later.
@@ -39,8 +43,7 @@ Main use-case: `init` is called on app startup. `cleanUp` and `init` called late
39
43
  ## Extension interface
40
44
 
41
45
  The app calls the `init` and `cleanUp` function with a `IExtensionObject` object.
42
- See https://github.com/bpatrik/pigallery2/blob/master/src/backend/model/extension/IExtension.ts for details.
43
-
46
+ See https://github.com/bpatrik/pigallery2/blob/master/src/backend/model/extension/IExtension.ts for details.
44
47
 
45
48
  `IExtensionObject` exposes lifecycle events, configs, RestAPis with some limitation.
46
49
  Changes made during the these public apis you do not need to clean up in the `cleanUp` function.
@@ -50,7 +53,7 @@ App also exposes private `_app` object to provide access to low level API. Any c
50
53
 
51
54
  See sample server.js at https://github.com/bpatrik/pigallery2-sample-extension.
52
55
 
53
- It is recommended to do the development in `ts`, so creating a `server.ts`.
56
+ It is recommended to do the development in `ts`, so creating a `server.ts`.
54
57
  Note: You need to manually transpile your `server.ts` file to `server.js` as the app does not do that for you.
55
58
  This doc assumes you do the development in `ts`.
56
59
 
@@ -59,23 +62,23 @@ This doc assumes you do the development in `ts`.
59
62
  You can import package from both the main app package.json and from your extension package.json.
60
63
  To import packages from the main app, you import as usual.
61
64
  For packages from the extension, you always need to write relative path. i.e.: prefix with `./node_modules`
65
+
62
66
  ```ts
63
67
  // Including dev-kit interfaces. It is not necessary, only helps development with types.
64
68
  // You need to prefix them with ./node_modules
65
- import {IExtensionObject} from './node_modules/pigallery2-extension-kit';
69
+ import { IExtensionObject } from "./node_modules/pigallery2-extension-kit";
66
70
 
67
71
  // Including prod extension packages. You need to prefix them with ./node_modules
68
72
  // lodash does not have types
69
73
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
70
74
  // @ts-ignore
71
- import * as _ from './node_modules/lodash';
75
+ import * as _ from "./node_modules/lodash";
72
76
 
73
77
  // Importing packages that are available in the main app (listed in the packages.json in pigallery2)
74
- import {Column, Entity, Index, PrimaryGeneratedColumn} from 'typeorm';
78
+ import { Column, Entity, Index, PrimaryGeneratedColumn } from "typeorm";
75
79
  ```
76
80
 
77
-
78
- #### pigallery2 extension dev-kit
81
+ #### pigallery2 extension dev-kit
79
82
 
80
83
  It is recommended to use the `pigallery2-extension-kit` node package.
81
84
  `npm install pigallery2-extension-kit --save` to your extension.
@@ -87,17 +90,14 @@ You can then `import {IExtensionObject} from './node_modules/pigallery2-extensio
87
90
 
88
91
  See https://github.com/bpatrik/pigallery2/blob/master/src/backend/model/extension/IExtension.ts to understand what contains `IExtensionObject`.
89
92
 
90
- NOTE: this is not needed to create an extension it only helps your IDE and your development. These type definitions are removed when you compile `ts` to `js`.
93
+ NOTE: this is not needed to create an extension it only helps your IDE and your development. These type definitions are removed when you compile `ts` to `js`.
91
94
 
92
95
  #### `init` function
93
96
 
94
97
  You need to implement the `init` function for a working extension:
95
98
 
96
99
  ```ts
97
-
98
- export const init = async (extension: IExtensionObject<void>): Promise<void> => {
99
-
100
- }
100
+ export const init = async (extension: IExtensionObject<void>): Promise<void> => {};
101
101
  ```
102
102
 
103
103
  #### pigallery2 lifecycle `events`
@@ -105,5 +105,4 @@ export const init = async (extension: IExtensionObject<void>): Promise<void> =>
105
105
  Tha app exposes multiple interfaces for the extensions to interact with the main app. `events` are one of the main interfaces.
106
106
  Here are their flow:
107
107
 
108
-
109
108
  ![events_lifecycle](events.png)
package/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export {IServerExtension,IExtensionObject} from './lib/backend/model/extension/IExtension';
1
+ export {IServerExtension,IExtensionObject, IClientExtension} from './lib/backend/model/extension/IExtension';
2
2
 
@@ -1,5 +1,5 @@
1
- export declare type logFN = (...args: (string | number | (() => string))[]) => void;
2
- export declare type LoggerArgs = (string | number | (() => string));
1
+ import { ErrorCodes } from '../common/entities/Error';
2
+ export declare type LoggerArgs = (string | number | (() => string) | Record<any, unknown> | Error);
3
3
  export declare type LoggerFunction = (...args: LoggerArgs[]) => void;
4
4
  export interface ILogger {
5
5
  silly: LoggerFunction;
@@ -18,4 +18,5 @@ export declare class Logger {
18
18
  static warn(...args: LoggerArgs[]): void;
19
19
  static error(...args: LoggerArgs[]): void;
20
20
  private static log;
21
+ static logLevelForError(e: ErrorCodes): LoggerFunction;
21
22
  }
@@ -4,6 +4,8 @@ exports.ExtensionConfigTemplateLoader = void 0;
4
4
  const fs = require("fs");
5
5
  const path = require("path");
6
6
  const ServerExtensionsConfig_1 = require("../../../common/config/private/subconfigs/ServerExtensionsConfig");
7
+ const child_process = require("child_process");
8
+ const execSync = child_process.execSync;
7
9
  const LOG_TAG = '[ExtensionConfigTemplateLoader]';
8
10
  /**
9
11
  * This class decouples the extension management and the config.
@@ -31,30 +33,36 @@ class ExtensionConfigTemplateLoader {
31
33
  }
32
34
  // already loaded
33
35
  if (!this.loaded) {
34
- this.extensionList = (fs
35
- .readdirSync(this.extensionsFolder))
36
- .filter((f) => fs.statSync(path.join(this.extensionsFolder, f)).isDirectory());
37
- this.extensionList.sort();
38
36
  this.extensionTemplates = [];
39
- for (let i = 0; i < this.extensionList.length; ++i) {
40
- const extFolder = this.extensionList[i];
41
- const extPath = path.join(this.extensionsFolder, extFolder);
42
- const serverExtPath = path.join(extPath, 'server.js');
43
- if (!fs.existsSync(serverExtPath)) {
44
- continue;
45
- }
46
- // eslint-disable-next-line @typescript-eslint/no-var-requires
47
- const ext = require(serverExtPath);
48
- if (typeof ext?.initConfig === 'function') {
49
- ext?.initConfig({
50
- setConfigTemplate: (template) => {
51
- this.extensionTemplates.push({ folder: extFolder, template: template });
37
+ if (fs.existsSync(this.extensionsFolder)) {
38
+ this.extensionList = (fs
39
+ .readdirSync(this.extensionsFolder))
40
+ .filter((f) => fs.statSync(path.join(this.extensionsFolder, f)).isDirectory());
41
+ this.extensionList.sort();
42
+ for (let i = 0; i < this.extensionList.length; ++i) {
43
+ const extFolder = this.extensionList[i];
44
+ const extPath = path.join(this.extensionsFolder, extFolder);
45
+ const configExtPath = path.join(extPath, 'config.js');
46
+ const serverExtPath = path.join(extPath, 'server.js');
47
+ // if server.js is missing, it's not a valid extension
48
+ if (!fs.existsSync(serverExtPath)) {
49
+ continue;
50
+ }
51
+ if (fs.existsSync(configExtPath)) {
52
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
53
+ const extCfg = require(configExtPath);
54
+ if (typeof extCfg?.initConfig === 'function') {
55
+ extCfg?.initConfig({
56
+ setConfigTemplate: (template) => {
57
+ this.extensionTemplates.push({ folder: extFolder, template: template });
58
+ }
59
+ });
52
60
  }
53
- });
54
- }
55
- else {
56
- //also create basic config extensions that do not have any
57
- this.extensionTemplates.push({ folder: extFolder });
61
+ }
62
+ else {
63
+ //also create basic config extensions that do not have any
64
+ this.extensionTemplates.push({ folder: extFolder });
65
+ }
58
66
  }
59
67
  }
60
68
  this.loaded = true;
@@ -79,6 +87,8 @@ class ExtensionConfigTemplateLoader {
79
87
  if (ext.template) {
80
88
  c.configs = new ext.template();
81
89
  }
90
+ // TODO: this does not hold if the order of the extensions mixes up.
91
+ // TODO: experiment with a map instead of an array
82
92
  config.Extensions.extensions.push(c);
83
93
  }
84
94
  }
@@ -4,6 +4,6 @@ import { PrivateConfigClass } from '../../../common/config/private/PrivateConfig
4
4
  * Wraps to original config and makes sure all extension related config is loaded
5
5
  */
6
6
  export declare class ExtensionConfigWrapper {
7
- static original(showError?: boolean): Promise<PrivateConfigClass & IConfigClass>;
8
- static originalSync(showError?: boolean): PrivateConfigClass & IConfigClass;
7
+ static original(showDetailedError?: boolean): Promise<PrivateConfigClass & IConfigClass>;
8
+ static originalSync(showDetailedError?: boolean): PrivateConfigClass & IConfigClass;
9
9
  }
@@ -5,38 +5,51 @@ const PrivateConfigClass_1 = require("../../../common/config/private/PrivateConf
5
5
  const node_1 = require("typeconfig/node");
6
6
  const ExtensionConfigTemplateLoader_1 = require("./ExtensionConfigTemplateLoader");
7
7
  const NotifocationManager_1 = require("../NotifocationManager");
8
+ const fs = require("fs");
8
9
  const LOG_TAG = '[ExtensionConfigWrapper]';
9
10
  /**
10
11
  * Wraps to original config and makes sure all extension related config is loaded
11
12
  */
12
13
  class ExtensionConfigWrapper {
13
- static async original(showError = false) {
14
+ static async original(showDetailedError = false) {
14
15
  const pc = node_1.ConfigClassBuilder.attachPrivateInterface(new PrivateConfigClass_1.PrivateConfigClass());
15
16
  ExtensionConfigTemplateLoader_1.ExtensionConfigTemplateLoader.Instance.loadExtensionTemplates(pc);
16
17
  try {
18
+ // make sure the config file exists by the time we load it.
19
+ // TODO: remove this once typeconfig is fixed and can properly load defaults in arrays
20
+ if (!fs.existsSync(pc.__options.configPath)) {
21
+ await pc.save();
22
+ }
17
23
  await pc.load(); // loading the basic configs, but we do not know the extension config hierarchy yet
24
+ // TODO make sure that all extensions are present even after loading them from file
18
25
  }
19
26
  catch (e) {
20
- if (showError) {
21
- console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.');
27
+ console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.');
28
+ console.error(e);
29
+ if (showDetailedError) {
22
30
  console.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.');
23
- console.error(e);
24
31
  NotifocationManager_1.NotificationManager.error('Can\'t load config. Reverting to default. This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.', (e.toString ? e.toString() : JSON.stringify(e)));
25
32
  }
26
33
  }
27
34
  return pc;
28
35
  }
29
- static originalSync(showError = false) {
36
+ static originalSync(showDetailedError = false) {
30
37
  const pc = node_1.ConfigClassBuilder.attachPrivateInterface(new PrivateConfigClass_1.PrivateConfigClass());
31
38
  ExtensionConfigTemplateLoader_1.ExtensionConfigTemplateLoader.Instance.loadExtensionTemplates(pc);
32
39
  try {
40
+ // make sure the config file exists by the time we load it.
41
+ // TODO: remove this once typeconfig is fixed and can properly load defaults in arrays
42
+ if (!fs.existsSync(pc.__options.configPath)) {
43
+ pc.saveSync();
44
+ }
33
45
  pc.loadSync(); // loading the basic configs, but we do not know the extension config hierarchy yet
46
+ // TODO make sure that all extensions are present even after loading them from file
34
47
  }
35
48
  catch (e) {
36
- if (showError) {
37
- console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.');
49
+ console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.');
50
+ console.error(e);
51
+ if (showDetailedError) {
38
52
  console.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.');
39
- console.error(e);
40
53
  NotifocationManager_1.NotificationManager.error('Ca\'nt load config. Reverting to default. This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.', (e.toString ? e.toString() : JSON.stringify(e)));
41
54
  }
42
55
  }