Skip to content

Commit

Permalink
chore(pkg): new linter and updated typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbluhm committed Nov 30, 2024
1 parent 6211bb0 commit 3b67538
Show file tree
Hide file tree
Showing 41 changed files with 552 additions and 385 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 18.x, 20.x, 22.x]
node-version: [18.x, 20.x, 22.x]

steps:
- name: Checkout Project
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 18.x, 20.x, 22.x]
node-version: [18.x, 20.x, 22.x]

steps:
- name: Checkout Project
Expand Down
8 changes: 2 additions & 6 deletions dist/env-cmd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { EnvCmdOptions } from './types';
* @param {string[]} args Command line argument to pass in ['-f', './.env']
* @returns {Promise<{ [key: string]: any }>}
*/
export declare function CLI(args: string[]): Promise<{
[key: string]: any;
}>;
export declare function CLI(args: string[]): Promise<Record<string, any>>;
/**
* The main env-cmd program. This will spawn a new process and run the given command using
* various environment file solutions.
Expand All @@ -16,6 +14,4 @@ export declare function CLI(args: string[]): Promise<{
* @param {EnvCmdOptions} { command, commandArgs, envFile, rc, options }
* @returns {Promise<{ [key: string]: any }>} Returns an object containing [environment variable name]: value
*/
export declare function EnvCmd({ command, commandArgs, envFile, rc, options }: EnvCmdOptions): Promise<{
[key: string]: any;
}>;
export declare function EnvCmd({ command, commandArgs, envFile, rc, options }: EnvCmdOptions): Promise<Record<string, any>>;
4 changes: 1 addition & 3 deletions dist/expand-envs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
* expandEnvs Replaces $var in args and command with environment variables
* the environment variable doesn't exist, it leaves it as is.
*/
export declare function expandEnvs(str: string, envs: {
[key: string]: any;
}): string;
export declare function expandEnvs(str: string, envs: Record<string, any>): string;
12 changes: 3 additions & 9 deletions dist/get-env-vars.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { GetEnvVarOptions } from './types';
export declare function getEnvVars(options?: GetEnvVarOptions): Promise<{
[key: string]: any;
}>;
export declare function getEnvVars(options?: GetEnvVarOptions): Promise<Record<string, any>>;
export declare function getEnvFile({ filePath, fallback, verbose }: {
filePath?: string;
fallback?: boolean;
verbose?: boolean;
}): Promise<{
[key: string]: any;
}>;
}): Promise<Record<string, any>>;
export declare function getRCFile({ environments, filePath, verbose }: {
environments: string[];
filePath?: string;
verbose?: boolean;
}): Promise<{
[key: string]: any;
}>;
}): Promise<Record<string, any>>;
2 changes: 1 addition & 1 deletion dist/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const commander = require("commander");
const utils_1 = require("./utils");
// Use commonjs require to prevent a weird folder hierarchy in dist
const packageJson = require('../package.json'); /* eslint-disable-line */
const packageJson = require('../package.json');
/**
* Parses the arguments passed into the cli
*/
Expand Down
12 changes: 3 additions & 9 deletions dist/parse-env-file.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
/**
* Gets the environment vars from an env file
*/
export declare function getEnvFileVars(envFilePath: string): Promise<{
[key: string]: any;
}>;
export declare function getEnvFileVars(envFilePath: string): Promise<Record<string, any>>;
/**
* Parse out all env vars from a given env file string and return an object
*/
export declare function parseEnvString(envFileString: string): {
[key: string]: string;
};
export declare function parseEnvString(envFileString: string): Record<string, string>;
/**
* Parse out all env vars from an env file string
*/
export declare function parseEnvVars(envString: string): {
[key: string]: string;
};
export declare function parseEnvVars(envString: string): Record<string, string>;
/**
* Strips out comments from env file string
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/parse-env-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function getEnvFileVars(envFilePath) {
const ext = path.extname(absolutePath).toLowerCase();
let env = {};
if (REQUIRE_HOOK_EXTENSIONS.includes(ext)) {
const possiblePromise = require(absolutePath); /* eslint-disable-line */
const possiblePromise = require(absolutePath);
env = utils_1.isPromise(possiblePromise) ? await possiblePromise : possiblePromise;
}
else {
Expand Down
4 changes: 1 addition & 3 deletions dist/parse-rc-file.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
export declare function getRCFileVars({ environments, filePath }: {
environments: string[];
filePath: string;
}): Promise<{
[key: string]: any;
}>;
}): Promise<Record<string, any>>;
2 changes: 1 addition & 1 deletion dist/parse-rc-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function getRCFileVars({ environments, filePath }) {
let parsedData;
try {
if (ext === '.json' || ext === '.js' || ext === '.cjs') {
const possiblePromise = require(absolutePath); /* eslint-disable-line */
const possiblePromise = require(absolutePath);
parsedData = utils_1.isPromise(possiblePromise) ? await possiblePromise : possiblePromise;
}
else {
Expand Down
2 changes: 1 addition & 1 deletion dist/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export declare function parseArgList(list: string): string[];
/**
* A simple function to test if the value is a promise
*/
export declare function isPromise(value: any | PromiseLike<Object>): value is Promise<any>;
export declare function isPromise(value: any | PromiseLike<object>): value is Promise<any>;
43 changes: 43 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const eslint = require('@eslint/js')
const tseslint = require('typescript-eslint')
const globals = require('globals')
const stylistic = require('@stylistic/eslint-plugin')

module.exports = tseslint.config(
{
ignores: ['dist/*', 'bin/*'],
rules: {
'@typescript-eslint/no-require-imports': 'off',
},
languageOptions: {
globals: {
...globals.node,
},
parserOptions: {
projectService: {
allowDefaultProject: ['test/*.ts'],
},
},
},
extends: [
eslint.configs.recommended,
stylistic.configs['recommended-flat'],
tseslint.configs.strictTypeChecked,
tseslint.configs.stylisticTypeChecked,
],
},
// Disable Type Checking JS files
{
files: ['**/*.js'],
extends: [tseslint.configs.disableTypeChecked],
},
{
// For test files ignore some rules
files: ['test/*.ts'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
},
},
)
26 changes: 26 additions & 0 deletions eslint.config.js.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = (async function config() {
const { default: love } = await import('eslint-config-love')

return [
love,
{
files: [
'src/**/*.[j|t]s',
// 'src/**/*.ts',
'test/**/*.[j|t]s',
// 'test/**/*.ts'
],
languageOptions: {
parserOptions: {
projectService: {
allowDefaultProject: ['eslint.config.js', 'bin/env-cmd.js'],
defaultProject: './tsconfig.json',
},
},
},
},
{
ignores: ['dist/'],
}
]
})()
15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test": "mocha -r ts-node/register ./test/**/*.ts",
"test-cover": "nyc npm test",
"coveralls": "coveralls < coverage/lcov.info",
"lint": "ts-standard --fix && tsc --noEmit",
"lint": "npx eslint .",
"build": "tsc",
"watch": "tsc -w"
},
Expand Down Expand Up @@ -53,20 +53,23 @@
"devDependencies": {
"@commitlint/cli": "^8.0.0",
"@commitlint/config-conventional": "^8.0.0",
"@eslint/js": "^9.15.0",
"@stylistic/eslint-plugin": "^2.11.0",
"@types/chai": "^4.0.0",
"@types/cross-spawn": "^6.0.0",
"@types/mocha": "^7.0.0",
"@types/node": "^12.0.0",
"@types/sinon": "^9.0.0",
"chai": "^4.0.0",
"coveralls": "^3.0.0",
"globals": "^15.12.0",
"husky": "^4.0.0",
"mocha": "^7.0.0",
"nyc": "^15.0.0",
"sinon": "^9.0.0",
"ts-node": "^8.0.0",
"ts-standard": "^8.0.0",
"typescript": "^5.7.2"
"typescript": "^5.7.2",
"typescript-eslint": "^8.15.0"
},
"nyc": {
"include": [
Expand All @@ -85,12 +88,6 @@
"sourceMap": true,
"instrument": true
},
"ts-standard": {
"project": "./tsconfig.eslint.json",
"ignore": [
"dist"
]
},
"greenkeeper": {
"ignore": [
"@types/node"
Expand Down
31 changes: 17 additions & 14 deletions src/env-cmd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn } from './spawn'
import { EnvCmdOptions } from './types'
import { EnvCmdOptions, Environment } from './types'
import { TermSignals } from './signal-termination'
import { parseArgs } from './parse-args'
import { getEnvVars } from './get-env-vars'
Expand All @@ -9,16 +9,17 @@ import { expandEnvs } from './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 }>}
* @returns {Promise<Environment>}
*/
export async function CLI (args: string[]): Promise<{ [key: string]: any }> {
export async function CLI(args: string[]): Promise<Environment> {
// Parse the args from the command line
const parsedArgs = parseArgs(args)

// Run EnvCmd
try {
return await (exports.EnvCmd(parsedArgs) as Promise<{ [key: string]: any }>)
} catch (e) {
return await (exports as { EnvCmd: typeof EnvCmd }).EnvCmd(parsedArgs)
}
catch (e) {
console.error(e)
return process.exit(1)
}
Expand All @@ -30,29 +31,31 @@ export async function CLI (args: string[]): Promise<{ [key: string]: any }> {
*
* @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 async function EnvCmd (
export async function EnvCmd(
{
command,
commandArgs,
envFile,
rc,
options = {}
}: EnvCmdOptions
): Promise<{ [key: string]: any }> {
let env: { [name: string]: string } = {}
options = {},
}: EnvCmdOptions,
): Promise<Environment> {
let env: Environment = {}
try {
env = await getEnvVars({ envFile, rc, verbose: options.verbose })
} catch (e) {
}
catch (e) {
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)
} else {
}
else {
// Add in the system environment variables to our environment list
env = Object.assign({}, process.env, env)
}
Expand All @@ -66,7 +69,7 @@ export async function EnvCmd (
const proc = spawn(command, commandArgs, {
stdio: 'inherit',
shell: options.useShell,
env
env: env as Record<string, string>,
})

// Handle any termination signals for parent and child proceses
Expand Down
10 changes: 6 additions & 4 deletions src/expand-envs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Environment } from './types'

/**
* 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 function expandEnvs (str: string, envs: { [key: string]: any }): string {
return str.replace(/(?<!\\)\$[a-zA-Z0-9_]+/g, varName => {
export function expandEnvs(str: string, envs: Environment): string {
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()
})
}
Loading

0 comments on commit 3b67538

Please sign in to comment.