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 { existsSync, readFileSync } from 'node:fs';
|
||||||
import * as path from 'path';
|
import { extname } from 'node:path';
|
||||||
|
import { pathToFileURL } from 'node:url';
|
||||||
import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js';
|
import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js';
|
||||||
/**
|
/**
|
||||||
* Gets the environment vars from an env file
|
* Gets the environment vars from an env file
|
||||||
*/
|
*/
|
||||||
export async function getEnvFileVars(envFilePath) {
|
export async function getEnvFileVars(envFilePath) {
|
||||||
const absolutePath = resolveEnvFilePath(envFilePath);
|
const absolutePath = resolveEnvFilePath(envFilePath);
|
||||||
if (!fs.existsSync(absolutePath)) {
|
if (!existsSync(absolutePath)) {
|
||||||
const pathError = new Error(`Invalid env file path (${envFilePath}).`);
|
const pathError = new Error(`Invalid env file path (${envFilePath}).`);
|
||||||
pathError.name = 'PathError';
|
pathError.name = 'PathError';
|
||||||
throw pathError;
|
throw pathError;
|
||||||
}
|
}
|
||||||
// Get the file extension
|
// Get the file extension
|
||||||
const ext = path.extname(absolutePath).toLowerCase();
|
const ext = extname(absolutePath).toLowerCase();
|
||||||
let env = {};
|
let env = {};
|
||||||
if (IMPORT_HOOK_EXTENSIONS.includes(ext)) {
|
if (IMPORT_HOOK_EXTENSIONS.includes(ext)) {
|
||||||
// For some reason in ES Modules, only JSON file types need to be specifically delinated when importing them
|
// 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') {
|
if (ext === '.json') {
|
||||||
attributeTypes = { with: { type: 'json' } };
|
attributeTypes = { with: { type: 'json' } };
|
||||||
}
|
}
|
||||||
const res = await import(absolutePath, attributeTypes);
|
const res = await import(pathToFileURL(absolutePath).href, attributeTypes);
|
||||||
if ('default' in res) {
|
if ('default' in res) {
|
||||||
env = res.default;
|
env = res.default;
|
||||||
}
|
}
|
||||||
@ -33,7 +34,7 @@ export async function getEnvFileVars(envFilePath) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const file = fs.readFileSync(absolutePath, { encoding: 'utf8' });
|
const file = readFileSync(absolutePath, { encoding: 'utf8' });
|
||||||
env = parseEnvString(file);
|
env = parseEnvString(file);
|
||||||
}
|
}
|
||||||
return env;
|
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 { stat, readFile } from 'node:fs';
|
||||||
import { promisify } from 'util';
|
import { promisify } from 'node:util';
|
||||||
import { extname } from 'path';
|
import { extname } from 'node:path';
|
||||||
|
import { pathToFileURL } from 'node:url';
|
||||||
import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js';
|
import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js';
|
||||||
const statAsync = promisify(stat);
|
const statAsync = promisify(stat);
|
||||||
const readFileAsync = promisify(readFile);
|
const readFileAsync = promisify(readFile);
|
||||||
@ -27,7 +28,7 @@ export async function getRCFileVars({ environments, filePath }) {
|
|||||||
if (ext === '.json') {
|
if (ext === '.json') {
|
||||||
attributeTypes = { with: { type: 'json' } };
|
attributeTypes = { with: { type: 'json' } };
|
||||||
}
|
}
|
||||||
const res = await import(absolutePath, attributeTypes);
|
const res = await import(pathToFileURL(absolutePath).href, attributeTypes);
|
||||||
if ('default' in res) {
|
if ('default' in res) {
|
||||||
parsedData = res.default;
|
parsedData = res.default;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import * as fs from 'fs'
|
import { existsSync, readFileSync } from 'node:fs'
|
||||||
import * as path from 'path'
|
import { extname } from 'node:path'
|
||||||
|
import { pathToFileURL } from 'node:url'
|
||||||
import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js'
|
import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js'
|
||||||
import type { Environment } from './types.ts'
|
import type { Environment } from './types.ts'
|
||||||
|
|
||||||
@ -8,14 +9,14 @@ import type { Environment } from './types.ts'
|
|||||||
*/
|
*/
|
||||||
export async function getEnvFileVars(envFilePath: string): Promise<Environment> {
|
export async function getEnvFileVars(envFilePath: string): Promise<Environment> {
|
||||||
const absolutePath = resolveEnvFilePath(envFilePath)
|
const absolutePath = resolveEnvFilePath(envFilePath)
|
||||||
if (!fs.existsSync(absolutePath)) {
|
if (!existsSync(absolutePath)) {
|
||||||
const pathError = new Error(`Invalid env file path (${envFilePath}).`)
|
const pathError = new Error(`Invalid env file path (${envFilePath}).`)
|
||||||
pathError.name = 'PathError'
|
pathError.name = 'PathError'
|
||||||
throw pathError
|
throw pathError
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the file extension
|
// Get the file extension
|
||||||
const ext = path.extname(absolutePath).toLowerCase()
|
const ext = extname(absolutePath).toLowerCase()
|
||||||
let env: Environment = {}
|
let env: Environment = {}
|
||||||
if (IMPORT_HOOK_EXTENSIONS.includes(ext)) {
|
if (IMPORT_HOOK_EXTENSIONS.includes(ext)) {
|
||||||
// For some reason in ES Modules, only JSON file types need to be specifically delinated when importing them
|
// 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') {
|
if (ext === '.json') {
|
||||||
attributeTypes = { with: { type: '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) {
|
if ('default' in res) {
|
||||||
env = res.default as Environment
|
env = res.default as Environment
|
||||||
} else {
|
} else {
|
||||||
@ -35,7 +36,7 @@ export async function getEnvFileVars(envFilePath: string): Promise<Environment>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const file = fs.readFileSync(absolutePath, { encoding: 'utf8' })
|
const file = readFileSync(absolutePath, { encoding: 'utf8' })
|
||||||
env = parseEnvString(file)
|
env = parseEnvString(file)
|
||||||
}
|
}
|
||||||
return env
|
return env
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { stat, readFile } from 'fs'
|
import { stat, readFile } from 'node:fs'
|
||||||
import { promisify } from 'util'
|
import { promisify } from 'node:util'
|
||||||
import { extname } from 'path'
|
import { extname } from 'node:path'
|
||||||
|
import { pathToFileURL } from 'node:url'
|
||||||
import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js'
|
import { resolveEnvFilePath, IMPORT_HOOK_EXTENSIONS, isPromise } from './utils.js'
|
||||||
import type { Environment, RCEnvironment } from './types.ts'
|
import type { Environment, RCEnvironment } from './types.ts'
|
||||||
|
|
||||||
@ -34,7 +35,7 @@ export async function getRCFileVars(
|
|||||||
if (ext === '.json') {
|
if (ext === '.json') {
|
||||||
attributeTypes = { with: { type: '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) {
|
if ('default' in res) {
|
||||||
parsedData = res.default as RCEnvironment
|
parsedData = res.default as RCEnvironment
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user