1 |
|
2 | var TAG_MSG = ".git/TAG_MSG"
|
3 | , child = require("child_process")
|
4 | , path = require("path")
|
5 | , cli = require("./")
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | if (module.parent) {
|
11 |
|
12 | exports.execute = execute
|
13 | }
|
14 |
|
15 | function execute(args, i) {
|
16 | var g, i, msg
|
17 | , now = (new Date().toISOString().slice(2, 8) + "00").split(/-0?/)
|
18 | , com = JSON.parse(child.execSync("git show HEAD:package.json").toString("utf8"))
|
19 | , cur = require(path.resolve("package.json"))
|
20 | , junks = com.version.split(".")
|
21 | , len = junks.length
|
22 | , rewrite = args[i] === "-f"
|
23 | , lastTag = child.execSync("git describe --tags --abbrev=0 @^").toString("utf8").trim()
|
24 | , group = [
|
25 | { name: "New Features", re: /add\b/i, log: [] },
|
26 | { name: "Removed Features", re: /remove\b/i, log: [] },
|
27 | { name: "API Changes", re: /api\b/i, log: [] },
|
28 | { name: "Breaking Changes", re: /breake\b/i,
|
29 | log: child.spawnSync("git", [
|
30 | "log", "-z", "--grep", "break", "-i", lastTag + "..@"
|
31 | ], {stdio: ["ignore", "pipe", "inherit"]})
|
32 | .stdout.toString("utf8").split("\0").filter(Boolean)
|
33 | },
|
34 | { name: "Fixes", re: /fix\b/i, log: [] },
|
35 | { name: "Enhancements", re: null, log: [] }
|
36 | ]
|
37 |
|
38 | if (!rewrite && com.version === cur.version) {
|
39 | if (len > 3 || !(now[0] > junks[0] || now[1] > junks[1])) {
|
40 | junks[len - 1] = parseInt(junks[len - 1], 10) + 1
|
41 | } else {
|
42 | junks = now
|
43 | }
|
44 | cur.version = junks.join(".")
|
45 | cli.writeFile("package.json", JSON.stringify(cur, null, " ") + "\n")
|
46 | }
|
47 |
|
48 | msg = "Release " + cur.version + "\n\n"
|
49 | run(["build"])
|
50 | run(["test"])
|
51 |
|
52 |
|
53 | child.spawnSync("git", ["add", "-u"])
|
54 | child.spawnSync("lj", ["build"])
|
55 | child.spawnSync("git", ["add", "-u"])
|
56 | child.spawnSync("lj", ["build"])
|
57 |
|
58 | child.spawnSync("git", ["commit", "-a", "-m", msg, (rewrite ? "--amend" : "--")], { stdio: "inherit" })
|
59 |
|
60 | child.spawnSync("git", [
|
61 | "log", "--pretty=format:%s (%aN)", lastTag + "..@"
|
62 | ]).stdout.toString("utf8").split("\n").forEach(function(row) {
|
63 | for (var g, i = 0; g = group[i++]; ) {
|
64 | if (!g.re || g.re.test(row)) {
|
65 | return g.log.push(row)
|
66 | }
|
67 | }
|
68 | })
|
69 | msg = ""
|
70 | for (i = 0; g = group[i++]; ) {
|
71 | if (g.log.length) {
|
72 | msg += g.name + ":\n\n - " + g.log.join("\n - ") + "\n\n"
|
73 | }
|
74 | }
|
75 |
|
76 | cli.writeFile(TAG_MSG, msg)
|
77 |
|
78 | child.spawn(process.env.EDITOR || "vim", [TAG_MSG], { stdio: "inherit" })
|
79 | .on("exit", function (e, code) {
|
80 |
|
81 | child.spawnSync("git", ["tag", "-a", "v" + cur.version, "-F", TAG_MSG, rewrite ? "-f" : "--"], { stdio: "inherit" })
|
82 |
|
83 | console.log(`VERSION: ${cur.version}`)
|
84 | if (!cur.private) {
|
85 | console.log(`PUBLISH: npm publish${len === 3?'':' --tag next'}`)
|
86 | }
|
87 | })
|
88 |
|
89 | function run(args) {
|
90 | var sub = child.spawnSync("lj", args)
|
91 | if (sub.status) {
|
92 | console.error(`EXIT: ${sub.status}`, args)
|
93 | process.stderr.write(sub.stderr)
|
94 | process.exit(1)
|
95 | }
|
96 | msg += sub.stdout.toString("utf8")
|
97 | }
|
98 | }
|
99 |
|