<commit_before>/* eslint-disable comma-dangle */

const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const {trimEnd} = _;

function getAbsoluteModuleDirectory(modulePathRelative) {
    return path.join(require.resolve(path.join(modulePathRelative, 'package.json')), '../');
}

function getPaths(distRelative) {
    const extensionRootPath = trimEnd(
        getAbsoluteModuleDirectory(distRelative.slice(0, distRelative.indexOf('/dist/'))),
        '/',
    );
    const extensionSrcPath = trimEnd(
        path.join(extensionRootPath, 'src'),
        '/',
    );

    const cssInDist = trimEnd(
        path.join(extensionRootPath, 'dist/index.css'),
        '/',
    );
    const cssInSrc = trimEnd(
        path.join(extensionRootPath, 'src/index.css'),
        '/',
    );

    const extensionCssFilePath = fs.existsSync(cssInDist) ? cssInDist : cssInSrc;

    return {
        extensionRootPath,
        extensionSrcPath,
        extensionCssFilePath,
    };
}

function getFileExtension(path) {
    const lastDotAt = path.lastIndexOf('.');

    if (lastDotAt < 0) {
        return null;
    } else {
        return path.slice(lastDotAt);
    }
}

function getIndexFilePath(indexFilePathRelative) {
    const fileExtensions = ['.js', '.jsx', '.ts', '.tsx'];

    // paths in `importApps` are relative client/dist
    const base = path.join('dist', indexFilePathRelative);

    const maybeExtension = getFileExtension(base);

    if (maybeExtension != null && fileExtensions.includes(maybeExtension)) {
        return base;
    }

    for (const ext of fileExtensions) {
        const file = base + ext;

        if (fs.existsSync(file)) {
            return file;
        }

        const indexFileInDirectory = base + '/index' + ext;

        if (fs.existsSync(indexFileInDirectory)) {
            return indexFileInDirectory;
        }
    }

    throw new Error('Index file not found');
}

function getExtensionDirectoriesSync() {
    const config = require('../superdesk.config.js')();

    const paths = config.importApps || config.apps || [];

    // paths in `importApps` are relative client/dist
    const indexFilePathRelative = paths.find((path) => path.startsWith('../'));

    if (indexFilePathRelative == null) {
        return [];
    }

    var indexFile = fs.readFileSync(getIndexFilePath(indexFilePathRelative), 'utf-8');

    /**
     * Regex for extracting extension id and import path(there is a capturing group for each)
     * from index file where extensions are registered.
     *
     * Here's a sample of extension registration code that the regex is targeting:
        {
            id: 'planning-extension',
            load: () =>
                import('superdesk-planning/client/planning-extension/dist/planning-extension/src/extension')
                    .then((res) => res.default),
        }
     */
    var extensionRegistrationPattern = /id:\W*'(.*)',\W*load:\W*import\('(.*\/dist.+)'/g;

    const matches = [];

    while ((_match = extensionRegistrationPattern.exec(indexFile)) !== null) {
        const extensionName = _match[1];
        const {extensionRootPath, extensionSrcPath, extensionCssFilePath} = getPaths(_match[2]);<commit_msg>expect root dir when registering extensions (#3885)<commit_after>/* eslint-disable comma-dangle */

const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const {trimEnd} = _;

function getAbsoluteModuleDirectory(modulePathRelative) {
    return path.join(require.resolve(path.join(modulePathRelative, 'package.json')), '../');
}

function getPaths(extensionRootRelative) {
    const extensionRootPath = trimEnd(
        getAbsoluteModuleDirectory(extensionRootRelative),
        '/',
    );
    const extensionSrcPath = trimEnd(
        path.join(extensionRootPath, 'src'),
        '/',
    );

    const cssInDist = trimEnd(
        path.join(extensionRootPath, 'dist/index.css'),
        '/',
    );
    const cssInSrc = trimEnd(
        path.join(extensionRootPath, 'src/index.css'),
        '/',
    );

    const extensionCssFilePath = fs.existsSync(cssInDist) ? cssInDist : cssInSrc;

    return {
        extensionRootPath,
        extensionSrcPath,
        extensionCssFilePath,
    };
}

function getFileExtension(path) {
    const lastDotAt = path.lastIndexOf('.');

    if (lastDotAt < 0) {
        return null;
    } else {
        return path.slice(lastDotAt);
    }
}

function getIndexFilePath(indexFilePathRelative) {
    const fileExtensions = ['.js', '.jsx', '.ts', '.tsx'];

    // paths in `importApps` are relative client/dist
    const base = path.join('dist', indexFilePathRelative);

    const maybeExtension = getFileExtension(base);

    if (maybeExtension != null && fileExtensions.includes(maybeExtension)) {
        return base;
    }

    for (const ext of fileExtensions) {
        const file = base + ext;

        if (fs.existsSync(file)) {
            return file;
        }

        const indexFileInDirectory = base + '/index' + ext;

        if (fs.existsSync(indexFileInDirectory)) {
            return indexFileInDirectory;
        }
    }

    throw new Error('Index file not found');
}

function getExtensionDirectoriesSync() {
    const config = require('../superdesk.config.js')();

    const paths = config.importApps || config.apps || [];

    // paths in `importApps` are relative client/dist
    const indexFilePathRelative = paths.find((path) => path.startsWith('../'));

    if (indexFilePathRelative == null) {
        return [];
    }

    var indexFile = fs.readFileSync(getIndexFilePath(indexFilePathRelative), 'utf-8');

    /**
     * Regex for extracting extension id and import path(there is a capturing group for each)
     * from index file where extensions are registered.
     *
     * Here's a sample of extension registration code that the regex is targeting:
        {
            id: 'planning-extension',
            load: () =>
                import('superdesk-planning/client/planning-extension/dist/planning-extension/src/extension')
                    .then((res) => res.default),
        }
     */
    var extensionRegistrationPattern = /id:\W*'(.*)',\W*load:\W*import\('(.+?)'/g;

    const matches = [];

    while ((_match = extensionRegistrationPattern.exec(indexFile)) !== null) {
        const extensionName = _match[1];
        const {extensionRootPath, extensionSrcPath, extensionCssFilePath} = getPaths(_match[2]);
{'fraction_non_alphanumeric': 0.13201985727582996, 'fraction_numerical': 0.002016754576481539, 'mean_word_length': 2.7768014059753954, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1788432', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1790, 'n_words': 404}