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)

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="/' + root + '/')
// rm first two, and last ()
paths = paths.slice(2) //
paths.forEach((path, index) => {
let end = path.indexOf('"')

Jake Read
committed
paths[index] = path.slice(0, end)

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

Jake Read
committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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