From 9e2bcc1b4fe789a0c61640088d8d4a18cd341f19 Mon Sep 17 00:00:00 2001
From: Jake Read <jake.read@cba.mit.edu>
Date: Wed, 2 Oct 2019 11:36:40 -0400
Subject: [PATCH] server-side module lists
---
cf.js | 33 +++++++++++++--
filesys.js | 42 +++++++++++++++++++
gogetter.js | 96 ++------------------------------------------
hunks/manager.js | 9 +----
view/vcontextmenu.js | 3 +-
view/vptch.js | 2 +-
6 files changed, 79 insertions(+), 106 deletions(-)
create mode 100644 filesys.js
diff --git a/cf.js b/cf.js
index 35c9667..639fb61 100644
--- a/cf.js
+++ b/cf.js
@@ -2,6 +2,8 @@
const express = require('express')
const app = express()
+// our fs tools,
+const filesys = require('./filesys.js')
// will use these to figure where tf we are
const os = require('os')
let ifaces = os.networkInterfaces()
@@ -9,10 +11,33 @@ let ifaces = os.networkInterfaces()
// serve everything: https://expressjs.com/en/resources/middleware/serve-static.html
app.use(express.static(__dirname))
// if these don't exist, they get 'nexted' to any other 'middleware' we write
-app.get('/hunklist', (req, res) => {
- // we would fs/ through our list, and serve that,
- res.send('the-false-file-list')
+app.get('/fileList', (req, res) => {
+ try{
+ // we would fs/ through our list, and serve that,
+ filesys.getDirTree(req.query.path).then((list) => {
+ // take the query out of the front of the path, and swap all \ for /
+ for(i in list){
+ list[i] = list[i].substring(req.query.path.length + 1)
+ let decomp = list[i].split('\\')
+ let np = ''
+ for(l in decomp){
+ np += `/${decomp[l]}`
+ }
+ // wash of first / items and of file extension,
+ list[i] = np.substring(2, np.indexOf('.'))
+ }
+ res.send(list)
+ // ship up north,
+ }).catch((err) => {
+ res.send(err)
+ // ship err back
+ })
+ } catch (err) {
+ console.log(err)
+ res.send('server-side error retrieving list')
+ }
})
+
// we also handle file-saving this way,
app.put('/save/systems/:file', (req, res) => {
console.log('put req to ' + req.params.file)
@@ -22,7 +47,7 @@ let port = 8080
// and listen,
app.listen(port)
-// want to announce our existence,
+// want to announce our existence, this just logs our IPs to the console:
Object.keys(ifaces).forEach(function(ifname) {
var alias = 0;
diff --git a/filesys.js b/filesys.js
new file mode 100644
index 0000000..303217a
--- /dev/null
+++ b/filesys.js
@@ -0,0 +1,42 @@
+// do file management
+
+const fs = require('fs')
+
+module.exports = {
+ // get a tree: takes the root (relative the process) and returns all branches below,
+ // includes route-to-root in list
+ getDirTree: (dir, debug) => {
+ return new Promise((resolve, reject) => {
+ // items and count,
+ let list = []
+ let count = 0
+ // recursor,
+ let launch = (dir) => {
+ if(debug) console.log('GDT launch at', dir)
+ // just counting actually,
+ count ++
+ fs.readdir(dir, (err, files) => {
+ if(err){
+ reject(err)
+ }
+ count --
+ for (file of files) {
+ if (file.includes('.')) {
+ if(debug) console.log('GDT pushing', `${dir}\\${file}`)
+ list.push(`${dir.substring(__dirname.length)}\\${file}`)
+ } else {
+ launch(`${dir}\\${file}`)
+ }
+ }
+ if(debug) console.log('GDT size', count)
+ if(!count){
+ // we sort,
+ list.sort()
+ resolve(list)
+ }
+ }) // end fs.readdir
+ } // end launch
+ launch(`${__dirname}\\${dir}`)
+ })
+ }
+}
diff --git a/gogetter.js b/gogetter.js
index 3fbcc2f..f71cf1b 100644
--- a/gogetter.js
+++ b/gogetter.js
@@ -5,103 +5,15 @@ function GoGetter() {
this.interpreterName = 'cuttlefish'
this.interpreterVersion = 'v0.1'
- this.getHunkList = () => {
+ this.recursivePathSearch = (root, debug) => {
return new Promise((resolve, reject) => {
- jQuery.get('hunklist', (resp) => {
- console.log('req for hunklist returns', resp)
+ jQuery.get(`/fileList?path=${root}`, (resp) => {
+ console.log('resp at jq', resp)
+ resolve(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
- 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);
- let igot = unreturned.indexOf(header)
- if (debug) console.log('GG: unreturned', unreturned)
- if (igot != -1) {
- 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)
- paths[index] = path.slice(0, end)
- })
-
- if(debug) console.log('paths', paths)
-
- 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))
- //console.log('getting', recurse)
- 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})`)
- returned.push(path)
- }
- }
- })
-
- 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
- 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)
- }
- }
- returned.sort()
- 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.
diff --git a/hunks/manager.js b/hunks/manager.js
index 05850c2..06dd4d9 100644
--- a/hunks/manager.js
+++ b/hunks/manager.js
@@ -46,18 +46,11 @@ function Manager() {
// for now, managers take callback paths to pipe data back through ?
let getListOfAvailableComponents = () => {
return new Promise((resolve, reject) => {
- gg.getHunkList().then((list) => {
+ gg.recursivePathSearch('hunks').then((list) => {
resolve(list)
}).catch((err) => {
reject(err)
})
- /*
- gg.recursivePathSearch('hunks', '.js').then((list) => {
- resolve(list)
- }).catch((err) => {
- reject(err)
- })
- */
})
}
diff --git a/view/vcontextmenu.js b/view/vcontextmenu.js
index 3181abc..f666d9c 100644
--- a/view/vcontextmenu.js
+++ b/view/vcontextmenu.js
@@ -138,6 +138,7 @@ function cfContextMenu(evt, view, dt) {
/* ---------------- LOCAL OPTIONS, for SCOPE ----------------- */
/* --------------------------- ---------------------------- */
+ /*
addContextOption(`debug option`, (ce) => {
jQuery.ajax({
url: `/save/systems/sysname.json`,
@@ -154,6 +155,7 @@ function cfContextMenu(evt, view, dt) {
}
})
})
+ */
addContextOption(`<i class="em em-twisted_rightwards_arrows"></i> refresh the view`, (ce) => {
// this actually gets wiped when the first hunk arrives, so
@@ -174,7 +176,6 @@ function cfContextMenu(evt, view, dt) {
// req a new hunk,
addContextOption('<i class="em em-construction_worker"></i> add a hunk', (ce) => {
- //vw.msgbox.write('requested a list of hunks...')
$(ce.target).closest('li').text('requesting a list of hunks...')
scope.requestListAvail().then((stringlist) => {
changeContextTitle('load:')
diff --git a/view/vptch.js b/view/vptch.js
index 646982e..5e3e122 100644
--- a/view/vptch.js
+++ b/view/vptch.js
@@ -190,7 +190,7 @@ function PatchSet(View, MsgBox) {
if (!existing) {
try {
//console.log('add conn from', opHunkIndex, opIndex, inHunkIndex, inIndex)
- // TODO: sometimes hunks change, and inputs go away ... we need to catch those errs here
+ // TODO: sometimes hunks change, and inputs go away ... we need to catch those errs here
await view.requestAddLink(opDef, ipDef)
} catch (err) {
reject(err)
--
GitLab