Skip to content
Snippets Groups Projects
filesys.js 2.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jake Read's avatar
    Jake Read committed
    /*
    filesys.js
    
    Jake Read at the Center for Bits and Atoms
    (c) Massachusetts Institute of Technology 2019
    
    This work may be reproduced, modified, distributed, performed, and
    displayed for any purpose, but must acknowledge the squidworks and cuttlefish projects.
    Copyright is retained and must be preserved. The work is provided as is;
    no warranty is provided, and users accept all liability.
    */
    
    // server-side file management, just a brief wrap on node fs 
    
    Jake Read's avatar
    Jake Read committed
    
    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) => {
    
    Jake Read's avatar
    Jake Read committed
        let tld = dir
    
    Jake Read's avatar
    Jake Read committed
        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)
    
    Jake Read's avatar
    Jake Read committed
            // just counting actually,
    
    Jake Read's avatar
    Jake Read committed
            fs.readdir(dir, (err, files) => {
    
    Jake Read's avatar
    Jake Read committed
                reject(err)
              }
    
    Jake Read's avatar
    Jake Read committed
                for (file of files) {
                  if (file.includes('.')) {
                    let listAddition = `${dir.substring(__dirname.length + tld.length)}/${file}`
    
                    if (debug) console.log('GDT pushing', listAddition)
    
    Jake Read's avatar
    Jake Read committed
                    list.push(listAddition)
                  } else {
                    let launchPoint = `${dir}${file}`
    
                    if (debug) console.log('GDT launching', launchPoint)
    
    Jake Read's avatar
    Jake Read committed
                    launch(launchPoint)
                  }
    
    Jake Read's avatar
    Jake Read committed
                }
    
    Jake Read's avatar
    Jake Read committed
              } catch (err) {
                // just walk on ...
                console.log('walkover error:', err)
                console.log('problem dir:', dir)
                // and push up, give up on counting
                list.sort()
                resolve(list)
    
    Jake Read's avatar
    Jake Read committed
              }
    
              if (debug) console.log('GDT size', count)
              if (!count) {
    
    Jake Read's avatar
    Jake Read committed
                // we sort,
                list.sort()
    
                if(debug) console.log('list at fin getDirTree', list)
    
    Jake Read's avatar
    Jake Read committed
                resolve(list)
              }
            }) // end fs.readdir
          } // end launch
    
          launch(`${__dirname}/${dir}`)