"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/validate-npm-package-name/index.js" (8 Mar 2017, 2688 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 var scopedPackagePattern = new RegExp("^(?:@([^/]+?)[/])?([^/]+?)$");
    2 var builtins = require("builtins")
    3 var blacklist = [
    4   "node_modules",
    5   "favicon.ico"
    6 ];
    7 
    8 var validate = module.exports = function(name) {
    9 
   10   var warnings = []
   11   var errors = []
   12 
   13   if (name === null) {
   14     errors.push("name cannot be null")
   15     return done(warnings, errors)
   16   }
   17 
   18   if (name === undefined) {
   19     errors.push("name cannot be undefined")
   20     return done(warnings, errors)
   21   }
   22 
   23   if (typeof name !== "string") {
   24     errors.push("name must be a string")
   25     return done(warnings, errors)
   26   }
   27 
   28   if (!name.length) {
   29     errors.push("name length must be greater than zero")
   30   }
   31 
   32   if (name.match(/^\./)) {
   33     errors.push("name cannot start with a period")
   34   }
   35 
   36   if (name.match(/^_/)) {
   37     errors.push("name cannot start with an underscore")
   38   }
   39 
   40   if (name.trim() !== name) {
   41     errors.push("name cannot contain leading or trailing spaces")
   42   }
   43 
   44   // No funny business
   45   blacklist.forEach(function(blacklistedName){
   46     if (name.toLowerCase() === blacklistedName) {
   47       errors.push(blacklistedName + " is a blacklisted name")
   48     }
   49   })
   50 
   51   // Generate warnings for stuff that used to be allowed
   52 
   53   // core module names like http, events, util, etc
   54   builtins.forEach(function(builtin){
   55     if (name.toLowerCase() === builtin) {
   56       warnings.push(builtin + " is a core module name")
   57     }
   58   })
   59 
   60   // really-long-package-names-------------------------------such--length-----many---wow
   61   // the thisisareallyreallylongpackagenameitshouldpublishdowenowhavealimittothelengthofpackagenames-poch.
   62   if (name.length > 214) {
   63     warnings.push("name can no longer contain more than 214 characters")
   64   }
   65 
   66   // mIxeD CaSe nAMEs
   67   if (name.toLowerCase() !== name) {
   68     warnings.push("name can no longer contain capital letters")
   69   }
   70 
   71   if (encodeURIComponent(name) !== name) {
   72 
   73     // Maybe it's a scoped package name, like @user/package
   74     var nameMatch = name.match(scopedPackagePattern)
   75     if (nameMatch) {
   76       var user = nameMatch[1]
   77       var pkg = nameMatch[2]
   78       if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
   79         return done(warnings, errors)
   80       }
   81     }
   82 
   83     errors.push("name can only contain URL-friendly characters")
   84   }
   85 
   86   return done(warnings, errors)
   87 
   88 }
   89 
   90 validate.scopedPackagePattern = scopedPackagePattern
   91 
   92 var done = function (warnings, errors) {
   93   var result = {
   94     validForNewPackages: errors.length === 0 && warnings.length === 0,
   95     validForOldPackages: errors.length === 0,
   96     warnings: warnings,
   97     errors: errors
   98   }
   99   if (!result.warnings.length) delete result.warnings
  100   if (!result.errors.length) delete result.errors
  101   return result
  102 }