Skip to content

Commit 0ad3f0e

Browse files
committed
fix: move dotenv init to utils
1 parent 5f5a215 commit 0ad3f0e

File tree

7 files changed

+24
-13
lines changed

7 files changed

+24
-13
lines changed

.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ [email protected]
1111
# Only needed if Teams email approach is used
1212
1313
# App env (optional)
14-
DATA_PATH=./data# path to the data folder where tokens are stored, default is ./data
14+
DATA_PATH=.data# path to the data folder where tokens are stored, default is .data

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ router.post('/cm-webhook', async (req, res, next) => {
8686
* @property {string} [clientId] - The client ID (default: from environment variable CLIENT_ID).
8787
* @property {string} [title] - The title of the notification (default: 'Cloud Manager Pipeline Notification').
8888
* @property {string} [fromEmail] - The sender's email address (default: from environment variable EMAIL_FROM).
89-
* @property {string} [dataPath] - The path to the directory containing data JSON files (default: from environment variable DATA_PATH or './data').
89+
* @property {string} [dataPath] - The path to the directory containing data JSON files (default: from environment variable DATA_PATH or '.data').
9090
* @property {string} [secret] - The secret used for verification, can be client_secret string, path to PublicKey or content of Public Key (default: from environment variable SECRET).
9191
*/
9292
class CMNotify {
@@ -175,7 +175,7 @@ [email protected]
175175
# Only needed if Teams email approach is used
176176
177177
# App env (optional)
178-
DATA_PATH=./data# path to the data folder wher tokens are stored, default is ./data
178+
DATA_PATH=.data# path to the data folder wher tokens are stored, default is .data
179179
```
180180

181181
## Cloud Manager Webhook

core/auth.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { URL } = require('node:url');
22
const { AuthorizationCode } = require('simple-oauth2');
33
const { CMUtils } = require('../utils');
4-
const { DEFAULT_CONFIG } = require('./config');
54

65
/**
76
* @typedef {Object} CredentialsConfig
@@ -24,12 +23,12 @@ const { DEFAULT_CONFIG } = require('./config');
2423
class OAuth2Client {
2524
/**
2625
* @constructor
27-
* @param [fromEmail] {string} - The email address of the sender.
28-
* @param [dataPath] {string} - The path to the directory containing credentials JSON file.
26+
* @param fromEmail {string} - The email address of the sender.
27+
* @param dataPath {string} - The path to the directory containing credentials JSON file.
2928
* @throws {Error} - If fromEmail is not provided.
3029
* @returns {OAuth2ClientType} - The OAuth2Client instance.
3130
*/
32-
constructor(fromEmail = DEFAULT_CONFIG.fromEmail, dataPath = DEFAULT_CONFIG.dataPath) {
31+
constructor(fromEmail, dataPath) {
3332
this.fromEmail = fromEmail;
3433
this.dataPath = dataPath;
3534
if (!this.fromEmail) {

core/config.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
require('dotenv').config();
1+
const { CMUtils } = require('../utils');
2+
3+
CMUtils.initDotEnv();
24

35
const DEFAULT_CONFIG = {
46
slackWebhook: process.env.SLACK_WEBHOOK,
57
teamsWebhook: process.env.TEAMS_WEBHOOK,
68
teamsEmail: process.env.TEAMS_EMAIL,
79
orgName: process.env.ORGANIZATION_NAME,
810
clientId: process.env.CLIENT_ID,
9-
title: 'Cloud Manager Pipeline Notification',
11+
title: process.env.TITLE || 'Cloud Manager Pipeline Notification',
1012
fromEmail: process.env.EMAIL_FROM,
11-
dataPath: process.env.DATA_PATH || './data',
13+
dataPath: process.env.DATA_PATH || '.data',
1214
secret: process.env.SECRET,
1315
}
1416

core/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { CMVerify } = require('./verify');
1313
* @property {string} [clientId] - The client ID (default: from environment variable CLIENT_ID).
1414
* @property {string} [title] - The title of the notification (default: 'Cloud Manager Pipeline Notification').
1515
* @property {string} [fromEmail] - The sender's email address (default: from environment variable EMAIL_FROM).
16-
* @property {string} [dataPath] - The path to the directory containing data JSON files (default: from environment variable DATA_PATH or './data').
16+
* @property {string} [dataPath] - The path to the directory containing data JSON files (default: from environment variable DATA_PATH or '.data').
1717
* @property {string} [secret] - The secret used for HMAC verification (default: from environment variable SECRET).
1818
*/
1919

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ class CMUtils {
9494
const base64Regex = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
9595
return base64Regex.test(str);
9696
}
97+
98+
static initDotEnv() {
99+
const dotenv = require('dotenv');
100+
const envFilePath = this.getValidPath('.env');
101+
if (envFilePath) {
102+
dotenv.config({ path: envFilePath });
103+
} else {
104+
console.log('No .env file found. Skipping dotenv initialization.');
105+
}
106+
}
97107
}
98108

99109
module.exports = { CMUtils };

0 commit comments

Comments
 (0)