2016-05-25 1 views
0

Iveは、最大5レベルまでのディープディレクトリ構造を持つGulpを持つNode.jsアプリケーションを取得しました。ノードアプリケーションでのパスツリーの定義

残念ながら、私はGulpビルドのパスを定義するためのより良い方法を見つけることができませんでした。だから私は、だから、最後に、私は例えばpaths.src.libとしてのパスにアクセスすることができ、次のことをやって

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

を開始しました。

しかし、これはあまりにも面倒です。同じことを達成するためのより良い方法が必要です。

誰かがこれについて何かアドバイスできますか?

答えて

1
あなたはES2015はこの

プロキシのために機能を使用することができ

const proxyPath = require('../'); 

var tmp = proxyPath('/tmp'); 

console.log(tmp.cache['project-1']['..'].files + ''); // /tmp/cache/files 

プロキシコード:

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

クラスアレイ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(', ')); 
関連する問題