Skip to content
Snippets Groups Projects
gogetter.js 4.46 KiB
Newer Older
  • Learn to ignore specific revisions
  • // fs abstraction for sys
    
    Jake Read's avatar
    Jake Read committed
    
    function GoGetter() {
        this.findHunks = (debug) => {
            return new Promise((resolve, reject) => {
                let htmlTreeDiver = (response) => {
    
                    // do we try to pick links out of this plaintext ? like a monkey ?
                    // I suppose we do
                    // header, for names
    
    Jake Read's avatar
    Jake Read committed
                    let hfront = response.indexOf('<h1>Index of /hunks') + 14
                    let hback = response.indexOf('</h1>')
                    let header = response.slice(hfront, hback)
    
    Jake Read's avatar
    Jake Read committed
                    if (debug) console.log('DIVE:', header);
    
    Jake Read's avatar
    Jake Read committed
                    let igot = unreturned.indexOf(header)
                    if (igot != -1) {
                        //console.log('return success')
                        unreturned.splice(igot, 1)
                    }
    
                    // git those paths (these aren't names, are they?)
                    let paths = response.split('<a href="/hunks/')
                    // rm first two, and last ()
    
                    paths = paths.slice(2) //
    
    Jake Read's avatar
    Jake Read committed
    
                    paths.forEach((path, index) => {
                        let end = path.indexOf('"')
                        //console.log('index', index, 'end', end)
                        paths[index] = path.slice(0, end)
                    })
    
                    paths.forEach((path, index) => {
                        if (path[path.length - 1] == '/') {
                            //console.log('folder', path)
                            let recurse = 'hunks/' + path
                            //console.log('getting', recurse)
                            unreturned.push(recurse)
                            jQuery.get(recurse, htmlTreeDiver)
                        } else if (path.includes('.js')) {
                            //console.log('likely hunk', path)
    
                            // secrets are rming .js
    
    Jake Read's avatar
    Jake Read committed
                            path = path.slice(0, path.indexOf('.'))
                            if (path === 'hunks' || path === 'manager' || path === 'template' || path.includes('hidden/')) {
    
                                // don't add these special hunks
    
    Jake Read's avatar
    Jake Read committed
                            } else {
                                returned.push(path)
                            }
                        }
                    })
    
                    if (unreturned.length === 0) {
    
                        // TODO cull hunks and hidden paths ...
                        // now, writing menu options for each path
                        // sort alphabetically
    
    Jake Read's avatar
    Jake Read committed
                        returned.sort()
    
                        //console.log('GG returns this', JSON.parse(JSON.stringify(returned)))
    
    Jake Read's avatar
    Jake Read committed
                        resolve(returned)
                    }
                }
    
                let unreturned = new Array()
                let returned = new Array()
                unreturned.push('hunks/')
                jQuery.get('hunks/', htmlTreeDiver)
            })
        }
    
    
    Jake Read's avatar
    Jake Read committed
        // https://github.com/tc39/proposal-dynamic-import
        this.importSource = (url) => {
            // escape characters that are used to delimit the module URL.
            // this way the following module works: 'data:text/javascript,console.log("hello")'
            url = url.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
    
            return new Promise((resolve, reject) => {
                const script = document.createElement("script")
                const tempGlobal = "__tempModuleLoadingVariable" + Math.random().toString(32).substring(2)
    
                function cleanup() {
                    delete window[tempGlobal]
                    script.remove()
                }
    
                window[tempGlobal] = function(module) {
                    cleanup()
    
                    //console.log('ADDHUNK (2) import resolves', url)
    
    Jake Read's avatar
    Jake Read committed
                    resolve(module[Object.keys(module)[0]])
                }
    
                script.type = "module"
                script.textContent = `import * as m from "${url}"; window.${tempGlobal}(m);`
    
                script.onerror = () => {
                    reject(new Error("Failed to load module script with URL " + url))
                    cleanup()
                }
    
                document.documentElement.appendChild(script)
            })
        }
    
        this.getJson = (path) => {
            return new Promise((resolve, reject) => {
                $.ajax({
                	url: path,
    
    Jake Read's avatar
    Jake Read committed
                	success: (data) => {
                		resolve(data)
                	},
                	error: (err) => {
    
    Jake Read's avatar
    Jake Read committed
                		reject(new Error("failure at GG ajax get for json object: program probably doesn't exist"))
    
    
        // I'm keeping this here because GoGetter is localized to the environment, mgr is not
        this.interpretername = 'cuttlefish 0.1'
    
    export default GoGetter