"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/install.js" (8 Mar 2017, 35741 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 (function() {
    2   var CSON, Command, Git, Install, RebuildModuleCache, _, assert, async, config, fs, hostedGitInfo, isDeprecatedPackage, path, request, semver, temp, yargs,
    3     bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    4     extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    5     hasProp = {}.hasOwnProperty,
    6     slice = [].slice;
    7 
    8   assert = require('assert');
    9 
   10   path = require('path');
   11 
   12   _ = require('underscore-plus');
   13 
   14   async = require('async');
   15 
   16   CSON = require('season');
   17 
   18   yargs = require('yargs');
   19 
   20   Git = require('git-utils');
   21 
   22   semver = require('semver');
   23 
   24   temp = require('temp');
   25 
   26   hostedGitInfo = require('hosted-git-info');
   27 
   28   config = require('./apm');
   29 
   30   Command = require('./command');
   31 
   32   fs = require('./fs');
   33 
   34   RebuildModuleCache = require('./rebuild-module-cache');
   35 
   36   request = require('./request');
   37 
   38   isDeprecatedPackage = require('./deprecated-packages').isDeprecatedPackage;
   39 
   40   module.exports = Install = (function(superClass) {
   41     extend(Install, superClass);
   42 
   43     Install.commandNames = ['install', 'i'];
   44 
   45     function Install() {
   46       this.installGitPackageDependencies = bind(this.installGitPackageDependencies, this);
   47       this.installModules = bind(this.installModules, this);
   48       this.installNode = bind(this.installNode, this);
   49       this.atomDirectory = config.getAtomDirectory();
   50       this.atomPackagesDirectory = path.join(this.atomDirectory, 'packages');
   51       this.atomNodeDirectory = path.join(this.atomDirectory, '.node-gyp');
   52       this.atomNpmPath = require.resolve('npm/bin/npm-cli');
   53     }
   54 
   55     Install.prototype.parseOptions = function(argv) {
   56       var options;
   57       options = yargs(argv).wrap(100);
   58       options.usage("\nUsage: apm install [<package_name>...]\n       apm install <package_name>@<package_version>\n       apm install <git_remote>\n       apm install <github_username>/<github_project>\n       apm install --packages-file my-packages.txt\n       apm i (with any of the previous argument usage)\n\nInstall the given Atom package to ~/.atom/packages/<package_name>.\n\nIf no package name is given then all the dependencies in the package.json\nfile are installed to the node_modules folder in the current working\ndirectory.\n\nA packages file can be specified that is a newline separated list of\npackage names to install with optional versions using the\n`package-name@version` syntax.");
   59       options.alias('c', 'compatible').string('compatible').describe('compatible', 'Only install packages/themes compatible with this Atom version');
   60       options.alias('h', 'help').describe('help', 'Print this usage message');
   61       options.alias('s', 'silent').boolean('silent').describe('silent', 'Set the npm log level to silent');
   62       options.alias('q', 'quiet').boolean('quiet').describe('quiet', 'Set the npm log level to warn');
   63       options.boolean('check').describe('check', 'Check that native build tools are installed');
   64       options.boolean('verbose')["default"]('verbose', false).describe('verbose', 'Show verbose debug information');
   65       options.string('packages-file').describe('packages-file', 'A text file containing the packages to install');
   66       return options.boolean('production').describe('production', 'Do not install dev dependencies');
   67     };
   68 
   69     Install.prototype.installNode = function(callback) {
   70       var atomNodeGypPath, env, installNodeArgs, opts, proxy, ref, useStrictSsl;
   71       installNodeArgs = ['install'];
   72       installNodeArgs.push("--runtime=electron");
   73       installNodeArgs.push("--target=" + this.electronVersion);
   74       installNodeArgs.push("--dist-url=" + (config.getElectronUrl()));
   75       installNodeArgs.push("--arch=" + (config.getElectronArch()));
   76       installNodeArgs.push("--ensure");
   77       if (this.verbose) {
   78         installNodeArgs.push("--verbose");
   79       }
   80       env = _.extend({}, process.env, {
   81         HOME: this.atomNodeDirectory,
   82         RUSTUP_HOME: config.getRustupHomeDirPath()
   83       });
   84       if (config.isWin32()) {
   85         env.USERPROFILE = env.HOME;
   86       }
   87       fs.makeTreeSync(this.atomDirectory);
   88       useStrictSsl = (ref = this.npm.config.get('strict-ssl')) != null ? ref : true;
   89       if (!useStrictSsl) {
   90         env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
   91       }
   92       proxy = this.npm.config.get('https-proxy') || this.npm.config.get('proxy') || env.HTTPS_PROXY || env.HTTP_PROXY;
   93       if (proxy) {
   94         installNodeArgs.push("--proxy=" + proxy);
   95       }
   96       opts = {
   97         env: env,
   98         cwd: this.atomDirectory
   99       };
  100       if (this.verbose) {
  101         opts.streaming = true;
  102       }
  103       atomNodeGypPath = process.env.ATOM_NODE_GYP_PATH || require.resolve('node-gyp/bin/node-gyp');
  104       return this.fork(atomNodeGypPath, installNodeArgs, opts, function(code, stderr, stdout) {
  105         if (stderr == null) {
  106           stderr = '';
  107         }
  108         if (stdout == null) {
  109           stdout = '';
  110         }
  111         if (code === 0) {
  112           return callback();
  113         } else {
  114           return callback(stdout + "\n" + stderr);
  115         }
  116       });
  117     };
  118 
  119     Install.prototype.installModule = function(options, pack, modulePath, callback) {
  120       var env, installArgs, installDirectory, installGlobally, installOptions, nodeModulesDirectory, ref, vsArgs;
  121       installGlobally = (ref = options.installGlobally) != null ? ref : true;
  122       installArgs = ['--globalconfig', config.getGlobalConfigPath(), '--userconfig', config.getUserConfigPath(), 'install'];
  123       installArgs.push(modulePath);
  124       installArgs.push("--runtime=electron");
  125       installArgs.push("--target=" + this.electronVersion);
  126       installArgs.push("--arch=" + (config.getElectronArch()));
  127       if (installGlobally) {
  128         installArgs.push("--global-style");
  129       }
  130       if (options.argv.silent) {
  131         installArgs.push('--silent');
  132       }
  133       if (options.argv.quiet) {
  134         installArgs.push('--quiet');
  135       }
  136       if (options.argv.production) {
  137         installArgs.push('--production');
  138       }
  139       if (vsArgs = this.getVisualStudioFlags()) {
  140         installArgs.push(vsArgs);
  141       }
  142       env = _.extend({}, process.env, {
  143         HOME: this.atomNodeDirectory,
  144         RUSTUP_HOME: config.getRustupHomeDirPath()
  145       });
  146       this.addBuildEnvVars(env);
  147       installOptions = {
  148         env: env
  149       };
  150       if (this.verbose) {
  151         installOptions.streaming = true;
  152       }
  153       if (installGlobally) {
  154         installDirectory = temp.mkdirSync('apm-install-dir-');
  155         nodeModulesDirectory = path.join(installDirectory, 'node_modules');
  156         fs.makeTreeSync(nodeModulesDirectory);
  157         installOptions.cwd = installDirectory;
  158       }
  159       return this.fork(this.atomNpmPath, installArgs, installOptions, (function(_this) {
  160         return function(code, stderr, stdout) {
  161           var child, children, commands, destination, error, source;
  162           if (stderr == null) {
  163             stderr = '';
  164           }
  165           if (stdout == null) {
  166             stdout = '';
  167           }
  168           if (code === 0) {
  169             if (installGlobally) {
  170               commands = [];
  171               children = fs.readdirSync(nodeModulesDirectory).filter(function(dir) {
  172                 return dir !== ".bin";
  173               });
  174               assert.equal(children.length, 1, "Expected there to only be one child in node_modules");
  175               child = children[0];
  176               source = path.join(nodeModulesDirectory, child);
  177               destination = path.join(_this.atomPackagesDirectory, child);
  178               commands.push(function(next) {
  179                 return fs.cp(source, destination, next);
  180               });
  181               commands.push(function(next) {
  182                 return _this.buildModuleCache(pack.name, next);
  183               });
  184               commands.push(function(next) {
  185                 return _this.warmCompileCache(pack.name, next);
  186               });
  187               return async.waterfall(commands, function(error) {
  188                 if (error != null) {
  189                   _this.logFailure();
  190                 } else {
  191                   if (!options.argv.json) {
  192                     _this.logSuccess();
  193                   }
  194                 }
  195                 return callback(error, {
  196                   name: child,
  197                   installPath: destination
  198                 });
  199               });
  200             } else {
  201               return callback(null, {
  202                 name: child,
  203                 installPath: destination
  204               });
  205             }
  206           } else {
  207             if (installGlobally) {
  208               fs.removeSync(installDirectory);
  209               _this.logFailure();
  210             }
  211             error = stdout + "\n" + stderr;
  212             if (error.indexOf('code ENOGIT') !== -1) {
  213               error = _this.getGitErrorMessage(pack);
  214             }
  215             return callback(error);
  216           }
  217         };
  218       })(this));
  219     };
  220 
  221     Install.prototype.getGitErrorMessage = function(pack) {
  222       var message;
  223       message = "Failed to install " + pack.name + " because Git was not found.\n\nThe " + pack.name + " package has module dependencies that cannot be installed without Git.\n\nYou need to install Git and add it to your path environment variable in order to install this package.\n";
  224       switch (process.platform) {
  225         case 'win32':
  226           message += "\nYou can install Git by downloading, installing, and launching GitHub for Windows: https://windows.github.com\n";
  227           break;
  228         case 'linux':
  229           message += "\nYou can install Git from your OS package manager.\n";
  230       }
  231       message += "\nRun apm -v after installing Git to see what version has been detected.";
  232       return message;
  233     };
  234 
  235     Install.prototype.installModules = function(options, callback) {
  236       if (!options.argv.json) {
  237         process.stdout.write('Installing modules ');
  238       }
  239       return this.forkInstallCommand(options, (function(_this) {
  240         return function() {
  241           var args;
  242           args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  243           if (options.argv.json) {
  244             return _this.logCommandResultsIfFail.apply(_this, [callback].concat(slice.call(args)));
  245           } else {
  246             return _this.logCommandResults.apply(_this, [callback].concat(slice.call(args)));
  247           }
  248         };
  249       })(this));
  250     };
  251 
  252     Install.prototype.forkInstallCommand = function(options, callback) {
  253       var env, installArgs, installOptions, vsArgs;
  254       installArgs = ['--globalconfig', config.getGlobalConfigPath(), '--userconfig', config.getUserConfigPath(), 'install'];
  255       installArgs.push("--runtime=electron");
  256       installArgs.push("--target=" + this.electronVersion);
  257       installArgs.push("--arch=" + (config.getElectronArch()));
  258       if (options.argv.silent) {
  259         installArgs.push('--silent');
  260       }
  261       if (options.argv.quiet) {
  262         installArgs.push('--quiet');
  263       }
  264       if (options.argv.production) {
  265         installArgs.push('--production');
  266       }
  267       if (vsArgs = this.getVisualStudioFlags()) {
  268         installArgs.push(vsArgs);
  269       }
  270       env = _.extend({}, process.env, {
  271         HOME: this.atomNodeDirectory,
  272         RUSTUP_HOME: config.getRustupHomeDirPath()
  273       });
  274       if (config.isWin32()) {
  275         this.updateWindowsEnv(env);
  276       }
  277       this.addNodeBinToEnv(env);
  278       this.addProxyToEnv(env);
  279       installOptions = {
  280         env: env
  281       };
  282       if (options.cwd) {
  283         installOptions.cwd = options.cwd;
  284       }
  285       if (this.verbose) {
  286         installOptions.streaming = true;
  287       }
  288       return this.fork(this.atomNpmPath, installArgs, installOptions, callback);
  289     };
  290 
  291     Install.prototype.requestPackage = function(packageName, callback) {
  292       var requestSettings;
  293       requestSettings = {
  294         url: (config.getAtomPackagesUrl()) + "/" + packageName,
  295         json: true,
  296         retries: 4
  297       };
  298       return request.get(requestSettings, function(error, response, body) {
  299         var message;
  300         if (body == null) {
  301           body = {};
  302         }
  303         if (error != null) {
  304           message = "Request for package information failed: " + error.message;
  305           if (error.code) {
  306             message += " (" + error.code + ")";
  307           }
  308           return callback(message);
  309         } else if (response.statusCode !== 200) {
  310           message = request.getErrorMessage(response, body);
  311           return callback("Request for package information failed: " + message);
  312         } else {
  313           if (body.releases.latest) {
  314             return callback(null, body);
  315           } else {
  316             return callback("No releases available for " + packageName);
  317           }
  318         }
  319       });
  320     };
  321 
  322     Install.prototype.downloadPackage = function(packageUrl, installGlobally, callback) {
  323       var requestSettings;
  324       requestSettings = {
  325         url: packageUrl
  326       };
  327       return request.createReadStream(requestSettings, (function(_this) {
  328         return function(readStream) {
  329           readStream.on('error', function(error) {
  330             return callback("Unable to download " + packageUrl + ": " + error.message);
  331           });
  332           return readStream.on('response', function(response) {
  333             var chunks, filePath, writeStream;
  334             if (response.statusCode === 200) {
  335               filePath = path.join(temp.mkdirSync(), 'package.tgz');
  336               writeStream = fs.createWriteStream(filePath);
  337               readStream.pipe(writeStream);
  338               writeStream.on('error', function(error) {
  339                 return callback("Unable to download " + packageUrl + ": " + error.message);
  340               });
  341               return writeStream.on('close', function() {
  342                 return callback(null, filePath);
  343               });
  344             } else {
  345               chunks = [];
  346               response.on('data', function(chunk) {
  347                 return chunks.push(chunk);
  348               });
  349               return response.on('end', function() {
  350                 var error, error1, message, parseError, ref, ref1, ref2, ref3;
  351                 try {
  352                   error = JSON.parse(Buffer.concat(chunks));
  353                   message = (ref = (ref1 = error.message) != null ? ref1 : error.error) != null ? ref : error;
  354                   if (installGlobally) {
  355                     _this.logFailure();
  356                   }
  357                   return callback("Unable to download " + packageUrl + ": " + ((ref2 = response.headers.status) != null ? ref2 : response.statusCode) + " " + message);
  358                 } catch (error1) {
  359                   parseError = error1;
  360                   if (installGlobally) {
  361                     _this.logFailure();
  362                   }
  363                   return callback("Unable to download " + packageUrl + ": " + ((ref3 = response.headers.status) != null ? ref3 : response.statusCode));
  364                 }
  365               });
  366             }
  367           });
  368         };
  369       })(this));
  370     };
  371 
  372     Install.prototype.getPackageCachePath = function(packageName, packageVersion, callback) {
  373       var cacheDir, cachePath, tempPath;
  374       cacheDir = config.getCacheDirectory();
  375       cachePath = path.join(cacheDir, packageName, packageVersion, 'package.tgz');
  376       if (fs.isFileSync(cachePath)) {
  377         tempPath = path.join(temp.mkdirSync(), path.basename(cachePath));
  378         return fs.cp(cachePath, tempPath, function(error) {
  379           if (error != null) {
  380             return callback(error);
  381           } else {
  382             return callback(null, tempPath);
  383           }
  384         });
  385       } else {
  386         return process.nextTick(function() {
  387           return callback(new Error(packageName + "@" + packageVersion + " is not in the cache"));
  388         });
  389       }
  390     };
  391 
  392     Install.prototype.isPackageInstalled = function(packageName, packageVersion) {
  393       var error, error1, ref, version;
  394       try {
  395         version = ((ref = CSON.readFileSync(CSON.resolve(path.join('node_modules', packageName, 'package')))) != null ? ref : {}).version;
  396         return packageVersion === version;
  397       } catch (error1) {
  398         error = error1;
  399         return false;
  400       }
  401     };
  402 
  403     Install.prototype.installRegisteredPackage = function(metadata, options, callback) {
  404       var installGlobally, label, packageName, packageVersion, ref;
  405       packageName = metadata.name;
  406       packageVersion = metadata.version;
  407       installGlobally = (ref = options.installGlobally) != null ? ref : true;
  408       if (!installGlobally) {
  409         if (packageVersion && this.isPackageInstalled(packageName, packageVersion)) {
  410           callback(null, {});
  411           return;
  412         }
  413       }
  414       label = packageName;
  415       if (packageVersion) {
  416         label += "@" + packageVersion;
  417       }
  418       if (!options.argv.json) {
  419         process.stdout.write("Installing " + label + " ");
  420         if (installGlobally) {
  421           process.stdout.write("to " + this.atomPackagesDirectory + " ");
  422         }
  423       }
  424       return this.requestPackage(packageName, (function(_this) {
  425         return function(error, pack) {
  426           var commands, installNode, ref1, ref2, ref3, tarball;
  427           if (error != null) {
  428             _this.logFailure();
  429             return callback(error);
  430           } else {
  431             if (packageVersion == null) {
  432               packageVersion = _this.getLatestCompatibleVersion(pack);
  433             }
  434             if (!packageVersion) {
  435               _this.logFailure();
  436               callback("No available version compatible with the installed Atom version: " + _this.installedAtomVersion);
  437               return;
  438             }
  439             tarball = ((ref1 = (ref2 = pack.versions[packageVersion]) != null ? ref2.dist : void 0) != null ? ref1 : {}).tarball;
  440             if (!tarball) {
  441               _this.logFailure();
  442               callback("Package version: " + packageVersion + " not found");
  443               return;
  444             }
  445             commands = [];
  446             commands.push(function(next) {
  447               return _this.getPackageCachePath(packageName, packageVersion, function(error, packagePath) {
  448                 if (packagePath) {
  449                   return next(null, packagePath);
  450                 } else {
  451                   return _this.downloadPackage(tarball, installGlobally, next);
  452                 }
  453               });
  454             });
  455             installNode = (ref3 = options.installNode) != null ? ref3 : true;
  456             if (installNode) {
  457               commands.push(function(packagePath, next) {
  458                 return _this.installNode(function(error) {
  459                   return next(error, packagePath);
  460                 });
  461               });
  462             }
  463             commands.push(function(packagePath, next) {
  464               return _this.installModule(options, pack, packagePath, next);
  465             });
  466             commands.push(function(arg, next) {
  467               var installPath, json;
  468               installPath = arg.installPath;
  469               if (installPath != null) {
  470                 metadata = JSON.parse(fs.readFileSync(path.join(installPath, 'package.json'), 'utf8'));
  471                 json = {
  472                   installPath: installPath,
  473                   metadata: metadata
  474                 };
  475                 return next(null, json);
  476               } else {
  477                 return next(null, {});
  478               }
  479             });
  480             return async.waterfall(commands, function(error, json) {
  481               if (!installGlobally) {
  482                 if (error != null) {
  483                   _this.logFailure();
  484                 } else {
  485                   if (!options.argv.json) {
  486                     _this.logSuccess();
  487                   }
  488                 }
  489               }
  490               return callback(error, json);
  491             });
  492           }
  493         };
  494       })(this));
  495     };
  496 
  497     Install.prototype.installPackageDependencies = function(options, callback) {
  498       var commands, fn, name, ref, version;
  499       options = _.extend({}, options, {
  500         installGlobally: false,
  501         installNode: false
  502       });
  503       commands = [];
  504       ref = this.getPackageDependencies();
  505       fn = (function(_this) {
  506         return function(name, version) {
  507           return commands.push(function(next) {
  508             return _this.installRegisteredPackage({
  509               name: name,
  510               version: version
  511             }, options, next);
  512           });
  513         };
  514       })(this);
  515       for (name in ref) {
  516         version = ref[name];
  517         fn(name, version);
  518       }
  519       return async.series(commands, callback);
  520     };
  521 
  522     Install.prototype.installDependencies = function(options, callback) {
  523       var commands;
  524       options.installGlobally = false;
  525       commands = [];
  526       commands.push(this.installNode);
  527       commands.push((function(_this) {
  528         return function(callback) {
  529           return _this.installModules(options, callback);
  530         };
  531       })(this));
  532       commands.push((function(_this) {
  533         return function(callback) {
  534           return _this.installPackageDependencies(options, callback);
  535         };
  536       })(this));
  537       return async.waterfall(commands, callback);
  538     };
  539 
  540     Install.prototype.getPackageDependencies = function() {
  541       var error, error1, metadata, packageDependencies, ref;
  542       try {
  543         metadata = fs.readFileSync('package.json', 'utf8');
  544         packageDependencies = ((ref = JSON.parse(metadata)) != null ? ref : {}).packageDependencies;
  545         return packageDependencies != null ? packageDependencies : {};
  546       } catch (error1) {
  547         error = error1;
  548         return {};
  549       }
  550     };
  551 
  552     Install.prototype.createAtomDirectories = function() {
  553       fs.makeTreeSync(this.atomDirectory);
  554       fs.makeTreeSync(this.atomPackagesDirectory);
  555       return fs.makeTreeSync(this.atomNodeDirectory);
  556     };
  557 
  558     Install.prototype.checkNativeBuildTools = function(callback) {
  559       process.stdout.write('Checking for native build tools ');
  560       return this.installNode((function(_this) {
  561         return function(error) {
  562           var buildArgs, buildOptions, env, vsArgs;
  563           if (error != null) {
  564             _this.logFailure();
  565             return callback(error);
  566           }
  567           buildArgs = ['--globalconfig', config.getGlobalConfigPath(), '--userconfig', config.getUserConfigPath(), 'build'];
  568           buildArgs.push(path.resolve(__dirname, '..', 'native-module'));
  569           buildArgs.push("--runtime=electron");
  570           buildArgs.push("--target=" + _this.electronVersion);
  571           buildArgs.push("--arch=" + (config.getElectronArch()));
  572           if (vsArgs = _this.getVisualStudioFlags()) {
  573             buildArgs.push(vsArgs);
  574           }
  575           env = _.extend({}, process.env, {
  576             HOME: _this.atomNodeDirectory,
  577             RUSTUP_HOME: config.getRustupHomeDirPath()
  578           });
  579           if (config.isWin32()) {
  580             _this.updateWindowsEnv(env);
  581           }
  582           _this.addNodeBinToEnv(env);
  583           _this.addProxyToEnv(env);
  584           buildOptions = {
  585             env: env
  586           };
  587           if (_this.verbose) {
  588             buildOptions.streaming = true;
  589           }
  590           fs.removeSync(path.resolve(__dirname, '..', 'native-module', 'build'));
  591           return _this.fork(_this.atomNpmPath, buildArgs, buildOptions, function() {
  592             var args;
  593             args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  594             return _this.logCommandResults.apply(_this, [callback].concat(slice.call(args)));
  595           });
  596         };
  597       })(this));
  598     };
  599 
  600     Install.prototype.packageNamesFromPath = function(filePath) {
  601       var packages;
  602       filePath = path.resolve(filePath);
  603       if (!fs.isFileSync(filePath)) {
  604         throw new Error("File '" + filePath + "' does not exist");
  605       }
  606       packages = fs.readFileSync(filePath, 'utf8');
  607       return this.sanitizePackageNames(packages.split(/\s/));
  608     };
  609 
  610     Install.prototype.buildModuleCache = function(packageName, callback) {
  611       var packageDirectory, rebuildCacheCommand;
  612       packageDirectory = path.join(this.atomPackagesDirectory, packageName);
  613       rebuildCacheCommand = new RebuildModuleCache();
  614       return rebuildCacheCommand.rebuild(packageDirectory, function() {
  615         return callback();
  616       });
  617     };
  618 
  619     Install.prototype.warmCompileCache = function(packageName, callback) {
  620       var packageDirectory;
  621       packageDirectory = path.join(this.atomPackagesDirectory, packageName);
  622       return this.getResourcePath((function(_this) {
  623         return function(resourcePath) {
  624           var CompileCache, onDirectory, onFile;
  625           try {
  626             CompileCache = require(path.join(resourcePath, 'src', 'compile-cache'));
  627             onDirectory = function(directoryPath) {
  628               return path.basename(directoryPath) !== 'node_modules';
  629             };
  630             onFile = function(filePath) {
  631               try {
  632                 return CompileCache.addPathToCache(filePath, _this.atomDirectory);
  633               } catch (undefined) {}
  634             };
  635             fs.traverseTreeSync(packageDirectory, onFile, onDirectory);
  636           } catch (undefined) {}
  637           return callback(null);
  638         };
  639       })(this));
  640     };
  641 
  642     Install.prototype.isBundledPackage = function(packageName, callback) {
  643       return this.getResourcePath(function(resourcePath) {
  644         var atomMetadata, error, error1, ref;
  645         try {
  646           atomMetadata = JSON.parse(fs.readFileSync(path.join(resourcePath, 'package.json')));
  647         } catch (error1) {
  648           error = error1;
  649           return callback(false);
  650         }
  651         return callback(atomMetadata != null ? (ref = atomMetadata.packageDependencies) != null ? ref.hasOwnProperty(packageName) : void 0 : void 0);
  652       });
  653     };
  654 
  655     Install.prototype.getLatestCompatibleVersion = function(pack) {
  656       var engine, latestVersion, metadata, ref, ref1, ref2, ref3, version;
  657       if (!this.installedAtomVersion) {
  658         if (isDeprecatedPackage(pack.name, pack.releases.latest)) {
  659           return null;
  660         } else {
  661           return pack.releases.latest;
  662         }
  663       }
  664       latestVersion = null;
  665       ref1 = (ref = pack.versions) != null ? ref : {};
  666       for (version in ref1) {
  667         metadata = ref1[version];
  668         if (!semver.valid(version)) {
  669           continue;
  670         }
  671         if (!metadata) {
  672           continue;
  673         }
  674         if (isDeprecatedPackage(pack.name, version)) {
  675           continue;
  676         }
  677         engine = (ref2 = (ref3 = metadata.engines) != null ? ref3.atom : void 0) != null ? ref2 : '*';
  678         if (!semver.validRange(engine)) {
  679           continue;
  680         }
  681         if (!semver.satisfies(this.installedAtomVersion, engine)) {
  682           continue;
  683         }
  684         if (latestVersion == null) {
  685           latestVersion = version;
  686         }
  687         if (semver.gt(version, latestVersion)) {
  688           latestVersion = version;
  689         }
  690       }
  691       return latestVersion;
  692     };
  693 
  694     Install.prototype.getHostedGitInfo = function(name) {
  695       return hostedGitInfo.fromUrl(name);
  696     };
  697 
  698     Install.prototype.installGitPackage = function(packageUrl, options, callback) {
  699       var cloneDir, iteratee, tasks;
  700       tasks = [];
  701       cloneDir = temp.mkdirSync("atom-git-package-clone-");
  702       tasks.push((function(_this) {
  703         return function(data, next) {
  704           var urls;
  705           urls = _this.getNormalizedGitUrls(packageUrl);
  706           return _this.cloneFirstValidGitUrl(urls, cloneDir, options, function(err) {
  707             return next(err, data);
  708           });
  709         };
  710       })(this));
  711       tasks.push((function(_this) {
  712         return function(data, next) {
  713           return _this.installGitPackageDependencies(cloneDir, options, function(err) {
  714             return next(err, data);
  715           });
  716         };
  717       })(this));
  718       tasks.push((function(_this) {
  719         return function(data, next) {
  720           return _this.getRepositoryHeadSha(cloneDir, function(err, sha) {
  721             data.sha = sha;
  722             return next(err, data);
  723           });
  724         };
  725       })(this));
  726       tasks.push(function(data, next) {
  727         var metadataFilePath;
  728         metadataFilePath = CSON.resolve(path.join(cloneDir, 'package'));
  729         return CSON.readFile(metadataFilePath, function(err, metadata) {
  730           data.metadataFilePath = metadataFilePath;
  731           data.metadata = metadata;
  732           return next(err, data);
  733         });
  734       });
  735       tasks.push(function(data, next) {
  736         data.metadata.apmInstallSource = {
  737           type: "git",
  738           source: packageUrl,
  739           sha: data.sha
  740         };
  741         return CSON.writeFile(data.metadataFilePath, data.metadata, function(err) {
  742           return next(err, data);
  743         });
  744       });
  745       tasks.push((function(_this) {
  746         return function(data, next) {
  747           var name, targetDir;
  748           name = data.metadata.name;
  749           targetDir = path.join(_this.atomPackagesDirectory, name);
  750           if (!options.argv.json) {
  751             process.stdout.write("Moving " + name + " to " + targetDir + " ");
  752           }
  753           return fs.cp(cloneDir, targetDir, function(err) {
  754             var json;
  755             if (err) {
  756               return next(err);
  757             } else {
  758               if (!options.argv.json) {
  759                 _this.logSuccess();
  760               }
  761               json = {
  762                 installPath: targetDir,
  763                 metadata: data.metadata
  764               };
  765               return next(null, json);
  766             }
  767           });
  768         };
  769       })(this));
  770       iteratee = function(currentData, task, next) {
  771         return task(currentData, next);
  772       };
  773       return async.reduce(tasks, {}, iteratee, callback);
  774     };
  775 
  776     Install.prototype.getNormalizedGitUrls = function(packageUrl) {
  777       var packageInfo;
  778       packageInfo = this.getHostedGitInfo(packageUrl);
  779       if (packageUrl.indexOf('file://') === 0) {
  780         return [packageUrl];
  781       } else if (packageInfo["default"] === 'sshurl') {
  782         return [packageInfo.toString()];
  783       } else if (packageInfo["default"] === 'https') {
  784         return [packageInfo.https().replace(/^git\+https:/, "https:")];
  785       } else if (packageInfo["default"] === 'shortcut') {
  786         return [packageInfo.https().replace(/^git\+https:/, "https:"), packageInfo.sshurl()];
  787       }
  788     };
  789 
  790     Install.prototype.cloneFirstValidGitUrl = function(urls, cloneDir, options, callback) {
  791       return async.detectSeries(urls, (function(_this) {
  792         return function(url, next) {
  793           return _this.cloneNormalizedUrl(url, cloneDir, options, function(error) {
  794             return next(!error);
  795           });
  796         };
  797       })(this), function(result) {
  798         var invalidUrls, invalidUrlsError;
  799         if (!result) {
  800           invalidUrls = "Couldn't clone " + (urls.join(' or '));
  801           invalidUrlsError = new Error(invalidUrls);
  802           return callback(invalidUrlsError);
  803         } else {
  804           return callback();
  805         }
  806       });
  807     };
  808 
  809     Install.prototype.cloneNormalizedUrl = function(url, cloneDir, options, callback) {
  810       var Develop, develop;
  811       Develop = require('./develop');
  812       develop = new Develop();
  813       return develop.cloneRepository(url, cloneDir, options, function(err) {
  814         return callback(err);
  815       });
  816     };
  817 
  818     Install.prototype.installGitPackageDependencies = function(directory, options, callback) {
  819       options.cwd = directory;
  820       return this.installDependencies(options, callback);
  821     };
  822 
  823     Install.prototype.getRepositoryHeadSha = function(repoDir, callback) {
  824       var err, error1, repo, sha;
  825       try {
  826         repo = Git.open(repoDir);
  827         sha = repo.getReferenceTarget("HEAD");
  828         return callback(null, sha);
  829       } catch (error1) {
  830         err = error1;
  831         return callback(err);
  832       }
  833     };
  834 
  835     Install.prototype.run = function(options) {
  836       var callback, commands, error, error1, installPackage, iteratee, packageNames, packagesFilePath;
  837       callback = options.callback;
  838       options = this.parseOptions(options.commandArgs);
  839       packagesFilePath = options.argv['packages-file'];
  840       this.createAtomDirectories();
  841       if (options.argv.check) {
  842         config.loadNpm((function(_this) {
  843           return function(error, npm) {
  844             _this.npm = npm;
  845             return _this.loadInstalledAtomMetadata(function() {
  846               return _this.checkNativeBuildTools(callback);
  847             });
  848           };
  849         })(this));
  850         return;
  851       }
  852       this.verbose = options.argv.verbose;
  853       if (this.verbose) {
  854         request.debug(true);
  855         process.env.NODE_DEBUG = 'request';
  856       }
  857       installPackage = (function(_this) {
  858         return function(name, nextInstallStep) {
  859           var atIndex, gitPackageInfo, version;
  860           gitPackageInfo = _this.getHostedGitInfo(name);
  861           if (gitPackageInfo || name.indexOf('file://') === 0) {
  862             return _this.installGitPackage(name, options, nextInstallStep);
  863           } else if (name === '.') {
  864             return _this.installDependencies(options, nextInstallStep);
  865           } else {
  866             atIndex = name.indexOf('@');
  867             if (atIndex > 0) {
  868               version = name.substring(atIndex + 1);
  869               name = name.substring(0, atIndex);
  870             }
  871             return _this.isBundledPackage(name, function(isBundledPackage) {
  872               if (isBundledPackage) {
  873                 console.error(("The " + name + " package is bundled with Atom and should not be explicitly installed.\nYou can run `apm uninstall " + name + "` to uninstall it and then the version bundled\nwith Atom will be used.").yellow);
  874               }
  875               return _this.installRegisteredPackage({
  876                 name: name,
  877                 version: version
  878               }, options, nextInstallStep);
  879             });
  880           }
  881         };
  882       })(this);
  883       if (packagesFilePath) {
  884         try {
  885           packageNames = this.packageNamesFromPath(packagesFilePath);
  886         } catch (error1) {
  887           error = error1;
  888           return callback(error);
  889         }
  890       } else {
  891         packageNames = this.packageNamesFromArgv(options.argv);
  892         if (packageNames.length === 0) {
  893           packageNames.push('.');
  894         }
  895       }
  896       commands = [];
  897       commands.push((function(_this) {
  898         return function(callback) {
  899           return config.loadNpm(function(error, npm) {
  900             _this.npm = npm;
  901             return callback(error);
  902           });
  903         };
  904       })(this));
  905       commands.push((function(_this) {
  906         return function(callback) {
  907           return _this.loadInstalledAtomMetadata(function() {
  908             return callback();
  909           });
  910         };
  911       })(this));
  912       packageNames.forEach(function(packageName) {
  913         return commands.push(function(callback) {
  914           return installPackage(packageName, callback);
  915         });
  916       });
  917       iteratee = function(item, next) {
  918         return item(next);
  919       };
  920       return async.mapSeries(commands, iteratee, function(err, installedPackagesInfo) {
  921         if (err) {
  922           return callback(err);
  923         }
  924         installedPackagesInfo = _.compact(installedPackagesInfo);
  925         installedPackagesInfo = installedPackagesInfo.filter(function(item, idx) {
  926           return packageNames[idx] !== ".";
  927         });
  928         if (options.argv.json) {
  929           console.log(JSON.stringify(installedPackagesInfo, null, "  "));
  930         }
  931         return callback(null);
  932       });
  933     };
  934 
  935     return Install;
  936 
  937   })(Command);
  938 
  939 }).call(this);