forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.js
117 lines (104 loc) · 3.53 KB
/
version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// npm version <newver>
module.exports = version
var exec = require("child_process").execFile
, semver = require("semver")
, path = require("path")
, fs = require("graceful-fs")
, chain = require("slide").chain
, log = require("npmlog")
, which = require("which")
, npm = require("./npm.js")
, git = require("./utils/git.js")
version.usage = "npm version [<newversion> | major | minor | patch | prerelease | preminor | premajor ]\n"
+ "\n(run in package dir)\n"
+ "'npm -v' or 'npm --version' to print npm version "
+ "("+npm.version+")\n"
+ "'npm view <pkg> version' to view a package's "
+ "published version\n"
+ "'npm ls' to inspect current package/dependency versions"
function version (args, silent, cb_) {
if (typeof cb_ !== "function") cb_ = silent, silent = false
if (args.length > 1) return cb_(version.usage)
fs.readFile(path.join(npm.localPrefix, "package.json"), function (er, data) {
if (!args.length) {
var v = {}
Object.keys(process.versions).forEach(function (k) {
v[k] = process.versions[k]
})
v.npm = npm.version
try {
data = JSON.parse(data.toString())
} catch (er) {
data = null
}
if (data && data.name && data.version) {
v[data.name] = data.version
}
if (npm.config.get("json")) {
v = JSON.stringify(v, null, 2)
}
console.log(v)
return cb_()
}
if (er) {
log.error("version", "No package.json found")
return cb_(er)
}
try {
data = JSON.parse(data)
} catch (er) {
log.error("version", "Bad package.json data")
return cb_(er)
}
var newVer = semver.valid(args[0])
if (!newVer) newVer = semver.inc(data.version, args[0])
if (!newVer) return cb_(version.usage)
if (data.version === newVer) return cb_(new Error("Version not changed"))
data.version = newVer
fs.stat(path.join(npm.localPrefix, ".git"), function (er, s) {
function cb (er) {
if (!er && !silent) console.log("v" + newVer)
cb_(er)
}
var tags = npm.config.get('git-tag-version')
var doGit = !er && s.isDirectory() && tags
if (!doGit) return write(data, cb)
else checkGit(data, cb)
})
})
}
function checkGit (data, cb) {
var args = [ "status", "--porcelain" ]
var options = {env: process.env}
// check for git
git.whichAndExec(args, options, function (er, stdout) {
var lines = stdout.trim().split("\n").filter(function (line) {
return line.trim() && !line.match(/^\?\? /)
}).map(function (line) {
return line.trim()
})
if (lines.length) return cb(new Error(
"Git working directory not clean.\n"+lines.join("\n")))
write(data, function (er) {
if (er) return cb(er)
var message = npm.config.get("message").replace(/%s/g, data.version)
, sign = npm.config.get("sign-git-tag")
, flag = sign ? "-sm" : "-am"
chain
( [ git.chainableExec([ "add", "package.json" ], {env: process.env})
, git.chainableExec([ "commit", "-m", message ], {env: process.env})
, sign && function (cb) {
npm.spinner.stop()
cb()
}
, git.chainableExec([ "tag", "v" + data.version, flag, message ]
, {env: process.env}) ]
, cb )
})
})
}
function write (data, cb) {
fs.writeFile( path.join(npm.localPrefix, "package.json")
, new Buffer(JSON.stringify(data, null, 2) + "\n")
, cb )
}