From 395ebbf6049aa935a2996578a8e11f34b6a34288 Mon Sep 17 00:00:00 2001 From: Gustavo Perdomo Date: Sat, 14 Oct 2023 13:28:08 -0300 Subject: [PATCH] feat(core): add support for trimWhitespace to getMultilineInput fn --- packages/core/src/lib/get-input-list.spec.ts | 4 +-- packages/core/src/lib/get-input.spec.ts | 29 ++++++++++++++------ packages/core/src/lib/get-input.ts | 13 +++++++-- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/packages/core/src/lib/get-input-list.spec.ts b/packages/core/src/lib/get-input-list.spec.ts index 8028f544..1cf3a4b3 100644 --- a/packages/core/src/lib/get-input-list.spec.ts +++ b/packages/core/src/lib/get-input-list.spec.ts @@ -1,5 +1,5 @@ -import * as fs from 'fs'; -import * as path from 'path'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; import { getPosixName } from './get-input'; import { getInputList } from './get-input-list'; diff --git a/packages/core/src/lib/get-input.spec.ts b/packages/core/src/lib/get-input.spec.ts index 5c0cb76c..8553c9f7 100644 --- a/packages/core/src/lib/get-input.spec.ts +++ b/packages/core/src/lib/get-input.spec.ts @@ -27,15 +27,8 @@ const testEnvVars = { INPUT_BOOLEAN_INPUT_FALSE3: 'FALSE', INPUT_WRONG_BOOLEAN_INPUT: 'wrong', INPUT_WITH_TRAILING_WHITESPACE: ' some val ', - INPUT_MY_INPUT_LIST: 'val1\nval2\nval3', - - // Save inputs - STATE_TEST_1: 'state_val', - - // File Commands - GITHUB_PATH: '', - GITHUB_ENV: '', + INPUT_LIST_WITH_TRAILING_WHITESPACE: ' val1 \n val2 \n ', }; describe('getPosixName', () => { @@ -176,5 +169,25 @@ describe('getInputs', () => { it('getMultilineInput works', () => { expect(getMultilineInput('my input list')).toEqual(['val1', 'val2', 'val3']); }); + + it('getMultilineInput trims whitespace by default', () => { + expect(getMultilineInput('list with trailing whitespace')).toEqual(['val1', 'val2']); + }); + + it('getMultilineInput trims whitespace when option is explicitly true', () => { + expect( + getMultilineInput('list with trailing whitespace', { + trimWhitespace: true, + }) + ).toEqual(['val1', 'val2']); + }); + + it('getMultilineInput does not trim whitespace when option is false', () => { + expect( + getMultilineInput('list with trailing whitespace', { + trimWhitespace: false, + }) + ).toEqual([' val1 ', ' val2 ', ' ']); + }); }); }); diff --git a/packages/core/src/lib/get-input.ts b/packages/core/src/lib/get-input.ts index 2b058601..bf38cfd7 100644 --- a/packages/core/src/lib/get-input.ts +++ b/packages/core/src/lib/get-input.ts @@ -6,10 +6,13 @@ import { names } from '@nx/devkit'; export interface InputOptions { /** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */ required?: boolean; + /** Optional. Whether leading/trailing whitespace will be trimmed for the input. Defaults to true */ trimWhitespace?: boolean; + /** Optional. Default value */ fallback?: string; + /** Optional. Default value */ prefix?: string; } @@ -41,11 +44,11 @@ export function getInput(name: string, options?: InputOptions): string { val = options.fallback; } - if (options && options.required && !val) { + if (options?.required && !val) { throw new Error(`Input required and not supplied: ${name}`); } - if (options && options.trimWhitespace === false) { + if (options?.trimWhitespace === false) { return val; } @@ -65,7 +68,11 @@ export function getMultilineInput(name: string, options?: InputOptions): string[ .split('\n') .filter((x) => x !== ''); - return inputs; + if (options?.trimWhitespace === false) { + return inputs; + } + + return inputs.map((input) => input.trim()); } /**