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 | 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 2 1 1 1 2 2 1 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 | ///<reference path=".d.ts"/> "use strict"; var path = require("path"); var semver = require("semver"); var npm = require("npm"); var constants = require("./constants"); var NpmInstallationManager = (function () { function NpmInstallationManager($npm, $logger, $lockfile, $errors, $options, $fs) { this.$npm = $npm; this.$logger = $logger; this.$lockfile = $lockfile; this.$errors = $errors; this.$options = $options; this.$fs = $fs; this.packageSpecificDirectories = { "tns-android": constants.PROJECT_FRAMEWORK_FOLDER_NAME, "tns-ios": constants.PROJECT_FRAMEWORK_FOLDER_NAME, "tns-template-hello-world": constants.APP_RESOURCES_FOLDER_NAME }; this.versionsCache = {}; this.$npm.load().wait(); } NpmInstallationManager.prototype.getCacheRootPath = function () { return this.$npm.getCache(); }; NpmInstallationManager.prototype.getCachedPackagePath = function (packageName, version) { return path.join(this.getCacheRootPath(), packageName, version, "package"); }; NpmInstallationManager.prototype.addToCache = function (packageName, version) { var _this = this; return (function () { var cachedPackagePath = _this.getCachedPackagePath(packageName, version); if (!_this.$fs.exists(cachedPackagePath).wait()) { _this.addToCacheCore(packageName, version).wait(); } if (!_this.isShasumOfPackageCorrect(packageName, version).wait()) { _this.addCleanCopyToCache(packageName, version).wait(); } }).future()(); }; NpmInstallationManager.prototype.cacheUnpack = function (packageName, version, unpackTarget) { unpackTarget = unpackTarget || path.join(npm.cache, packageName, version, "package"); return this.$npm.cacheUnpack(packageName, version, unpackTarget); }; NpmInstallationManager.prototype.getLatestVersion = function (packageName) { var _this = this; return (function () { var data = _this.$npm.view(packageName, "dist-tags").wait(); var latestVersion = _.first(_.keys(data)); _this.$logger.trace("Using version %s. ", latestVersion); return latestVersion; }).future()(); }; NpmInstallationManager.prototype.install = function (packageName, opts) { var _this = this; return (function () { _this.$lockfile.lock().wait(); try { var packageToInstall = packageName; var pathToSave = (opts && opts.pathToSave) || npm.cache; var version = (opts && opts.version) || null; return _this.installCore(packageToInstall, pathToSave, version).wait(); } catch (error) { _this.$logger.debug(error); _this.$errors.fail("%s. Error: %s", NpmInstallationManager.NPM_LOAD_FAILED, error); } finally { _this.$lockfile.unlock().wait(); } }).future()(); }; NpmInstallationManager.prototype.addCleanCopyToCache = function (packageName, version) { var _this = this; return (function () { var packagePath = path.join(_this.getCacheRootPath(), packageName, version); _this.$logger.trace("Deleting: " + packagePath + "."); _this.$fs.deleteDirectory(packagePath).wait(); _this.addToCacheCore(packageName, version).wait(); if (!_this.isShasumOfPackageCorrect(packageName, version).wait()) { _this.$errors.failWithoutHelp("Unable to add package " + packageName + " with version " + version + " to npm cache. Try cleaning your cache and execute the command again."); } }).future()(); }; NpmInstallationManager.prototype.addToCacheCore = function (packageName, version) { var _this = this; return (function () { _this.$npm.cache(packageName, version).wait(); var packagePath = path.join(_this.getCacheRootPath(), packageName, version, "package"); if (!_this.isPackageUnpacked(packagePath, packageName).wait()) { _this.cacheUnpack(packageName, version).wait(); } }).future()(); }; NpmInstallationManager.prototype.isShasumOfPackageCorrect = function (packageName, version) { var _this = this; return (function () { var shasumProperty = "dist.shasum"; var cachedPackagePath = _this.getCachedPackagePath(packageName, version); var realShasum = _this.$npm.view(packageName + "@" + version, shasumProperty).wait()[version][shasumProperty]; var packageTgz = cachedPackagePath + ".tgz"; var currentShasum = ""; if (_this.$fs.exists(packageTgz).wait()) { currentShasum = _this.$fs.getFileShasum(packageTgz).wait(); } _this.$logger.trace("Checking shasum of package: " + packageName + "@" + version + ": expected " + realShasum + ", actual " + currentShasum + "."); return realShasum === currentShasum; }).future()(); }; NpmInstallationManager.prototype.installCore = function (packageName, pathToSave, version) { var _this = this; return (function () { if (_this.$options.frameworkPath) { if (_this.$fs.getFsStats(_this.$options.frameworkPath).wait().isFile()) { _this.npmInstall(packageName, pathToSave, version).wait(); var pathToNodeModules = path.join(pathToSave, "node_modules"); var folders = _this.$fs.readDirectory(pathToNodeModules).wait(); return path.join(pathToNodeModules, folders[0]); } return _this.$options.frameworkPath; } else { version = version || _this.getLatestVersion(packageName).wait(); var packagePath = _this.getCachedPackagePath(packageName, version); _this.addToCache(packageName, version).wait(); return packagePath; } }).future()(); }; NpmInstallationManager.prototype.npmInstall = function (packageName, pathToSave, version) { this.$logger.out("Installing ", packageName); var incrementedVersion = semver.inc(version, constants.ReleaseType.MINOR); if (!this.$options.frameworkPath && packageName.indexOf("@") < 0) { packageName = packageName + "@<" + incrementedVersion; } return this.$npm.install(packageName, pathToSave); }; NpmInstallationManager.prototype.isPackageUnpacked = function (packagePath, packageName) { var _this = this; return (function () { var additionalDirectoryToCheck = _this.packageSpecificDirectories[packageName]; return _this.$fs.getFsStats(packagePath).wait().isDirectory() && (!additionalDirectoryToCheck || _this.hasFilesInDirectory(path.join(packagePath, additionalDirectoryToCheck)).wait()); }).future()(); }; NpmInstallationManager.prototype.hasFilesInDirectory = function (directory) { var _this = this; return (function () { return _this.$fs.exists(directory).wait() && _this.$fs.enumerateFilesInDirectorySync(directory).length > 0; }).future()(); }; NpmInstallationManager.NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later."; return NpmInstallationManager; })(); exports.NpmInstallationManager = NpmInstallationManager; $injector.register("npmInstallationManager", NpmInstallationManager); |