1 // npm version <newver>
3 module.exports = version
5 var exec = require("child_process").execFile
6 , semver = require("semver")
7 , path = require("path")
8 , fs = require("graceful-fs")
9 , chain = require("slide").chain
10 , log = require("npmlog")
11 , which = require("which")
12 , npm = require("./npm.js")
14 version.usage = "npm version [<newversion> | major | minor | patch]\n"
15 + "\n(run in package dir)\n"
16 + "'npm -v' or 'npm --version' to print npm version "
17 + "("+npm.version+")\n"
18 + "'npm view <pkg> version' to view a package's "
19 + "published version\n"
20 + "'npm ls' to inspect current package/dependency versions"
22 function version (args, silent, cb_) {
23 if (typeof cb_ !== "function") cb_ = silent, silent = false
24 if (args.length > 1) return cb_(version.usage)
25 fs.readFile(path.join(process.cwd(), "package.json"), function (er, data) {
28 Object.keys(process.versions).forEach(function (k) {
29 v[k] = process.versions[k]
33 data = JSON.parse(data.toString())
37 if (data && data.name && data.version) {
38 v[data.name] = data.version
45 log.error("version", "No package.json found")
50 data = JSON.parse(data)
52 log.error("version", "Bad package.json data")
56 var newVer = semver.valid(args[0])
57 if (!newVer) newVer = semver.inc(data.version, args[0])
58 if (!newVer) return cb_(version.usage)
59 if (data.version === newVer) return cb_(new Error("Version not changed"))
62 fs.stat(path.join(process.cwd(), ".git"), function (er, s) {
64 if (!er && !silent) console.log("v" + newVer)
68 var tags = npm.config.get('git-tag-version')
69 var doGit = !er && s.isDirectory() && tags
70 if (!doGit) return write(data, cb)
71 else checkGit(data, cb)
76 function checkGit (data, cb) {
77 var git = npm.config.get("git")
78 var args = [ "status", "--porcelain" ]
82 which(git, function (err) {
91 function gitFound () {
92 exec(git, args, {env: env}, function (er, stdout, stderr) {
93 var lines = stdout.trim().split("\n").filter(function (line) {
94 return line.trim() && !line.match(/^\?\? /)
95 }).map(function (line) {
98 if (lines.length) return cb(new Error(
99 "Git working directory not clean.\n"+lines.join("\n")))
100 write(data, function (er) {
101 if (er) return cb(er)
102 var message = npm.config.get("message").replace(/%s/g, data.version)
103 , sign = npm.config.get("sign-git-tag")
104 , flag = sign ? "-sm" : "-am"
106 ( [ [ exec, git, [ "add", "package.json" ], {env: process.env} ]
107 , [ exec, git, [ "commit", "-m", message ], {env: process.env} ]
108 , [ exec, git, [ "tag", "v" + data.version, flag, message ]
109 , {env: process.env} ] ]
116 function write (data, cb) {
117 fs.writeFile( path.join(process.cwd(), "package.json")
118 , new Buffer(JSON.stringify(data, null, 2) + "\n")