Code coverage report for lib/tools/broccoli/node-modules-dest-copy.js

Statements: 79.75% (63 / 79)      Branches: 60.71% (17 / 28)      Functions: 88.89% (8 / 9)      Lines: 79.75% (63 / 79)      Ignored: none     

All files » lib/tools/broccoli/ » node-modules-dest-copy.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    1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2   1 2 2 7 4 4 4 4 4 6 6 6           6 6                     6           2 6 6 6 6     6           2 2     1             1 2 2 2   1 5 5 2 2 3 3 3   3 3 1       5   1   1 1  
///<reference path="../../.d.ts"/>
"use strict";
var fs = require("fs");
var path = require("path");
var semver = require("semver");
var shelljs = require("shelljs");
var broccoli_plugin_wrapper_factory_1 = require('./broccoli-plugin-wrapper-factory');
var constants = require("../../constants");
var DestCopy = (function () {
    function DestCopy(inputPath, cachePath, outputRoot, projectDir, platform, $fs, $projectFilesManager, $pluginsService) {
        this.inputPath = inputPath;
        this.cachePath = cachePath;
        this.outputRoot = outputRoot;
        this.projectDir = projectDir;
        this.platform = platform;
        this.$fs = $fs;
        this.$projectFilesManager = $projectFilesManager;
        this.$pluginsService = $pluginsService;
        this.dependencies = null;
        this.devDependencies = null;
        this.dependencies = Object.create(null);
        this.devDependencies = this.getDevDependencies(projectDir);
    }
    DestCopy.prototype.rebuildChangedDirectories = function (changedDirectories, platform) {
        var _this = this;
        _.each(changedDirectories, function (changedDirectoryAbsolutePath) {
            if (!_this.devDependencies[path.basename(changedDirectoryAbsolutePath)]) {
                var pathToPackageJson = path.join(changedDirectoryAbsolutePath, constants.PACKAGE_JSON_FILE_NAME);
                var packageJsonFiles = fs.existsSync(pathToPackageJson) ? [pathToPackageJson] : [];
                var nodeModulesFolderPath = path.join(changedDirectoryAbsolutePath, constants.NODE_MODULES_FOLDER_NAME);
                packageJsonFiles = packageJsonFiles.concat(_this.enumeratePackageJsonFilesSync(nodeModulesFolderPath));
                _.each(packageJsonFiles, function (packageJsonFilePath) {
                    var fileContent = require(packageJsonFilePath);
                    Eif (!_this.devDependencies[fileContent.name]) {
                        var currentDependency = {
                            name: fileContent.name,
                            version: fileContent.version,
                            directory: path.dirname(packageJsonFilePath),
                            nativescript: fileContent.nativescript
                        };
                        var addedDependency = _this.dependencies[currentDependency.name];
                        Iif (addedDependency) {
                            if (semver.gt(currentDependency.version, addedDependency.version)) {
                                var currentDependencyMajorVersion = semver.major(currentDependency.version);
                                var addedDependencyMajorVersion = semver.major(addedDependency.version);
                                var message = "The depedency located at " + addedDependency.directory + " with version  " + addedDependency.version + " will be replaced with dependency located at " + currentDependency.directory + " with version " + currentDependency.version;
                                var logger = $injector.resolve("$logger");
                                currentDependencyMajorVersion === addedDependencyMajorVersion ? logger.out(message) : logger.warn(message);
                                _this.dependencies[currentDependency.name] = currentDependency;
                            }
                        }
                        else {
                            _this.dependencies[currentDependency.name] = currentDependency;
                        }
                    }
                });
            }
        });
        _.each(this.dependencies, function (dependency) {
            shelljs.cp("-Rf", dependency.directory, _this.outputRoot);
            shelljs.rm("-rf", path.join(_this.outputRoot, dependency.name, "node_modules"));
            var isPlugin = !!dependency.nativescript;
            Iif (isPlugin) {
                _this.$pluginsService.prepare(dependency).wait();
            }
            Iif (dependency.name === constants.TNS_CORE_MODULES_NAME) {
                var tnsCoreModulesResourcePath = path.join(_this.outputRoot, constants.TNS_CORE_MODULES_NAME);
                shelljs.cp("-Rf", path.join(tnsCoreModulesResourcePath, "*"), _this.outputRoot);
                _this.$fs.deleteDirectory(tnsCoreModulesResourcePath).wait();
            }
        });
        Eif (!_.isEmpty(this.dependencies)) {
            this.$pluginsService.afterPrepareAllPlugins().wait();
        }
    };
    DestCopy.prototype.rebuild = function (treeDiff) {
        this.rebuildChangedDirectories(treeDiff.changedDirectories, "");
        var projectFilePath = path.join(this.projectDir, constants.PACKAGE_JSON_FILE_NAME);
        var projectFileContent = require(projectFilePath);
        projectFileContent[constants.NATIVESCRIPT_KEY_NAME][constants.NODE_MODULE_CACHE_PATH_KEY_NAME] = this.inputPath;
        fs.writeFileSync(projectFilePath, JSON.stringify(projectFileContent, null, "\t"), { encoding: "utf8" });
    };
    DestCopy.prototype.getDevDependencies = function (projectDir) {
        var projectFilePath = path.join(projectDir, constants.PACKAGE_JSON_FILE_NAME);
        var projectFileContent = require(projectFilePath);
        return projectFileContent.devDependencies || {};
    };
    DestCopy.prototype.enumeratePackageJsonFilesSync = function (nodeModulesDirectoryPath, foundFiles) {
        foundFiles = foundFiles || [];
        if (fs.existsSync(nodeModulesDirectoryPath)) {
            var contents = fs.readdirSync(nodeModulesDirectoryPath);
            for (var i = 0; i < contents.length; ++i) {
                var packageJsonFilePath = path.join(nodeModulesDirectoryPath, contents[i], constants.PACKAGE_JSON_FILE_NAME);
                Eif (fs.existsSync(packageJsonFilePath)) {
                    foundFiles.push(packageJsonFilePath);
                }
                var directoryPath = path.join(nodeModulesDirectoryPath, contents[i], constants.NODE_MODULES_FOLDER_NAME);
                if (fs.existsSync(directoryPath)) {
                    this.enumeratePackageJsonFilesSync(directoryPath, foundFiles);
                }
            }
        }
        return foundFiles;
    };
    return DestCopy;
})();
exports.DestCopy = DestCopy;
exports.default = broccoli_plugin_wrapper_factory_1.wrapBroccoliPlugin(DestCopy);