lib/configuration.js
function load(opts, cb) {
  if(typeof opts === 'function') {
    cb = opts;
    opts = undefined;
  }

  if(typeof opts === 'string') {
    file = opts;
  }else if(typeof opts === 'object') {
    file = opts.file;
  }

  if(file === undefined) file = DEFAULT_FILE;

  var sync = typeof cb !== 'function'
    , stream
    , file
    , reader
    , decoder = new Decoder(this, this.options, file);

  if(!file || typeof file !== 'string' && !(file instanceof Readable)){
    return this.emit('error',
      new Error('cannot load configuration, path or stream expected'));
  }

  // need absolute path to be able to
  // correctly find circular references
  if(typeof file === 'string') {
    file = resolve(file);
  }

  this._file = file;

  if(sync && file && typeof file === 'string') {
    this._lines = (fs.readFileSync(file) + '').split(this.options.eol);
    decoder.parse(this._lines);
    if(opts && opts.directives) {
      this.directives(opts.directives);
    }
    return decoder;
  }

  if(file instanceof Readable) {
    stream = file;
  }else{
    /* istanbul ignore next: cannot mock stdin, readable only */
    stream = opts && opts.stdin ? process.stdin
      : fs.createReadStream(file, this.options.read)
  }

  reader = new LineReader(this.options);

  this.once('load', cb);

  function onEnd() {

    if(opts && opts.directives) {
      this.directives(opts.directives);
    }

    decoder.removeAllListeners('error');
    reader.removeAllListeners('error');
    stream.removeAllListeners('error');

    //console.dir(opts);
    //
    this.onLoad();

    this.emit('load', this);
  }

  function onLines(lines) {
    this._lines = this._lines.concat(lines);
  }

  var onError = this.onError.bind(this);
  decoder.on('error', onError);
  stream.on('error', onError);
  reader.on('error', onError);

  // listen for includes
  this.on('include', this.onInclude.bind(this))

  // listen for lines on the primary configuration.
  decoder.on('lines', onLines.bind(this));

  stream.on('end', onEnd.bind(this));
  stream.pipe(reader).pipe(decoder);
  return reader;
}
function load(opts, cb) {
  if(typeof opts === 'function') {
    cb = opts;
    opts = undefined;
  }

  if(typeof opts === 'string') {
    file = opts;
  }else if(typeof opts === 'object') {
    file = opts.file;
  }

  if(file === undefined) file = DEFAULT_FILE;

  var sync = typeof cb !== 'function'
    , stream
    , file
    , reader
    , decoder = new Decoder(this, this.options, file);

  if(!file || typeof file !== 'string' && !(file instanceof Readable)){
    return this.emit('error',
      new Error('cannot load configuration, path or stream expected'));
  }

  // need absolute path to be able to
  // correctly find circular references
  if(typeof file === 'string') {
    file = resolve(file);
  }

  this._file = file;

  if(sync && file && typeof file === 'string') {
    this._lines = (fs.readFileSync(file) + '').split(this.options.eol);
    decoder.parse(this._lines);
    if(opts && opts.directives) {
      this.directives(opts.directives);
    }
    return decoder;
  }

  if(file instanceof Readable) {
    stream = file;
  }else{
    /* istanbul ignore next: cannot mock stdin, readable only */
    stream = opts && opts.stdin ? process.stdin
      : fs.createReadStream(file, this.options.read)
  }

  reader = new LineReader(this.options);

  this.once('load', cb);

  function onEnd() {

    if(opts && opts.directives) {
      this.directives(opts.directives);
    }

    decoder.removeAllListeners('error');
    reader.removeAllListeners('error');
    stream.removeAllListeners('error');

    //console.dir(opts);
    //
    this.onLoad();

    this.emit('load', this);
  }

  function onLines(lines) {
    this._lines = this._lines.concat(lines);
  }

  var onError = this.onError.bind(this);
  decoder.on('error', onError);
  stream.on('error', onError);
  reader.on('error', onError);

  // listen for includes
  this.on('include', this.onInclude.bind(this))

  // listen for lines on the primary configuration.
  decoder.on('lines', onLines.bind(this));

  stream.on('end', onEnd.bind(this));
  stream.pipe(reader).pipe(decoder);
  return reader;
}
function load ( opts , cb ) { if ( typeof opts === 'function' ) { cb = opts ; opts = undefined ; } if ( typeof opts === 'string' ) { file = opts ; } else if ( typeof opts === 'object' ) { file = opts . file ; } if ( file === undefined ) file = DEFAULT_FILE ; var sync = typeof cb !== 'function' , stream , file , reader , decoder = new Decoder ( this , this . options , file ) ; if ( ! file || typeof file !== 'string' && ! ( file instanceof Readable ) ) { return this . emit ( 'error' , new Error ( 'cannot load configuration, path or stream expected' ) ) ; } // need absolute path to be able to // correctly find circular references if ( typeof file === 'string' ) { file = resolve ( file ) ; } this . _file = file ; if ( sync && file && typeof file === 'string' ) { this . _lines = ( fs . readFileSync ( file ) + '' ) . split ( this . options . eol ) ; decoder . parse ( this . _lines ) ; if ( opts && opts . directives ) { this . directives ( opts . directives ) ; } return decoder ; } if ( file instanceof Readable ) { stream = file ; } else { /* istanbul ignore next: cannot mock stdin, readable only */ stream = opts && opts . stdin ? process . stdin : fs . createReadStream ( file , this . options . read ) } reader = new LineReader ( this . options ) ; this . once ( 'load' , cb ) ; function onEnd ( ) { if ( opts && opts . directives ) { this . directives ( opts . directives ) ; } decoder . removeAllListeners ( 'error' ) ; reader . removeAllListeners ( 'error' ) ; stream . removeAllListeners ( 'error' ) ; //console.dir(opts); // this . onLoad ( ) ; this . emit ( 'load' , this ) ; } function onLines ( lines ) { this . _lines = this . _lines . concat ( lines ) ; } var onError = this . onError . bind ( this ) ; decoder . on ( 'error' , onError ) ; stream . on ( 'error' , onError ) ; reader . on ( 'error' , onError ) ; // listen for includes this . on ( 'include' , this . onInclude . bind ( this ) ) // listen for lines on the primary configuration. decoder . on ( 'lines' , onLines . bind ( this ) ) ; stream . on ( 'end' , onEnd . bind ( this ) ) ; stream . pipe ( reader ) . pipe ( decoder ) ; return reader ; }
Read a file and pipe it to a decoder.
Read a file and pipe it to a decoder .