Skip to content

Commit

Permalink
feat(core): add support for trimWhitespace to getMultilineInput fn
Browse files Browse the repository at this point in the history
  • Loading branch information
gperdomor committed Oct 14, 2023
1 parent a946253 commit 395ebbf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/lib/get-input-list.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
29 changes: 21 additions & 8 deletions packages/core/src/lib/get-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 ', ' ']);
});
});
});
13 changes: 10 additions & 3 deletions packages/core/src/lib/get-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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());
}

/**
Expand Down

0 comments on commit 395ebbf

Please sign in to comment.