All files / src/plugins StdOutPlugin.ts

100% Statements 16/16
62.5% Branches 5/8
100% Functions 4/4
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 411x                 1x 5x     5x   5x 4x           8x 8x 8x   5x 4x 4x 4x 4x 4x           1x      
import * as _ from "lodash";
import { IArgdownPlugin, IArgdownRequest, IRequestHandler } from "@argdown/core";
export interface IStdoutSettings {
  dataKey?: string;
  isRequestData?: boolean;
}
export interface IStdoutRequest {
  stdout?: IStdoutSettings;
}
export class StdOutPlugin implements IArgdownPlugin {
  name = "StdOutPlugin";
  defaults: IStdoutSettings;
  constructor(config?: IStdoutSettings) {
    this.defaults = _.defaultsDeep({}, config, {});
  }
  prepare: IRequestHandler = request => {
    _.defaultsDeep(this.getSettings(request), this.defaults);
  };
  // there can be several instances of this plugin in the same ArgdownApplication
  // Because of this, we can not add the instance default settings to the request object as in other plugins
  // Instead we have to add it each time the getSettings method is called to avoid keeping request specific state
  getSettings(request: IArgdownRequest): IStdoutSettings {
    const r = <IStdoutRequest>request;
    r.stdout = r.stdout || {};
    return r.stdout;
  }
  run: IRequestHandler = (request, response) => {
    const settings = this.getSettings(request);
    Eif (settings.dataKey) {
      let content = !settings.isRequestData ? (<any>response)[settings.dataKey] : (<any>request)[settings.dataKey];
      Eif (content !== undefined) {
        process.stdout.write(content);
      }
    }
  };
}
 
module.exports = {
  StdOutPlugin: StdOutPlugin
};