From d8bd422b2b0d8df87d9e2f8932c892ab24e6d463 Mon Sep 17 00:00:00 2001 From: Dominik Kundel Date: Wed, 29 Jul 2020 18:01:40 -0700 Subject: [PATCH] :rotating_light: update linter --- .eslintrc | 12 +++++++++++- __mocks__/fs.ts | 2 +- bin/node-env-run.ts | 7 +------ lib/__tests__/run-invalid-script.ts | 5 ++--- lib/__tests__/run-missing-env-file.ts | 4 +--- lib/__tests__/utils.ts | 1 - lib/cli.ts | 4 +--- lib/utils.ts | 21 +++++++++++++-------- package.json | 17 +++++------------ 9 files changed, 35 insertions(+), 38 deletions(-) diff --git a/.eslintrc b/.eslintrc index 8729a04..a087e17 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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" + ] } diff --git a/__mocks__/fs.ts b/__mocks__/fs.ts index b800a7a..56aa13d 100644 --- a/__mocks__/fs.ts +++ b/__mocks__/fs.ts @@ -5,7 +5,7 @@ type FileContentDict = Dictionary; type DirectoryListing = Dictionary; const fs: any = jest.genMockFromModule('fs'); -const originalFs = require.requireActual('fs'); +const originalFs = jest.requireActual('fs'); let mockFiles: DirectoryListing = {}; let allMockFiles: FileContentDict = {}; diff --git a/bin/node-env-run.ts b/bin/node-env-run.ts index f18f4a4..4caa6e9 100644 --- a/bin/node-env-run.ts +++ b/bin/node-env-run.ts @@ -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}"`); diff --git a/lib/__tests__/run-invalid-script.ts b/lib/__tests__/run-invalid-script.ts index be8ecca..a258442 100644 --- a/lib/__tests__/run-invalid-script.ts +++ b/lib/__tests__/run-invalid-script.ts @@ -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; @@ -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'); } }); diff --git a/lib/__tests__/run-missing-env-file.ts b/lib/__tests__/run-missing-env-file.ts index db0d15b..55c864f 100644 --- a/lib/__tests__/run-missing-env-file.ts +++ b/lib/__tests__/run-missing-env-file.ts @@ -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; diff --git a/lib/__tests__/utils.ts b/lib/__tests__/utils.ts index f5cd0e1..570a619 100644 --- a/lib/__tests__/utils.ts +++ b/lib/__tests__/utils.ts @@ -1,4 +1,3 @@ -/* eslint-disable import/no-absolute-path */ jest.mock('fs'); jest.mock( '/fake/path/to/package.json', diff --git a/lib/cli.ts b/lib/cli.ts index 45491c2..d4c6545 100644 --- a/lib/cli.ts +++ b/lib/cli.ts @@ -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, @@ -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. @@ -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', diff --git a/lib/utils.ts b/lib/utils.ts index 9ff85fe..5bdff7f 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -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'); @@ -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; } @@ -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(',')}` + ); } /** @@ -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}"`; } diff --git a/package.json b/package.json index 5d85ff3..ae16515 100644 --- a/package.json +++ b/package.json @@ -46,12 +46,14 @@ "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", @@ -59,16 +61,7 @@ "typescript": "^3.9.7" }, "jest": { - "transform": { - "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js" - }, - "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx)$", - "moduleFileExtensions": [ - "ts", - "tsx", - "json", - "js" - ], + "preset": "ts-jest", "testPathIgnorePatterns": [ "/dist/", "/node_modules/"