mirror of
https://github.com/toddbluhm/env-cmd.git
synced 2025-12-08 18:23:33 +00:00
fix(file-url): fix file path to conform to ES module requirements
This commit is contained in:
parent
7eae0dae54
commit
a3c908bf6b
13
dist/parse-env-file.js
vendored
13
dist/parse-env-file.js
vendored
@ -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;
|
||||
|
||||
9
dist/parse-rc-file.js
vendored
9
dist/parse-rc-file.js
vendored
@ -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;
|
||||
}
|
||||
|
||||
@ -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<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
|
||||
@ -23,7 +24,7 @@ export async function getEnvFileVars(envFilePath: string): Promise<Environment>
|
||||
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<Environment>
|
||||
}
|
||||
}
|
||||
else {
|
||||
const file = fs.readFileSync(absolutePath, { encoding: 'utf8' })
|
||||
const file = readFileSync(absolutePath, { encoding: 'utf8' })
|
||||
env = parseEnvString(file)
|
||||
}
|
||||
return env
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user