2016-05-25 11 views
0

Ich habe eine Node.js-Anwendung mit Gulp, die eine tiefe Verzeichnisstruktur bis zu etwa 5 Ebenen hat.Pfadbaum in Knoten-Anwendung definieren

Leider konnte ich keinen besseren Weg finden, die Pfade für den Gulp Build zu definieren. Also begann ich folgende tun,

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

Also am Ende kann ich die Pfade Zugriff als paths.src.lib zum Beispiel.

Dies sieht jedoch viel zu umständlich. Es muss einen besseren Weg geben, dasselbe zu erreichen.

Kann jemand einen Rat geben?

Antwort

1

Sie können ES2015 für dieses

Proxy Funktionen verwenden:

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]; 
       } 
      } 
     } 
    }); 
} 

Klasse von Array geerbt as npm module:

const fs = require('fs'); 
const path = require('path'); 

const DIR = Symbol('DirectoryArray.Dir'); 

class DirectoryArray extends Array { 
    // Load directory structure if it exists 
    constructor(dir) { 
     super(); 

     this[DIR] = path.resolve(dir); 

     if (fs.existsSync(this[DIR])) { 
      this.push(...fs.readdirSync(this[DIR])); 
     } 
    } 

    // Create Directory array from relative path 
    // supports '..' for 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(', ')); 
Verwandte Themen