forked from ashward/jsinstallguard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostinstall.js
More file actions
119 lines (94 loc) · 2.81 KB
/
postinstall.js
File metadata and controls
119 lines (94 loc) · 2.81 KB
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
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
}
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);
}