forked from nanjingboy/nvmw
-
Notifications
You must be signed in to change notification settings - Fork 16
/
xclap.js
127 lines (111 loc) · 3.37 KB
/
xclap.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
118
119
120
121
122
123
124
125
126
127
"use strict";
const Fs = require("fs");
const Path = require("path");
const mkdirp = require("mkdirp");
const rimraf = require("rimraf");
const xclap = require("@xarc/run");
const pkgFile = Path.resolve("package.json");
let pkgData;
const moduleDev = require("@xarc/module-dev");
moduleDev.loadTasks({ xrun: xclap });
function readPkg() {
if (!pkgData) {
pkgData = Fs.readFileSync(pkgFile);
}
return pkgData;
}
function replaceLine(file, oldLine, newLine) {
const data = Fs.readFileSync(file, "utf8").split("\n");
let found = 0;
const newData = data.map(x => {
if (x === oldLine) {
found++;
return newLine;
}
return x;
});
if (found !== 1) {
throw new Error(`Replace file ${file} found ${found} old lines [${oldLine}]`);
}
Fs.writeFileSync(file, newData.join("\n"));
}
xclap.load("nvm", {
prepack: {
task: () => {
const data = readPkg();
const pkg = JSON.parse(data);
pkg.scripts = { preinstall: pkg.scripts.preinstall };
delete pkg.dependencies;
delete pkg.nyc;
delete pkg.devDependencies;
mkdirp.sync(Path.resolve(".tmp"));
Fs.writeFileSync(Path.resolve(".tmp/package.json"), data);
Fs.writeFileSync(pkgFile, `${JSON.stringify(pkg, null, 2)}\n`);
}
},
postpack: {
task: () => {
Fs.writeFileSync(pkgFile, readPkg());
}
},
".prepare": [".clean-dist", "nvm/bundle", "~$git diff --quiet", "nvm/prepack"],
release: {
desc: "Release a new version to npm. package.json must be updated.",
task: ["nvm/.prepare", "nvm/publish"],
finally: ["nvm/postpack"]
},
".clean-dist"() {
const dist = Path.resolve("dist");
rimraf.sync(dist);
mkdirp.sync(dist);
},
bundle: "webpack",
publish: "npm publish",
version: {
desc: "Bump version for release",
dep: ["bundle", "~$git diff --quiet"],
task() {
const data = readPkg();
const pkg = JSON.parse(data);
const oldVer = `${pkg.version}`;
let ver = oldVer.split(".").map(x => parseInt(x, 10));
const bump = this.argv[1];
switch (bump) {
case "--major":
ver[0]++;
ver[1] = ver[2] = 0;
break;
case "--minor":
ver[1]++;
ver[2] = 0;
break;
case "--patch":
ver[2]++;
break;
default:
ver = bump.substring(2).split(".");
console.log(`Using ${bump} as new version`);
break;
}
const newVer = ver.join(".");
replaceLine("install.ps1", `\$nvmVersion = "${oldVer}"`, `\$nvmVersion = "${newVer}"`);
replaceLine("install.sh", `NVM_VERSION="${oldVer}"`, `NVM_VERSION="${newVer}"`);
pkg.version = newVer;
Fs.writeFileSync("package.json", JSON.stringify(pkg, null, 2) + "\n");
const oldVerEsc = oldVer.replace(/\./g, "\\.");
const regex1 = new RegExp(`\\/v${oldVerEsc}`, "g");
const regex2 = new RegExp(`@${oldVerEsc}\\/`, "g");
const regex3 = new RegExp(`nvm-${oldVerEsc}`);
const readme = Fs.readFileSync("README.md", "utf8")
.replace(regex1, `/v${newVer}`)
.replace(regex2, `@${newVer}/`)
.replace(regex3, `nvm-${newVer}`);
Fs.writeFileSync("README.md", readme);
return xclap.serial([
`~$git add dist install.ps1 install.sh package.json README.md`,
`~$git commit -m "${newVer}"`,
`~$git tag "v${newVer}"`
]);
}
}
});