<commit_before>import 'source-sans-pro/source-sans-pro.css'
import 'source-code-pro/source-code-pro.css'
import '@fortawesome/fontawesome-free/css/solid.css'
import '@fortawesome/fontawesome-free/css/brands.css'
import '@fortawesome/fontawesome-free/css/regular.css'
import '@fortawesome/fontawesome-free/css/fontawesome.css'
import 'ngx-toastr/toastr.css'
import '../app/src/preload.scss'

// Required before other imports
import './polyfills.buffer'

const mocks = {}
const modules = {}

const customRequire = path => {
    if (mocks[path]) {
        console.log(':: mock', path)
        return mocks[path]
    }
    if (modules[path]) {
        return modules[path]
    }
    throw new Error(`Attempted to require ${path}`)
}

customRequire['resolve'] = (() => null) as any
customRequire['main'] = {
    paths: [],
}

async function webRequire (url) {
    console.log(`Loading ${url}`)
    const e = document.createElement('script')
    window['module'] = { exports: {} } as any
    window['exports'] = window['module'].exports
    await new Promise(resolve => {
        e.onload = resolve
        e.src = url
        document.querySelector('head').appendChild(e)
    })
    return window['module'].exports
}

const Tabby = {
    registerMock: (name, mod) => {
        mocks[name] = mod
    },
    registerModule: (name, mod) => {
        modules[name] = mod
    },
    loadPlugin: async (url) => {
        const pkg = await (await fetch(url + '/package.json')).json()
        url += '/' + pkg.main
        const module = await webRequire(url)
        Tabby.registerModule(`resources/builtin-plugins/${pkg.name}`, module)
        Tabby.registerModule(pkg.name, module)
        return module
    },
    bootstrap: (...args) => window['bootstrapTabby'](...args),
    webRequire,
}

Object.assign(window, {
    require: customRequire,
    module: {
        paths: [],
    },
    Tabby,
    process: {
        env: { },
        argv: ['tabby'],
        platform: 'darwin',
        on: () => null,
        stdout: {},
        stderr: {},
        resourcesPath: 'resources',
        version: '14.0.0',
        nextTick: (f, ...args) => setTimeout(() => f(...args)),
        cwd: () => '/',
    },
    global: window,
})<commit_msg>expose more plugin loader apis<commit_after>import 'source-sans-pro/source-sans-pro.css'
import 'source-code-pro/source-code-pro.css'
import '@fortawesome/fontawesome-free/css/solid.css'
import '@fortawesome/fontawesome-free/css/brands.css'
import '@fortawesome/fontawesome-free/css/regular.css'
import '@fortawesome/fontawesome-free/css/fontawesome.css'
import 'ngx-toastr/toastr.css'
import '../app/src/preload.scss'

// Required before other imports
import './polyfills.buffer'

const mocks = {}
const modules = {}

const customRequire = path => {
    if (mocks[path]) {
        console.log(':: mock', path)
        return mocks[path]
    }
    if (modules[path]) {
        return modules[path]
    }
    throw new Error(`Attempted to require ${path}`)
}

customRequire['resolve'] = (() => null) as any
customRequire['main'] = {
    paths: [],
}

async function webRequire (url) {
    console.log(`>> Loading ${url}`)
    const e = document.createElement('script')
    window['module'] = { exports: {} } as any
    window['exports'] = window['module'].exports
    await new Promise(resolve => {
        e.onload = resolve
        e.src = url
        document.querySelector('head').appendChild(e)
    })
    return window['module'].exports
}

async function prefetchURL (url) {
    console.log(`:: Prefetching ${url}`)
    await (await fetch(url)).text()
}

const Tabby = {
    registerMock: (name, mod) => {
        mocks[name] = mod
    },
    registerModule: (name, mod) => {
        modules[name] = mod
    },
    resolvePluginInfo: async (url): Promise<any> => {
        const pkg = await (await fetch(url + '/package.json')).json()
        url += '/' + pkg.main
        return { ...pkg, url }
    },
    registerPluginModule: (packageName, module) => {
        Tabby.registerModule(`resources/builtin-plugins/${packageName}`, module)
        Tabby.registerModule(packageName, module)
    },
    loadPlugin: async (url) => {
        const info = await Tabby.resolvePluginInfo(url)
        const module = await webRequire(info.url)
        Tabby.registerPluginModule(info.name, module)
        return module
    },
    loadPlugins: async (urls) => {
        const infos: any[] = await Promise.all(urls.map(Tabby.resolvePluginInfo))
        await Promise.all(infos.map(x => prefetchURL(x.url)))
        const pluginModules = []
        for (const info of infos) {
            const module = await webRequire(info.url)
            Tabby.registerPluginModule(info.name, module)
            pluginModules.push(module)
        }
        return pluginModules
    },
    bootstrap: (...args) => window['bootstrapTabby'](...args),
    webRequire,
}

Object.assign(window, {
    require: customRequire,
    module: {
        paths: [],
    },
    Tabby,
    process: {
        env: { },
        argv: ['tabby'],
        platform: 'darwin',
        on: () => null,
        stdout: {},
        stderr: {},
        resourcesPath: 'resources',
        version: '14.0.0',
        nextTick: (f, ...args) => setTimeout(() => f(...args)),
        cwd: () => '/',
    },
    global: window,
})
{'fraction_non_alphanumeric': 0.16387266905255227, 'fraction_numerical': 0.0015068751177246186, 'mean_word_length': 2.61716621253406, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 32, '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': '3474005', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1671, 'n_words': 342}