<commit_before>import * as fs from "fs-extra";
import * as path from "path";
import * as utils from "./utils";
import HelperArray from "../helpers/array";
import HelperFS from "../helpers/fs";
import HelperObject from "../helpers/object";
import HelperString from "../helpers/string";

interface ScriptItem {
    name: string;
    absoluteFilename: string;
    relativeFilename: string;
    sortFirst: boolean;
}

const SERVER_DIR: string = utils.getServerDir();

function getModelSnippet(item: ScriptItem): string {
    return `import ${item.name} from "./${item.relativeFilename}";
export {default as ${item.name}} from "./${item.relativeFilename}";
export function ${HelperString.toCamelCase(item.name)}(): orm.Repository<${item.name}> { return orm.getRepository(${item.name}); }`;
}

function getIndexSnippet(item: ScriptItem): string {
    return `export {default as ${item.name}} from "./${item.relativeFilename}";`;
}

function getExportSnippet(item: ScriptItem): string[] {
    let lines: string[] = (fs.readFileSync(item.absoluteFilename.replace(/\.js/g, ".ts"), "utf-8")).split("\n")
        .map(l => l.replace(/ default /g, " ").replace(/\r/g, ""))
        .filter(l => {
            l = l.trim();
            return !l.startsWith("@") &&
                !l.startsWith("import ") &&
                l.length > 0 &&
                !(l.startsWith("export ") && l.endsWith(";"));
        });
    const stopIndex: number = lines.map(l => l.trim()).indexOf("// stop-generate");
    if (stopIndex !== -1) {
        lines = lines.splice(0, stopIndex);
        lines.push("}");
    }
    return lines;
}

async function getScriptItems(dirs: string[], fileEnding: string, baseDir: string = SERVER_DIR): Promise<ScriptItem[]> {
    let items: ScriptItem[] = [];
    await HelperArray.forEachAsync(dirs, async dir => {
        items = items.concat((await HelperFS.recursiveReaddir(dir)).map(f => {
            f = f.replace(/\\/g, "/");
            const sortFirst: boolean = fs.readFileSync(f, "utf-8").includes("// generate:sort-first");
            if (sortFirst) {
                console.log("Found a sort-first: ", f);
            }
            return <ScriptItem>{
                name: path.basename(f, fileEnding),
                relativeFilename: path.relative(baseDir, f).replace(/\\/g, "/"),
                absoluteFilename: f,
                sortFirst
            };
        }).filter(f =><commit_msg>Remove logging call from generate script<commit_after>import * as fs from "fs-extra";
import * as path from "path";
import * as utils from "./utils";
import HelperArray from "../helpers/array";
import HelperFS from "../helpers/fs";
import HelperObject from "../helpers/object";
import HelperString from "../helpers/string";

interface ScriptItem {
    name: string;
    absoluteFilename: string;
    relativeFilename: string;
    sortFirst: boolean;
}

const SERVER_DIR: string = utils.getServerDir();

function getModelSnippet(item: ScriptItem): string {
    return `import ${item.name} from "./${item.relativeFilename}";
export {default as ${item.name}} from "./${item.relativeFilename}";
export function ${HelperString.toCamelCase(item.name)}(): orm.Repository<${item.name}> { return orm.getRepository(${item.name}); }`;
}

function getIndexSnippet(item: ScriptItem): string {
    return `export {default as ${item.name}} from "./${item.relativeFilename}";`;
}

function getExportSnippet(item: ScriptItem): string[] {
    let lines: string[] = (fs.readFileSync(item.absoluteFilename.replace(/\.js/g, ".ts"), "utf-8")).split("\n")
        .map(l => l.replace(/ default /g, " ").replace(/\r/g, ""))
        .filter(l => {
            l = l.trim();
            return !l.startsWith("@") &&
                !l.startsWith("import ") &&
                l.length > 0 &&
                !(l.startsWith("export ") && l.endsWith(";"));
        });
    const stopIndex: number = lines.map(l => l.trim()).indexOf("// stop-generate");
    if (stopIndex !== -1) {
        lines = lines.splice(0, stopIndex);
        lines.push("}");
    }
    return lines;
}

async function getScriptItems(dirs: string[], fileEnding: string, baseDir: string = SERVER_DIR): Promise<ScriptItem[]> {
    let items: ScriptItem[] = [];
    await HelperArray.forEachAsync(dirs, async dir => {
        items = items.concat((await HelperFS.recursiveReaddir(dir)).map(f => {
            f = f.replace(/\\/g, "/");
            const sortFirst: boolean = fs.readFileSync(f, "utf-8").includes("// generate:sort-first");
            return <ScriptItem>{
                name: path.basename(f, fileEnding),
                relativeFilename: path.relative(baseDir, f).replace(/\\/g, "/"),
                absoluteFilename: f,
                sortFirst
            };
        }).filter(f =>
{'fraction_non_alphanumeric': 0.17603860679815359, 'fraction_numerical': 0.00209819555182543, 'mean_word_length': 2.7803330689928627, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, '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': '6301612', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1429, 'n_words': 325}