Skip to content

Commit

Permalink
chore(inspector): fold rules pkg into inspector pkg; closes #43
Browse files Browse the repository at this point in the history
removes a duplicate function, moved some stuff into inspector and out of
the core observable API. and export builtin rules OperatorFunction from
core observable API.

normalized paths in `jsconfig.json`
  • Loading branch information
boneskull committed Aug 3, 2019
1 parent 9e86a04 commit 86b3da0
Show file tree
Hide file tree
Showing 23 changed files with 143 additions and 229 deletions.
20 changes: 9 additions & 11 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
"target": "ES2015",
"baseUrl": ".",
"paths": {
"@report-toolkit/cli": ["./packages/cli/src/index.js"],
"@report-toolkit/common": ["./packages/common/src/index.js"],
"@report-toolkit/config": ["./packages/config/src/index.js"],
"@report-toolkit/core": ["./packages/core/src/index.js"],
"@report-toolkit/diff": ["./packages/diff/src/index.js"],
"@report-toolkit/fs": ["./packages/fs/src/index.js"],
"report-toolkit": ["./packages/report-toolkit/src/index.js"],
"@report-toolkit/inspector": ["./packages/inspector/src/index.js"],
"@report-toolkit/report": ["./packages/report/src/index.js"],
"@report-toolkit/rules": ["./packages/rules/src/index.js"],
"@report-toolkit/transformers": ["./packages/transformers/src/index.js"]
"@report-toolkit/cli": ["packages/cli/src/index.js"],
"@report-toolkit/common": ["packages/common/src/index.js"],
"@report-toolkit/config": ["packages/config/src/index.js"],
"@report-toolkit/core": ["packages/core/src/index.js"],
"@report-toolkit/diff": ["packages/diff/src/index.js"],
"@report-toolkit/fs": ["packages/fs/src/index.js"],
"report-toolkit": ["packages/report-toolkit/src/index.js"],
"@report-toolkit/inspector": ["packages/inspector/src/index.js"],
"@report-toolkit/transformers": ["packages/transformers/src/index.js"]
},
"allowSyntheticDefaultImports": true,
"checkJs": true,
Expand Down
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"@report-toolkit/core": "^0.0.0",
"@report-toolkit/fs": "^0.0.0",
"@report-toolkit/inspector": "^0.0.0",
"@report-toolkit/rules": "^0.0.0",
"cli-table3": "^0.5.1",
"log-symbols": "^3.0.0",
"strip-ansi": "^5.2.0",
Expand Down
17 changes: 10 additions & 7 deletions packages/cli/src/commands/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
observable
} from '@report-toolkit/common';
import {stream} from '@report-toolkit/core';
import {rules} from '@report-toolkit/rules';

import {fail, ok, toOutput} from '../console-utils.js';
import {
Expand All @@ -16,8 +15,14 @@ import {
} from './common.js';

const {ERROR, INFO, WARNING} = constants;
const {toInspection, toRuleConfig, transform, fromTransformerChain} = stream;
const {share, from} = observable;
const {
fromBuiltinRules,
inspectReports,
toRuleConfig,
transform,
fromTransformerChain
} = stream;
const {share} = observable;
const debug = createDebugPipe('cli', 'commands', 'inspect');

export const command = 'inspect <file..>';
Expand Down Expand Up @@ -79,12 +84,10 @@ export const handler = argv => {

const config = commandConfig('inspect', argv, DEFAULT_INSPECT_CONFIG);

const source = from(
_.map(([id, ruleDef]) => ({id, ruleDef}), _.toPairs(rules))
).pipe(
const source = fromBuiltinRules().pipe(
debug(rule => [`loading rule: %O`, rule]),
toRuleConfig(config),
toInspection(fromFilepathToReport(file, config), {severity}),
inspectReports(fromFilepathToReport(file, config), {severity}),
share()
);

Expand Down
17 changes: 13 additions & 4 deletions packages/cli/src/commands/list-rules.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {_, observable} from '@report-toolkit/common';
import {stream} from '@report-toolkit/core';
import {ruleDefs} from '@report-toolkit/rules';

import {toOutput} from '../console-utils.js';
import {commandConfig, getOptions, OPTIONS} from './common.js';

const {from, share} = observable;
const {transform, fromTransformerChain} = stream;
const {map, share} = observable;
const {transform, fromTransformerChain, fromBuiltinRules} = stream;

const DEFAULT_LIST_RULES_CONFIG = {
fields: [
Expand Down Expand Up @@ -35,7 +34,17 @@ export const builder = yargs =>
});

export const handler = argv => {
const source = from(ruleDefs).pipe(share());
const source = fromBuiltinRules().pipe(
map(({id, ruleDefinition}) => ({
id,
description: _.getOr(
'(no description)',
'meta.docs.description',
ruleDefinition
)
})),
share()
);
fromTransformerChain(
argv.transform,
commandConfig('list-rules', argv, DEFAULT_LIST_RULES_CONFIG)
Expand Down
1 change: 0 additions & 1 deletion packages/common/src/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export const kFlattenedConfig = Symbol('report-toolkit-flattened-config');
export const kRedacted = Symbol('report-toolkit-redacted');
export const kReport = Symbol('report-toolkit-report');
export const kReportFilepath = Symbol('report-toolkit-report-filepath');
export const kRuleFilepath = Symbol('report-toolkit-rule-filepath');
export const kRuleId = Symbol('report-toolkit-rule-id');
export const kRuleInspect = Symbol('report-toolkit-rule-inspect');
export const kRuleMeta = Symbol('report-toolkit-rule-meta');
86 changes: 18 additions & 68 deletions packages/core/src/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,119 +2,67 @@ import {
_,
constants,
createDebugPipe,
createReport,
error,
isReport,
observable,
redact
observable
} from '@report-toolkit/common';
import {diffReports} from '@report-toolkit/diff';
import {
createRule,
createRuleConfig,
inspectReports
fromBuiltinRules,
inspectReports,
toReportFromObject
} from '@report-toolkit/inspector';
import {
runTransformer,
toTransformer,
validateTransformerChain
} from '@report-toolkit/transformers';
const {ERROR} = constants;

const {DEFAULT_DIFF_OPTIONS, DEFAULT_LOAD_REPORT_OPTIONS} = constants;

const {RTKERR_INVALID_PARAMETER} = error;
const {
defer,
filter,
from,
fromAny,
isObservable,
map,
mergeMap,
of,
pipeIf,
sort,
switchMapTo,
take,
throwRTkError,
toArray,
toObjectFromJSON
} = observable;

const debug = createDebugPipe('core', 'stream');

export const toReportDiff = (opts = {}) => {
const {properties, showSecretsUnsafe} = _.defaults(
DEFAULT_DIFF_OPTIONS,
opts
);
return reports =>
reports.pipe(
take(2),
pipeIf(
value => !isReport(value),
mergeMap(report => reportFrom(report, {showSecretsUnsafe}))
mergeMap(report => reportFrom(report, opts))
),
toArray(),
diffReports({properties})
diffReports(opts)
);
};

export const diff = (reports, opts = {}) => {
const {properties, showSecretsUnsafe} = _.defaults(
DEFAULT_DIFF_OPTIONS,
opts
);
return fromAny(reports).pipe(
export const diff = (reports, opts = {}) =>
fromAny(reports).pipe(
take(2),
toReport({showSecretsUnsafe}),
toReport(opts),
toArray(),
toReportDiff({properties, showSecretsUnsafe})
toReportDiff(opts)
);
};

export const toInspection = (reports, opts = DEFAULT_LOAD_REPORT_OPTIONS) => {
const {severity} = opts;

return isObservable(reports)
? ruleConfigs => ruleConfigs.pipe(inspectReports(reports, {severity}))
: ruleConfigs =>
ruleConfigs.pipe(
switchMapTo(
throwRTkError(
RTKERR_INVALID_PARAMETER,
'Parameter to toInspection() must be of type Observable<Report>'
)
)
);
};

export const inspect = (reports, rules, config, {severity} = {}) =>
export const inspect = (reports, rules, config, {severity = ERROR} = {}) =>
fromAny(rules).pipe(
map(createRuleConfig(config)),
toInspection(fromAny(reports).pipe(toReport), {severity})
inspectReports(fromAny(reports).pipe(toReport), {severity})
);

export const toReportFromObject = (opts = {}) => {
const {disableSort, showSecretsUnsafe, sortDirection, sortField} = _.defaults(
DEFAULT_LOAD_REPORT_OPTIONS,
opts
);
return observable =>
observable.pipe(
pipeIf(
!showSecretsUnsafe,
map(obj =>
obj.rawReport
? {...obj, rawReport: redact(obj.rawReport)}
: {rawReport: redact(obj)}
)
),
pipeIf(!disableSort, sort(`rawReport.${sortField}`, sortDirection)),
map(({filepath, rawReport}) => createReport(rawReport, filepath))
);
};

export const reportFrom = (value, opts = {}) =>
defer(() =>
(_.isString(value) ? reportFromJSON : reportFromObject)(value, opts)
Expand All @@ -138,13 +86,12 @@ export const toRuleConfig = (config = {}) => {
ruleDefs.pipe(
pipeIf(!ruleIdsCount, debug(() => 'whitelisting rules by default')),
pipeIf(ruleIdsCount, filter(({id}) => Boolean(_.get(id, config.rules)))),
map(({filepath, id, ruleDef}) =>
createRule(ruleDef, {filepath, id}).toRuleConfig(config)
map(({id, ruleDefinition}) =>
createRule({...ruleDefinition, id}).toRuleConfig(config)
)
);
};

export {createRule};
export {parseConfig} from '@report-toolkit/config';

export const fromTransformerChain = (transformerIds, config = {}) =>
Expand All @@ -168,6 +115,9 @@ export const transform = (source, options = {}) => observable =>
runTransformer(source)
);

export {fromBuiltinRules};
export {toReportFromObject, inspectReports};

/**
* @template T
* @typedef {import('@report-toolkit/common/src/observable').Observable} Observable
Expand Down
44 changes: 2 additions & 42 deletions packages/core/test/stream.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {ERROR} from '@report-toolkit/common/src/constants.js';
import {RTKERR_INVALID_PARAMETER} from '@report-toolkit/common/src/error.js';
import {of} from '@report-toolkit/common/src/observable.js';
// @ts-ignore
import REPORT_001 from '@report-toolkit/common/test/fixture/reports/report-001.json';
// @ts-ignore
import REPORT_002 from '@report-toolkit/common/test/fixture/reports/report-002-library-mismatch.json';

const REPORT_001_FILEPATH = require.resolve(
Expand Down Expand Up @@ -46,47 +47,6 @@ describe('@report-toolkit/core:stream', function() {
});

describe('function', function() {
describe('toInspection()', function() {
let toInspection;

beforeEach(function() {
toInspection = subject.toInspection;
});

describe('when called without an Observable of reports', function() {
it('should emit an error', function() {
return expect(
of({}).pipe(toInspection()),
'to emit error satisfying',
{
code: RTKERR_INVALID_PARAMETER
}
);
});
});

describe('when called with an Observable of Reports', function() {
it('should delegate to @report-toolkit/inspector.inspectReports()', function() {
return expect(
of({}).pipe(toInspection(of(REPORT_001))),
'to complete with value',
msg001
);
});
});

describe('when called with two (2) report files', function() {
it('should emit a message for each enabled rule', function() {
return expect(
of({}).pipe(toInspection(of(REPORT_001, REPORT_002))),
'to complete with values',
msg001,
msg002
);
});
});
});

describe('toReportDiff()', function() {
let toReportDiff;

Expand Down
11 changes: 6 additions & 5 deletions packages/fs/src/fs-rule-loader.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {observable} from '@report-toolkit/common';
import {createDebugPipe, observable} from '@report-toolkit/common';
import {readdir as readdirNodeback} from 'fs';
import {basename, extname, join} from 'path';

const debug = createDebugPipe('fs', 'rule-loader');
const {bindNodeCallback, filter, fromAny, map, mergeAll} = observable;

const readdir = bindNodeCallback(readdirNodeback);

const toRuleDefinitionFromFilepath = (extension = '.js') => observable =>
observable.pipe(
map(filepath => ({
filepath,
id: basename(filepath, extension),
ruleDef: require(filepath)
}))
...require(filepath),
id: basename(filepath, extension)
})),
debug(result => [`loaded rule from fs: %O`, result])
);

/**
Expand Down
Loading

0 comments on commit 86b3da0

Please sign in to comment.