Skip to content

Commit

Permalink
NMEA-PARSER: Get __dirname from CJS or ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
crisconru committed May 15, 2024
1 parent 9547618 commit 62b6be9
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 10 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.11",
"version": "1.6.12",
"description": "Library to parse NMEA 0183 sentences",
"author": "CoreMarine",
"license": "ISC",
Expand Down
4 changes: 2 additions & 2 deletions packages/nmea-parser/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/nmea-parser/src/parser.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/nmea-parser/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/nmea-parser/tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/nmea-parser/tests/protocols.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 62b6be9

Please sign in to comment.