Code coverage report for lib/resources.js

Statements: 89.46% (382 / 427)      Branches: 81.15% (211 / 260)      Functions: 93.33% (56 / 60)      Lines: 89.46% (382 / 427)     

All files » lib/ » resources.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978    1                                                           1 4 4 4 4   4         4 4 4         4 4   4 4 4   4     1     1 1           1 4 1         1 2 82 82 30   52 52         1 1 1     4       1 1 1                     1 1               1 1     1 1   1 8   8 130 130 7 7   130 80   50 28     8 130 130 110   20 20         1 1   1 1 1 7 7 7     1                 1 48   48       5 5 5         1       206 206     206 24     182                   182 5 5 5 5       177 117 117 117         182 182       345 345             345 150     150 150   3     150 291     291 3 288   9 9 27     27   279   189 90   81     291 300   300     300 300 300     150     345     345 195     150   150   150       4 4             4   4 48                               48 16 16 16   16               48 6   6 6 6         48 6 6   6   6 6       48 8         8 8           48 48 40   40 40   40 8 8       40   40 40     40             40       40   8     40   6     40 8 8   32 32 32           48 16       48       4 4   4 36     36 28     8         8 48 48     8   8     4 4       4 4       4 4       140 140       4 4     4     4 4 4 4 4 4       4 4 4       4   4       4 4 1 1   4 4       4 1     1   1         4 1 1       4 1       1     3       4             4         4                             4 4   4 3     3   3         4 4       4 3 3     4 18   18       18   3 3 15   15   9 6 6     18 30     30 30             104 104             104 384 384 72   384   104 156 156 384 384       104       48 48                       48       48 48     224           2496           48 48 48 48 48     48   48 48 48 376 320     48     48   48                     48 376 288   88     8   8 6   8   16 16 12   16   8 6   8   40     40   40                   48       24 24 32 32 48 48 48           8 8                   8         8 8     416     8 8     8   8 8     8       8   8 8     8           8       8         8   2     8 8         8 8       4 4         4 1     3   3 156 156     87       3       4 4                 4     4     4 1     3 3     3             3 1101           1101 969 969 5004 924 924       132     969 924         924   99 99 99   3                               99 99 99     99 141     99             3       1
/*jslint nomen:true, forin: true, continue: true, stupid: true */
 
var Y = require('yui').YUI({useSync: true}).use('base-base'),
    YUIFactory = require('mojito/lib/yui-sandbox.js'),
    libstore = require('mojito/lib/store.js'),
    libpath = require('path'),
    fs = require('fs'),
    bootstrap = require('./bootstrap/bootstrap.js'),
    YUI_ALIASES = require('./yui.js').ALIASES,
    DEFAULT_RESOURCES = {
        'controllers': true,
        'binders': true,
        'views': true,
        'langs': true,
        'assets': {
            js: true,
            css: true
        },
        'mojito': {
            'mojito-client': true
        },
        'yui': {
            'yui-bootstrap-override': true,
            'yui-base': true,
            'loader-base': true,
            'loader-yui3': true,
            'node': true,
            'yui-bootstrap-core': true
        },
        'bootstrap': true
    };
 
function ShakerResources(context, root, logger) {
    'use strict';
    this.logger = logger;
    this.cwd = process.cwd();
    this.root = root;
 
    this.store = this._makeStore({
        root: this.root,
        context: context
    });
 
    this.context = context;
    this.shakerConfig = this._getShakerConfig(context);
    this.organizedResources = {
        app: {},
        mojito: {},
        yui: {}
    };
    this.appResources = {};
    this.loaderResources = {};
 
    this._mojitoResources = {};
    this._yuiResources = {};
    this._bootstrapResources = bootstrap.resources(root);
 
    this._getResources();
}
 
ShakerResources.prototype = {
 
    _updateLocations: function (locationMap) {
        'use strict';
        var originalProduceMeta = this.store.yui._produceMeta,
            base = '/' + this.shakerConfig.app.prefix + '/',
            self = this,
            processedLangs = {};
 
        // replace yui RS addon's _produceMeta function
        this.store.yui._produceMeta = function (name, lang, appMetaData, yuiMetaData) {
            if (!processedLangs[lang]) {
                var meta = {
                    base: JSON.parse(appMetaData.base[lang]),
                    full: JSON.parse(appMetaData.full[lang])
                };
 
                Y.Object.each(meta, function (subMeta) {
                    Y.Object.each(subMeta, function (module, moduleName) {
                        var url = base + module.path;
                        if (locationMap[url]) {
                            module.path = locationMap[url].split('/').pop();
                        } else {
                            delete module.path;
                            module.fullpath = url;
                        }
                    });
                });
 
                appMetaData.base[lang] = JSON.stringify(meta.base);
                appMetaData.full[lang] = JSON.stringify(meta.full);
                processedLangs[lang] = true;
            }
 
            return originalProduceMeta(name, lang, appMetaData, yuiMetaData);
        };
 
        // empty appModulesDetails to prevent warning messages
        this.store.yui.appModulesDetails = {};
        this.store.resolveResourceVersions();
        this.store.yui._produceMeta = originalProduceMeta;
    },
 
    resolveResourceVersions: function (resourcesMap) {
        'use strict';
        // empty appModulesDetails to prevent warning messages
        this.store.yui.appModulesDetails = {};
        this.store.resolveResourceVersions(resourcesMap);
    },
 
    getLoaderResources: function (locationsMap) {
        'use strict';
        var ress,
            m,
            mojit,
            mojits = [],
            yuiModules,
            seedModules,
            moduleRess = {};
 
        this._updateLocations(locationsMap);
        yuiModules = this.store.yui.getYUIURLDetails();
 
        //mojito doesnt provide default lang in build time.
        this.context.lang = this.context.lang || 'en';
        seedModules = this.store.yui.getAppSeedFiles(this.context);
 
        function processRess(ress) {
            var r,
                res;
            for (r = 0; r < ress.length; r += 1) {
                res = ress[r];
                if (res.type === 'view') {
                    res.yui = {name: res.url};
                    moduleRess[res.url] = res;
                }
                if ('common' !== res.affinity.affinity) {
                    continue;
                }
                if (res.yui && res.yui.name) {
                    moduleRess[res.yui.name] = res;
                }
            }
            for (r = 0; r < ress.length; r += 1) {
                res = ress[r];
                if ('client' !== res.affinity.affinity) {
                    continue;
                }
                Eif (res.yui && res.yui.name) {
                    moduleRess[res.yui.name] = res;
                }
            }
        }
 
        ress = this.store.getResourceVersions({});
        processRess(ress);
 
        mojits = this.store.listAllMojits();
        mojits.push('shared');
        for (m = 0; m < mojits.length; m += 1) {
            mojit = mojits[m];
            ress = this.store.getResourceVersions({mojit: mojit});
            processRess(ress);
        }
 
        for (m = 0; m < seedModules.length; m += 1) {
            ress = yuiModules[seedModules[m]];
 
            if (ress) {
                moduleRess[seedModules[m]] = ress;
            }
        }
 
        // get loader resources
        for (m in moduleRess) {
            ress = moduleRess[m];
            // only add loader resources
            if (m.indexOf('loader') === 0) {
                // get loader only if langs are on
                // or loader is not language specific
                // or loader is specific to en_US (this is the default language so it should be included)
                Eif ((this.resources && this.resources.langs) || ress.yui.name.indexOf('_') === -1  || ress.yui.name.indexOf('_en-US') !== -1) {
                    ress.type = 'loader';
                    this.loaderResources[m] = this._addResource(ress, 'js');
                }
 
            }
        }
        return this.loaderResources;
    },
 
    _addResource: function (rawResource, subtype) {
        'use strict';
        var self = this,
            resource = this.appResources[rawResource.url];
 
        if (resource) {
            return resource;
        }
 
        resource = {
            basename: rawResource.source.fs.basename,
            url: rawResource.url,
            path: rawResource.source.fs.fullPath,
            relativePath: libpath.relative(self.cwd, rawResource.source.fs.fullPath),
            type: rawResource.type,
            subtype: subtype,
            selector: rawResource.selector
        };
 
        if (rawResource.subtype === 'synthetic') {
            resource.read = function (callback) {
                self.store.getResourceContent(self.store.makeStaticHandlerDetails(rawResource), function (err, content) {
                    resource.content = content.toString();
                    callback(err, resource.content);
                });
            };
        } else {
            resource.read = function (callback) {
                fs.readFile(resource.path, 'utf8', function (err, content) {
                    resource.content = content;
                    callback(err, content);
                });
            };
        }
 
        this.appResources[rawResource.url] = resource;
        return resource;
    },
 
    _storeYUIModule: function (rawResource, yuiResources) {
        'use strict';
        var resource,
            requiredResourceName,
            requiredResource,
 
            dependencies = {},
            self = this;
 
        function getDependencies(rawResource) {
            var dependencies = {},
                module = {};
 
            try {
                module = self.store.yui._makeYUIModuleConfig('client', rawResource);
            } catch (e) {
                module.requires = [];
            }
 
            Y.Array.each(module.requires, function (requiredModule) {
                var rawDependencies;
 
                // dependency is found in given yui resources list
                if (yuiResources[requiredModule]) {
                    rawDependencies = [yuiResources[requiredModule]];
                } else if (YUI_ALIASES[requiredModule]) {
                    // dependency is an alias for various yui modules
                    rawDependencies = [];
                    Y.Array.each(YUI_ALIASES[requiredModule], function (module) {
                        Iif (!self._yuiResources[module]) {
                            return;
                        }
                        rawDependencies.push(self._yuiResources[module].getRawResource());
                    });
                } else if (self._yuiResources[requiredModule]) {
                    // dependency is a yui module
                    rawDependencies = [self._yuiResources[requiredModule].getRawResource()];
                } else if (self._mojitoResources[requiredModule]) {
                    // dependency is a mojito module
                    rawDependencies = [self._mojitoResources[requiredModule]];
                }
 
                Y.Array.each(rawDependencies, function (rawDependency) {
                    var dependency;
 
                    Iif (!rawDependency) {
                        return;
                    }
                    dependency = self._storeYUIModule(rawDependency, yuiResources);
                    dependencies = Y.merge(dependencies, dependency.dependencies);
                    dependencies[dependency.url] = dependency;
                });
            });
            return dependencies;
        }
 
        rawResource.url = rawResource.url || 'yui-module--' + rawResource.name;
 
 
        if (self.appResources[rawResource.url]) {
            return self.appResources[rawResource.url];
        }
 
        resource = self._addResource(rawResource, 'js');
 
        resource.dependencies = getDependencies(rawResource);
 
        return resource;
    },
 
    _getResources: function () {
        'use strict';
        var self = this,
            mojits = self._getMojits(),
            contexts = self._getContexts(),
            shakenMojit = {},
            shakenApp = {},
            poslVisited = {};
 
        self.logger.info('Retrieving Application Resources');
 
        function addMojitResources(mojit, shakenMojit, poslStr) {
            var resource,
                mojitResources = {},
                controllerResources = [],
                d,
                b,
                a,
                l,
                langs = [],
                dependencies,
                rawResource,
                action,
                assetAction,
                pathArray,
                containingDir;
 
            // Get view resources
            for (action in shakenMojit.actions) {
                mojitResources[action] = {};
                Eif (!Y.Object.isEmpty(shakenMojit.assets)) {
                    mojitResources[action].assets = {};
                }
                Iif (shakenMojit.clientAffinity && shakenMojit.views[action]) {
                    rawResource = shakenMojit.views[action];
                    resource = self._addResource(rawResource, 'html');
                    mojitResources[action].view = resource;
                }
            }
 
            // Get controller resources
            if (!Y.Object.isEmpty(shakenMojit.controller)) {
                rawResource = shakenMojit.controller;
                //mojitBelongsInPOSL |= rawResource.selector === minorSelector;
                resource = self._storeYUIModule(rawResource, shakenMojit.yuiResources, self.appResources);
                for (action in shakenMojit.actions) {
                    mojitResources[action].controller = resource;
                }
            }
 
            // Get binder resources
            for (b in shakenMojit.binders) {
                rawResource = shakenMojit.binders[b];
                action = rawResource.name;
                //mojitBelongsInPOSL |= rawResource.selector === minorSelector;
                resource = self._storeYUIModule(rawResource, shakenMojit.yuiResources, self.appResources);
                // TODO: name of binder potentially does not have correponding view?
                mojitResources[action] = mojitResources[action] || {};
                mojitResources[action].binder = resource;
            }
 
            // Get lang resources
            if (shakenMojit.clientAffinity) {
                for (l in shakenMojit.langs) {
                    rawResource = shakenMojit.langs[l];
                    resource = self._storeYUIModule(rawResource, shakenMojit.yuiResources, self.appResources);
                    langs.push(resource);
                }
                for (action in shakenMojit.actions) {
                    mojitResources[action].langs = langs;
                }
            }
 
            // Get assets resources
            // only get assets if specified in the config
            Eif (self.shakerConfig.resources && self.shakerConfig.resources.assets) {
                for (a in shakenMojit.assets) {
                    rawResource = shakenMojit.assets[a];
 
                    assetAction = null;
                    for (action in shakenMojit.actions) {
                        // check if the asset belongs to a specific action
                        if (rawResource.name.indexOf(action) === 0) {
                            assetAction = action;
                            break;
                        }
                    }
 
                    resource = self._addResource(rawResource, rawResource.subtype);
 
                    pathArray = resource.path.split('/');
                    containingDir = pathArray[pathArray.length - 2];
 
                    // determine if resource is not to be inclucded
                    Iif (resource.basename.indexOf('-void') !== -1 || containingDir.indexOf('void') !== -1) {
                        // remove resource from appResources
                        delete self.appResources[resource.url];
                        continue;
                    }
 
                    // mark resource as manual inline if specified by name or containing directory
                    Iif (resource.basename.indexOf('-manual-inline') !== -1 || containingDir.indexOf('manual-inline') !== -1) {
                        resource.inline = true;
                        // manual inline resource should not be included in assets since they are added manually
                        continue;
                    } else if (resource.basename.indexOf('-inline') !== -1 || containingDir.indexOf('inline') !== -1) {
                        // mark resource as inline if specified by name or containing directory
                        resource.inline = true;
                    }
                    // mark resource as typeInline if this type of resource is specified in the config to be inlined
                    if ((resource.subtype === 'css' && self.shakerConfig.resources.assets.css === 'inline')
                            || (resource.subtype === 'js' && self.shakerConfig.resources.assets.js === 'inline')) {
                        resource.typeInline = true;
                    }
 
                    if (assetAction) {
                        mojitResources[assetAction].assets[resource.subtype] = mojitResources[assetAction].assets[resource.subtype] || {};
                        mojitResources[assetAction].assets[resource.subtype][rawResource.url] = resource;
                    } else {
                        for (action in shakenMojit.actions) {
                            mojitResources[action].assets[resource.subtype] = mojitResources[action].assets[resource.subtype] || {};
                            mojitResources[action].assets[resource.subtype][rawResource.url] = resource;
                        }
                    }
                }
 
                // sort assets for each action
                for (action in mojitResources) {
                    self._sortAssets(mojitResources[action].assets);
                }
            }
 
            self.organizedResources.app[poslStr].mojits[mojit] = mojitResources;
        }
 
 
        self._getMojitoResources();
        self._getYUIResources();
 
        Y.Array.each(contexts, function (context) {
            var posl = self._getPOSLFromContext(context),
                poslStr = posl.join('-');
 
            if (poslVisited[poslStr]) {
                return;
            }
 
            self.organizedResources.app[poslStr] = {
                app: {},
                mojits: {}
            };
 
            Y.Array.each(mojits, function (mojit) {
                shakenMojit = self._shakeMojit(mojit, context);
                addMojitResources(mojit, shakenMojit, poslStr);
            });
 
            self._shakeApp(context, poslStr);
 
            poslVisited[poslStr] = true;
        });
 
        self._shakeMojito();
        self._shakeYUI();
    },
 
    _getContexts: function () {
        'use strict';
        return this.store.selector._listUsedContexts();
    },
 
    _getMojits: function () {
        'use strict';
        return this.store.listAllMojits();
    },
 
    _getPOSLFromContext: function (context) {
        'use strict';
        return this.store.selector.getPOSLFromContext(context);
    },
 
    _makeStore: function (cfg) {
        'use strict';
        var originalGetYUI = YUIFactory.getYUI,
            store;
 
        this.logger.info('Starting Resource Store');
 
        // hack for turning off yui logs
        YUIFactory.getYUI = function () {
            var YUI = originalGetYUI();
            YUIFactory.getYUI = originalGetYUI;
            return function (config) {
                config.debug = false;
                return YUI(config);
            };
        };
 
        process.shakerCompiler = true;
        store = libstore.createStore(cfg);
        return store;
    },
 
    _getShakerConfig: function (context) {
        'use strict';
 
        var appConfig = this.store.getAppConfig(context || {}),
            shakerConfig = appConfig.shaker || {};
 
        // set default resources
        shakerConfig.resources = shakerConfig.resources === false ? false : shakerConfig.resources || {};
        if (!Y.Lang.isObject(shakerConfig.resources) && !Y.Lang.isBoolean(shakerConfig.resources)) {
        	this.logger.error('Config Validation', 'resources option should be an object. Using default resources.');
        	shakerConfig.resources = {};
        }
        Eif (shakerConfig.resources) {
            shakerConfig.resources = shakerConfig.resources === true || shakerConfig.resources === null ?
                    {} : shakerConfig.resources;
 
            // initialize configuration for each type of resource
            Y.Object.each(shakerConfig.resources, function (config, resourceType) {
                Iif (config === true) {
                    // delete so that it is replaced by default
                    delete shakerConfig.resources[resourceType];
                } else Eif (config !== false) {
                    // make sure the configuration is an object
                    shakerConfig.resources[resourceType] = config || {};
                }
            });
 
            // if there is no location then yui and mojito resources are not necessary
            if (!shakerConfig.locations) {
                shakerConfig.resources.yui = false;
                shakerConfig.resources.mojito = false;
            }
 
            // if there is no location and no tasks then only the assets resources are necessary
            if (!shakerConfig.tasks && !shakerConfig.locations) {
                shakerConfig.resources = {
                    assets: shakerConfig.resources.assets || {}
                };
 
                Y.mix(shakerConfig.resources.assets, DEFAULT_RESOURCES.assets, false, null, 0, true);
            } else {
                // add default configuration for missing configs
                Y.mix(shakerConfig.resources, DEFAULT_RESOURCES, false, null, 0, true);
            }
 
            // if bootstrap is off remove bootstrap from yui
            Iif (!shakerConfig.resources.bootstrap && shakerConfig.resources.yui) {
                delete shakerConfig.resources.yui['yui-bootstrap-override'];
                delete shakerConfig.resources.yui['yui-bootstrap-core'];
            }
 
        }
 
        shakerConfig.app = {
            appName: this.store.url.config.appName,
            prefix: this.store.url.config.prefix,
            root: this.root
        };
        return shakerConfig;
    },
 
    _getStoreConfigs: function (context) {
        'use strict';
        var appConfig = this.store.getAppConfig(context);
 
        return {
            prefix: this.store.url.config.prefix,
            appName: appConfig.staticHandling.appName,
            context: context || {}
        };
    },
 
    _shakeMojito: function () {
        'use strict';
        var self = this;
 
        Y.Object.each(self.shakerConfig.resources.mojito, function (includeModule, mojitoModule) {
            Iif (!includeModule) {
                return;
            }
            var rawResource = self._mojitoResources[mojitoModule],
                resource = self._storeYUIModule(rawResource, {});
            self.organizedResources.mojito[rawResource.url] = resource;
        });
    },
 
    _shakeYUI: function () {
        'use strict';
        var self = this,
            resource;
 
        // add inline bootstrap if bootstrap is on
        if (self.shakerConfig.resources && self.shakerConfig.resources.bootstrap) {
            resource = self._addResource(self._bootstrapResources['yui-bootstrap-inline'], 'js');
            resource.inline = true;
        }
 
        Y.Object.each(self.shakerConfig.resources.yui, function (includeModule, yuiModule) {
            var rawResources;
 
            Iif (!includeModule) {
                return;
            }
 
            if (YUI_ALIASES[yuiModule]) {
                // yuiModule is an alias for various yui modules
                rawResources = [];
                Y.Array.each(YUI_ALIASES[yuiModule], function (module) {
                    rawResources.push(self._yuiResources[module].getRawResource());
                });
            } else if (self._yuiResources[yuiModule]) {
                // add yui module
                rawResources = [self._yuiResources[yuiModule].getRawResource()];
            } else Eif (self._bootstrapResources[yuiModule]) {
                rawResources = [self._bootstrapResources[yuiModule]];
            }
 
            Y.Array.each(rawResources, function (rawResource) {
                Iif (!rawResource) {
                    return;
                }
                resource = self._storeYUIModule(rawResource, {});
                self.organizedResources.yui[resource.url] = resource;
            });
        });
    },
 
    // takes a list of resources and returns only those that match a context
    _filterResourceVersionsByContext: function (ctx, ress) {
        'use strict';
        var posl = this._getPOSLFromContext(ctx),
            bySelector = {},    // selector: id: resource
            out = {},           // id: resource
            r,
            res,
            s,
            selector;
        for (r = 0; r < ress.length; r += 1) {
            res = ress[r];
            if (!bySelector[res.selector]) {
                bySelector[res.selector] = {};
            }
            bySelector[res.selector][res.id] = res;
        }
        for (s = posl.length; s > 0; s -= 1) {
            selector = posl[s - 1];
            for (r in bySelector[selector]) {
                Eif (bySelector[selector].hasOwnProperty(r)) {
                    out[r] = bySelector[selector][r];
                }
            }
        }
        return Y.Object.values(out);
    },
 
    _shakeMojit: function (mojitName, context) {
        'use strict';
        var self = this,
            rawResources,
            yuiResources, //transform yui modules on a hash object using key as yui-name
            mojitResources = {
                clientAffinity: false,
                actions: {},
                binders: {},
                langs: {},
                assets: {},
                views: {}
            };
 
        Iif (!self.shakerConfig.resources) {
            return mojitResources;
        }
 
        function getMojitResourcesByContext(mojitName, ctx) {
            var mojitResources = self.store.getResourceVersions({mojit: mojitName}),
                mojitResourcesFilterIterator = function (item) {
                    //remove mojito Framework + shared (app level) assets
                    return !(item.source.pkg.name === 'mojito' || item.source.pkg.name === 'mojito-shaker' ||
                        (item.mojit === 'shared' && item.type === 'asset')) &&
                        (item.affinity.affinity === 'common' || item.affinity.affinity === 'client');
                },
                sharedResources = self.store.getResourceVersions({mojit: 'shared'}),
                sharedResourcesFilterIterator = function (item) {
                    return !(item.source.pkg.name === 'mojito' || item.source.pkg.name === 'mojito-shaker' ||
                        item.type === 'asset') &&
                        (item.affinity.affinity === 'common' || item.affinity.affinity === 'client');
                };
 
            // Return the resources in the 'shared' mojit too, since Mojito considers those as part of each mojit.
            mojitResources = mojitResources.filter(mojitResourcesFilterIterator);
            mojitResources = self._filterResourceVersionsByContext(ctx, mojitResources);
            sharedResources = sharedResources.filter(sharedResourcesFilterIterator);
            sharedResources = self._filterResourceVersionsByContext(ctx, sharedResources);
            return sharedResources.concat(mojitResources);
        }
 
        rawResources = getMojitResourcesByContext(mojitName, context);
 
        function getYUIModuleNamesFromResources(rawResources) {
            var cleanList = {};
            rawResources.forEach(function (item, key) {
                if (item.yui && item.yui.name) {
                    cleanList[item.yui.name] = item;
                }
            });
            return cleanList;
        }
 
        mojitResources.yuiResources = getYUIModuleNamesFromResources(rawResources);
 
        function matchDependencies(sortedNeededYui, yuiModules) {
            var matched = {};
            Y.Array.each(sortedNeededYui, function (name, index) {
                if (yuiModules[name]) {
                    matched[name] = yuiModules[name];
                }
            });
            return matched;
        }
 
        // group all dependencies
        rawResources.forEach(function (rawResource) {
            if (rawResource.mojit === 'shared') {
                return;
            }
            switch (rawResource.type) {
            case 'controller':
                // determine affinity from controller
                mojitResources.clientAffinity = true;
                // we create some properties due to the way _precomputeYUIDependencies works.
                if (self.shakerConfig.resources.controllers) {
                    mojitResources.controller = rawResource;
                }
                break;
            case 'view':
                mojitResources.actions[rawResource.name] = true; // capture the mojit actions
                if (self.shakerConfig.resources.views) {
                    mojitResources.views[rawResource.url] = rawResource;
                }
                break;
            case 'binder':
                if (self.shakerConfig.resources.binders) {
                    mojitResources.binders[rawResource.url] = rawResource;
                }
                break;
            case 'asset':
                Eif (self.shakerConfig.resources.assets &&
                        ((rawResource.subtype === 'js' && self.shakerConfig.resources.assets.js) ||
                        (rawResource.subtype === 'css' && self.shakerConfig.resources.assets.css))) {
                    mojitResources.assets[rawResource.url] = rawResource;
                }
                break;
            case 'yui-lang':
                // TODO: langs
                if (self.shakerConfig.resources.langs === true) {
                    mojitResources.langs[rawResource.url] = rawResource;
                }
                break;
            }
        });
 
        return mojitResources;
    },
 
    _sortAssets: function (assets) {
        'use strict';
        Y.Object.each(assets, function (typeAssets, type) {
            var sortedKeys = Object.keys(typeAssets).sort();
            Y.Array.each(sortedKeys, function (key) {
                var asset = typeAssets[key];
                delete typeAssets[key];
                typeAssets[key] = asset;
            });
        });
    },
 
    _shakeApp: function (context, poslStr) {
        'use strict';
        var self = this,
            appResources,
            assets = {},
            i,
            rawResource,
            resource,
            pathArray,
            containingDir;
 
        // dont add app assets if specified by config
        Iif (!self.shakerConfig.resources || !self.shakerConfig.resources.assets) {
            self.organizedResources.app[poslStr].app = assets;
            return;
        }
 
        function getAppResourcesByContext(context, poslStr) {
            var rawResources = self.store.getResourceVersions({mojit: 'shared'}),
                appResourcesFilterIterator = function (item) {
                    //remove mojito Framework + app autoloads (included in the mojits already)
                    return item.source.pkg.name !== 'mojito' && item.type !== 'yui-module' &&
                        (item.affinity.affinity === 'common' || item.affinity.affinity === 'client');
                };
            rawResources = rawResources.filter(appResourcesFilterIterator);
            return self._filterResourceVersionsByContext(context, rawResources);
        }
 
        appResources = getAppResourcesByContext(context);
 
        for (i in appResources) {
            rawResource = appResources[i];
            // get asset type resources and ignore resources under assets/compiled
            // skip type of resource as specified by config
            Eif (rawResource.type === 'asset' && rawResource.source.fs.fullPath.indexOf('assets/compiled') === -1 &&
                    ((rawResource.subtype === 'js' && self.shakerConfig.resources.assets.js) ||
                    (rawResource.subtype === 'css' && self.shakerConfig.resources.assets.css))) {
                //mojitBelongsInPOSL |= rawResource.selector === minorSelector;
                resource = self._addResource(rawResource, rawResource.subtype);
 
                pathArray = resource.path.split('/');
                containingDir = pathArray[pathArray.length - 2];
 
                // determine if resource is not to be inclucded
                Iif (resource.basename.indexOf('-void') !== -1 || containingDir.indexOf('void') !== -1) {
                    // remove resource from appResources
                    delete self.appResources[resource.url];
                    continue;
                }
                // mark resource as manual inline if specified by name or containing directory
                Iif (resource.basename.indexOf('-manual-inline') !== -1 || containingDir.indexOf('manual-inline') !== -1) {
                    resource.inline = true;
                    // manual inline resource should not be included in assets since they are added manually
                    continue;
                } else Iif (resource.basename.indexOf('-inline') !== -1 || containingDir.indexOf('inline') !== -1) {
                    // mark resource as inline if specified by name or containing directory
                    resource.inline = true;
                }
                // mark resource as typeInline if this type of resource is specified in the config to be inlined
                if ((resource.subtype === 'css' && self.shakerConfig.resources.assets.css === 'inline')
                        || (resource.subtype === 'js' && self.shakerConfig.resources.assets.js === 'inline')) {
                    resource.typeInline = true;
                }
 
                assets[rawResource.subtype] = assets[rawResource.subtype] || {};
                assets[rawResource.subtype][rawResource.url] = resource;
            }
        }
 
        // sort and add assets
        self._sortAssets(assets);
        self.organizedResources.app[poslStr].app = assets;
    },
 
    _getMojitoResources: function () {
        'use strict';
        var rawResources,
            rawResource,
            i,
            self = this;
 
        if (!self.shakerConfig.resources || !self.shakerConfig.resources.mojito) {
            return;
        }
 
        rawResources = self.store.getResourceVersions({mojit: 'shared'});
 
        for (i in rawResources) {
            rawResource = rawResources[i];
            if ((rawResource.source.pkg.name === 'mojito' || rawResource.source.pkg.name === 'mojito-shaker') &&
                    (rawResource.affinity.affinity === 'common' || rawResource.affinity.affinity === 'client') &&
                    rawResource.source.fs.ext === '.js') {
                self._mojitoResources[rawResource.name] = rawResource;
            }
        }
 
        return self._mojitoResources;
    },
 
    _getYUIResources: function () {
        'use strict';
        var self = this,
            yuiPath = self.root + '/node_modules/',
            libWalker = require('mojito/lib/app/autoload/package-walker.server'),
            walker = new libWalker.BreadthFirst(yuiPath),
            yuiModuleConfigs = {},
            rawYuiResources = {},
            yuiResources = {};
 
        // use this app's yui library if it exists otherwise use Mojito's
        Iif (fs.existsSync(yuiPath + 'yui')) {
            yuiPath += 'yui';
        } else {
            yuiPath += 'mojito/node_modules/yui';
        }
 
        if (!self.shakerConfig.resources || !self.shakerConfig.resources.yui) {
            return;
        }
 
        walker.walk(function (err, info) {
            Iif (err) {
                throw err;
            }
            var pkg = {
                    name: info.pkg.name,
                    version: info.pkg.version,
                    depth: info.depth
                },
                yuiDirs = fs.readdirSync(yuiPath);
 
            Y.Array.each(yuiDirs, function (yuiDir) {
                var moduleFiles,
                    res,
                    yuiModule,
                    files,
                    source;
 
                if (fs.statSync(yuiPath + '/' + yuiDir).isDirectory()) {
                    files = fs.readdirSync(yuiPath + '/' + yuiDir);
                    Y.Array.some(files, function (file) {
                        if (file === yuiDir + '.js') {
                            yuiModule = yuiDir;
                            return true;
                        }
                    });
                } else {
                    return;
                }
 
                if (yuiModule) {
                    source = {
                        fs: self.store.makeResourceFSMeta(yuiPath + '/' + yuiDir, 'bundle', '.', yuiModule + '-min.js', true),//me.makeResourceFSMeta(dir, dirType, subdir, file, isFile),
                        pkg: pkg
                    };
 
                    self._yuiResources[yuiModule] = {
                        getRawResource: function () {
                            var rawResource = null;
                            try {
                                rawResource = self.store.parseResourceVersion(source, 'yui-module', 'js', 'shared');
                            } catch (e) {
                                rawResource = {
                                    affinity: 'server',
                                    id: 'yui-module--' + yuiModule,
                                    mojit: 'shared',
                                    name: yuiModule,
                                    selector: '*',
                                    source: source,
                                    type: 'yui-module',
                                    yui: {
                                        meta: {
                                            requires: []
                                        },
                                        name: yuiModule
                                    }
                                };
                            }
                            Eif (rawResource) {
                                rawResource.type = 'yui-library';
                                rawResource.name = rawResource.name || yuiModule;
                            }
 
                            this.getRawResource = function () {
                                return rawResource;
                            };
 
                            return rawResource;
                        }
                    };
                }
            });
        });
 
        return self._yuiResources;
    }
};
 
exports.ShakerResources = ShakerResources;