-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpostinstall.js
53 lines (51 loc) · 2.11 KB
/
postinstall.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
const fs = require("fs");
const path = require("path");
const { getUrmConfig, setUrmConfig } = require("./lib/utils");
const { presetRegistries, urmRcFile } = require("./lib/const");
// npm scripts 中的 postinstall 既会在我们安装项目依赖时执行,也会在用户安装 urm 时执行,但我们预期的行为是:
// 1. 本地开发时,为了做 eslint,安装项目依赖后需要执行 “husky install” 以激活 git hooks,但用户安装 urm 时不需要这个功能。
// 2. 用户安装完成 urm 后,需要创建(或更新).urmrc 配置文件,但本地开发时,不需要这个功能
const toogle = process.argv[2];
if (toogle) {
// 读取 package.json 的内容
const pkgFile = path.join(path.dirname(process.argv[1]), "package.json");
const pkgBuffer = fs.readFileSync(pkgFile);
const pkgContent = pkgBuffer.toString("utf8");
const pkg = JSON.parse(pkgContent);
// 获取 package.json 的缩进大小
const match = /^[ ]+|\t+/m.exec(pkgContent);
const indent = match ? match[0] : null;
if (toogle === "--enable") {
pkg.scripts._postinstall = pkg.scripts._postinstall || pkg.scripts.postinstall;
pkg.scripts.postinstall = "node postinstall.js";
} else if (toogle === "--disable") {
pkg.scripts.postinstall = pkg.scripts._postinstall || pkg.scripts.postinstall;
delete pkg.scripts._postinstall;
}
// 获取 package.json 的 EOL
const POSIX_EOL = "\n";
const WINDOWS_EOL = "\r\n";
const lf = POSIX_EOL.charCodeAt(0);
const cr = WINDOWS_EOL.charCodeAt(0);
let eol;
for (let i = 0; i < pkgBuffer.length; ++i) {
if (pkgBuffer[i] === lf) {
eol = POSIX_EOL;
break;
}
if (pkgBuffer[i] === cr) {
eol = WINDOWS_EOL;
break;
}
}
// 更新 package.json 文件
const newPkgContent = JSON.stringify(pkg, null, indent).replace(/\n/g, eol);
fs.writeFileSync(pkgFile, newPkgContent);
} else {
const urmConfig = getUrmConfig();
urmConfig.presetRegistries = presetRegistries;
setUrmConfig(urmConfig);
if (process.platform !== "win32" && process.getuid && process.getuid() === 0) {
fs.chmod(urmRcFile, 0o664, () => {});
}
}