Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(update): Update Project to Recent Node Versions #392

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/mocharc.json",
"require": ["tsx/esm", "esmock"],
"extensions": ["ts"],
"spec": [
"test/**/*.ts"
]
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Todd Bluhm
Copyright (c) Todd Bluhm

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 2 additions & 1 deletion bin/env-cmd.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#! /usr/bin/env node
require('../dist').CLI(process.argv.slice(2))
import { CLI } from '../dist/index.js'
CLI(process.argv.slice(2))
8 changes: 8 additions & 0 deletions dist/cli.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Environment } from './types.ts';
/**
* Executes env - cmd using command line arguments
* @export
* @param {string[]} args Command line argument to pass in ['-f', './.env']
* @returns {Promise<Environment>}
*/
export declare function CLI(args: string[]): Promise<Environment>;
21 changes: 21 additions & 0 deletions dist/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as processLib from 'node:process';
import { EnvCmd } from './env-cmd.js';
import { parseArgs } from './parse-args.js';
/**
* Executes env - cmd using command line arguments
* @export
* @param {string[]} args Command line argument to pass in ['-f', './.env']
* @returns {Promise<Environment>}
*/
export async function CLI(args) {
// Parse the args from the command line
const parsedArgs = parseArgs(args);
// Run EnvCmd
try {
return await EnvCmd(parsedArgs);
}
catch (e) {
console.error(e);
return processLib.exit(1);
}
}
13 changes: 3 additions & 10 deletions dist/env-cmd.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { EnvCmdOptions } from './types';
/**
* Executes env - cmd using command line arguments
* @export
* @param {string[]} args Command line argument to pass in ['-f', './.env']
* @returns {Promise<{ [key: string]: any }>}
*/
export declare function CLI(args: string[]): Promise<Record<string, any>>;
import type { EnvCmdOptions, Environment } from './types.ts';
/**
* The main env-cmd program. This will spawn a new process and run the given command using
* various environment file solutions.
*
* @export
* @param {EnvCmdOptions} { command, commandArgs, envFile, rc, options }
* @returns {Promise<{ [key: string]: any }>} Returns an object containing [environment variable name]: value
* @returns {Promise<Environment>} Returns an object containing [environment variable name]: value
*/
export declare function EnvCmd({ command, commandArgs, envFile, rc, options }: EnvCmdOptions): Promise<Record<string, any>>;
export declare function EnvCmd({ command, commandArgs, envFile, rc, options, }: EnvCmdOptions): Promise<Environment>;
55 changes: 16 additions & 39 deletions dist/env-cmd.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,47 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const spawn_1 = require("./spawn");
const signal_termination_1 = require("./signal-termination");
const parse_args_1 = require("./parse-args");
const get_env_vars_1 = require("./get-env-vars");
const expand_envs_1 = require("./expand-envs");
/**
* Executes env - cmd using command line arguments
* @export
* @param {string[]} args Command line argument to pass in ['-f', './.env']
* @returns {Promise<{ [key: string]: any }>}
*/
async function CLI(args) {
// Parse the args from the command line
const parsedArgs = parse_args_1.parseArgs(args);
// Run EnvCmd
try {
return await exports.EnvCmd(parsedArgs);
}
catch (e) {
console.error(e);
return process.exit(1);
}
}
exports.CLI = CLI;
import { default as spawn } from 'cross-spawn';
import { TermSignals } from './signal-termination.js';
import { getEnvVars } from './get-env-vars.js';
import { expandEnvs } from './expand-envs.js';
import * as processLib from 'node:process';
/**
* The main env-cmd program. This will spawn a new process and run the given command using
* various environment file solutions.
*
* @export
* @param {EnvCmdOptions} { command, commandArgs, envFile, rc, options }
* @returns {Promise<{ [key: string]: any }>} Returns an object containing [environment variable name]: value
* @returns {Promise<Environment>} Returns an object containing [environment variable name]: value
*/
async function EnvCmd({ command, commandArgs, envFile, rc, options = {} }) {
var _a;
export async function EnvCmd({ command, commandArgs, envFile, rc, options = {}, }) {
let env = {};
try {
env = await get_env_vars_1.getEnvVars({ envFile, rc, verbose: options.verbose });
env = await getEnvVars({ envFile, rc, verbose: options.verbose });
}
catch (e) {
if (!((_a = options.silent) !== null && _a !== void 0 ? _a : false)) {
if (!(options.silent ?? false)) {
throw e;
}
}
// Override the merge order if --no-override flag set
if (options.noOverride === true) {
env = Object.assign({}, env, process.env);
env = Object.assign({}, env, processLib.env);
}
else {
// Add in the system environment variables to our environment list
env = Object.assign({}, process.env, env);
env = Object.assign({}, processLib.env, env);
}
if (options.expandEnvs === true) {
command = expand_envs_1.expandEnvs(command, env);
commandArgs = commandArgs.map(arg => expand_envs_1.expandEnvs(arg, env));
command = expandEnvs(command, env);
commandArgs = commandArgs.map(arg => expandEnvs(arg, env));
}
// Execute the command with the given environment variables
const proc = spawn_1.spawn(command, commandArgs, {
const proc = spawn(command, commandArgs, {
stdio: 'inherit',
shell: options.useShell,
env
env: env,
});
// Handle any termination signals for parent and child proceses
const signals = new signal_termination_1.TermSignals({ verbose: options.verbose });
const signals = new TermSignals({ verbose: options.verbose });
signals.handleUncaughtExceptions();
signals.handleTermSignals(proc);
return env;
}
exports.EnvCmd = EnvCmd;
5 changes: 3 additions & 2 deletions dist/expand-envs.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Environment } from './types.ts';
/**
* expandEnvs Replaces $var in args and command with environment variables
* the environment variable doesn't exist, it leaves it as is.
* if the environment variable doesn't exist, it leaves it as is.
*/
export declare function expandEnvs(str: string, envs: Record<string, any>): string;
export declare function expandEnvs(str: string, envs: Environment): string;
12 changes: 5 additions & 7 deletions dist/expand-envs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* expandEnvs Replaces $var in args and command with environment variables
* the environment variable doesn't exist, it leaves it as is.
* if the environment variable doesn't exist, it leaves it as is.
*/
function expandEnvs(str, envs) {
return str.replace(/(?<!\\)\$[a-zA-Z0-9_]+/g, varName => {
export function expandEnvs(str, envs) {
return str.replace(/(?<!\\)\$[a-zA-Z0-9_]+/g, (varName) => {
const varValue = envs[varName.slice(1)];
return varValue === undefined ? varName : varValue;
// const test = 42;
return varValue === undefined ? varName : varValue.toString();
});
}
exports.expandEnvs = expandEnvs;
8 changes: 4 additions & 4 deletions dist/get-env-vars.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { GetEnvVarOptions } from './types';
export declare function getEnvVars(options?: GetEnvVarOptions): Promise<Record<string, any>>;
import type { GetEnvVarOptions, Environment } from './types.ts';
export declare function getEnvVars(options?: GetEnvVarOptions): Promise<Environment>;
export declare function getEnvFile({ filePath, fallback, verbose }: {
filePath?: string;
fallback?: boolean;
verbose?: boolean;
}): Promise<Record<string, any>>;
}): Promise<Environment>;
export declare function getRCFile({ environments, filePath, verbose }: {
environments: string[];
filePath?: string;
verbose?: boolean;
}): Promise<Record<string, any>>;
}): Promise<Environment>;
70 changes: 39 additions & 31 deletions dist/get-env-vars.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const parse_rc_file_1 = require("./parse-rc-file");
const parse_env_file_1 = require("./parse-env-file");
import { getRCFileVars } from './parse-rc-file.js';
import { getEnvFileVars } from './parse-env-file.js';
const RC_FILE_DEFAULT_LOCATIONS = ['./.env-cmdrc', './.env-cmdrc.js', './.env-cmdrc.json'];
const ENV_FILE_DEFAULT_LOCATIONS = ['./.env', './.env.js', './.env.json'];
async function getEnvVars(options = {}) {
options.envFile = options.envFile !== undefined ? options.envFile : {};
export async function getEnvVars(options = {}) {
options.envFile = options.envFile ?? {};
// Check for rc file usage
if (options.rc !== undefined) {
return await getRCFile({
environments: options.rc.environments,
filePath: options.rc.filePath,
verbose: options.verbose
verbose: options.verbose,
});
}
return await getEnvFile({
filePath: options.envFile.filePath,
fallback: options.envFile.fallback,
verbose: options.verbose
verbose: options.verbose,
});
}
exports.getEnvVars = getEnvVars;
async function getEnvFile({ filePath, fallback, verbose }) {
export async function getEnvFile({ filePath, fallback, verbose }) {
// Use env file
if (filePath !== undefined) {
try {
const env = await parse_env_file_1.getEnvFileVars(filePath);
const env = await getEnvFileVars(filePath);
if (verbose === true) {
console.info(`Found .env file at path: ${filePath}`);
}
return env;
}
catch (e) {
catch {
if (verbose === true) {
console.info(`Failed to find .env file at path: ${filePath}`);
}
// Ignore error as we are just trying this location
}
if (fallback !== true) {
throw new Error(`Failed to find .env file at path: ${filePath}`);
Expand All @@ -43,40 +41,43 @@ async function getEnvFile({ filePath, fallback, verbose }) {
// Use the default env file locations
for (const path of ENV_FILE_DEFAULT_LOCATIONS) {
try {
const env = await parse_env_file_1.getEnvFileVars(path);
const env = await getEnvFileVars(path);
if (verbose === true) {
console.info(`Found .env file at default path: ${path}`);
}
return env;
}
catch (e) { }
catch {
// Ignore error because we are just trying this location
}
}
const error = `Failed to find .env file at default paths: [${ENV_FILE_DEFAULT_LOCATIONS.join(',')}]`;
if (verbose === true) {
console.info(error);
}
throw new Error(error);
}
exports.getEnvFile = getEnvFile;
async function getRCFile({ environments, filePath, verbose }) {
export async function getRCFile({ environments, filePath, verbose }) {
// User provided an .rc file path
if (filePath !== undefined) {
try {
const env = await parse_rc_file_1.getRCFileVars({ environments, filePath });
const env = await getRCFileVars({ environments, filePath });
if (verbose === true) {
console.info(`Found environments: [${environments.join(',')}] for .rc file at path: ${filePath}`);
}
return env;
}
catch (e) {
if (e.name === 'PathError') {
if (verbose === true) {
console.info(`Failed to find .rc file at path: ${filePath}`);
if (e instanceof Error) {
if (e.name === 'PathError') {
if (verbose === true) {
console.info(`Failed to find .rc file at path: ${filePath}`);
}
}
}
if (e.name === 'EnvironmentError') {
if (verbose === true) {
console.info(`Failed to find environments: [${environments.join(',')}] for .rc file at path: ${filePath}`);
if (e.name === 'EnvironmentError') {
if (verbose === true) {
console.info(`Failed to find environments: [${environments.join(',')}] for .rc file at path: ${filePath}`);
}
}
}
throw e;
Expand All @@ -85,19 +86,27 @@ async function getRCFile({ environments, filePath, verbose }) {
// Use the default .rc file locations
for (const path of RC_FILE_DEFAULT_LOCATIONS) {
try {
const env = await parse_rc_file_1.getRCFileVars({ environments, filePath: path });
const env = await getRCFileVars({ environments, filePath: path });
if (verbose === true) {
console.info(`Found environments: [${environments.join(',')}] for default .rc file at path: ${path}`);
}
return env;
}
catch (e) {
if (e.name === 'EnvironmentError') {
const errorText = `Failed to find environments: [${environments.join(',')}] for .rc file at path: ${path}`;
if (verbose === true) {
console.info(errorText);
if (e instanceof Error) {
if (e.name === 'EnvironmentError') {
const errorText = `Failed to find environments: [${environments.join(',')}] for .rc file at path: ${path}`;
if (verbose === true) {
console.info(errorText);
}
throw new Error(errorText);
}
if (e.name === 'ParseError') {
if (verbose === true) {
console.info(e.message);
}
throw new Error(e.message);
}
throw new Error(errorText);
}
}
}
Expand All @@ -107,4 +116,3 @@ async function getRCFile({ environments, filePath, verbose }) {
}
throw new Error(errorText);
}
exports.getRCFile = getRCFile;
7 changes: 4 additions & 3 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getEnvVars } from './get-env-vars';
export * from './types';
export * from './env-cmd';
import { getEnvVars } from './get-env-vars.js';
export * from './types.js';
export * from './cli.js';
export * from './env-cmd.js';
export declare const GetEnvVars: typeof getEnvVars;
14 changes: 6 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
const get_env_vars_1 = require("./get-env-vars");
__export(require("./env-cmd"));
exports.GetEnvVars = get_env_vars_1.getEnvVars;
import { getEnvVars } from './get-env-vars.js';
// Export the core env-cmd API
export * from './types.js';
export * from './cli.js';
export * from './env-cmd.js';
export const GetEnvVars = getEnvVars;
5 changes: 2 additions & 3 deletions dist/parse-args.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as commander from 'commander';
import { EnvCmdOptions } from './types';
import type { EnvCmdOptions, CommanderOptions } from './types.ts';
/**
* Parses the arguments passed into the cli
*/
export declare function parseArgs(args: string[]): EnvCmdOptions;
export declare function parseArgsUsingCommander(args: string[]): commander.Command;
export declare function parseArgsUsingCommander(args: string[]): CommanderOptions;
Loading
Loading