/
setup.js
executable file
·198 lines (180 loc) · 4.79 KB
/
setup.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const {
existsSync,
appendFileSync,
readFileSync,
writeFileSync,
} = require('node:fs')
const { spawnSync } = require('node:child_process')
const path = require('node:path')
const { FEATURE } = require('./toggles')
const NPM = {
RCFILE: '.npmrc',
CONF: {
SCRIPTS: 'ignore-scripts=true',
BINS: 'bin-links=false',
},
}
const YARN1 = {
RCFILE: '.yarnrc',
CONF: {
SCRIPTS: 'ignore-scripts true',
BINS: '--*.no-bin-links true',
},
}
const YARN3 = {
RCFILE: '.yarnrc.yml',
CONF: {
SCRIPTS: 'enableScripts: false',
},
}
module.exports = {
writeRcFile,
areBinsBlocked,
editPackageJson,
}
/**
* @param {string} filename
* @returns {string}
*/
function addInstallParentDir(filename) {
const rootDir = process.env.INIT_CWD || process.cwd()
return path.join(rootDir, filename)
}
/**
* @param {string} entry
* @param {string} file
* @returns {boolean}
*/
function isEntryPresent(entry, file) {
const rcPath = addInstallParentDir(file)
if (!existsSync(rcPath)) {
return false
}
const rcFileContents = readFileSync(rcPath, 'utf8')
return rcFileContents.includes(entry)
}
/**
* @param {WriteRcFileContentOpts} param0
*/
function writeRcFileContent({ file, entry }) {
const rcPath = addInstallParentDir(file)
if (isEntryPresent(entry, file)) {
console.log(
`@lavamoat/allow-scripts: file ${rcPath} already exists with entry: ${entry}.`
)
} else {
appendFileSync(rcPath, entry + '\n')
console.log(`@lavamoat/allow-scripts: added entry to ${rcPath}: ${entry}.`)
}
}
/**
* @type {boolean}
*/
let binsBlockedMemo
/**
* @param {AreBinsBlockedOpts} args
* @returns {boolean}
*/
function areBinsBlocked({ noMemoization = false } = {}) {
if (noMemoization || binsBlockedMemo === undefined) {
binsBlockedMemo =
isEntryPresent(NPM.CONF.BINS, NPM.RCFILE) ||
isEntryPresent(YARN1.CONF.BINS, YARN1.RCFILE)
// Once yarn3 support via plugin comes in, this function would need to detect that, or cease to exist.
}
return binsBlockedMemo
}
function writeRcFile() {
const yarnRcExists = existsSync(addInstallParentDir(YARN1.RCFILE))
const yarnYmlExists = existsSync(addInstallParentDir(YARN3.RCFILE))
const npmRcExists = existsSync(addInstallParentDir(NPM.RCFILE))
const yarnLockExists = existsSync(addInstallParentDir('yarn.lock'))
const configs = []
if (yarnRcExists || yarnLockExists) {
configs.push({
file: YARN1.RCFILE,
exists: yarnRcExists,
entry: YARN1.CONF.SCRIPTS,
})
if (FEATURE.bins) {
configs.push({
file: YARN1.RCFILE,
exists: yarnRcExists,
entry: YARN1.CONF.BINS,
})
}
}
if (yarnYmlExists || yarnLockExists) {
configs.push({
file: YARN3.RCFILE,
exists: yarnYmlExists,
entry: YARN3.CONF.SCRIPTS,
})
}
if (configs.length === 0) {
// default to npm, because that's what everyone has anyway
configs.push({
file: NPM.RCFILE,
exists: npmRcExists,
entry: NPM.CONF.SCRIPTS,
})
if (FEATURE.bins) {
configs.push({
file: NPM.RCFILE,
exists: npmRcExists,
entry: NPM.CONF.BINS,
})
}
}
configs.forEach(writeRcFileContent)
}
function editPackageJson() {
let cmd, cmdArgs
if (existsSync('./.npmrc')) {
cmd = 'npm'
cmdArgs = ['install', '-d', '@lavamoat/preinstall-always-fail']
} else {
cmd = 'yarn'
cmdArgs = ['add', '-D', '@lavamoat/preinstall-always-fail']
}
let result = spawnSync(cmd, cmdArgs, {})
if (result.status !== 0) {
process.stderr.write(result.stderr)
console.log(
'@lavamoat/allow-scripts: Could not add @lavamoat/preinstall-always-fail.'
)
} else {
console.log(
'@lavamoat/allow-scripts: Added dependency @lavamoat/preinstall-always-fail.'
)
}
if (FEATURE.bins) {
// no motivation to fix lint here, there's a better implementation of this in a neighboring branch
const packageJson = /** @type {import('type-fest').PackageJson} */ (
require(addInstallParentDir('package.json'))
)
if (!packageJson.scripts) {
packageJson.scripts = {}
}
// If you think `node ` is redundant below, be aware that `./cli.js` won't work on Windows,
// but passing a unix-style path to node on Windows works fine.
packageJson.scripts['allow-scripts'] =
'node ./node_modules/@lavamoat/allow-scripts/src/cli.js --experimental-bins'
console.log(
'@lavamoat/allow-scripts: Adding allow-scripts as a package.json script with direct path.'
)
writeFileSync(
addInstallParentDir('package.json'),
JSON.stringify(packageJson, null, 2)
)
}
}
/**
* @typedef WriteRcFileContentOpts
* @property {string} file
* @property {string} entry
*/
/**
* @typedef AreBinsBlockedOpts
* @property {boolean} [noMemoization] Turn off memoization, make a fresh lookup
*/