<commit_before>// const debug = require('@tryghost/debug')('comments-counts-assets');
const Minifier = require('@tryghost/minifier');
const path = require('path');
const fs = require('fs').promises;
const logging = require('@tryghost/logging');
const config = require('../../../shared/config');

class AdminAuthAssetsService {
    constructor(options = {}) {
        this.src = options.src || path.join(config.get('paths').assetSrc, 'admin-auth');
        this.dest = options.dest || path.join(config.getContentPath('public'), 'admin-auth');
        this.minifier = new Minifier({src: this.src, dest: this.dest});
    }

    generateGlobs() {
        return {
            'admin-auth.min.js': '*.js'
        };
    }

    async minify(globs) {
        try {
            return await this.minifier.minify(globs);
        } catch (error) {
            if (error.code === 'EACCES') {
                logging.error('Ghost was not able to write admin-auth asset files due to permissions.');
                return;
            }

            throw error;
        }
    }

    async copyStatic() {
        try {
            return await fs.copyFile(path.join(this.src, 'index.html'), path.join(this.dest, 'index.html'));
        } catch (error) {
            if (error.code === 'EACCES') {
                logging.error('Ghost was not able to write admin-auth asset files due to permissions.');
                return;
            }

            throw error;
        }
    }

    async clearFiles() {
        const rmFile = async (name) => {
            await fs.unlink(path.join(this.dest, name));
        };

        let promises = [
            // @deprecated switch this to use fs.rm when we drop support for Node v12
            rmFile('admin-auth.min.js'),
            rmFile('index.html')
        ];

        // We don't care if removing these files fails as it's valid for them to not exist
        return Promise.allSettled(promises);
    }

    async load() {
        await this.clearFiles();
        this.copyStatic();
        const globs = this.generateGlobs();
        await this.minify(globs);
    }
}

module.exports = AdminAuthAssetsService;
<commit_msg>Fixed admin auth asset loading (#15018)<commit_after>// const debug = require('@tryghost/debug')('comments-counts-assets');
const Minifier = require('@tryghost/minifier');
const path = require('path');
const fs = require('fs').promises;
const logging = require('@tryghost/logging');
const config = require('../../../shared/config');

class AdminAuthAssetsService {
    constructor(options = {}) {
        /** @private */
        this.src = options.src || path.join(config.get('paths').assetSrc, 'admin-auth');
        /** @private */
        this.dest = options.dest || path.join(config.getContentPath('public'), 'admin-auth');
        /** @private */
        this.minifier = new Minifier({src: this.src, dest: this.dest});
    }

    /**
     * @private
     */
    generateGlobs() {
        return {
            'admin-auth.min.js': '*.js'
        };
    }

    /**
     * @private
     * @returns {Promise<void>}
     */
    async minify(globs) {
        try {
            await this.minifier.minify(globs);
        } catch (error) {
            if (error.code === 'EACCES') {
                logging.error('Ghost was not able to write admin-auth asset files due to permissions.');
                return;
            }

            throw error;
        }
    }

    /**
     * @private
     * @returns {Promise<void>}
     */
    async copyStatic() {
        try {
            await fs.copyFile(path.join(this.src, 'index.html'), path.join(this.dest, 'index.html'));
        } catch (error) {
            if (error.code === 'EACCES') {
                logging.error('Ghost was not able to write admin-auth asset files due to permissions.');
                return;
            }

            throw error;
        }
    }

    /**
     * @private
     * @returns {Promise<void>}
     */
    async clearFiles() {
        const rmFile = async (name) => {
            await fs.unlink(path.join(this.dest, name));
        };

        let promises = [
            // @deprecated switch this to use fs.rm when we drop support for Node v12
            rmFile('admin-auth.min.js'),
            rmFile('index.html')
        ];

        // We don't care if removing these files fails as it's valid for them to not exist
        await Promise.allSettled(promises);
    }

    /**
     * Minify, move into the destination directory, and clear existing asset files.
     *
     * @returns {Promise<void>}
     */
    async load() {
        const globs = this.generateGlobs();
        await this.clearFiles();
        await this.minify(globs);
        await this.copyStatic();
    }
}

module.exports = AdminAuthAssetsService;

{'fraction_non_alphanumeric': 0.14246861924686194, 'fraction_numerical': 0.0018828451882845188, 'mean_word_length': 1.9170225747406955, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3429193', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1371, 'n_words': 332}