From 62b6be96c735e1a191e89a7ed3a299addac26869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crist=C3=B3bal=20Contreras=20Rubio?= Date: Wed, 15 May 2024 21:09:47 +0200 Subject: [PATCH] NMEA-PARSER: Get __dirname from CJS or ESM --- packages/nmea-parser/package.json | 2 +- packages/nmea-parser/src/constants.ts | 4 ++-- packages/nmea-parser/src/parser.ts | 4 ++-- packages/nmea-parser/tests/index.test.ts | 4 ++-- packages/nmea-parser/tests/parser.test.ts | 4 ++-- packages/nmea-parser/tests/protocols.test.ts | 3 ++- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/nmea-parser/package.json b/packages/nmea-parser/package.json index 964a760..0cf68c5 100644 --- a/packages/nmea-parser/package.json +++ b/packages/nmea-parser/package.json @@ -1,6 +1,6 @@ { "name": "@coremarine/nmea-parser", - "version": "1.6.11", + "version": "1.6.12", "description": "Library to parse NMEA 0183 sentences", "author": "CoreMarine", "license": "ISC", diff --git a/packages/nmea-parser/src/constants.ts b/packages/nmea-parser/src/constants.ts index 614c5ed..10c1dfc 100644 --- a/packages/nmea-parser/src/constants.ts +++ b/packages/nmea-parser/src/constants.ts @@ -2,9 +2,9 @@ import { fileURLToPath } from 'node:url' // DIR const getDirname = (): string => { try { - return fileURLToPath(import.meta.url) - } catch (error) { return __dirname + } catch (error) { + return fileURLToPath(import.meta.url) } } diff --git a/packages/nmea-parser/src/parser.ts b/packages/nmea-parser/src/parser.ts index 8f8dbfb..8dd3131 100644 --- a/packages/nmea-parser/src/parser.ts +++ b/packages/nmea-parser/src/parser.ts @@ -1,7 +1,7 @@ import { readdirSync } from 'node:fs' import Path from 'node:path' import * as v from 'valibot' -import { END_FLAG, END_FLAG_LENGTH, MAX_CHARACTERS, NMEA_ID_LENGTH, START_FLAG, START_FLAG_LENGTH } from './constants' +import { DIRNAME, END_FLAG, END_FLAG_LENGTH, MAX_CHARACTERS, NMEA_ID_LENGTH, START_FLAG, START_FLAG_LENGTH } from './constants' import { BooleanSchema, NMEALikeSchema, ProtocolsInputSchema, StringSchema, UnsignedIntegerSchema } from './schemas' import type { Data, FieldType, FieldUnknown, NMEAKnownSentence, NMEALike, NMEAParser, NMEAPreParsed, NMEASentence, NMEAUknownSentence, ParserSentences, ProtocolOutput, ProtocolsFile, ProtocolsInput, Sentence, StoredSentences } from './types' import { getSentencesByProtocol, getStoreSentences, readProtocolsFile, readProtocolsString } from './protocols' @@ -29,7 +29,7 @@ export class Parser implements NMEAParser { } private readInternalProtocols (): void { - const folder = Path.join(__dirname, 'protocols') + const folder = Path.join(DIRNAME, 'protocols') const files = readdirSync(folder, { encoding: 'utf-8' }) files.forEach(file => { const absoluteFile = Path.join(folder, file) diff --git a/packages/nmea-parser/tests/index.test.ts b/packages/nmea-parser/tests/index.test.ts index e095d29..bc2d85e 100644 --- a/packages/nmea-parser/tests/index.test.ts +++ b/packages/nmea-parser/tests/index.test.ts @@ -2,13 +2,13 @@ import fs from 'node:fs' import path from 'node:path' import { describe, test, expect } from 'vitest' import * as v from 'valibot' -import { NMEAParser as Parser } from '../src' +import { DIRNAME, NMEAParser as Parser } from '../src' import { generateSentenceFromModel, getFakeSentence } from '../src/sentences' import { NMEAKnownSentenceSchema, NMEALikeSchema, NMEAUknownSentenceSchema } from '../src/schemas' import { readProtocolsFile } from '../src/protocols' import { Protocol } from '../src/types' -const NORSUB_FILE = path.join(__dirname, 'norsub.yaml') +const NORSUB_FILE = path.join(DIRNAME, 'norsub.yaml') describe('Parser', () => { test('Default constructor', () => { diff --git a/packages/nmea-parser/tests/parser.test.ts b/packages/nmea-parser/tests/parser.test.ts index 76bee79..bb96338 100644 --- a/packages/nmea-parser/tests/parser.test.ts +++ b/packages/nmea-parser/tests/parser.test.ts @@ -5,11 +5,11 @@ import * as v from 'valibot' import { Parser } from '../src/parser' import { generateSentenceFromModel, getFakeSentence } from '../src/sentences' import { NMEAKnownSentenceSchema, NMEASentenceSchema, NMEAUknownSentenceSchema } from '../src/schemas' -import { TALKERS, TALKERS_SPECIAL } from '../src/constants' +import { DIRNAME, TALKERS, TALKERS_SPECIAL } from '../src/constants' import { readProtocolsFile } from '../src/protocols' import { NMEASentence, Protocol } from '../src/types' -const NORSUB_FILE = path.join(__dirname, 'norsub.yaml') +const NORSUB_FILE = path.join(DIRNAME, 'norsub.yaml') describe('Parser', () => { test('Default constructor', () => { diff --git a/packages/nmea-parser/tests/protocols.test.ts b/packages/nmea-parser/tests/protocols.test.ts index 399d609..3fcbbd0 100644 --- a/packages/nmea-parser/tests/protocols.test.ts +++ b/packages/nmea-parser/tests/protocols.test.ts @@ -5,8 +5,9 @@ import { describe, test, expect } from 'vitest' import { Protocol, StoredSentence } from '../src/types' import { getStoreSentences, readProtocolsFile, readProtocolsString } from '../src/protocols' import { ProtocolSchema } from '../src/schemas' +import { DIRNAME } from '../src' -const PROTOCOLS_FILE = path.join(__dirname, 'norsub.yaml') +const PROTOCOLS_FILE = path.join(DIRNAME, 'norsub.yaml') const EXPECTED_PROTOCOLS: Protocol[] = [ { protocol: 'NORSUB8',