Skip to content

Commit

Permalink
fix(shared): proper ts rootDir (#1887)
Browse files Browse the repository at this point in the history
## Describe your changes

Fixes NAN-617

- rootDir was not pointing to lib
It currently output everything to dist/lib which is inconvenient but
also makes the paths unreliable since it changes from ts to js

- Fix package.json loading (and cache it)
This package.json is loaded on every call to `/meta`. 
And it was spamming errors in tests
  • Loading branch information
bodinsamuel committed Mar 21, 2024
1 parent e6a91ee commit bf74650
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/cli/docker/docker-compose.yaml
Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Expand Up @@ -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": {
Expand Down
4 changes: 3 additions & 1 deletion 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;
6 changes: 2 additions & 4 deletions 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';
Expand Down Expand Up @@ -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, {
Expand Down
6 changes: 2 additions & 4 deletions 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';
Expand All @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions packages/shared/lib/utils/utils.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -423,9 +423,15 @@ export function getConnectionConfig(queryParams: any): Record<string, string> {
return Object.fromEntries(arr) as Record<string, string>;
}

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 {
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/package.json
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/tsconfig.json
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"rootDir": "lib",
"outDir": "dist"
},
"references": [
Expand Down

0 comments on commit bf74650

Please sign in to comment.