<commit_before>import chalk from 'chalk';
import { writeFile, readFileSync } from 'fs';
import { join as joinPath, resolve as resolvePath } from 'path';
import { fromDir } from './from-dir';
import { StringBuilder } from './string-builder';

interface SandboxFileInformation {
    key: string;
    searchKey: string;
    name: string;
    label: string;
    scenarioMenuItems: {
        key: string;
        description: string;
    }[];
}

export function buildSandboxes(srcPath: string, chunk: boolean): Promise<string> {
    const chunkMode = chunk ? 'lazy' : 'eager';
    const home = resolvePath(srcPath);
    const sandboxes = findSandboxes(home);
    const filePath = resolvePath(__dirname, '../../build/src/shared/sandboxes.js');
    const fileContent = buildSandboxFileContents(sandboxes, home, chunkMode);

    return new Promise((resolve, reject) => {
        writeFile(filePath, fileContent, err => {
            if (err) {
                console.log(chalk.red('Unable to compile sandboxes.\n'));
                reject(err);
            }
            console.log('Successfully compiled sandbox files.');
            resolve(filePath);
        });
    });
}

export function findSandboxes(home: string): SandboxFileInformation[] {
    const sandboxes = [];

    fromDir(home, /\.sandbox.ts$/, (filename) => {
        let sandboxPath = filename.replace(home, '.').replace(/.ts$/, '').replace(/\\/g, '/');
        const contents = readFileSync(filename, 'utf8');

        const matchSandboxOf = /\s?sandboxOf\s*\(\s*([^)]+?)\s*\)/g.exec(contents);
        if (matchSandboxOf) {
            const typeName = matchSandboxOf[1].split(',')[0].trim();
            const labelText = /label\s*:\s*['"](.+)['"]/g.exec(matchSandboxOf[0]);

            let scenarioMenuItems = [];
            const scenarioRegex = /[^\/\/]\.add\s*\(\s*['"](.+)['"]\s*,\s*{/g;
            let scenarioMatches;
            let scenarioIndex = 1;
            while ((scenarioMatches = scenarioRegex.exec(contents)) !== null) {
                scenarioMenuItems.push({ key: scenarioIndex, description: scenarioMatches[1] });
                scenarioIndex++;
            }

            let label = labelText ? labelText[1] : '';<commit_msg>remove extranneous regex<commit_after>import chalk from 'chalk';
import { writeFile, readFileSync } from 'fs';
import { join as joinPath, resolve as resolvePath } from 'path';
import { fromDir } from './from-dir';
import { StringBuilder } from './string-builder';

interface SandboxFileInformation {
    key: string;
    searchKey: string;
    name: string;
    label: string;
    scenarioMenuItems: {
        key: string;
        description: string;
    }[];
}

export function buildSandboxes(srcPath: string, chunk: boolean): Promise<string> {
    const chunkMode = chunk ? 'lazy' : 'eager';
    const home = resolvePath(srcPath);
    const sandboxes = findSandboxes(home);
    const filePath = resolvePath(__dirname, '../../build/src/shared/sandboxes.js');
    const fileContent = buildSandboxFileContents(sandboxes, home, chunkMode);

    return new Promise((resolve, reject) => {
        writeFile(filePath, fileContent, err => {
            if (err) {
                console.log(chalk.red('Unable to compile sandboxes.\n'));
                reject(err);
            }
            console.log('Successfully compiled sandbox files.');
            resolve(filePath);
        });
    });
}

export function findSandboxes(home: string): SandboxFileInformation[] {
    const sandboxes = [];

    fromDir(home, /\.sandbox.ts$/, (filename) => {
        let sandboxPath = filename.replace(home, '.').replace(/.ts$/, '').replace(/\\/g, '/');
        const contents = readFileSync(filename, 'utf8');

        const matchSandboxOf = /\s?sandboxOf\s*\(\s*([^)]+?)\s*\)/g.exec(contents);
        if (matchSandboxOf) {
            const typeName = matchSandboxOf[1].split(',')[0].trim();
            const labelText = /label\s*:\s*['"](.+)['"]/g.exec(matchSandboxOf[0]);

            let scenarioMenuItems = [];
            const scenarioRegex = /\.add\s*\(\s*['"](.+)['"]\s*,\s*{/g;
            let scenarioMatches;
            let scenarioIndex = 1;
            while ((scenarioMatches = scenarioRegex.exec(contents)) !== null) {
                scenarioMenuItems.push({ key: scenarioIndex, description: scenarioMatches[1] });
                scenarioIndex++;
            }

            let label = labelText ? labelText[1] : '';
{'fraction_non_alphanumeric': 0.16134113411341133, 'fraction_numerical': 0.0031503150315031502, 'mean_word_length': 2.5991902834008096, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '2976866', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1282, 'n_words': 284}