<commit_before>'use strict';
import cheerio = require('cheerio');
import episode from './episode';
import fs = require('fs');
const fse = require('fs-extra');
import request = require('./request');
import path = require('path');
import url = require('url');
import log  = require('./log');
const persistent = '.crpersistent';

/**
 * Streams the series to disk.
 */
export default function(config: IConfig, address: string, done: (err: Error) => void)
{
  const persistentPath = path.join(config.output || process.cwd(), persistent);

  /* Make a backup of the persistent file in case of */
  fse.copySync(persistentPath, persistentPath + '.backup');

  fs.readFile(persistentPath, 'utf8', (err: Error, contents: string) =>
  {
    const cache = config.cache ? {} : JSON.parse(contents || '{}');

    page(config, address, (errP, page) =>
    {
      if (errP)
      {
        return done(errP);
      }

      let i = 0;
      (function next()
      {
        if (i >= page.episodes.length) return done(null);
        download(cache, config, address, page.episodes[i], (errD, ignored) =>
        {
          if (errD)
          {
            return done(errD);
          }

          if ((ignored === false) || (ignored === undefined))
          {
            const newCache = JSON.stringify(cache, null, '  ');
            fs.writeFile(persistentPath, newCache, (errW: Error) =>
            {
              if (errW)
              {<commit_msg>Fix #19, better to check if a file exist before trying to copy it 😂<commit_after>'use strict';
import cheerio = require('cheerio');
import episode from './episode';
import fs = require('fs');
const fse = require('fs-extra');
import request = require('./request');
import path = require('path');
import url = require('url');
import log  = require('./log');
const persistent = '.crpersistent';

/**
 * Check if a file exist..
 */
function fileExist(path: string)
{
  try
  {
    fs.statSync(path);
    return true;
  } catch (e)
  {
    return false;
  }
}

/**
 * Streams the series to disk.
 */
export default function(config: IConfig, address: string, done: (err: Error) => void)
{
  const persistentPath = path.join(config.output || process.cwd(), persistent);

  /* Make a backup of the persistent file in case of */
  if (fileExist(persistentPath))
  {
    fse.copySync(persistentPath, persistentPath + '.backup');
  }

  fs.readFile(persistentPath, 'utf8', (err: Error, contents: string) =>
  {
    const cache = config.cache ? {} : JSON.parse(contents || '{}');

    page(config, address, (errP, page) =>
    {
      if (errP)
      {
        return done(errP);
      }

      let i = 0;
      (function next()
      {
        if (i >= page.episodes.length) return done(null);
        download(cache, config, address, page.episodes[i], (errD, ignored) =>
        {
          if (errD)
          {
            return done(errD);
          }

          if ((ignored === false) || (ignored === undefined))
          {
            const newCache = JSON.stringify(cache, null, '  ');
            fs.writeFile(persistentPath, newCache, (errW: Error) =>
            {
              if (errW)
              {
{'fraction_non_alphanumeric': 0.1548223350253807, 'fraction_numerical': 0.0019035532994923859, 'mean_word_length': 2.464835164835165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6385204', 'n_tokens_mistral': 968, 'n_tokens_neox': 950, 'n_words': 264}