From 47ef696169df12bfbf9ff64cda84dbea0b129428 Mon Sep 17 00:00:00 2001 From: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com> Date: Wed, 20 Mar 2024 19:36:10 +0100 Subject: [PATCH] fix(shared): proper ts out dir --- packages/cli/docker/docker-compose.yaml | 4 ++-- packages/cli/package.json | 2 +- packages/shared/lib/db/knexfile.cjs | 4 +++- packages/shared/lib/utils/analytics.ts | 6 ++---- packages/shared/lib/utils/error.manager.ts | 6 ++---- packages/shared/lib/utils/utils.ts | 14 ++++++++++---- packages/shared/package.json | 4 ++-- packages/shared/tsconfig.json | 2 +- 8 files changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/cli/docker/docker-compose.yaml b/packages/cli/docker/docker-compose.yaml index 265f3de1e3..7354a34da2 100644 --- a/packages/cli/docker/docker-compose.yaml +++ b/packages/cli/docker/docker-compose.yaml @@ -42,7 +42,7 @@ services: depends_on: - nango-db volumes: - - "${NANGO_INTEGRATIONS_LOCATION:-./}:/usr/nango-server/src/packages/shared/dist/lib/nango-integrations" + - '${NANGO_INTEGRATIONS_LOCATION:-./}:/usr/nango-server/src/packages/shared/dist/nango-integrations' networks: - nango @@ -64,7 +64,7 @@ services: - nango-db - temporal volumes: - - "${NANGO_INTEGRATIONS_LOCATION:-./}:/usr/nango-jobs/src/packages/shared/dist/lib/nango-integrations" + - '${NANGO_INTEGRATIONS_LOCATION:-./}:/usr/nango-jobs/src/packages/shared/dist/nango-integrations' networks: - nango diff --git a/packages/cli/package.json b/packages/cli/package.json index ca6485c338..0fd589b559 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -19,7 +19,7 @@ "scripts": { "build": "tsc && npm run copyfiles", "copyfiles": "copyfiles -u 1 lib/templates/* dist", - "postbuild": "cp `node -p \"require.resolve('@nangohq/shared/dist/lib/sdk/sync.d.ts')\"` './dist/nango-sync.d.ts'", + "postbuild": "cp `node -p \"require.resolve('@nangohq/shared/dist/sdk/sync.d.ts')\"` './dist/nango-sync.d.ts'", "prepublishOnly": "npm install && npm run build" }, "dependencies": { diff --git a/packages/shared/lib/db/knexfile.cjs b/packages/shared/lib/db/knexfile.cjs index b71d28161c..0192b8a4a7 100644 --- a/packages/shared/lib/db/knexfile.cjs +++ b/packages/shared/lib/db/knexfile.cjs @@ -1,4 +1,6 @@ // Knex CLI for migration needs this Knexfile but doesn't play well with ESM modules. // That's why the content of the Knex config is moved to ./config.ts to be imported by the app in ESM-fashion, and the Knexfile is only used for the Knex CLI in CommonJS-fashion. -const { config } = require('../../dist/lib/db/config'); +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { config } = require('../../dist/db/config.js'); + module.exports = config; diff --git a/packages/shared/lib/utils/analytics.ts b/packages/shared/lib/utils/analytics.ts index 9995e04c92..59fac245da 100644 --- a/packages/shared/lib/utils/analytics.ts +++ b/packages/shared/lib/utils/analytics.ts @@ -1,9 +1,7 @@ import { PostHog } from 'posthog-node'; -import { getBaseUrl, localhostUrl, dirname, UserType, isCloud, isStaging } from '../utils/utils.js'; +import { getBaseUrl, localhostUrl, UserType, isCloud, isStaging, packageJsonFile } from '../utils/utils.js'; import ip from 'ip'; import errorManager, { ErrorSourceEnum } from './error.manager.js'; -import { readFileSync } from 'fs'; -import path from 'path'; import accountService from '../services/account.service.js'; import environmentService from '../services/environment.service.js'; import userService from '../services/user.service.js'; @@ -52,7 +50,7 @@ class Analytics { if (process.env['TELEMETRY']?.toLowerCase() !== 'false' && !isStaging()) { this.client = new PostHog('phc_4S2pWFTyPYT1i7zwC8YYQqABvGgSAzNHubUkdEFvcTl'); this.client.enable(); - this.packageVersion = JSON.parse(readFileSync(path.resolve(dirname(), '../../../package.json'), 'utf8')).version; + this.packageVersion = packageJsonFile().version; } } catch (e) { errorManager.report(e, { diff --git a/packages/shared/lib/utils/error.manager.ts b/packages/shared/lib/utils/error.manager.ts index f8a3edd54e..0cd931a877 100644 --- a/packages/shared/lib/utils/error.manager.ts +++ b/packages/shared/lib/utils/error.manager.ts @@ -1,14 +1,12 @@ import * as uuid from 'uuid'; import type { EventHint } from '@sentry/node'; import sentry from '@sentry/node'; -import { readFileSync } from 'fs'; -import path from 'path'; import type { Tracer } from 'dd-trace'; import type { ErrorEvent } from '@sentry/types'; import logger from '../logger/console.js'; import { NangoError } from './error.js'; import type { Response, Request } from 'express'; -import { isCloud, getEnvironmentId, getAccountIdAndEnvironmentIdFromSession, dirname, isApiAuthenticated, isUserAuthenticated } from './utils.js'; +import { isCloud, getEnvironmentId, getAccountIdAndEnvironmentIdFromSession, isApiAuthenticated, isUserAuthenticated, packageJsonFile } from './utils.js'; import environmentService from '../services/environment.service.js'; import accountService from '../services/account.service.js'; import userService from '../services/user.service.js'; @@ -33,7 +31,7 @@ class ErrorManager { constructor() { try { if (isCloud() && process.env['SENTRY_DNS']) { - const packageVersion = JSON.parse(readFileSync(path.resolve(dirname(), '../../../package.json'), 'utf8')).version; + const packageVersion = packageJsonFile().version; sentry.init({ dsn: process.env['SENTRY_DNS'], beforeSend(event: ErrorEvent, _: EventHint) { diff --git a/packages/shared/lib/utils/utils.ts b/packages/shared/lib/utils/utils.ts index 5aa13d5fe2..931e608683 100644 --- a/packages/shared/lib/utils/utils.ts +++ b/packages/shared/lib/utils/utils.ts @@ -163,8 +163,8 @@ export function isValidHttpUrl(str: string) { } } -export function dirname() { - return path.dirname(fileURLToPath(import.meta.url)); +export function dirname(thisFile?: string) { + return path.dirname(fileURLToPath(thisFile || import.meta.url)); } export function parseTokenExpirationDate(expirationDate: any): Date { @@ -423,9 +423,15 @@ export function getConnectionConfig(queryParams: any): Record { return Object.fromEntries(arr) as Record; } +let packageJsonCache: PackageJson | undefined; export function packageJsonFile(): PackageJson { - const localPath = process.env['SERVER_RUN_MODE'] === 'DOCKERIZED' ? 'packages/shared/package.json' : '../shared/package.json'; - return JSON.parse(readFileSync(resolve(process.cwd(), localPath)).toString('utf-8')); + if (packageJsonCache) { + return packageJsonCache; + } + + const localPath = '../../package.json'; + packageJsonCache = JSON.parse(readFileSync(resolve(dirname(), localPath)).toString('utf-8')) as PackageJson; + return packageJsonCache; } export function safeStringify(obj: any): string { diff --git a/packages/shared/package.json b/packages/shared/package.json index cbcb306900..bb39982310 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -3,8 +3,8 @@ "version": "0.39.5", "description": "Nango's shared components.", "type": "module", - "main": "dist/lib/index.js", - "typings": "dist/lib/index.d.ts", + "main": "dist/index.js", + "typings": "dist/index.d.ts", "scripts": { "build": "rimraf dist && tsc", "prepublishOnly": "npm install && npm run build" diff --git a/packages/shared/tsconfig.json b/packages/shared/tsconfig.json index d2f585f7ef..f0cd3a5058 100644 --- a/packages/shared/tsconfig.json +++ b/packages/shared/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "rootDir": ".", + "rootDir": "lib", "outDir": "dist" }, "references": [