lib/plugins/aws/package/compile/events/apiGateway/lib/resources.js
function applyResource(resource, isMethod) {
      let root;
      let parent;
      let currentPath;
      const path = resource.path.replace(/^\//, '').replace(/\/$/, '');
      const pathParts = path.split('/');

      function applyNodeResource(node, parts, index) {
        const n = node;
        if (index === parts.length - 1) {
          n.name = resource.name;
          if (resource.resourceId) {
            n.resourceId = resource.resourceId;
            if (_.every(predefinedResourceNodes, (iter) => iter.path !== n.path)) {
              predefinedResourceNodes.push(node);
            }
          }
          if (isMethod && !node.hasMethod) {
            n.hasMethod = true;
            if (_.every(methodNodes, (iter) => iter.path !== n.path)) {
              methodNodes.push(node);
            }
          }
        }

        parent = node;
      }

      pathParts.forEach((pathPart, index) => {
        currentPath = currentPath ? `${currentPath}/${pathPart}` : pathPart;
        root = root || _.find(trees, (node) => node.path === currentPath);
        parent = parent || root;

        let node;
        if (parent) {
          if (parent.path === currentPath) {
            applyNodeResource(parent, pathParts, index);
            return;
          } else if (parent.children.length > 0) {
            node = _.find(parent.children, (n) => n.path === currentPath);
            if (node) {
              applyNodeResource(node, pathParts, index);
              return;
            }
          }
        }

        node = {
          path: currentPath,
          pathPart,
          parent,

          level: index,
          children: [],
        };

        if (parent) {
          parent.children.push(node);
        }

        if (!root) {
          root = node;
          trees.push(root);
        }

        applyNodeResource(node, pathParts, index);
      });
    }
function applyResource(resource, isMethod) {
      let root;
      let parent;
      let currentPath;
      const path = resource.path.replace(/^\//, '').replace(/\/$/, '');
      const pathParts = path.split('/');

      function applyNodeResource(node, parts, index) {
        const n = node;
        if (index === parts.length - 1) {
          n.name = resource.name;
          if (resource.resourceId) {
            n.resourceId = resource.resourceId;
            if (_.every(predefinedResourceNodes, (iter) => iter.path !== n.path)) {
              predefinedResourceNodes.push(node);
            }
          }
          if (isMethod && !node.hasMethod) {
            n.hasMethod = true;
            if (_.every(methodNodes, (iter) => iter.path !== n.path)) {
              methodNodes.push(node);
            }
          }
        }

        parent = node;
      }

      pathParts.forEach((pathPart, index) => {
        currentPath = currentPath ? `${currentPath}/${pathPart}` : pathPart;
        root = root || _.find(trees, (node) => node.path === currentPath);
        parent = parent || root;

        let node;
        if (parent) {
          if (parent.path === currentPath) {
            applyNodeResource(parent, pathParts, index);
            return;
          } else if (parent.children.length > 0) {
            node = _.find(parent.children, (n) => n.path === currentPath);
            if (node) {
              applyNodeResource(node, pathParts, index);
              return;
            }
          }
        }

        node = {
          path: currentPath,
          pathPart,
          parent,

          level: index,
          children: [],
        };

        if (parent) {
          parent.children.push(node);
        }

        if (!root) {
          root = node;
          trees.push(root);
        }

        applyNodeResource(node, pathParts, index);
      });
    }
function applyResource ( resource , isMethod ) { let root ; let parent ; let currentPath ; const path = resource . path . replace ( / ^\/ / , '' ) . replace ( / \/$ / , '' ) ; const pathParts = path . split ( '/' ) ; function applyNodeResource ( node , parts , index ) { const n = node ; if ( index === parts . length - 1 ) { n . name = resource . name ; if ( resource . resourceId ) { n . resourceId = resource . resourceId ; if ( _ . every ( predefinedResourceNodes , ( iter ) => iter . path !== n . path ) ) { predefinedResourceNodes . push ( node ) ; } } if ( isMethod && ! node . hasMethod ) { n . hasMethod = true ; if ( _ . every ( methodNodes , ( iter ) => iter . path !== n . path ) ) { methodNodes . push ( node ) ; } } } parent = node ; } pathParts . forEach ( ( pathPart , index ) => { currentPath = currentPath ? ` ${ currentPath } ${ pathPart } ` : pathPart ; root = root || _ . find ( trees , ( node ) => node . path === currentPath ) ; parent = parent || root ; let node ; if ( parent ) { if ( parent . path === currentPath ) { applyNodeResource ( parent , pathParts , index ) ; return ; } else if ( parent . children . length > 0 ) { node = _ . find ( parent . children , ( n ) => n . path === currentPath ) ; if ( node ) { applyNodeResource ( node , pathParts , index ) ; return ; } } } node = { path : currentPath , pathPart , parent , level : index , children : [ ] , } ; if ( parent ) { parent . children . push ( node ) ; } if ( ! root ) { root = node ; trees . push ( root ) ; } applyNodeResource ( node , pathParts , index ) ; } ) ; }
organize all resource paths into N-ary tree
organize all resource paths into N - ary tree