@drenso/homey-log 8.37.1 → 8.39.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.
package/build.js ADDED
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ // eslint-disable-next-line import/no-extraneous-dependencies,node/no-unpublished-require
4
+ const { buildSync } = require('esbuild');
5
+ const { execSync } = require('child_process');
6
+
7
+ const defaultOptions = {
8
+ entryPoints: ['index.js'],
9
+ bundle: true,
10
+ outdir: 'build',
11
+ platform: 'node',
12
+ external: ['homey'],
13
+ minify: true,
14
+ treeShaking: true,
15
+ };
16
+
17
+ // CommonJS build
18
+ buildSync({
19
+ ...defaultOptions,
20
+ ...{
21
+ format: 'cjs',
22
+ },
23
+ });
24
+
25
+ // ESM build
26
+ buildSync({
27
+ ...defaultOptions,
28
+ ...{
29
+ format: 'esm',
30
+ outExtension: {
31
+ '.js': '.mjs',
32
+ },
33
+ },
34
+ });
35
+
36
+ // Generate types
37
+ execSync('npm run typings:generate');
package/index.js ADDED
@@ -0,0 +1,216 @@
1
+ 'use strict';
2
+
3
+ const os = require('node:os');
4
+
5
+ if (!process.env.HOME) {
6
+ // Sentry SDK requires the HOME env var to be set, which is not guaranteed on Homey
7
+ process.env.HOME = os.tmpdir();
8
+ }
9
+
10
+ // eslint-disable-next-line import/no-extraneous-dependencies,node/no-unpublished-require
11
+ const Sentry = require('@sentry/node');
12
+ const HomeyModule = require('homey');
13
+
14
+ class Log {
15
+
16
+ _capturedMessages = [];
17
+ _capturedExceptions = [];
18
+
19
+ /**
20
+ * Construct a new Log instance.
21
+ * @param {object} args
22
+ * @param {import('homey').default} args.homey - `this.homey` instance in
23
+ * your app (e.g. `App#homey`/`Driver#homey`/`Device#homey`).
24
+ *
25
+ * @param {object} [args.options] - Additional options for Sentry
26
+ *
27
+ * @example
28
+ * class MyApp extends Homey.App {
29
+ * onInit() {
30
+ * this.homeyLog = new Log({ homey: this.homey });
31
+ * }
32
+ * }
33
+ */
34
+ constructor({ homey, options }) {
35
+ if (typeof homey === 'undefined') {
36
+ return Log._error('Error: missing `homey` constructor parameter');
37
+ }
38
+
39
+ if (!HomeyModule.env) {
40
+ return Log._error('Error: could not access `Homey.env`');
41
+ }
42
+
43
+ if (typeof HomeyModule.env.HOMEY_LOG_URL !== 'string') {
44
+ return Log._error('Error: expected `HOMEY_LOG_URL` env variable, homey-log is disabled');
45
+ }
46
+
47
+ // Check if debug mode is enabled
48
+ const disableSentry = process.env.DEBUG === '1' && !Log._isLogForced();
49
+ if (disableSentry) {
50
+ Log._log('App is running in debug mode, not enabling Sentry logging');
51
+ }
52
+
53
+ this._manifest = HomeyModule.manifest;
54
+ this._homeyVersion = homey.version;
55
+ this._managerCloud = homey.cloud;
56
+ this._homeyPlatform = homey.platform;
57
+ this._homeyPlatformVersion = homey.platformVersion;
58
+
59
+ // Init Sentry, pass enabled option to prevent sending events upstream when in debug mode
60
+ this.init(HomeyModule.env.HOMEY_LOG_URL, { ...{ enabled: !disableSentry }, ...options });
61
+ }
62
+
63
+ /**
64
+ * Init Sentry.
65
+ * @param {string} dsn The Sentry DSN
66
+ * @param {object} opts Options to be passed to the Sentry init
67
+ * @returns {Log}
68
+ * @private
69
+ */
70
+ init(dsn, opts) {
71
+ Sentry.initWithoutDefaultIntegrations({
72
+ ...{
73
+ dsn,
74
+ integrations: [
75
+ ...Sentry.getDefaultIntegrationsWithoutPerformance(),
76
+ ],
77
+ },
78
+ ...opts,
79
+ });
80
+
81
+ this.setTags({
82
+ appId: this._manifest.id,
83
+ appVersion: this._manifest.version,
84
+ homeyVersion: this._homeyVersion,
85
+ homeyPlatform: this._homeyPlatform,
86
+ homeyPlatformVersion: this._homeyPlatformVersion,
87
+ });
88
+
89
+ // Get homey cloud id and set as tag
90
+ this._managerCloud.getHomeyId()
91
+ .then(homeyId => this.setTags({ homeyId }))
92
+ .catch(err => Log._error('Error: could not get `homeyId`', err));
93
+
94
+ Log._log(`App ${this._manifest.id} v${this._manifest.version} logging on Homey v${this._homeyVersion}...`);
95
+ return this;
96
+ }
97
+
98
+ /**
99
+ * Set `tags` that will be sent as context with every message or error. See the Sentry Node.js
100
+ * documentation: https://docs.sentry.io/platforms/javascript/guides/node/enriching-events/tags/
101
+ * @param {object} tags
102
+ * @returns {Log}
103
+ */
104
+ setTags(tags) {
105
+ Sentry.setTags(tags);
106
+ return this;
107
+ }
108
+
109
+ /**
110
+ * Set `user` that will be sent as context with every message or error. See the Sentry Node.js
111
+ * documentation: https://docs.sentry.io/platforms/javascript/guides/node/enriching-events/identify-user/.
112
+ * @param {object} user
113
+ * @returns {Log}
114
+ */
115
+ setUser(user) {
116
+ Sentry.setUser(user);
117
+ return this;
118
+ }
119
+
120
+ /**
121
+ * Create and send message event to Sentry. See the Sentry Node.js documentation:
122
+ * https://docs.sentry.io/platforms/javascript/guides/node/usage/
123
+ * @param {string} message - Message to be sent
124
+ * @returns {Promise<string>|undefined}
125
+ */
126
+ async captureMessage(message) {
127
+ Log._log('captureMessage:', message);
128
+
129
+ if (this._capturedMessages.indexOf(message) > -1) {
130
+ Log._log('Prevented sending a duplicate message');
131
+ return;
132
+ }
133
+
134
+ this._capturedMessages.push(message);
135
+
136
+ // eslint-disable-next-line consistent-return
137
+ return new Promise((resolve, reject) => {
138
+ try {
139
+ resolve(Sentry.captureMessage(message));
140
+ } catch (e) {
141
+ reject(e);
142
+ }
143
+ });
144
+ }
145
+
146
+ /**
147
+ * Create and send exception event to Sentry. See the sentry Node.js documentation:
148
+ * https://docs.sentry.io/platforms/javascript/guides/node/usage/
149
+ * @param {Error} err - Error instance to be sent
150
+ * @returns {Promise<string>|undefined}
151
+ */
152
+ async captureException(err) {
153
+ Log._log('captureException:', err);
154
+
155
+ if (this._capturedExceptions.indexOf(err) > -1) {
156
+ Log._log('Prevented sending a duplicate log');
157
+ return;
158
+ }
159
+
160
+ this._capturedExceptions.push(err);
161
+
162
+ // eslint-disable-next-line consistent-return
163
+ return new Promise((resolve, reject) => {
164
+ try {
165
+ resolve(Sentry.captureException(err));
166
+ } catch (e) {
167
+ reject(e);
168
+ }
169
+ });
170
+ }
171
+
172
+ /**
173
+ * Mimic SDK log method.
174
+ * @private
175
+ */
176
+ static _log() {
177
+ // eslint-disable-next-line prefer-spread,prefer-rest-params,no-console
178
+ console.log.bind(null, Log._logTime(), '[homey-log]').apply(null, arguments);
179
+ }
180
+
181
+ /**
182
+ * Mimic SDK error method.
183
+ * @private
184
+ */
185
+ static _error() {
186
+ // eslint-disable-next-line prefer-spread,prefer-rest-params,no-console
187
+ console.error.bind(null, Log._logTime(), '[homey-log]').apply(null, arguments);
188
+ }
189
+
190
+ /**
191
+ * Mimic SDK timestamp.
192
+ * @returns {string}
193
+ * @private
194
+ */
195
+ static _logTime() {
196
+ return `\x1b[35m${(new Date()).toISOString()}\x1b[0m`;
197
+ }
198
+
199
+ /**
200
+ * Whether logging is forced.
201
+ * @returns {boolean}
202
+ * @private
203
+ */
204
+ static _isLogForced() {
205
+ switch (HomeyModule.env.HOMEY_LOG_FORCE) {
206
+ case '1':
207
+ case 'true':
208
+ return true;
209
+ default:
210
+ return false;
211
+ }
212
+ }
213
+
214
+ }
215
+
216
+ module.exports = { Log };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drenso/homey-log",
3
- "version": "8.37.1",
3
+ "version": "8.39.0",
4
4
  "description": "Interface with Sentry for Homey",
5
5
  "main": "./build/index.js",
6
6
  "module": "./build/index.mjs",
@@ -29,13 +29,13 @@
29
29
  },
30
30
  "homepage": "https://github.com/Drenso/homey-log#readme",
31
31
  "devDependencies": {
32
- "@sentry/node": "8.37.1",
32
+ "@sentry/node": "8.39.0",
33
33
  "@types/homey": "npm:homey-apps-sdk-v3-types@0.3.9",
34
34
  "@types/node": "^16.18.0",
35
35
  "esbuild": "^0.24.0",
36
36
  "eslint": "^7.32.0",
37
37
  "eslint-config-athom": "^2.1.1",
38
- "homey": "3.7.11",
38
+ "homey": "3.7.12",
39
39
  "jsdoc": "^3.6.11",
40
40
  "jsdoc-ts-utils": "^1.1.2",
41
41
  "npm-watch": "^0.13.0",