<commit_before>const Path = require('path')
const {opendir} = require('fs/promises')
const {isChildOf, isFile} = require('./filesystem')
const {GITHUB_ROOT, LOCAL_CRAN_REPO, libPath} = require('./config')
const {runScript} = require('./script')
const log = require('sepal/log').getLogger('github')

const isGitHubRepoPath = path =>
    isChildOf(GITHUB_ROOT, path)

const getGitHubRepoPath = (section, path = '') => {
    if (!['src', 'bin'].includes(section)) {
        throw new Error('Illegal section:', section)
    }
    const repoPath = Path.join(GITHUB_ROOT, section, path)
    if (!isGitHubRepoPath(repoPath)) {
        throw new Error('Illegal path:', path)
    }
    return repoPath
}

const getGitHubPackageInfo = path => {
    const GITHUB_MATCHER = /^(([^/]+)\/([^/]*)(.*)(\.tar\.gz))/
    const result = path.match(GITHUB_MATCHER)
    if (result) {
        const [_, path, user, name, ref, ext] = result
        return {path, user, name, ref, ext}
    }
    return null
}

const isGitHubPackageSourceCached = async path =>
    isFile(getGitHubRepoPath('src', path))

const getGitHubTarget = path =>
    `https://github.com/${path}`

const installLocalPackage = async (name, path) => {
    try {
        log.debug(`Installing ${name} (${path})`)
        await runScript('install_local_package.r', [name, path, libPath, LOCAL_CRAN_REPO])
        log.info(`Installed ${name} (${path})`)
        return true
    } catch (error) {
        log.warn(`Could not install ${name} (${path})`, error)
        return false
    }
}
        
const installRemotePackage = async (name, url) => {<commit_msg>Fixed r-proxy Github pattern matcher.<commit_after>const Path = require('path')
const {opendir} = require('fs/promises')
const {isChildOf, isFile} = require('./filesystem')
const {GITHUB_ROOT, LOCAL_CRAN_REPO, libPath} = require('./config')
const {runScript} = require('./script')
const log = require('sepal/log').getLogger('github')

const isGitHubRepoPath = path =>
    isChildOf(GITHUB_ROOT, path)

const getGitHubRepoPath = (section, path = '') => {
    if (!['src', 'bin'].includes(section)) {
        throw new Error('Illegal section:', section)
    }
    const repoPath = Path.join(GITHUB_ROOT, section, path)
    if (!isGitHubRepoPath(repoPath)) {
        throw new Error('Illegal path:', path)
    }
    return repoPath
}

const getGitHubPackageInfo = path => {
    const GITHUB_MATCHER = /^(?:\/github\/)?(([^\/]+)\/([^\/]*)(.*)(\.tar\.gz))/
    const result = path.match(GITHUB_MATCHER)
    if (result) {
        const [_, path, user, name, ref, ext] = result
        return {path, user, name, ref, ext}
    }
    return null
}

const isGitHubPackageSourceCached = async path =>
    isFile(getGitHubRepoPath('src', path))

const getGitHubTarget = path =>
    `https://github.com/${path}`

const installLocalPackage = async (name, path) => {
    try {
        log.debug(`Installing ${name} (${path})`)
        await runScript('install_local_package.r', [name, path, libPath, LOCAL_CRAN_REPO])
        log.info(`Installed ${name} (${path})`)
        return true
    } catch (error) {
        log.warn(`Could not install ${name} (${path})`, error)
        return false
    }
}
        
const installRemotePackage = async (name, url) => {
{'fraction_non_alphanumeric': 0.16897506925207756, 'fraction_numerical': 0.0, 'mean_word_length': 3.4338335607094135, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '2949078', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1076, 'n_words': 250}