@quatrain/core 1.1.19 → 1.1.22

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/lib/Core.d.ts CHANGED
@@ -2,18 +2,18 @@ import { User } from './components/User';
2
2
  import { DataObjectClass } from './components/types/DataObjectClass';
3
3
  import { AbstractLoggerAdapter, LogLevel } from '@quatrain/log';
4
4
  export declare class Core {
5
- static me: string;
6
- static storage: any;
7
- static storagePrefix: string;
8
- static userClass: typeof User;
9
- static classRegistry: {
5
+ static readonly me: string;
6
+ static readonly storage: any;
7
+ static readonly storagePrefix = "core";
8
+ static readonly classRegistry: {
10
9
  [key: string]: any;
11
10
  };
12
- static logLevel: LogLevel;
13
- static logger: AbstractLoggerAdapter;
11
+ static readonly logLevel = LogLevel.DEBUG;
12
+ static readonly logger: AbstractLoggerAdapter;
13
+ static userClass: typeof User;
14
14
  static addLogger(alias?: string): any;
15
15
  static setLogLevel(level: LogLevel): void;
16
- static timestamp: () => string;
16
+ static readonly timestamp: () => string;
17
17
  static definition(key: string): {
18
18
  manifest: {
19
19
  type: StringConstructor;
@@ -24,6 +24,16 @@ export declare class Core {
24
24
  static getConfig(key: string): Promise<any>;
25
25
  static addClass(name: string, obj: any): void;
26
26
  static getClass(name: string): any;
27
+ /**
28
+ * Execute an external command in a promise
29
+ * @see https://stackoverflow.com/questions/46289682/how-to-wait-for-child-process-spawn-execution-with-async-await
30
+ * @see https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node
31
+ * @param string command
32
+ * @param array args
33
+ * @return Promise
34
+ */
35
+ static readonly execPromise: (command: string, args?: any[], cwd?: string) => Promise<any>;
36
+ static readonly getSystemCommandPath: (command: string) => Promise<string>;
27
37
  /**
28
38
  * Returns the class to use for a data object
29
39
  * This is currently just a stub that will be implemented from config in the future
package/lib/Core.js CHANGED
@@ -8,12 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  var _a;
12
15
  Object.defineProperty(exports, "__esModule", { value: true });
13
16
  exports.Core = void 0;
14
17
  const DataObject_1 = require("./components/DataObject");
15
18
  const User_1 = require("./components/User");
16
19
  const log_1 = require("@quatrain/log");
20
+ const child_process_1 = require("child_process");
21
+ const which_1 = __importDefault(require("which"));
17
22
  class Core {
18
23
  static addLogger(alias = this.name) {
19
24
  return log_1.Log.addLogger('@' + alias, new log_1.DefaultLoggerAdapter(alias, this.logLevel), true);
@@ -83,9 +88,43 @@ _a = Core;
83
88
  Core.me = _a.name;
84
89
  Core.storage = require('node-persist');
85
90
  Core.storagePrefix = 'core';
86
- Core.userClass = User_1.User;
87
91
  Core.classRegistry = {};
88
92
  Core.logLevel = log_1.LogLevel.DEBUG;
89
93
  Core.logger = _a.addLogger();
94
+ Core.userClass = User_1.User;
90
95
  // How timestamp are formatted
91
96
  Core.timestamp = () => new Date().toISOString();
97
+ /**
98
+ * Execute an external command in a promise
99
+ * @see https://stackoverflow.com/questions/46289682/how-to-wait-for-child-process-spawn-execution-with-async-await
100
+ * @see https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node
101
+ * @param string command
102
+ * @param array args
103
+ * @return Promise
104
+ */
105
+ Core.execPromise = (command, args = [], cwd = process.cwd()) => {
106
+ try {
107
+ _a.info(`Executing command ${command} in ${cwd} with arguments:`);
108
+ args.forEach((arg) => console.log(`\t${arg}`));
109
+ return new Promise((resolve, reject) => {
110
+ const child = (0, child_process_1.spawn)(command, args, { cwd });
111
+ child.stdout.on('data', (data) => _a.debug(data.toString()));
112
+ child.stderr.on('data', (data) => _a.debug(data.toString()));
113
+ child.on('close', (code) => {
114
+ if (code !== 0) {
115
+ _a.error(`Command execution failed with code: ${code}`);
116
+ reject(new Error(`Process failed and returned code: ${code}`));
117
+ }
118
+ else {
119
+ _a.info(`Command execution completed with code: ${code}`);
120
+ resolve(undefined);
121
+ }
122
+ });
123
+ });
124
+ }
125
+ catch (err) {
126
+ _a.error(err.message);
127
+ throw err;
128
+ }
129
+ };
130
+ Core.getSystemCommandPath = (command) => (0, which_1.default)(command);
@@ -247,11 +247,10 @@ class DataObject {
247
247
  const data = {};
248
248
  Object.keys(this._properties).forEach((key) => {
249
249
  const prop = Reflect.get(this._properties, key);
250
- if (ignoreNulls && !prop.val())
250
+ if (ignoreNulls && (prop.val() === null || prop.val() === undefined))
251
251
  return;
252
252
  if (ignoreUnchanged && prop.hasChanged === false)
253
253
  return;
254
- // console.log(prop.constructor.name);
255
254
  switch (prop.constructor.name) {
256
255
  case 'CollectionProperty':
257
256
  // ignore
@@ -56,7 +56,8 @@ class BaseProperty {
56
56
  this._protected === true) {
57
57
  throw new Error(`Value '${this._name}' already defined as '${this._value}' and protected from change`);
58
58
  }
59
- if (value !== this._value) {
59
+ if (JSON.stringify({ value: value }) !==
60
+ JSON.stringify({ value: this._value })) {
60
61
  this._value = value;
61
62
  this._hasChanged = setChanged;
62
63
  }
@@ -17,7 +17,7 @@ export declare class HashProperty extends StringProperty {
17
17
  constructor(config: HashPropertyType);
18
18
  _hash(value: string): string;
19
19
  /**
20
- * Never return the password which is hashed anyway
20
+ * Never return the value which is hashed anyway
21
21
  * @param transform
22
22
  * @returns
23
23
  */
@@ -35,7 +35,7 @@ class HashProperty extends StringProperty_1.StringProperty {
35
35
  return hash;
36
36
  }
37
37
  /**
38
- * Never return the password which is hashed anyway
38
+ * Never return the value which is hashed anyway
39
39
  * @param transform
40
40
  * @returns
41
41
  */
@@ -23,51 +23,56 @@ class ObjectProperty extends BaseProperty_1.BaseProperty {
23
23
  return this._instanceOf;
24
24
  }
25
25
  val(transform = undefined) {
26
- if (typeof this._instanceOf === 'string') {
27
- this._instanceOf = Core_1.Core.getClass(this._instanceOf);
28
- }
29
- if (!this._value) {
30
- return this._defaultValue;
31
- }
32
- switch (transform) {
33
- case returnAs.AS_DATAOBJECTS:
34
- if (this._value instanceof DataObject_1.DataObject) {
35
- Core_1.Core.log(`Returning already existing dataObject`);
26
+ try {
27
+ if (typeof this._instanceOf === 'string') {
28
+ this._instanceOf = Core_1.Core.getClass(this._instanceOf);
29
+ }
30
+ if (!this._value) {
31
+ return this._defaultValue;
32
+ }
33
+ switch (transform) {
34
+ case returnAs.AS_DATAOBJECTS:
35
+ if (this._value instanceof DataObject_1.DataObject) {
36
+ Core_1.Core.debug(`Returning already existing dataObject`);
37
+ return this._value;
38
+ }
39
+ else if (this._value instanceof ObjectUri_1.ObjectUri) {
40
+ Core_1.Core.debug(`Converting objectUri -> dataObject`);
41
+ return DataObject_1.DataObject.factory({
42
+ properties: Reflect.get(this._instanceOf, 'PROPS_DEFINITION'),
43
+ uri: this._value,
44
+ });
45
+ }
46
+ else {
47
+ Core_1.Core.debug(`Converting instance -> dataObject`);
48
+ return this._value.dataObject;
49
+ }
50
+ case returnAs.AS_INSTANCES:
51
+ if (this._value instanceof DataObject_1.DataObject) {
52
+ Core_1.Core.debug(`Converting dataObject -> instance`);
53
+ return Reflect.construct(this._instanceOf, [this._value]);
54
+ }
55
+ else if (this._value instanceof ObjectUri_1.ObjectUri) {
56
+ Core_1.Core.debug(`Converting objectUri -> dataObject -> instance`);
57
+ const dao = DataObject_1.DataObject.factory({
58
+ properties: Reflect.get(this._instanceOf, 'PROPS_DEFINITION'),
59
+ uri: this._value,
60
+ });
61
+ return Reflect.construct(this._instanceOf, [dao]);
62
+ }
63
+ else {
64
+ Core_1.Core.log(`Returning already existing instance`);
65
+ return this._value;
66
+ }
67
+ case returnAs.AS_OBJECTURIS:
68
+ default:
36
69
  return this._value;
37
- }
38
- else if (this._value instanceof ObjectUri_1.ObjectUri) {
39
- Core_1.Core.log(`Converting objectUri -> dataObject`);
40
- return DataObject_1.DataObject.factory({
41
- properties: Reflect.get(this._instanceOf, 'PROPS_DEFINITION'),
42
- uri: this._value,
43
- });
44
- }
45
- else {
46
- Core_1.Core.log(`Converting instance -> dataObject`);
47
- return this._value.dataObject;
48
- }
49
- case returnAs.AS_INSTANCES:
50
- if (this._value instanceof DataObject_1.DataObject) {
51
- Core_1.Core.log(`Converting dataObject -> instance`);
52
- return Reflect.construct(this._instanceOf, [this._value]);
53
- }
54
- else if (this._value instanceof ObjectUri_1.ObjectUri) {
55
- // console.log('ObjectProperty', this)
56
- Core_1.Core.log(`Converting objectUri -> dataObject -> instance`);
57
- // console.log(this._instanceOf)
58
- const dao = DataObject_1.DataObject.factory({
59
- properties: Reflect.get(this._instanceOf, 'PROPS_DEFINITION'),
60
- uri: this._value,
61
- });
62
- return Reflect.construct(this._instanceOf, [dao]);
63
- }
64
- else {
65
- Core_1.Core.log(`Returning already existing instance`);
66
- return this._value;
67
- }
68
- case returnAs.AS_OBJECTURIS:
69
- default:
70
- return this._value;
70
+ }
71
+ }
72
+ catch (err) {
73
+ Core_1.Core.error(`Error in ObjectProperty.val() for property ${this._name}: ${err}`);
74
+ console.log(err);
75
+ return undefined;
71
76
  }
72
77
  }
73
78
  set(value, setChanged = true) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/core",
3
- "version": "1.1.19",
3
+ "version": "1.1.22",
4
4
  "description": "Core objects for business apps",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -12,21 +12,21 @@
12
12
  "license": "MIT",
13
13
  "devDependencies": {
14
14
  "@tsconfig/recommended": "^1.0.1",
15
- "@types/jest": "^27.0.3",
15
+ "@types/jest": "^30.0.0",
16
16
  "@types/node": "^22.10.1",
17
17
  "@types/node-persist": "^3.1.8",
18
- "jest": "^27.4.7",
19
- "jest-node-exports-resolver": "^1.1.6",
18
+ "@types/which": "^3.0.4",
19
+ "jest": "^30.1.1",
20
20
  "trace-unhandled": "^2.0.1",
21
- "ts-jest": "^27.1.2",
21
+ "ts-jest": "^29.4.1",
22
22
  "ts-node": "^10.4.0",
23
23
  "typescript": "^5.1.5"
24
24
  },
25
25
  "dependencies": {
26
26
  "@faker-js/faker": "^7.6.0",
27
- "@quatrain/log": "^0.1.3",
28
- "loglevel": "^1.9.2",
29
- "node-persist": "^4.0.3"
27
+ "@quatrain/log": "^1.0.2",
28
+ "node-persist": "^4.0.4",
29
+ "which": "^5.0.0"
30
30
  },
31
31
  "scripts": {
32
32
  "test-ci": "jest --runInBand",