Skip to content
Snippets Groups Projects
gogetter.js 5.93 KiB
Newer Older
  • Learn to ignore specific revisions
  • // fs abstraction for sys
    
    Jake Read's avatar
    Jake Read committed
    
    function GoGetter() {
    
      // I'm keeping this here because GoGetter is localized to the environment, mgr is not
      this.interpreterName = 'cuttlefish'
      this.interpreterVersion = 'v0.1'
    
    
      this.getHunkList = () => {
        return new Promise((resolve, reject) => {
          jQuery.get('hunklist', (resp) => {
            console.log('req for hunklist returns', resp)
          })
        })
      }
    
    
      this.recursivePathSearch = (root, extension, 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
            let faceString = '<h1>Index of /' + root
    
    Jake Read's avatar
    Jake Read committed
            if (debug) console.log('GG: facestring', faceString)
    
            let hfront = response.indexOf(faceString) + 14
            let hback = response.indexOf('</h1>')
            let header = response.slice(hfront, hback)
    
            if (debug) console.log('GG: header:', header);
    
            if (debug) console.log('GG: unreturned', unreturned)
    
              if(debug) console.log('return success')
    
              unreturned.splice(igot, 1)
            }
    
            // git those paths (these aren't names, are they?)
    
            let paths = response.split('<a href="/' + root)
    
            // rm first two, and last ()
            paths = paths.slice(2) //
    
            paths.forEach((path, index) => {
              let end = path.indexOf('"')
    
              //if (debug) console.log('GG: path index', index, 'end', end)
    
    Jake Read's avatar
    Jake Read committed
    
    
    Jake Read's avatar
    Jake Read committed
            if(debug) console.log('paths', paths)
    
              if(debug) console.log(`GG: path: ${path}`)
              if (path.includes('?')) {
                if(debug) console.log('GG: folder', path)
                let recurse = root + path//.slice(0, path.indexOf('/', 2))
    
                let rt = recurse.slice(0, recurse.indexOf('?'))
                unreturned.push(rt)// + mt)
                if(debug) console.log(`GG: RECURSE INTO(${recurse})`)
    
                jQuery.get(recurse, htmlTreeDiver)
              } else if (path.includes(extension)) {
    
                if(debug) console.log('GG: likely item', path)
    
                // secrets are rming .js
                path = path.slice(0, path.indexOf('.'))
                if (path === 'hunks' || path === 'manager' || path === 'template' || path.includes('hidden/')) {
                  // don't add these special hunks
                } else {
    
                  if(debug) console.log(`GG: returned.push(${path})`)
    
    Jake Read's avatar
    Jake Read committed
                }
    
    Jake Read's avatar
    Jake Read committed
            })
    
            if(debug) console.log('unreturned:', unreturned)
            if(debug) console.log('returned:', returned)
    
    
            if (unreturned.length === 0) {
              // TODO cull hunks and hidden paths ...
              // now, writing menu options for each path
              // sort alphabetically
    
              // hacking this to death bc tbd replaced by server side req not string parsing
    
    Jake Read's avatar
    Jake Read committed
              if(extension == '.js'){
                for(let i in returned){
                  returned[i] = returned[i].slice(1)
                }
                for(let i in returned){
                  if(returned[i] === "hunks") returned.splice(i, 1)
                }
                for(let i in returned){
                  if(returned[i] === "manager") returned.splice(i, 1)
                }
    
              if(debug) console.log('GG returns this', JSON.parse(JSON.stringify(returned)))
    
              resolve(returned)
            }
          }
    
          let unreturned = new Array()
          let returned = new Array()
    
          let mt = `?mtime=${performance.now()}`
          if(debug) console.log('GG: kickoff')
          //unreturned.push(root)// + mt)
          jQuery.get(root + mt, htmlTreeDiver)
    
        })
      }
    
      // 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, '\\"');
    
    Jake Read's avatar
    Jake Read committed
        //
        let ogurl = url;
    
        // try adding this query modifier to convince the browser it's new (if not, cannot refresh source)
        url += `?mtime=${performance.now()}`
        // to try...
        return new Promise((resolve, reject) => {
          import(url).then((obj) => {
            resolve(obj.default)
          }).catch((err) => {
    
    Jake Read's avatar
    Jake Read committed
            // at these levels, probably ah syntax error
            if(err.name === "SyntaxError"){
              console.error("Syntax Error at Load")
              console.error("in:", ogurl)
              console.error("at line:", err.lineNumber)
              reject(err)
            } else {
              reject(err)
            }
    
          })
        })
        // ... previously, this hack... but why?
        /*
    
        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)
            resolve(module[Object.keys(module)[0]])
          }
          script.type = "module"
          script.onerror = () => {
    
            console.error(`error from imported script...`, err)
    
            reject(new Error("Failed to load module script with URL " + url))
            cleanup()
          }
    
          script.textContent = `import * as m from "${url}"; window.${tempGlobal}(m);`
    
      }
    
      this.getJson = (path) => {
        return new Promise((resolve, reject) => {
          $.ajax({
            url: path,
            type: 'GET',
            success: (data) => {
              resolve(data)
            },
            error: (err) => {
              reject(new Error("failure at GG ajax get for json object: program probably doesn't exist"))
            }
          })
        })
      }
    
    
    export default GoGetter