<commit_before>import fs from 'fs';
import path from 'path';
import temp from 'temp';
import opn from 'opn';
import { promisify } from 'bluebird';
import { compile } from 'handlebars';
import { readFile } from './run';

temp.track();

const mkDir = promisify(temp.mkdir);
const writeFile = promisify(fs.writeFile);
const readDirectory = promisify(fs.readdir);

/**
 * Returns the source Handlebars template as string
 *
 * @return {Promise}
 */
function getHandlebarsTemplate() {
    return readFile(path.join(__dirname, '../templates/preview.hb'), 'utf-8')
        .then(compile);
}

/**
 * Copies the resource files to the specified location
 *
 * @param {String} target
 * @return {Promise}
 */
const copyResources = target => {
    const resources = path.join(__dirname, '../resources/preview');
    return readDirectory(resources)
        .map(filename => {
            const fullPath = path.join(resources, filename);
            const targetPath = path.join(target, filename);
            return fs.createReadStream(fullPath)
                .pipe(fs.createWriteStream(targetPath));
        });
};

/**
 * Writes to a temp file and opens the content on the default browser
 *
 * @param {String} content
 * @return {Promise}<commit_msg>Copy files<commit_after>import fs from 'fs';
import path from 'path';
import temp from 'temp';
import opn from 'opn';
import { promisify } from 'bluebird';
import { compile } from 'handlebars';
import { readFile } from './run';

const mkDir = promisify(temp.mkdir);
const writeFile = promisify(fs.writeFile);
const readDirectory = promisify(fs.readdir);

/**
 * Copies a file
 *
 * @param {String} source
 * @param {String} target
 * @return {Promise}
 */
const copyFile = (source, target) => new Promise((resolve, reject) => {
    const read = fs.createReadStream(source);
    read.on('error', reject);
    const write = fs.createWriteStream(target);
    write.on('error', reject);
    write.on('close', resolve);
    read.pipe(write);
});

/**
 * Returns the source Handlebars template as string
 *
 * @return {Promise}
 */
function getHandlebarsTemplate() {
    return readFile(path.join(__dirname, '../templates/preview.hb'), 'utf-8')
        .then(compile);
}

/**
 * Copies the resource files to the specified location
 *
 * @param {String} target
 * @return {Promise}
 */
const copyResources = target => {
    const resources = path.join(__dirname, '../resources/preview');
    return readDirectory(resources)
        .map(filename => {
            const sourcePath = path.join(resources, filename);
            const targetPath = path.join(target, filename);
            return copyFile(sourcePath, targetPath);
        });
};

/**
 * Writes to a temp file and opens the content on the default browser
 *
 * @param {String} content
 * @return {Promise}
{'fraction_non_alphanumeric': 0.139186295503212, 'fraction_numerical': 0.0007137758743754461, 'mean_word_length': 3.520967741935484, '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': '5166557', 'n_tokens_mistral': 809, 'n_tokens_neox': 802, 'n_words': 242}