Skip to content

Commit

Permalink
fix(file-url): fix file path to conform to ES module requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbluhm committed Dec 3, 2024
1 parent 7eae0da commit a3c908b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
13 changes: 7 additions & 6 deletions dist/parse-env-file.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
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
let attributeTypes = {};
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;
}
Expand All @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions dist/parse-rc-file.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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;
}
Expand Down
13 changes: 7 additions & 6 deletions src/parse-env-file.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -8,22 +9,22 @@ import type { Environment } from './types.ts'
*/
export async function getEnvFileVars(envFilePath: string): Promise<Environment> {
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
let attributeTypes = {}
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 {
Expand All @@ -35,7 +36,7 @@ export async function getEnvFileVars(envFilePath: string): Promise<Environment>
}
}
else {
const file = fs.readFileSync(absolutePath, { encoding: 'utf8' })
const file = readFileSync(absolutePath, { encoding: 'utf8' })
env = parseEnvString(file)
}
return env
Expand Down
9 changes: 5 additions & 4 deletions src/parse-rc-file.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a3c908b

Please sign in to comment.