<commit_before>'use strict';

const fs         = require('fs')
const os         = require('os')
const path       = require('path')

const license    = require('./helpers/license')
const autofind   = require('./helpers/autofind')
const patch      = require('./helpers/patch')

const setup      = require('./tasks/setup')
const download   = require('./tasks/download')
const prerender  = require('./tasks/actions')('prerender')
const script     = require('./tasks/script')
const render     = require('./tasks/render')
const postrender = require('./tasks/actions')('postrender')
const cleanup    = require('./tasks/cleanup')

//
// https://video.stackexchange.com/questions/16706/rendered-file-with-after-effects-is-very-huge
//

module.exports = (job, settings) => {
    settings = Object.assign({}, settings);
    settings.logger = settings.logger || { log: function() {} };

    const binaryAuto = autofind(settings);
    const binaryUser = settings.binary && fs.existsSync(settings.binary) ? settings.binary : null;

    if (!binaryUser && !binaryAuto) {
        return Promise.reject(new Error('you should provide a proper path to After Effects\' \"aerender\" binary'))
    }

    if (binaryAuto && !binaryUser) {
        settings.logger.log('using automatically determined directory of After Effects installation:')
        settings.logger.log(' - ' + binaryAuto)
    }

    settings = Object.assign({
        workpath: path.join(os.tmpdir(), 'nexrender'),

        addLicense: true,
        forceCommandLinePatch: false,

        renderLogs: false,
        multiFrames: false,
        maxMemoryPercent: undefined,
        imageCachePercent: undefined,
    }, settings, {
        binary: binaryUser || binaryAuto,
    })

    // make sure we will have absolute path
    if (!path.isAbsolute(settings.workpath)) {
        settings.workpath = path.join(process.cwd(), settings.workpath);
    }

    // add license helper
    if (settings.addLicense) {
        license(settings);
    }

    // attempt to patch the default
    // Scripts/commandLineRenderer.jsx
    patch(settings);

    return Promise.resolve(job)
        .then(job => setup(job, settings))
        .then(job => download(job, settings))
        .then(job => prerender(job, settings))
        .then(job => script(job, settings))
        .then(job => render(job, settings))
        .then(job => postrender(job, settings))
        .then(job => cleanup(job, settings))
}<commit_msg>splitted core to init and render<commit_after>'use strict';

const fs         = require('fs')
const os         = require('os')
const path       = require('path')

const license    = require('./helpers/license')
const autofind   = require('./helpers/autofind')
const patch      = require('./helpers/patch')

const setup      = require('./tasks/setup')
const download   = require('./tasks/download')
const prerender  = require('./tasks/actions')('prerender')
const script     = require('./tasks/script')
const render     = require('./tasks/render')
const postrender = require('./tasks/actions')('postrender')
const cleanup    = require('./tasks/cleanup')

//
// https://video.stackexchange.com/questions/16706/rendered-file-with-after-effects-is-very-huge
//

const init = (settings) => {
    settings = Object.assign({}, settings);
    settings.logger = settings.logger || { log: function() {} };

    const binaryAuto = autofind(settings);
    const binaryUser = settings.binary && fs.existsSync(settings.binary) ? settings.binary : null;

    if (!binaryUser && !binaryAuto) {
        return Promise.reject(new Error('you should provide a proper path to After Effects\' \"aerender\" binary'))
    }

    if (binaryAuto && !binaryUser) {
        settings.logger.log('using automatically determined directory of After Effects installation:')
        settings.logger.log(' - ' + binaryAuto)
    }

    settings = Object.assign({
        workpath: path.join(os.tmpdir(), 'nexrender'),

        addLicense: true,
        forceCommandLinePatch: false,

        renderLogs: false,
        multiFrames: false,
        maxMemoryPercent: undefined,
        imageCachePercent: undefined,
    }, settings, {
        binary: binaryUser || binaryAuto,
    })

    // make sure we will have absolute path
    if (!path.isAbsolute(settings.workpath)) {
        settings.workpath = path.join(process.cwd(), settings.workpath);
    }

    // add license helper
    if (settings.addLicense) {
        license(settings);
    }

    // attempt to patch the default
    // Scripts/commandLineRenderer.jsx
    patch(settings);

    return settings;
}

const render = (job, settings) => {
    return Promise.resolve(job)
        .then(job => setup(job, settings))
        .then(job => download(job, settings))
        .then(job => prerender(job, settings))
        .then(job => script(job, settings))
        .then(job => render(job, settings))
        .then(job => postrender(job, settings))
        .then(job => cleanup(job, settings))
}

module.exports = { init, render }
{'fraction_non_alphanumeric': 0.14488693215929557, 'fraction_numerical': 0.0020012007204322593, 'mean_word_length': 2.91692789968652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '507583', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1372, 'n_words': 332}