lipsumar / design-system-framework

9 stars 2 forks source link

Fix external component ids #22

Closed lipsumar closed 8 years ago

lipsumar commented 8 years ago

When specifying an external component like so

    "external-components":{
        "pure":{
            "components-path": "node_modules/pure/src",
            "glob":{
                "css":"**/*.css"
            }
        }
    }

The component ids will not be pure/foo as expected, but /complete/absolute/path/pure/src/foo.

An external component specified with component-path (singular) is fine.

lipsumar commented 8 years ago
--- lib/dsf.js  2016-04-29 14:12:02.000000000 +0200
+++ lib/dsf-patch.js    2016-04-29 14:13:57.000000000 +0200
@@ -33,7 +33,7 @@

-function fetchComponents(fromDir, config){
+function fetchComponents(fromDir, componentIdPrefix, config){
     if(!util.path.isDirectory(fromDir)){
         return;
     }
@@ -43,7 +43,7 @@
         var componentName = dirName;
         var componentDir = path.join(fromDir, componentName);

-        addComponentPathCandidate(componentDir, null, config);
+        addComponentPathCandidate(componentDir, componentIdPrefix ? componentIdPrefix + '/' + componentName : null, config);
     });
 }

@@ -68,10 +68,16 @@
     state.candidatesResolved++;

     if(candidate.isComponent){
+
+        var componentId = removeAbsPath(candidate.absPath, candidate.config['components-path']);
+        if(candidate.options.id){
+            componentId = candidate.options.id;
+        }
+
         var component = new Component({
             path: candidate.path,
             dsf: API,
-            id: candidate.options.id || removeAbsPath(candidate.absPath),
+            id: componentId,
             config: candidate.config
         });
         if(CONFIG.base && CONFIG.base.css){
@@ -102,8 +108,8 @@
     });
 }

-function removeAbsPath(absPath){
-    var componentsPathAbs = util.path.absolutePath(CONFIG['components-path']);
+function removeAbsPath(absPath, toRemove){
+    var componentsPathAbs = util.path.absolutePath(toRemove || CONFIG['components-path']);
     if(componentsPathAbs[componentsPathAbs.length-1]!=='/'){
         componentsPathAbs+='/';
     }
@@ -210,7 +216,7 @@
             if(CONFIG['external-components']){
                 _.each(CONFIG['external-components'], function(external, componentId){
                     if(external['components-path']){
-                        fetchComponents(external['components-path'], external);
+                        fetchComponents(external['components-path'], componentId, external);
                     }else if(external['component-path']){
                         addComponentPathCandidate(external['component-path'], componentId, external);
                     }