"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/version.js" (11 Apr 2017, 7865 Bytes) of package /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 // npm version <newver>
    2 
    3 module.exports = version
    4 
    5 var semver = require('semver')
    6 var path = require('path')
    7 var fs = require('graceful-fs')
    8 var writeFileAtomic = require('write-file-atomic')
    9 var chain = require('slide').chain
   10 var log = require('npmlog')
   11 var npm = require('./npm.js')
   12 var git = require('./utils/git.js')
   13 var assert = require('assert')
   14 var lifecycle = require('./utils/lifecycle.js')
   15 var parseJSON = require('./utils/parse-json.js')
   16 var output = require('./utils/output.js')
   17 
   18 version.usage = 'npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]' +
   19                 '\n(run in package dir)\n' +
   20                 "'npm -v' or 'npm --version' to print npm version " +
   21                 '(' + npm.version + ')\n' +
   22                 "'npm view <pkg> version' to view a package's " +
   23                 'published version\n' +
   24                 "'npm ls' to inspect current package/dependency versions"
   25 
   26 function version (args, silent, cb_) {
   27   if (typeof cb_ !== 'function') {
   28     cb_ = silent
   29     silent = false
   30   }
   31   if (args.length > 1) return cb_(version.usage)
   32 
   33   readPackage(function (er, data) {
   34     if (!args.length) return dump(data, cb_)
   35 
   36     if (er) {
   37       log.error('version', 'No valid package.json found')
   38       return cb_(er)
   39     }
   40 
   41     if (args[0] === 'from-git') {
   42       retrieveTagVersion(silent, data, cb_)
   43     } else {
   44       var newVersion = semver.valid(args[0])
   45       if (!newVersion) newVersion = semver.inc(data.version, args[0])
   46       if (!newVersion) return cb_(version.usage)
   47       persistVersion(newVersion, silent, data, cb_)
   48     }
   49   })
   50 }
   51 
   52 function retrieveTagVersion (silent, data, cb_) {
   53   chain([
   54     verifyGit,
   55     parseLastGitTag
   56   ], function (er, results) {
   57     if (er) return cb_(er)
   58     var localData = {
   59       hasGit: true,
   60       existingTag: true
   61     }
   62 
   63     var version = results[results.length - 1]
   64     persistVersion(version, silent, data, localData, cb_)
   65   })
   66 }
   67 
   68 function parseLastGitTag (cb) {
   69   var options = { env: process.env }
   70   git.whichAndExec(['describe', '--abbrev=0'], options, function (er, stdout) {
   71     if (er) {
   72       if (er.message.indexOf('No names found') !== -1) return cb(new Error('No tags found'))
   73       return cb(er)
   74     }
   75 
   76     var tag = stdout.trim()
   77     var prefix = npm.config.get('tag-version-prefix')
   78     // Strip the prefix from the start of the tag:
   79     if (tag.indexOf(prefix) === 0) tag = tag.slice(prefix.length)
   80     var version = semver.valid(tag)
   81     if (!version) return cb(new Error(tag + ' is not a valid version'))
   82     cb(null, version)
   83   })
   84 }
   85 
   86 function persistVersion (newVersion, silent, data, localData, cb_) {
   87   if (typeof localData === 'function') {
   88     cb_ = localData
   89     localData = {}
   90   }
   91 
   92   if (data.version === newVersion) return cb_(new Error('Version not changed'))
   93   data.version = newVersion
   94   var lifecycleData = Object.create(data)
   95   lifecycleData._id = data.name + '@' + newVersion
   96 
   97   var where = npm.prefix
   98   chain([
   99     !localData.hasGit && [checkGit, localData],
  100     [lifecycle, lifecycleData, 'preversion', where],
  101     [updatePackage, newVersion, silent],
  102     [lifecycle, lifecycleData, 'version', where],
  103     [commit, localData, newVersion],
  104     [lifecycle, lifecycleData, 'postversion', where]
  105   ], cb_)
  106 }
  107 
  108 function readPackage (cb) {
  109   var packagePath = path.join(npm.localPrefix, 'package.json')
  110   fs.readFile(packagePath, function (er, data) {
  111     if (er) return cb(new Error(er))
  112     if (data) data = data.toString()
  113     try {
  114       data = JSON.parse(data)
  115     } catch (e) {
  116       er = e
  117       data = null
  118     }
  119     cb(er, data)
  120   })
  121 }
  122 
  123 function updatePackage (newVersion, silent, cb_) {
  124   function cb (er) {
  125     if (!er && !silent) output('v' + newVersion)
  126     cb_(er)
  127   }
  128 
  129   readPackage(function (er, data) {
  130     if (er) return cb(new Error(er))
  131     data.version = newVersion
  132     write(data, 'package.json', cb)
  133   })
  134 }
  135 
  136 function commit (localData, newVersion, cb) {
  137   updateShrinkwrap(newVersion, function (er, hasShrinkwrap) {
  138     if (er || !localData.hasGit) return cb(er)
  139     localData.hasShrinkwrap = hasShrinkwrap
  140     _commit(newVersion, localData, cb)
  141   })
  142 }
  143 
  144 function updateShrinkwrap (newVersion, cb) {
  145   fs.readFile(path.join(npm.localPrefix, 'npm-shrinkwrap.json'), function (er, data) {
  146     if (er && er.code === 'ENOENT') return cb(null, false)
  147 
  148     try {
  149       data = data.toString()
  150       data = parseJSON(data)
  151     } catch (er) {
  152       log.error('version', 'Bad npm-shrinkwrap.json data')
  153       return cb(er)
  154     }
  155 
  156     data.version = newVersion
  157     write(data, 'npm-shrinkwrap.json', function (er) {
  158       if (er) {
  159         log.error('version', 'Bad npm-shrinkwrap.json data')
  160         return cb(er)
  161       }
  162       cb(null, true)
  163     })
  164   })
  165 }
  166 
  167 function dump (data, cb) {
  168   var v = {}
  169 
  170   if (data && data.name && data.version) v[data.name] = data.version
  171   v.npm = npm.version
  172   Object.keys(process.versions).sort().forEach(function (k) {
  173     v[k] = process.versions[k]
  174   })
  175 
  176   if (npm.config.get('json')) v = JSON.stringify(v, null, 2)
  177 
  178   output(v)
  179   cb()
  180 }
  181 
  182 function statGitFolder (cb) {
  183   fs.stat(path.join(npm.localPrefix, '.git'), cb)
  184 }
  185 
  186 function callGitStatus (cb) {
  187   git.whichAndExec(
  188     [ 'status', '--porcelain' ],
  189     { env: process.env },
  190     cb
  191   )
  192 }
  193 
  194 function cleanStatusLines (stdout) {
  195   var lines = stdout.trim().split('\n').filter(function (line) {
  196     return line.trim() && !line.match(/^\?\? /)
  197   }).map(function (line) {
  198     return line.trim()
  199   })
  200 
  201   return lines
  202 }
  203 
  204 function verifyGit (cb) {
  205   function checkStatus (er) {
  206     if (er) return cb(er)
  207     callGitStatus(checkStdout)
  208   }
  209 
  210   function checkStdout (er, stdout) {
  211     if (er) return cb(er)
  212     var lines = cleanStatusLines(stdout)
  213     if (lines.length > 0) {
  214       return cb(new Error(
  215         'Git working directory not clean.\n' + lines.join('\n')
  216       ))
  217     }
  218 
  219     cb()
  220   }
  221 
  222   statGitFolder(checkStatus)
  223 }
  224 
  225 function checkGit (localData, cb) {
  226   statGitFolder(function (er) {
  227     var doGit = !er && npm.config.get('git-tag-version')
  228     if (!doGit) {
  229       if (er) log.verbose('version', 'error checking for .git', er)
  230       log.verbose('version', 'not tagging in git')
  231       return cb(null, false)
  232     }
  233 
  234     // check for git
  235     callGitStatus(function (er, stdout) {
  236       if (er && er.code === 'ENOGIT') {
  237         log.warn(
  238           'version',
  239           'This is a Git checkout, but the git command was not found.',
  240           'npm could not create a Git tag for this release!'
  241         )
  242         return cb(null, false)
  243       }
  244 
  245       var lines = cleanStatusLines(stdout)
  246       if (lines.length && !npm.config.get('force')) {
  247         return cb(new Error(
  248           'Git working directory not clean.\n' + lines.join('\n')
  249         ))
  250       }
  251       localData.hasGit = true
  252       cb(null, true)
  253     })
  254   })
  255 }
  256 
  257 function _commit (version, localData, cb) {
  258   var packagePath = path.join(npm.localPrefix, 'package.json')
  259   var options = { env: process.env }
  260   var message = npm.config.get('message').replace(/%s/g, version)
  261   var sign = npm.config.get('sign-git-tag')
  262   var flag = sign ? '-sm' : '-am'
  263   chain(
  264     [
  265       git.chainableExec([ 'add', packagePath ], options),
  266       localData.hasShrinkwrap && git.chainableExec([ 'add', 'npm-shrinkwrap.json' ], options),
  267       git.chainableExec([ 'commit', '-m', message ], options),
  268       !localData.existingTag && git.chainableExec([
  269         'tag',
  270         npm.config.get('tag-version-prefix') + version,
  271         flag,
  272         message
  273       ], options)
  274     ],
  275     cb
  276   )
  277 }
  278 
  279 function write (data, file, cb) {
  280   assert(data && typeof data === 'object', 'must pass data to version write')
  281   assert(typeof file === 'string', 'must pass filename to write to version write')
  282 
  283   log.verbose('version.write', 'data', data, 'to', file)
  284   writeFileAtomic(
  285     path.join(npm.localPrefix, file),
  286     new Buffer(JSON.stringify(data, null, 2) + '\n'),
  287     cb
  288   )
  289 }