Skip to content

Commit

Permalink
attempt to fix json reads in package
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Mar 5, 2024
1 parent ce0be84 commit ffab9db
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable security/detect-object-injection */
/* eslint-disable security/detect-non-literal-fs-filename, security/detect-object-injection */
import fs from 'node:fs/promises';
import path from 'node:path';
let codeTypes = {};
let codeTypeFieldValues = {};
const currentFolder = path.dirname(import.meta.filename ?? '.');
/**
* Returns an object of code types.
* @returns {Promise<Record<string, string>>} - An object with "code type" keys and "code type description" values.
*/
export async function getCodeTypes() {
if (Object.keys(codeTypes).length === 0) {
const codeTypesData = await fs.readFile('./data/codeTypes.json');
const codeTypesData = await fs.readFile(path.join(currentFolder, 'data', 'codeTypes.json'));
codeTypes = JSON.parse(codeTypesData);
}
return codeTypes;
Expand Down Expand Up @@ -40,8 +42,7 @@ export async function getCodeTypeDescription(codeType) {
export async function getFieldValues(codeType) {
if ((await isCodeType(codeType)) &&
!Object.hasOwn(codeTypeFieldValues, codeType)) {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const fieldValueData = await fs.readFile(`./data/${codeType}.json`);
const fieldValueData = await fs.readFile(path.join(currentFolder, 'data', `${codeType}.json`));
codeTypeFieldValues[codeType] = JSON.parse(fieldValueData);
}
return codeTypeFieldValues[codeType] ?? {};
Expand Down
10 changes: 6 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable security/detect-object-injection */
/* eslint-disable security/detect-non-literal-fs-filename, security/detect-object-injection */

import fs from 'node:fs/promises'
import path from 'node:path'

let codeTypes: Record<string, string> = {}
let codeTypeFieldValues: Record<string, Record<string, string>> = {}

const currentFolder = path.dirname(import.meta.filename ?? '.')

/**
* Returns an object of code types.
* @returns {Promise<Record<string, string>>} - An object with "code type" keys and "code type description" values.
*/
export async function getCodeTypes(): Promise<Record<string, string>> {
if (Object.keys(codeTypes).length === 0) {
const codeTypesData = await fs.readFile('./data/codeTypes.json')
const codeTypesData = await fs.readFile(path.join(currentFolder, 'data', 'codeTypes.json'))
codeTypes = JSON.parse(codeTypesData as unknown as string)
}

Expand Down Expand Up @@ -53,8 +56,7 @@ export async function getFieldValues(
(await isCodeType(codeType)) &&
!Object.hasOwn(codeTypeFieldValues, codeType)
) {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const fieldValueData = await fs.readFile(`./data/${codeType}.json`)
const fieldValueData = await fs.readFile(path.join(currentFolder, 'data', `${codeType}.json`))

codeTypeFieldValues[codeType] = JSON.parse(
fieldValueData as unknown as string
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cityssm/ncic-lookup",
"version": "0.1.0",
"version": "0.1.1",
"type": "module",
"engines": {
"node": ">=18.0.0"
Expand All @@ -16,6 +16,7 @@
"coverage": "c8 mocha"
},
"keywords": [
"automobiles",
"ncic",
"neim",
"vma"
Expand Down

0 comments on commit ffab9db

Please sign in to comment.