<commit_before>'use strict';

import fetch from 'node-fetch';
import chalk from 'chalk';
import moment from 'moment';

import {
  getData,
  writeFile,
  checkUpdated
} from './db';

export default async (name, link) => {
  try {
    const time = await getData('time') || /* istanbul ignore next */ {};
    const check = !time[name] || checkUpdated(name, time[name]);
    let data = await getData(name);

    if(!data || check) {
      /* istanbul ignore if */
      if(check) {
        console.log(chalk.cyan(`[db] update "${name}".`));
      } else {
        console.log(chalk.cyan(`[db] can not get "${name}" from db.`));
        console.log(chalk.cyan('[db] fetch data from the website.'));
      }

      /* istanbul ignore next */
      fetch(link)
        .then(res => res.json())
        .then(data => {
          time[name] = moment().format();
          writeFile('time', time);
          writeFile(name, data)
        })
        .catch(e => console.log(e));
    }

    return {
      updateTime: time[name],
      data
    };
  } catch(e) {
    /* istanbul ignore next */
    console.log(e);
    /* istanbul ignore next */
    return {};
  }
};
<commit_msg>coverage 100%<commit_after>'use strict';

import fetch from 'node-fetch';
import chalk from 'chalk';
import moment from 'moment';

import {
  getData,
  writeFile,
  checkUpdated
} from './db';

export default async (name, link) => {
  try {
    const time = await getData('time') || /* istanbul ignore next */ {};
    const check = !time[name] || checkUpdated(name, time[name]);
    let data = await getData(name);

    if(!data || check) {
      /* istanbul ignore next */
      if(check) {
        console.log(chalk.cyan(`[db] update "${name}".`));
      } else {
        console.log(chalk.cyan(`[db] can not get "${name}" from db.`));
        console.log(chalk.cyan('[db] fetch data from the website.'));
      }

      /* istanbul ignore next */
      fetch(link)
        .then(res => res.json())
        .then(data => {
          time[name] = moment().format();
          writeFile('time', time);
          writeFile(name, data)
        })
        .catch(e => console.log(e));
    }

    return {
      updateTime: time[name],
      data
    };
  } catch(e) {
    /* istanbul ignore next */
    console.log(e);
    /* istanbul ignore next */
    return {};
  }
};

{'fraction_non_alphanumeric': 0.17322497861420016, 'fraction_numerical': 0.0012831479897348161, 'mean_word_length': 2.248611111111111, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '7276412', 'n_tokens_mistral': 765, 'n_tokens_neox': 753, 'n_words': 187}