lib/configuration.js
function write(file, cb) {
  if(file === undefined) file = this._file;

  if(file === DEFAULT_FILE) {
    return this.emit(
      'error', new Error('cannot write to default configuration'));
  }

  var sync = typeof cb !== 'function'
    , stream
    , save
    , encoder = new Encoder(this.options)
    , lines = this._lines
    , size = 32
    , i = 0
    , len = (lines.length / size);


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

  if(typeof file === 'string') {
    file = resolve(file);
    save = file + Constants.SAVE;
  }

  if(sync && file && typeof file === 'string') {
    try {
      fs.writeFileSync(save, encoder.parse(this._lines));
      fs.renameSync(save, file);
    }catch(e) {
      /* istanbul ignore next: hard to mock a rename(2) error */
      return this.emit('error', e);
    }
    return encoder;
  }

  if(file instanceof Writable) {
    stream = file;
  }else{
    stream = fs.createWriteStream(save, this.options.read)
  }

  this.once('write', cb);

  function onFinish() {

    function onRename(err) {
      encoder.removeAllListeners('error');
      stream.removeAllListeners('error');
      /* istanbul ignore next: hard to mock a rename(2) error */
      if(err) return this.emit('error', err);
      this.emit('write', this);
    }

    onRename = onRename.bind(this);

    if(save) {
      fs.rename(save, file, onRename);
    }else{
      onRename();
    }

  }

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

  stream.on('finish', onFinish.bind(this));
  encoder.pipe(stream);

  if((lines.length % size) !== 0) len++;

  // not a very large config
  if(lines.length < size) {
    encoder.end(lines);
    return encoder;
  }

  // split the work for larger config files
  function writeLines() {
    var chunk = lines.slice(i * size, (i * size) + size);

    // all done
    if(!chunk.length) {
      return encoder.end();
    }

    // ensure we get correct line break between
    // line chunks, the encoder joins lines on EOL
    // this means that the last line in the chunk array
    // does not have an EOL, we need to ensure it does
    if(i > 0) {
      chunk.unshift('');
    }

    // write out the line chunk
    encoder.write(chunk);
    setImmediate(writeLines);
    i++;
  }

  writeLines();
  return encoder;
}
function write(file, cb) {
  if(file === undefined) file = this._file;

  if(file === DEFAULT_FILE) {
    return this.emit(
      'error', new Error('cannot write to default configuration'));
  }

  var sync = typeof cb !== 'function'
    , stream
    , save
    , encoder = new Encoder(this.options)
    , lines = this._lines
    , size = 32
    , i = 0
    , len = (lines.length / size);


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

  if(typeof file === 'string') {
    file = resolve(file);
    save = file + Constants.SAVE;
  }

  if(sync && file && typeof file === 'string') {
    try {
      fs.writeFileSync(save, encoder.parse(this._lines));
      fs.renameSync(save, file);
    }catch(e) {
      /* istanbul ignore next: hard to mock a rename(2) error */
      return this.emit('error', e);
    }
    return encoder;
  }

  if(file instanceof Writable) {
    stream = file;
  }else{
    stream = fs.createWriteStream(save, this.options.read)
  }

  this.once('write', cb);

  function onFinish() {

    function onRename(err) {
      encoder.removeAllListeners('error');
      stream.removeAllListeners('error');
      /* istanbul ignore next: hard to mock a rename(2) error */
      if(err) return this.emit('error', err);
      this.emit('write', this);
    }

    onRename = onRename.bind(this);

    if(save) {
      fs.rename(save, file, onRename);
    }else{
      onRename();
    }

  }

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

  stream.on('finish', onFinish.bind(this));
  encoder.pipe(stream);

  if((lines.length % size) !== 0) len++;

  // not a very large config
  if(lines.length < size) {
    encoder.end(lines);
    return encoder;
  }

  // split the work for larger config files
  function writeLines() {
    var chunk = lines.slice(i * size, (i * size) + size);

    // all done
    if(!chunk.length) {
      return encoder.end();
    }

    // ensure we get correct line break between
    // line chunks, the encoder joins lines on EOL
    // this means that the last line in the chunk array
    // does not have an EOL, we need to ensure it does
    if(i > 0) {
      chunk.unshift('');
    }

    // write out the line chunk
    encoder.write(chunk);
    setImmediate(writeLines);
    i++;
  }

  writeLines();
  return encoder;
}
function write ( file , cb ) { if ( file === undefined ) file = this . _file ; if ( file === DEFAULT_FILE ) { return this . emit ( 'error' , new Error ( 'cannot write to default configuration' ) ) ; } var sync = typeof cb !== 'function' , stream , save , encoder = new Encoder ( this . options ) , lines = this . _lines , size = 32 , i = 0 , len = ( lines . length / size ) ; if ( ! file || typeof file !== 'string' && ! ( file instanceof Writable ) ) { return this . emit ( 'error' , new Error ( 'cannot write configuration, path or stream expected' ) ) ; } if ( typeof file === 'string' ) { file = resolve ( file ) ; save = file + Constants . SAVE ; } if ( sync && file && typeof file === 'string' ) { try { fs . writeFileSync ( save , encoder . parse ( this . _lines ) ) ; fs . renameSync ( save , file ) ; } catch ( e ) { /* istanbul ignore next: hard to mock a rename(2) error */ return this . emit ( 'error' , e ) ; } return encoder ; } if ( file instanceof Writable ) { stream = file ; } else { stream = fs . createWriteStream ( save , this . options . read ) } this . once ( 'write' , cb ) ; function onFinish ( ) { function onRename ( err ) { encoder . removeAllListeners ( 'error' ) ; stream . removeAllListeners ( 'error' ) ; /* istanbul ignore next: hard to mock a rename(2) error */ if ( err ) return this . emit ( 'error' , err ) ; this . emit ( 'write' , this ) ; } onRename = onRename . bind ( this ) ; if ( save ) { fs . rename ( save , file , onRename ) ; } else { onRename ( ) ; } } var onError = this . onError . bind ( this ) ; encoder . on ( 'error' , onError ) ; stream . on ( 'error' , onError ) ; stream . on ( 'finish' , onFinish . bind ( this ) ) ; encoder . pipe ( stream ) ; if ( ( lines . length % size ) !== 0 ) len ++ ; // not a very large config if ( lines . length < size ) { encoder . end ( lines ) ; return encoder ; } // split the work for larger config files function writeLines ( ) { var chunk = lines . slice ( i * size , ( i * size ) + size ) ; // all done if ( ! chunk . length ) { return encoder . end ( ) ; } // ensure we get correct line break between // line chunks, the encoder joins lines on EOL // this means that the last line in the chunk array // does not have an EOL, we need to ensure it does if ( i > 0 ) { chunk . unshift ( '' ) ; } // write out the line chunk encoder . write ( chunk ) ; setImmediate ( writeLines ) ; i ++ ; } writeLines ( ) ; return encoder ; }
Write the in-memory configuration to a file or stream.
Write the in - memory configuration to a file or stream .