<commit_before>import { watch } from 'chokidar'
import { create } from 'exif-parser'
import { stat, readFile, readdirSync, statSync, Stats, rename } from 'fs'
import { join, dirname, extname, basename } from 'path'
import minimist = require('minimist')
import moment = require('moment')

const argv = minimist(process.argv.slice(2))
const dirs = argv._
const persistent = !!argv['no-watch']

/**
 * Check if the file name is a compatible image.
 */
function isImage (filename: string) {
  return /\.(?:jpe?g|png)$/.test(filename)
}

/**
 * Handle image renaming.
 */
function handleFile (filename: string, stats: Stats) {
  return new Promise((resolve, reject) => {
    // Skip formatting non-images.
    if (!isImage(filename)) {
      return
    }

    const ext = extname(filename)

    // Skip formatting already renamed files.
    if (/^\d{4}\-\d{2}\-\d{2}--\d{2}-\d{2}-\d{2}/.test(basename(filename, ext))) {
      return
    }

    // Rename the file by date.
    function renameByDate (date: Date | number) {
      const dateName = moment(date).utc().format('YYYY-MM-DD--H-mm-ss') + ext
      const newName = join(dirname(filename), dateName)

      rename(filename, newName, (err) => err ? resolve() : reject(err))
    }

    return readFile(filename, (err: Error, contents: Buffer) => {
      if (err) {
        return renameByDate(stats.mtime)
      }

      const parser = create(contents)
      const exif = parser.parse()

      if (exif.tags.DateTimeOriginal == null) {
        return renameByDate(stats.mtime)
      }

      return renameByDate(exif.tags.DateTimeOriginal)
    })
  })<commit_msg>Multiply date time original by 1000 for JS dates<commit_after>import { watch } from 'chokidar'
import { create } from 'exif-parser'
import { stat, readFile, readdirSync, statSync, Stats, rename } from 'fs'
import { join, dirname, extname, basename } from 'path'
import minimist = require('minimist')
import moment = require('moment')

const argv = minimist(process.argv.slice(2))
const dirs = argv._
const persistent = !!argv['no-watch']

/**
 * Check if the file name is a compatible image.
 */
function isImage (filename: string) {
  return /\.(?:jpe?g|png)$/.test(filename)
}

/**
 * Handle image renaming.
 */
function handleFile (filename: string, stats: Stats) {
  return new Promise((resolve, reject) => {
    // Skip formatting non-images.
    if (!isImage(filename)) {
      return
    }

    const ext = extname(filename)

    // Skip formatting already renamed files.
    if (/^\d{4}\-\d{2}\-\d{2}--\d{2}-\d{2}-\d{2}/.test(basename(filename, ext))) {
      return
    }

    // Rename the file by date.
    function renameByDate (date: Date | number) {
      const dateName = moment(date).utc().format('YYYY-MM-DD--H-mm-ss') + ext
      const newName = join(dirname(filename), dateName)

      rename(filename, newName, (err) => err ? resolve() : reject(err))
    }

    return readFile(filename, (err: Error, contents: Buffer) => {
      if (err) {
        return renameByDate(stats.mtime)
      }

      const parser = create(contents)
      const exif = parser.parse()

      if (exif.tags.DateTimeOriginal == null) {
        return renameByDate(stats.mtime)
      }

      return renameByDate(exif.tags.DateTimeOriginal * 1000)
    })
  })
{'fraction_non_alphanumeric': 0.15156154317207593, 'fraction_numerical': 0.006736068585425597, 'mean_word_length': 3.0483271375464684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '5483920', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1030, 'n_words': 273}