Skip to content

Commit

Permalink
NMEA-PARSER: Get directory path in ESM or CJS without errors
Browse files Browse the repository at this point in the history
  • Loading branch information
crisconru committed May 16, 2024
1 parent 62b6be9 commit 163869e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/nmea-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coremarine/nmea-parser",
"version": "1.6.12",
"version": "1.6.13",
"description": "Library to parse NMEA 0183 sentences",
"author": "CoreMarine",
"license": "ISC",
Expand Down
12 changes: 7 additions & 5 deletions packages/nmea-parser/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
// DIR
const getDirname = (): string => {
export const DIRNAME = ((): string => {
try {
// CJS
return __dirname
} catch (error) {
return fileURLToPath(import.meta.url)
// ESM
const filename = fileURLToPath(import.meta.url)
return path.dirname(filename)
}
}

export const DIRNAME = getDirname()
})()
// NMEA
export const START_FLAG = '$'
export const SEPARATOR = ','
Expand Down
2 changes: 1 addition & 1 deletion packages/nmea-parser/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NMEAKnownSentenceSchema, NMEALikeSchema, NMEAUknownSentenceSchema } fro
import { readProtocolsFile } from '../src/protocols'
import { Protocol } from '../src/types'

const NORSUB_FILE = path.join(DIRNAME, 'norsub.yaml')
const NORSUB_FILE = path.join(DIRNAME, '..', 'tests', 'norsub.yaml')

describe('Parser', () => {
test('Default constructor', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nmea-parser/tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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, '..', 'tests', 'norsub.yaml')

describe('Parser', () => {
test('Default constructor', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nmea-parser/tests/protocols.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getStoreSentences, readProtocolsFile, readProtocolsString } from '../sr
import { ProtocolSchema } from '../src/schemas'
import { DIRNAME } from '../src'

const PROTOCOLS_FILE = path.join(DIRNAME, 'norsub.yaml')
const PROTOCOLS_FILE = path.join(DIRNAME, '..', 'tests', 'norsub.yaml')
const EXPECTED_PROTOCOLS: Protocol[] = [
{
protocol: 'NORSUB8',
Expand Down

0 comments on commit 163869e

Please sign in to comment.