javascript - Defining path tree in Node application -


ive got node.js application gulp has deep directory structure 5 levels.

unfortunately, couldn't find better way define paths gulp build. started doing following,

var path = require('path'),      _joinpaths = function (parent, subpath) {         return parent.dir ? path.join(parent.dir, subpath) : subpath;     },      _subpath = function (parent, propname, subpath) {         subpath = subpath || propname;         parent[propname] = {             dir: _joinpaths(parent, subpath)         };     },      _entrypath = function (parent, entrypath) {         parent.entry = _joinpaths(parent, entrypath);     },      _pathpattern = function (parent, includes, excludes) {     };  function paths() {     var paths = {};      _subpath(paths, 'src', './');     _subpath(paths.src, 'lib');      // define more paths }; 

so in end can access paths paths.src.lib example.

however, looks way cumbersome. there has better way achieve same thing.

can give advice around ?

you can use es2015 features this

proxy:

const proxypath = require('../');  var tmp = proxypath('/tmp');  console.log(tmp.cache['project-1']['..'].files + ''); // /tmp/cache/files 

proxy code:

const path = require('path'); const fs = require('fs');  module.exports = structure;  const cache = new map();  function structure(root) {     var dir = path.resolve(root);     var dirs;      if (! cache.has(dir)) {         if (fs.existssync(dir)) {             dirs = fs.readdirsync(dir + '');         } else {             dirs = [];         }         cache.set(dir, dirs);     } else {         dirs = cache.get(dir);     }      function tostring() {         return dir;     }      return new proxy({}, {         has(target, prop) {             return dirs.indexof(prop) > -1;         },         ownkeys(target) {             return [...dirs];         },         get(target, prop) {             switch (prop) {                 case symbol.toprimitive:                 case 'tostring':                 case 'valueof':                     return tostring;                     break;                 default:                 if (typeof prop === 'string') {                     return structure(path.resolve(dir, prop));                 } else {                     return dir[prop];                 }             }         }     }); } 

class inherited array as npm module:

const fs = require('fs'); const path = require('path');  const dir = symbol('directoryarray.dir');  class directoryarray extends array {     // load directory structure if exists     constructor(dir) {         super();          this[dir] = path.resolve(dir);          if (fs.existssync(this[dir])) {             this.push(...fs.readdirsync(this[dir]));         }     }      // create directory array relative path     // supports '..' lookup     dir(dir) {         return new this.constructor(             path.resolve(                 this[dir], dir             )         );     }      tostring() {         return this[dir];     } }  directoryarray.dir = dir;   // usage example  var cwd = new directoryarray(process.cwd()); var test = cwd.dir('test');  console.log(cwd + ''); console.log(cwd.join(', ')); console.log(test + ''); console.log(test.join(', ')); 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -