"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/publish.js" (7 Feb 2017, 18278 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 Command, Git, Login, Packages, Publish, config, fs, path, request, semver, url, yargs,
    3     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; },
    4     hasProp = {}.hasOwnProperty,
    5     slice = [].slice;
    6 
    7   path = require('path');
    8 
    9   url = require('url');
   10 
   11   yargs = require('yargs');
   12 
   13   Git = require('git-utils');
   14 
   15   semver = require('semver');
   16 
   17   fs = require('./fs');
   18 
   19   config = require('./apm');
   20 
   21   Command = require('./command');
   22 
   23   Login = require('./login');
   24 
   25   Packages = require('./packages');
   26 
   27   request = require('./request');
   28 
   29   module.exports = Publish = (function(superClass) {
   30     extend(Publish, superClass);
   31 
   32     Publish.commandNames = ['publish'];
   33 
   34     function Publish() {
   35       this.userConfigPath = config.getUserConfigPath();
   36       this.atomNpmPath = require.resolve('npm/bin/npm-cli');
   37     }
   38 
   39     Publish.prototype.parseOptions = function(argv) {
   40       var options;
   41       options = yargs(argv).wrap(100);
   42       options.usage("\nUsage: apm publish [<newversion> | major | minor | patch | build]\n       apm publish --tag <tagname>\n       apm publish --rename <new-name>\n\nPublish a new version of the package in the current working directory.\n\nIf a new version or version increment is specified, then a new Git tag is\ncreated and the package.json file is updated with that new version before\nit is published to the apm registry. The HEAD branch and the new tag are\npushed up to the remote repository automatically using this option.\n\nIf a new name is provided via the --rename flag, the package.json file is\nupdated with the new name and the package's name is updated on Atom.io.\n\nRun `apm featured` to see all the featured packages or\n`apm view <packagename>` to see information about your package after you\nhave published it.");
   43       options.alias('h', 'help').describe('help', 'Print this usage message');
   44       options.alias('t', 'tag').string('tag').describe('tag', 'Specify a tag to publish');
   45       return options.alias('r', 'rename').string('rename').describe('rename', 'Specify a new name for the package');
   46     };
   47 
   48     Publish.prototype.versionPackage = function(version, callback) {
   49       var versionArgs;
   50       process.stdout.write('Preparing and tagging a new version ');
   51       versionArgs = ['version', version, '-m', 'Prepare %s release'];
   52       return this.fork(this.atomNpmPath, versionArgs, (function(_this) {
   53         return function(code, stderr, stdout) {
   54           if (stderr == null) {
   55             stderr = '';
   56           }
   57           if (stdout == null) {
   58             stdout = '';
   59           }
   60           if (code === 0) {
   61             _this.logSuccess();
   62             return callback(null, stdout.trim());
   63           } else {
   64             _this.logFailure();
   65             return callback((stdout + "\n" + stderr).trim());
   66           }
   67         };
   68       })(this));
   69     };
   70 
   71     Publish.prototype.pushVersion = function(tag, callback) {
   72       var pushArgs;
   73       process.stdout.write("Pushing " + tag + " tag ");
   74       pushArgs = ['push', 'origin', 'HEAD', tag];
   75       return this.spawn('git', pushArgs, (function(_this) {
   76         return function() {
   77           var args;
   78           args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
   79           return _this.logCommandResults.apply(_this, [callback].concat(slice.call(args)));
   80         };
   81       })(this));
   82     };
   83 
   84     Publish.prototype.waitForTagToBeAvailable = function(pack, tag, callback) {
   85       var interval, requestSettings, requestTags, retryCount;
   86       retryCount = 5;
   87       interval = 1000;
   88       requestSettings = {
   89         url: "https://api.github.com/repos/" + (Packages.getRepository(pack)) + "/tags",
   90         json: true
   91       };
   92       requestTags = function() {
   93         return request.get(requestSettings, function(error, response, tags) {
   94           var i, index, len, name;
   95           if (tags == null) {
   96             tags = [];
   97           }
   98           if ((response != null ? response.statusCode : void 0) === 200) {
   99             for (index = i = 0, len = tags.length; i < len; index = ++i) {
  100               name = tags[index].name;
  101               if (name === tag) {
  102                 return callback();
  103               }
  104             }
  105           }
  106           if (--retryCount <= 0) {
  107             return callback();
  108           } else {
  109             return setTimeout(requestTags, interval);
  110           }
  111         });
  112       };
  113       return requestTags();
  114     };
  115 
  116     Publish.prototype.packageExists = function(packageName, callback) {
  117       return Login.getTokenOrLogin(function(error, token) {
  118         var requestSettings;
  119         if (error != null) {
  120           return callback(error);
  121         }
  122         requestSettings = {
  123           url: (config.getAtomPackagesUrl()) + "/" + packageName,
  124           json: true,
  125           headers: {
  126             authorization: token
  127           }
  128         };
  129         return request.get(requestSettings, function(error, response, body) {
  130           if (body == null) {
  131             body = {};
  132           }
  133           if (error != null) {
  134             return callback(error);
  135           } else {
  136             return callback(null, response.statusCode === 200);
  137           }
  138         });
  139       });
  140     };
  141 
  142     Publish.prototype.registerPackage = function(pack, callback) {
  143       if (!pack.name) {
  144         callback('Required name field in package.json not found');
  145         return;
  146       }
  147       return this.packageExists(pack.name, (function(_this) {
  148         return function(error, exists) {
  149           var repository;
  150           if (error != null) {
  151             return callback(error);
  152           }
  153           if (exists) {
  154             return callback();
  155           }
  156           if (!(repository = Packages.getRepository(pack))) {
  157             callback('Unable to parse repository name/owner from package.json repository field');
  158             return;
  159           }
  160           process.stdout.write("Registering " + pack.name + " ");
  161           return Login.getTokenOrLogin(function(error, token) {
  162             var requestSettings;
  163             if (error != null) {
  164               _this.logFailure();
  165               callback(error);
  166               return;
  167             }
  168             requestSettings = {
  169               url: config.getAtomPackagesUrl(),
  170               json: true,
  171               body: {
  172                 repository: repository
  173               },
  174               headers: {
  175                 authorization: token
  176               }
  177             };
  178             return request.post(requestSettings, function(error, response, body) {
  179               var message;
  180               if (body == null) {
  181                 body = {};
  182               }
  183               if (error != null) {
  184                 return callback(error);
  185               } else if (response.statusCode !== 201) {
  186                 message = request.getErrorMessage(response, body);
  187                 _this.logFailure();
  188                 return callback("Registering package in " + repository + " repository failed: " + message);
  189               } else {
  190                 _this.logSuccess();
  191                 return callback(null, true);
  192               }
  193             });
  194           });
  195         };
  196       })(this));
  197     };
  198 
  199     Publish.prototype.createPackageVersion = function(packageName, tag, options, callback) {
  200       return Login.getTokenOrLogin(function(error, token) {
  201         var requestSettings;
  202         if (error != null) {
  203           callback(error);
  204           return;
  205         }
  206         requestSettings = {
  207           url: (config.getAtomPackagesUrl()) + "/" + packageName + "/versions",
  208           json: true,
  209           body: {
  210             tag: tag,
  211             rename: options.rename
  212           },
  213           headers: {
  214             authorization: token
  215           }
  216         };
  217         return request.post(requestSettings, function(error, response, body) {
  218           var message;
  219           if (body == null) {
  220             body = {};
  221           }
  222           if (error != null) {
  223             return callback(error);
  224           } else if (response.statusCode !== 201) {
  225             message = request.getErrorMessage(response, body);
  226             return callback("Creating new version failed: " + message);
  227           } else {
  228             return callback();
  229           }
  230         });
  231       });
  232     };
  233 
  234     Publish.prototype.publishPackage = function() {
  235       var callback, options, pack, remaining, tag;
  236       pack = arguments[0], tag = arguments[1], remaining = 3 <= arguments.length ? slice.call(arguments, 2) : [];
  237       if (remaining.length >= 2) {
  238         options = remaining.shift();
  239       }
  240       if (options == null) {
  241         options = {};
  242       }
  243       callback = remaining.shift();
  244       process.stdout.write("Publishing " + (options.rename || pack.name) + "@" + tag + " ");
  245       return this.createPackageVersion(pack.name, tag, options, (function(_this) {
  246         return function(error) {
  247           if (error != null) {
  248             _this.logFailure();
  249             return callback(error);
  250           } else {
  251             _this.logSuccess();
  252             return callback();
  253           }
  254         };
  255       })(this));
  256     };
  257 
  258     Publish.prototype.logFirstTimePublishMessage = function(pack) {
  259       process.stdout.write('Congrats on publishing a new package!'.rainbow);
  260       if (process.platform === 'darwin') {
  261         process.stdout.write(' \uD83D\uDC4D  \uD83D\uDCE6  \uD83C\uDF89');
  262       }
  263       return process.stdout.write("\nCheck it out at https://atom.io/packages/" + pack.name + "\n");
  264     };
  265 
  266     Publish.prototype.loadMetadata = function() {
  267       var error, error1, metadataPath, pack;
  268       metadataPath = path.resolve('package.json');
  269       if (!fs.isFileSync(metadataPath)) {
  270         throw new Error("No package.json file found at " + (process.cwd()) + "/package.json");
  271       }
  272       try {
  273         return pack = JSON.parse(fs.readFileSync(metadataPath));
  274       } catch (error1) {
  275         error = error1;
  276         throw new Error("Error parsing package.json file: " + error.message);
  277       }
  278     };
  279 
  280     Publish.prototype.saveMetadata = function(pack, callback) {
  281       var metadataJson, metadataPath;
  282       metadataPath = path.resolve('package.json');
  283       metadataJson = JSON.stringify(pack, null, 2);
  284       return fs.writeFile(metadataPath, metadataJson + "\n", callback);
  285     };
  286 
  287     Publish.prototype.loadRepository = function() {
  288       var currentBranch, currentDirectory, remoteName, repo, upstreamUrl;
  289       currentDirectory = process.cwd();
  290       repo = Git.open(currentDirectory);
  291       if (!(repo != null ? repo.isWorkingDirectory(currentDirectory) : void 0)) {
  292         throw new Error('Package must be in a Git repository before publishing: https://help.github.com/articles/create-a-repo');
  293       }
  294       if (currentBranch = repo.getShortHead()) {
  295         remoteName = repo.getConfigValue("branch." + currentBranch + ".remote");
  296       }
  297       if (remoteName == null) {
  298         remoteName = repo.getConfigValue('branch.master.remote');
  299       }
  300       if (remoteName) {
  301         upstreamUrl = repo.getConfigValue("remote." + remoteName + ".url");
  302       }
  303       if (upstreamUrl == null) {
  304         upstreamUrl = repo.getConfigValue('remote.origin.url');
  305       }
  306       if (!upstreamUrl) {
  307         throw new Error('Package must pushed up to GitHub before publishing: https://help.github.com/articles/create-a-repo');
  308       }
  309     };
  310 
  311     Publish.prototype.renamePackage = function(pack, name, callback) {
  312       var message;
  313       if ((name != null ? name.length : void 0) > 0) {
  314         if (pack.name === name) {
  315           return callback('The new package name must be different than the name in the package.json file');
  316         }
  317         message = "Renaming " + pack.name + " to " + name + " ";
  318         process.stdout.write(message);
  319         return this.setPackageName(pack, name, (function(_this) {
  320           return function(error) {
  321             if (error != null) {
  322               _this.logFailure();
  323               return callback(error);
  324             }
  325             return config.getSetting('git', function(gitCommand) {
  326               if (gitCommand == null) {
  327                 gitCommand = 'git';
  328               }
  329               return _this.spawn(gitCommand, ['add', 'package.json'], function(code, stderr, stdout) {
  330                 var addOutput;
  331                 if (stderr == null) {
  332                   stderr = '';
  333                 }
  334                 if (stdout == null) {
  335                   stdout = '';
  336                 }
  337                 if (code !== 0) {
  338                   _this.logFailure();
  339                   addOutput = (stdout + "\n" + stderr).trim();
  340                   return callback("`git add package.json` failed: " + addOutput);
  341                 }
  342                 return _this.spawn(gitCommand, ['commit', '-m', message], function(code, stderr, stdout) {
  343                   var commitOutput;
  344                   if (stderr == null) {
  345                     stderr = '';
  346                   }
  347                   if (stdout == null) {
  348                     stdout = '';
  349                   }
  350                   if (code === 0) {
  351                     _this.logSuccess();
  352                     return callback();
  353                   } else {
  354                     _this.logFailure();
  355                     commitOutput = (stdout + "\n" + stderr).trim();
  356                     return callback("Failed to commit package.json: " + commitOutput);
  357                   }
  358                 });
  359               });
  360             });
  361           };
  362         })(this));
  363       } else {
  364         return callback();
  365       }
  366     };
  367 
  368     Publish.prototype.setPackageName = function(pack, name, callback) {
  369       pack.name = name;
  370       return this.saveMetadata(pack, callback);
  371     };
  372 
  373     Publish.prototype.validateSemverRanges = function(pack) {
  374       var isValidRange, packageName, ref, ref1, ref2, semverRange;
  375       if (!pack) {
  376         return;
  377       }
  378       isValidRange = function(semverRange) {
  379         if (semver.validRange(semverRange)) {
  380           return true;
  381         }
  382         try {
  383           if (url.parse(semverRange).protocol.length > 0) {
  384             return true;
  385           }
  386         } catch (undefined) {}
  387         return semverRange === 'latest';
  388       };
  389       if (((ref = pack.engines) != null ? ref.atom : void 0) != null) {
  390         if (!semver.validRange(pack.engines.atom)) {
  391           throw new Error("The Atom engine range in the package.json file is invalid: " + pack.engines.atom);
  392         }
  393       }
  394       ref1 = pack.dependencies;
  395       for (packageName in ref1) {
  396         semverRange = ref1[packageName];
  397         if (!isValidRange(semverRange)) {
  398           throw new Error("The " + packageName + " dependency range in the package.json file is invalid: " + semverRange);
  399         }
  400       }
  401       ref2 = pack.devDependencies;
  402       for (packageName in ref2) {
  403         semverRange = ref2[packageName];
  404         if (!isValidRange(semverRange)) {
  405           throw new Error("The " + packageName + " dev dependency range in the package.json file is invalid: " + semverRange);
  406         }
  407       }
  408     };
  409 
  410     Publish.prototype.run = function(options) {
  411       var callback, error, error1, error2, error3, originalName, pack, ref, rename, tag, version;
  412       callback = options.callback;
  413       options = this.parseOptions(options.commandArgs);
  414       ref = options.argv, tag = ref.tag, rename = ref.rename;
  415       version = options.argv._[0];
  416       try {
  417         pack = this.loadMetadata();
  418       } catch (error1) {
  419         error = error1;
  420         return callback(error);
  421       }
  422       try {
  423         this.validateSemverRanges(pack);
  424       } catch (error2) {
  425         error = error2;
  426         return callback(error);
  427       }
  428       try {
  429         this.loadRepository();
  430       } catch (error3) {
  431         error = error3;
  432         return callback(error);
  433       }
  434       if ((version != null ? version.length : void 0) > 0 || (rename != null ? rename.length : void 0) > 0) {
  435         if (!((version != null ? version.length : void 0) > 0)) {
  436           version = 'patch';
  437         }
  438         if ((rename != null ? rename.length : void 0) > 0) {
  439           originalName = pack.name;
  440         }
  441         return this.registerPackage(pack, (function(_this) {
  442           return function(error, firstTimePublishing) {
  443             if (error != null) {
  444               return callback(error);
  445             }
  446             return _this.renamePackage(pack, rename, function(error) {
  447               if (error != null) {
  448                 return callback(error);
  449               }
  450               return _this.versionPackage(version, function(error, tag) {
  451                 if (error != null) {
  452                   return callback(error);
  453                 }
  454                 return _this.pushVersion(tag, function(error) {
  455                   if (error != null) {
  456                     return callback(error);
  457                   }
  458                   return _this.waitForTagToBeAvailable(pack, tag, function() {
  459                     if (originalName != null) {
  460                       rename = pack.name;
  461                       pack.name = originalName;
  462                     }
  463                     return _this.publishPackage(pack, tag, {
  464                       rename: rename
  465                     }, function(error) {
  466                       if (firstTimePublishing && (error == null)) {
  467                         _this.logFirstTimePublishMessage(pack);
  468                       }
  469                       return callback(error);
  470                     });
  471                   });
  472                 });
  473               });
  474             });
  475           };
  476         })(this));
  477       } else if ((tag != null ? tag.length : void 0) > 0) {
  478         return this.registerPackage(pack, (function(_this) {
  479           return function(error, firstTimePublishing) {
  480             if (error != null) {
  481               return callback(error);
  482             }
  483             return _this.publishPackage(pack, tag, function(error) {
  484               if (firstTimePublishing && (error == null)) {
  485                 _this.logFirstTimePublishMessage(pack);
  486               }
  487               return callback(error);
  488             });
  489           };
  490         })(this));
  491       } else {
  492         return callback('A version, tag, or new package name is required');
  493       }
  494     };
  495 
  496     return Publish;
  497 
  498   })(Command);
  499 
  500 }).call(this);