-
Notifications
You must be signed in to change notification settings - Fork 1
/
postinstall.js
152 lines (123 loc) · 3.69 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const FS = require("fs");
const Path = require("path");
const OS = require("os");
const npmrc = "onload-script=${PWD}/.jsig/jsig-npm.js";
const yarnrc = 'yarn-path "./.jsig/jsig-yarn.js"';
// First add (or replace) the .jsig directory
try {
let existingJsig = require("../../.jsig");
console.info("Found existing JSInstallGuard, version", existingJsig.version);
} catch (e) {
// Ignore - likely none found
}
function getNpmVersion() {
var matches = /npm\/((\d+)\.(\d+)\.(\d+)\S*)/.exec(process.env.npm_config_user_agent);
if(matches) {
return {
full: matches[1],
major: parseInt(matches[2]),
minor: parseInt(matches[3]),
patch: parseInt(matches[4])
}
}
}
var npmVersion = getNpmVersion();
if(npmVersion && npmVersion.major >= 7) {
console.error(
"================================= JSInstallGuard ===================================="
);
console.error(
"🚨 You are currently using npm version " + npmVersion.full
);
console.error(
"🚨 Due to breaking changes in npm version 7, JSInstallGuard can't support this version."
);
console.error(
"🚨 Please use npm 6 or below to benefit from JSInstallGuard."
);
console.error(
"====================================================================================="
);
}
const rootFiles = [
{
dir: ".",
name: "jsig-allow.json",
force: false,
},
{
dir: ".jsig",
name: "index.js",
force: true,
},
{
dir: ".jsig",
name: "jsig-npm.js",
force: true,
},
{
dir: ".jsig",
name: "jsig-yarn.js",
force: true,
},
];
const path1 = Path.parse(__dirname);
const path2 = Path.parse(path1.dir);
if (path2.base === "node_modules") {
// Looks like we're being installed as a project dependency
const rootDir = path2.dir;
// Remove the existing .jsig directory if there is one
const jsigDirectory = Path.join(rootDir, ".jsig");
if (FS.existsSync(jsigDirectory)) {
FS.readdirSync(jsigDirectory).forEach((file) =>
FS.unlinkSync(Path.resolve(jsigDirectory, file))
);
} else {
FS.mkdirSync(jsigDirectory);
}
// Copy the files
rootFiles.forEach((file) => {
const srcFile = Path.join("files", file.dir, file.name);
const destFile = Path.join(rootDir, file.dir, file.name);
if (!file.force && FS.existsSync(destFile)) {
console.warn(`${file.name} exists - won't overwrite`);
return;
}
FS.copyFileSync(srcFile, destFile);
});
// Manually create the .yarnrc and .npmrc files
// This can merge .yarnrc files
const yarnMerger = (content) => {
return mergeRcFile(content, /^yarn-path\s.*$/gm, yarnrc);
};
// This can merge .npmrc files
const npmMerger = (content) => {
return mergeRcFile(content, /^onload-script=.*$/gm, npmrc);
};
// This function will update an .xxxrc file on disk
function writeRcFile(file, merger) {
const destFile = Path.join(rootDir, file);
let newContent = "";
if (FS.existsSync(destFile)) {
newContent = FS.readFileSync(destFile, { encoding: "utf8" }).toString();
}
newContent = merger(newContent);
FS.writeFileSync(destFile, newContent, { encoding: "utf8" });
}
// This function will update the .xxxrc file content
function mergeRcFile(currentContent, pattern, newLine) {
let newContent = currentContent;
if (newContent.match(pattern)) {
newContent = newContent.replace(pattern, newLine);
} else {
if (!newContent.endsWith(OS.EOL)) {
newContent += OS.EOL;
}
newContent += newLine + OS.EOL;
}
return newContent;
}
// And these lines invoke the above functions to update the .xxxrc files
writeRcFile(".yarnrc", yarnMerger);
writeRcFile(".npmrc", npmMerger);
}