From a3c908bf6bc1856ccc130949b244f1910365dc11 Mon Sep 17 00:00:00 2001 From: Todd Bluhm Date: Tue, 3 Dec 2024 02:54:08 -0900 Subject: [PATCH] fix(file-url): fix file path to conform to ES module requirements --- dist/parse-env-file.js | 13 +++++++------ dist/parse-rc-file.js | 9 +++++---- src/parse-env-file.ts | 13 +++++++------ src/parse-rc-file.ts | 9 +++++---- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/dist/parse-env-file.js b/dist/parse-env-file.js index f7c07df..457b661 100644 --- a/dist/parse-env-file.js +++ b/dist/parse-env-file.js @@ -1,18 +1,19 @@ -import * as fs from 'fs'; -import * as path from 'path'; +import { existsSync, readFileSync } from 'node:fs'; +import { extname } from 'node:path'; +import { pathToFileURL } from 'node:url'; import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js'; /** * Gets the environment vars from an env file */ export async function getEnvFileVars(envFilePath) { const absolutePath = resolveEnvFilePath(envFilePath); - if (!fs.existsSync(absolutePath)) { + if (!existsSync(absolutePath)) { const pathError = new Error(`Invalid env file path (${envFilePath}).`); pathError.name = 'PathError'; throw pathError; } // Get the file extension - const ext = path.extname(absolutePath).toLowerCase(); + const ext = extname(absolutePath).toLowerCase(); let env = {}; if (IMPORT_HOOK_EXTENSIONS.includes(ext)) { // For some reason in ES Modules, only JSON file types need to be specifically delinated when importing them @@ -20,7 +21,7 @@ export async function getEnvFileVars(envFilePath) { if (ext === '.json') { attributeTypes = { with: { type: 'json' } }; } - const res = await import(absolutePath, attributeTypes); + const res = await import(pathToFileURL(absolutePath).href, attributeTypes); if ('default' in res) { env = res.default; } @@ -33,7 +34,7 @@ export async function getEnvFileVars(envFilePath) { } } else { - const file = fs.readFileSync(absolutePath, { encoding: 'utf8' }); + const file = readFileSync(absolutePath, { encoding: 'utf8' }); env = parseEnvString(file); } return env; diff --git a/dist/parse-rc-file.js b/dist/parse-rc-file.js index 1c80be6..fd63997 100644 --- a/dist/parse-rc-file.js +++ b/dist/parse-rc-file.js @@ -1,6 +1,7 @@ -import { stat, readFile } from 'fs'; -import { promisify } from 'util'; -import { extname } from 'path'; +import { stat, readFile } from 'node:fs'; +import { promisify } from 'node:util'; +import { extname } from 'node:path'; +import { pathToFileURL } from 'node:url'; import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js'; const statAsync = promisify(stat); const readFileAsync = promisify(readFile); @@ -27,7 +28,7 @@ export async function getRCFileVars({ environments, filePath }) { if (ext === '.json') { attributeTypes = { with: { type: 'json' } }; } - const res = await import(absolutePath, attributeTypes); + const res = await import(pathToFileURL(absolutePath).href, attributeTypes); if ('default' in res) { parsedData = res.default; } diff --git a/src/parse-env-file.ts b/src/parse-env-file.ts index 41cabb8..b0fb1a2 100644 --- a/src/parse-env-file.ts +++ b/src/parse-env-file.ts @@ -1,5 +1,6 @@ -import * as fs from 'fs' -import * as path from 'path' +import { existsSync, readFileSync } from 'node:fs' +import { extname } from 'node:path' +import { pathToFileURL } from 'node:url' import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js' import type { Environment } from './types.ts' @@ -8,14 +9,14 @@ import type { Environment } from './types.ts' */ export async function getEnvFileVars(envFilePath: string): Promise { const absolutePath = resolveEnvFilePath(envFilePath) - if (!fs.existsSync(absolutePath)) { + if (!existsSync(absolutePath)) { const pathError = new Error(`Invalid env file path (${envFilePath}).`) pathError.name = 'PathError' throw pathError } // Get the file extension - const ext = path.extname(absolutePath).toLowerCase() + const ext = extname(absolutePath).toLowerCase() let env: Environment = {} if (IMPORT_HOOK_EXTENSIONS.includes(ext)) { // For some reason in ES Modules, only JSON file types need to be specifically delinated when importing them @@ -23,7 +24,7 @@ export async function getEnvFileVars(envFilePath: string): Promise if (ext === '.json') { attributeTypes = { with: { type: 'json' } } } - const res = await import(absolutePath, attributeTypes) as Environment | { default: Environment } + const res = await import(pathToFileURL(absolutePath).href, attributeTypes) as Environment | { default: Environment } if ('default' in res) { env = res.default as Environment } else { @@ -35,7 +36,7 @@ export async function getEnvFileVars(envFilePath: string): Promise } } else { - const file = fs.readFileSync(absolutePath, { encoding: 'utf8' }) + const file = readFileSync(absolutePath, { encoding: 'utf8' }) env = parseEnvString(file) } return env diff --git a/src/parse-rc-file.ts b/src/parse-rc-file.ts index 7cc02a1..09ea9dd 100644 --- a/src/parse-rc-file.ts +++ b/src/parse-rc-file.ts @@ -1,6 +1,7 @@ -import { stat, readFile } from 'fs' -import { promisify } from 'util' -import { extname } from 'path' +import { stat, readFile } from 'node:fs' +import { promisify } from 'node:util' +import { extname } from 'node:path' +import { pathToFileURL } from 'node:url' import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js' import type { Environment, RCEnvironment } from './types.ts' @@ -34,7 +35,7 @@ export async function getRCFileVars( if (ext === '.json') { attributeTypes = { with: { type: 'json' } } } - const res = await import(absolutePath, attributeTypes) as RCEnvironment | { default: RCEnvironment } + const res = await import(pathToFileURL(absolutePath).href, attributeTypes) as RCEnvironment | { default: RCEnvironment } if ('default' in res) { parsedData = res.default as RCEnvironment } else {