Newer
Older
// fs abstraction for sys

Jake Read
committed
// 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
committed
let hfront = response.indexOf(faceString) + 14
let hback = response.indexOf('</h1>')
let header = response.slice(hfront, hback)
if (debug) console.log('GG: header:', header);

Jake Read
committed
let igot = unreturned.indexOf(header)
if (debug) console.log('GG: unreturned', unreturned)

Jake Read
committed
if (igot != -1) {
if(debug) console.log('return success')

Jake Read
committed
unreturned.splice(igot, 1)
}
// git those paths (these aren't names, are they?)
let paths = response.split('<a href="/' + root)

Jake Read
committed
// 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
committed
paths[index] = path.slice(0, end)

Jake Read
committed
paths.forEach((path, index) => {
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))

Jake Read
committed
//console.log('getting', recurse)
let rt = recurse.slice(0, recurse.indexOf('?'))
unreturned.push(rt)// + mt)
if(debug) console.log(`GG: RECURSE INTO(${recurse})`)

Jake Read
committed
jQuery.get(recurse, htmlTreeDiver)
} else if (path.includes(extension)) {
if(debug) console.log('GG: likely item', path)

Jake Read
committed
// 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
committed
returned.push(path)

Jake Read
committed
}
if(debug) console.log('unreturned:', unreturned)
if(debug) console.log('returned:', returned)

Jake Read
committed
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
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)
}

Jake Read
committed
returned.sort()
if(debug) console.log('GG returns this', JSON.parse(JSON.stringify(returned)))

Jake Read
committed
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)

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, '\\"');
// 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) => {
// 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?
/*

Jake Read
committed
return new Promise((resolve, reject) => {
const script = document.createElement("script")
const tempGlobal = "__tempModuleLoadingVariable" + Math.random().toString(32).substring(2

Jake Read
committed
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)

Jake Read
committed
reject(new Error("Failed to load module script with URL " + url))
cleanup()
}
script.textContent = `import * as m from "${url}"; window.${tempGlobal}(m);`

Jake Read
committed
document.documentElement.appendChild(script)
})

Jake Read
committed
}
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