Skip to content
Snippets Groups Projects
gogetter.js 3.95 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.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)
    
    Jake Read's avatar
    Jake Read committed
            if (debug) console.log('GG: DIVE:', header);
    
            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="/' + root + '/')
            // rm first two, and last ()
            paths = paths.slice(2) //
    
            paths.forEach((path, index) => {
              let end = path.indexOf('"')
    
    Jake Read's avatar
    Jake Read committed
              if (debug) console.log('GG: index', index, 'end', end)
    
    Jake Read's avatar
    Jake Read committed
    
    
            paths.forEach((path, index) => {
              if (path[path.length - 1] == '/') {
                //console.log('folder', path)
                let recurse = root + '/' + path
                //console.log('getting', recurse)
                unreturned.push(recurse)
                jQuery.get(recurse, htmlTreeDiver)
              } else if (path.includes(extension)) {
                //console.log('likely hunk', 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 {
                  returned.push(path)
    
    Jake Read's avatar
    Jake Read committed
                }
    
    Jake Read's avatar
    Jake Read committed
            })
    
            if (unreturned.length === 0) {
              // TODO cull hunks and hidden paths ...
              // now, writing menu options for each path
              // sort alphabetically
              returned.sort()
              //console.log('GG returns this', JSON.parse(JSON.stringify(returned)))
              resolve(returned)
            }
          }
    
          let unreturned = new Array()
          let returned = new Array()
          unreturned.push(root + '/')
          jQuery.get(root + '/', 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, '\\"');
    
        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.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,
            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