Skip to content

Commit

Permalink
馃毃 update linter
Browse files Browse the repository at this point in the history
  • Loading branch information
dkundel committed Jul 30, 2020
1 parent 51bcdc2 commit d8bd422
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 38 deletions.
12 changes: 11 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"extends": ["@dkundel/js", "@dkundel/ts", "@dkundel/js/jest"]
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"prettier/prettier": "error",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-empty-function": "off"
},
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
]
}
2 changes: 1 addition & 1 deletion __mocks__/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type FileContentDict = Dictionary<string>;
type DirectoryListing = Dictionary<string[]>;

const fs: any = jest.genMockFromModule('fs');
const originalFs = require.requireActual('fs');
const originalFs = jest.requireActual('fs');

let mockFiles: DirectoryListing = {};
let allMockFiles: FileContentDict = {};
Expand Down
7 changes: 1 addition & 6 deletions bin/node-env-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@ const debug = Debug('node-env-run');
* IMPORTANT: By default it will exit the process when child process exits
* @param {string} cmd The command to execute
* @param {string[]} cmdArgs An array of arguments to pass to the command
* @param {boolean} shouldExit Whether the parent process should finish when child finishes
* @returns {void}
*/
function runCommand(
cmd: string,
cmdArgs: string[],
shouldExit: boolean = true
): void {
function runCommand(cmd: string, cmdArgs: string[]): void {
const shell: string | boolean = process.env.SHELL || true;

debug(`Execute command: "${cmd}"`);
Expand Down
5 changes: 2 additions & 3 deletions lib/__tests__/run-invalid-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ jest.mock('./main.js', () => {}, { virtual: true });
console.error = jest.fn();

import { resolve } from 'path';

import { getScriptToExecute, setEnvironmentVariables } from '../utils';
import { init, parseArgs } from '../cli';
import { getScriptToExecute, setEnvironmentVariables } from '../utils';

const __setScriptToExecute = require('../utils').__setScriptToExecute;
const __setMockFiles = require('fs').__setMockFiles;
Expand Down Expand Up @@ -36,7 +35,7 @@ describe('test command without necessary parameters', () => {
const cli = init(parseArgs(CMD));
expect(cli.isRepl).toBeFalsy();
if (cli.isRepl === false) {
expect(cli.error.message).toBe('Failed to determine script to execute');
expect(cli?.error?.message).toBe('Failed to determine script to execute');
}
});

Expand Down
4 changes: 1 addition & 3 deletions lib/__tests__/run-missing-env-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ jest.mock('./main.js', () => {}, { virtual: true });
// turn off error logs
console.error = jest.fn();

import { resolve } from 'path';

import { getScriptToExecute, setEnvironmentVariables } from '../utils';
import { init, parseArgs } from '../cli';
import { getScriptToExecute, setEnvironmentVariables } from '../utils';

const __setScriptToExecute = require('../utils').__setScriptToExecute;
const __setMockFiles = require('fs').__setMockFiles;
Expand Down
1 change: 0 additions & 1 deletion lib/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable import/no-absolute-path */
jest.mock('fs');
jest.mock(
'/fake/path/to/package.json',
Expand Down
4 changes: 1 addition & 3 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as Debug from 'debug';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
import * as pkginfo from 'pkginfo';
import * as yargs from 'yargs';
import {
EnvironmentDictionary,
Expand Down Expand Up @@ -32,7 +31,6 @@ export type Cli =

const debug = Debug('node-env-run');
const cwd = process.cwd();
const pkg = pkginfo(module);

const usageDescription = stripIndent`
Runs the given script with set environment variables.
Expand All @@ -49,7 +47,7 @@ const usageDescription = stripIndent`
*/
export function parseArgs(argv: string[]): CliArgs {
const result = yargs
.usage('$0 [script]', usageDescription, yargs => {
.usage('$0 [script]', usageDescription, (yargs) => {
yargs.positional('script', {
describe: 'the file that should be executed',
type: 'string',
Expand Down
21 changes: 13 additions & 8 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as Debug from 'debug';
import * as fs from 'fs';
import { resolve } from 'path';

export type EnvironmentDictionary = { [key: string]: any };
export type EnvironmentDictionary = {
[key: string]: string | undefined;
};

const debug = Debug('node-env-run');

Expand Down Expand Up @@ -46,15 +48,16 @@ export function getScriptToExecute(script: string, cwd: string): string | null {
*/
export function setEnvironmentVariables(
readValues: EnvironmentDictionary,
force: boolean = false
force = false
): void {
if (force) {
debug('Force overriding enabled');
}

const envKeysToSet = Object.keys(readValues).filter(key => {
const envKeysToSet = Object.keys(readValues).filter((key) => {
if (force && typeof readValues[key] !== 'undefined') {
if (typeof readValues[key] === 'string' && readValues[key].length === 0) {
const val = readValues[key];
if (typeof val === 'string' && val.length === 0) {
debug(`Not overriding ${key}`);
return false;
}
Expand All @@ -66,11 +69,13 @@ export function setEnvironmentVariables(
return !process.env[key];
});

envKeysToSet.forEach(key => {
envKeysToSet.forEach((key) => {
process.env[key] = readValues[key];
});

debug(`Set the env variables: ${envKeysToSet.map(k => `"${k}"`).join(',')}`);
debug(
`Set the env variables: ${envKeysToSet.map((k) => `"${k}"`).join(',')}`
);
}

/**
Expand Down Expand Up @@ -100,8 +105,8 @@ const REGEX_NOT_REGULAR_CHARACTER = new RegExp(
*
* @param args list of arguments to pass to spawn
*/
export function escapeArguments(args: string[]) {
const escapedArguments = args.map(arg => {
export function escapeArguments(args: string[]): string[] {
const escapedArguments = args.map((arg) => {
if (arg.match(REGEX_NOT_REGULAR_CHARACTER)) {
return `"${arg}"`;
}
Expand Down
17 changes: 5 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,22 @@
"test": "jest"
},
"devDependencies": {
"@dkundel/eslint-config-js": "^2.0.0",
"@dkundel/eslint-config-ts": "^2.0.0",
"@types/cross-spawn": "^6.0.2",
"@types/jest": "^26.0.7",
"@typescript-eslint/eslint-plugin": "^3.7.1",
"@typescript-eslint/parser": "^3.7.1",
"all-contributors-cli": "^6.17.0",
"eslint": "^7.5.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.1.0",
"npm-run-all": "^4.0.2",
"prettier": "^2.0.5",
"ts-jest": "^26.1.4",
"typescript": "^3.9.7"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx)$",
"moduleFileExtensions": [
"ts",
"tsx",
"json",
"js"
],
"preset": "ts-jest",
"testPathIgnorePatterns": [
"<rootDir>/dist/",
"<rootDir>/node_modules/"
Expand Down

0 comments on commit d8bd422

Please sign in to comment.