-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
makePatch.ts
193 lines (173 loc) Β· 5.82 KB
/
makePatch.ts
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
import { green } from "chalk"
import * as fs from "fs"
import * as path from "./path"
import * as rimraf from "rimraf"
import * as tmp from "tmp"
import {
resolveRelativeFileDependenciesInPackageJson,
resolveRelativeFileDependenciesInPackageLock,
} from "./resolveRelativeFileDependencies"
import spawnSafeSync from "./spawnSafe"
import { getPatchFiles } from "./patchFs"
import * as fsExtra from "fs-extra"
import { PackageManager } from "./detectPackageManager"
import * as slash from "slash"
function deleteScripts(json: any) {
delete json.scripts
return json
}
export default function makePatch(
packageName: string,
appPath: string,
packageManager: PackageManager,
includePaths: RegExp,
excludePaths: RegExp,
) {
const nodeModulesPath = path.join(appPath, "node_modules")
const packagePath = path.join(nodeModulesPath, packageName)
const packageJsonPath = path.join(packagePath, "package.json")
if (!fs.existsSync(packageJsonPath)) {
printNoPackageFoundError(packageName, packageJsonPath)
process.exit(1)
}
const packageVersion = require(packageJsonPath).version
const tmpRepo = tmp.dirSync({ unsafeCleanup: true })
const tmpRepoNodeModulesPath = path.join(tmpRepo.name, "node_modules")
const tmpRepoPackageJsonPath = path.join(tmpRepo.name, "package.json")
const tmpRepoPackagePath = path.join(tmpRepoNodeModulesPath, packageName)
try {
const patchesDir = path.join(appPath, "patches")
if (!fs.existsSync(patchesDir)) {
fs.mkdirSync(patchesDir)
} else {
// remove exsiting patch for this package, if any
getPatchFiles(patchesDir).forEach(fileName => {
if (
fileName.startsWith(packageName + ":") ||
fileName.startsWith(packageName + "+")
) {
console.info(
green("β"),
"Removing existing",
path.relative(process.cwd(), path.join(patchesDir, fileName)),
)
fs.unlinkSync(path.join(patchesDir, fileName))
}
})
}
console.info(green("β"), "Creating temporary folder")
const tmpExec = (command: string, args?: string[]) =>
spawnSafeSync(command, args, { cwd: tmpRepo.name })
// reinstall a clean version of the user's node_modules in our tmp location
fsExtra.copySync(
path.join(appPath, "package.json"),
path.join(tmpRepo.name, "package.json"),
)
// resolve relative file paths in package.json
// also delete scripts
fs.writeFileSync(
tmpRepoPackageJsonPath,
JSON.stringify(
deleteScripts(
resolveRelativeFileDependenciesInPackageJson(
appPath,
require(tmpRepoPackageJsonPath),
),
),
),
)
if (packageManager === "yarn") {
fsExtra.copySync(
path.join(appPath, "yarn.lock"),
path.join(tmpRepo.name, "yarn.lock"),
)
console.info(green("β"), "Building clean node_modules with yarn")
tmpExec(`yarn`)
} else {
const lockFileName =
packageManager === "npm-shrinkwrap"
? "npm-shrinkwrap.json"
: "package-lock.json"
const lockFileContents = JSON.parse(
fsExtra.readFileSync(path.join(appPath, lockFileName)).toString(),
)
const resolvedLockFileContents = resolveRelativeFileDependenciesInPackageLock(
appPath,
lockFileContents,
)
fs.writeFileSync(
path.join(tmpRepo.name, lockFileName),
JSON.stringify(resolvedLockFileContents),
)
console.info(green("β"), "Building clean node_modules with npm")
tmpExec("npm", ["i"])
}
// commit the package
console.info(green("β"), "Diffing your files with clean files")
fs.writeFileSync(
path.join(tmpRepo.name, ".gitignore"),
"!/node_modules\n\n",
)
tmpExec("git", ["init"])
// don't commit package.json though
fs.unlinkSync(
path.join(tmpRepo.name, "node_modules", packageName, "package.json"),
)
tmpExec("git", ["add", "-f", slash(path.join("node_modules", packageName))])
tmpExec("git", ["commit", "-m", "init"])
// replace package with user's version
rimraf.sync(tmpRepoPackagePath)
fsExtra.copySync(packagePath, tmpRepoPackagePath, { recursive: true })
// remove package.json again
fs.unlinkSync(
path.join(tmpRepo.name, "node_modules", packageName, "package.json"),
)
// stage all files
tmpExec("git", ["add", "-f", slash(path.join("node_modules", packageName))])
// unstage any ignored files so they don't show up in the diff
tmpExec("git", ["diff", "--cached", "--name-only"])
.stdout.toString()
.split(/\r?\n/)
.filter(Boolean)
.forEach(fileName => {
if (!fileName.match(includePaths) || fileName.match(excludePaths)) {
tmpExec("git", ["reset", "HEAD", fileName])
}
})
// get diff of changes
const patch = tmpExec("git", [
"diff",
"--cached",
"--no-color",
"--ignore-space-at-eol",
]).stdout.toString()
if (patch.trim() === "") {
console.warn(`βοΈ Not creating patch file for package '${packageName}'`)
console.warn(`βοΈ There don't appear to be any changes.`)
process.exit(1)
} else {
const patchFileName = `${packageName}+${packageVersion}.patch`
const patchPath = path.join(patchesDir, patchFileName)
if (!fs.existsSync(path.dirname(patchPath))) {
// scoped package
fs.mkdirSync(path.dirname(patchPath))
}
fs.writeFileSync(patchPath, patch)
console.log(`${green("β")} Created file patches/${patchFileName}`)
}
} catch (e) {
console.error(e)
throw e
} finally {
tmpRepo.removeCallback()
}
}
function printNoPackageFoundError(
packageName: string,
packageJsonPath: string,
) {
console.error(
`No such package ${packageName}
File not found: ${packageJsonPath}`,
)
}