app/overwrite.js
function overwriteFile(path, options) {
  let fileContent = null;
  let newData = null;

  try {
    fileContent = fs.readFileSync(path, 'utf8');
    newData = visit(JSON.parse(fileContent), options);
  } catch (e) {
    console.error('Failed to retrieve json object from file');
    throw e;
  }

  let indent;

  if (options && options.indentSize) {
    indent = options.indentSize;
  } else {
    indent = detectIndent(fileContent).indent || DEFAULT_INDENT_SIZE;
  }

  const newLine = detectNewline(fileContent) || '\n';
  let newFileContent = JSON.stringify(newData, null, indent);

  if (!(options && options.noFinalNewLine)) {
    // Append a new line at EOF
    newFileContent += '\n';
  }

  if (newLine !== '\n') {
    newFileContent = newFileContent.replace(/\n/g, newLine);
  }

  fs.writeFileSync(path, newFileContent, 'utf8');
  return newData;
}
function overwriteFile(path, options) {
  let fileContent = null;
  let newData = null;

  try {
    fileContent = fs.readFileSync(path, 'utf8');
    newData = visit(JSON.parse(fileContent), options);
  } catch (e) {
    console.error('Failed to retrieve json object from file');
    throw e;
  }

  let indent;

  if (options && options.indentSize) {
    indent = options.indentSize;
  } else {
    indent = detectIndent(fileContent).indent || DEFAULT_INDENT_SIZE;
  }

  const newLine = detectNewline(fileContent) || '\n';
  let newFileContent = JSON.stringify(newData, null, indent);

  if (!(options && options.noFinalNewLine)) {
    // Append a new line at EOF
    newFileContent += '\n';
  }

  if (newLine !== '\n') {
    newFileContent = newFileContent.replace(/\n/g, newLine);
  }

  fs.writeFileSync(path, newFileContent, 'utf8');
  return newData;
}
function overwriteFile ( path , options ) { let fileContent = null ; let newData = null ; try { fileContent = fs . readFileSync ( path , 'utf8' ) ; newData = visit ( JSON . parse ( fileContent ) , options ) ; } catch ( e ) { console . error ( 'Failed to retrieve json object from file' ) ; throw e ; } let indent ; if ( options && options . indentSize ) { indent = options . indentSize ; } else { indent = detectIndent ( fileContent ) . indent || DEFAULT_INDENT_SIZE ; } const newLine = detectNewline ( fileContent ) || '\n' ; let newFileContent = JSON . stringify ( newData , null , indent ) ; if ( ! ( options && options . noFinalNewLine ) ) { // Append a new line at EOF newFileContent += '\n' ; } if ( newLine !== '\n' ) { newFileContent = newFileContent . replace ( / \n / g , newLine ) ; } fs . writeFileSync ( path , newFileContent , 'utf8' ) ; return newData ; }
Overwrite file with sorted json
@param {String} path                  - absolutePath
@param {Object} [options = {}]        - optional params
@returns {*}
Overwrite file with sorted json