<commit_before>var fs = require('fs');
var common = require('./common');

//@
//@ ### cd([dir])
//@ Changes to directory `dir` for the duration of the script. Changes to home
//@ directory if no argument is supplied.
function _cd(options, dir) {
  if (!dir)
    dir = common.getUserHome();

  if (dir === '-') {
    if (!common.state.previousDir)
      common.error('could not find previous directory');
    else
      dir = common.state.previousDir;
  }

  // This complexity is so that we only stat once.
  var error = null;
  try {
    var stat = fs.statSync(dir);
    if(stat.isDirectory()) {
      common.state.previousDir = process.cwd();
      process.chdir(dir);
    } else {
      error = 'not a directory: ' + dir;
    }
  } catch (e) {
    error = 'no such file or directory: ' + dir;
  }
  if (error) common.error(error);
}
module.exports = _cd;<commit_msg>Improved cd performance and fix error messages<commit_after>var fs = require('fs');
var common = require('./common');

//@
//@ ### cd([dir])
//@ Changes to directory `dir` for the duration of the script. Changes to home
//@ directory if no argument is supplied.
function _cd(options, dir) {
  if (!dir)
    dir = common.getUserHome();

  if (dir === '-') {
    if (!common.state.previousDir)
      common.error('could not find previous directory');
    else
      dir = common.state.previousDir;
  }

  try {
    var curDir = process.cwd();
    process.chdir(dir);
    common.state.previousDir = curDir;
  } catch (e) {
    // something went wrong, let's figure out the error
    var err;
    try {
      fs.statSync(dir); // if this succeeds, it must be some sort of file
      err = 'not a directory: ' + dir;
    } catch (e) {
      err = 'no such file or directory: ' + dir;
    }
    if (err) common.error(err);
  }
}
module.exports = _cd;
{'fraction_non_alphanumeric': 0.14947600661886376, 'fraction_numerical': 0.0, 'mean_word_length': 2.7096114519427403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6011558', 'n_tokens_mistral': 582, 'n_tokens_neox': 575, 'n_words': 185}