Skip to content

Commit

Permalink
fix(common): fix bug introduced by cab080a
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jul 17, 2019
1 parent 5bf23e0 commit 7301ed5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
28 changes: 17 additions & 11 deletions packages/cli/src/commands/transform.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import {_, createDebugPipe, error, observable} from '@report-toolkit/common';
import {FORMAT_CSV, FORMAT_JSON, FORMAT_PIPE} from '@report-toolkit/formatters';
import {toObjectFromFilepath} from '@report-toolkit/fs';
import {constants, transformers} from '@report-toolkit/transformers';
import {
constants as transformerNames,
transformers
} from '@report-toolkit/transformers';
import {writeFileSync} from 'fs';

import {colors, toFormattedString} from '../console-utils.js';
import {FORMAT_TABLE} from '../table-formatter.js';
import {GROUPS, OPTIONS} from './common.js';
import {fromFilepathToReport, GROUPS, OPTIONS} from './common.js';

const {iif, throwRTkError} = observable;

const {fromAny, iif, throwRTkError} = observable;
const {REPORT_TOOLKIT_ERR_INVALID_CLI_OPTION} = error;

const debug = createDebugPipe('cli', 'commands', 'transform');
const {TRANSFORMER_NUMERIC} = constants;
const ALLOWED_FORMATS = [FORMAT_CSV, FORMAT_JSON, FORMAT_TABLE, FORMAT_PIPE];

export const command = 'transform <transformer> <file..>';
Expand Down Expand Up @@ -43,15 +45,14 @@ export const builder = yargs =>
type: 'array'
})
.positional('transform', {
choices: [TRANSFORMER_NUMERIC]
choices: Object.keys(transformerNames)
});

export const handler = argv => {
const {
file: filepaths,
transformer,
config,
field,
truncate: truncateValues = true,
wrap: wrapValues = false,
format = FORMAT_JSON,
Expand All @@ -63,10 +64,15 @@ export const handler = argv => {
const transform = transformers[transformer];
iif(
() => transform,
fromAny(filepaths).pipe(
toObjectFromFilepath({...config.transform, showSecretsUnsafe}),
debug(transformer => `using transformer "${transformer}"`),
transform({...config.transform, field}),
fromFilepathToReport(filepaths, {
...config.transform,
showSecretsUnsafe
}).pipe(
debug(() => `using transformer "${transformer}"`),
transform({
...config.transform,
..._.getOr({}, `transform.${transformer}`, config)
}),
toFormattedString(format, {
color,
fields: [
Expand Down
26 changes: 5 additions & 21 deletions packages/common/src/observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,22 @@ import {
toArray
} from 'rxjs/operators/index.js';

import {RTkError} from './error.js';
import {createRTkError} from './error.js';
import {_} from './util.js';

/**
* Pipes source Observable to one or more Operators if the predicate is truthy.
* If falsy, just returns the source Observable.
* @param {Function|any} predicate - If a function, evaluated with the value from the source Observable. Anything else is evaluated when called.
* @param {import('rxjs').OperatorFunction<any,any>} operator - One or more RxJS operators to pipe to
* @param {...import('rxjs').OperatorFunction<any,any>} operators - More RxJS operators to pipe to
* @returns {import('rxjs').OperatorFunction<any,any>}
*/
export const pipeIf = (predicate, operator, ...operators) => {
export const pipeIf = (predicate, ...operators) => {
predicate = _.isFunction(predicate) ? predicate : _.constant(predicate);
return observable =>
observable.pipe(
mergeMap(v =>
predicate(v)
? of(v).pipe(
// this can't be how it is, right?
operator,
operators[0],
operators[1],
operators[2],
operators[3],
operators[4],
operators[5],
operators[6],
operators[7],
operators[8]
)
: of(v)
)
// @ts-ignore
mergeMap(v => (predicate(v) ? of(v).pipe(...operators) : of(v)))
);
};

Expand Down Expand Up @@ -118,7 +102,7 @@ export const fromAny = value =>
* @returns {import('rxjs').Observable} An Observable emitting a single error
*/
export const throwRTkError = (code, message, opts = {}) =>
throwError(RTkError.create(code, message, opts));
throwError(createRTkError(code, message, opts));

/**
* A simple operator that parses a JSON string into the resulting JS representation.
Expand Down

0 comments on commit 7301ed5

Please sign in to comment.