Skip to content

Commit 8b80357

Browse files
committed
add workaround for linked pkgs, add --verbose and --debug cli flags
1 parent 5eab211 commit 8b80357

File tree

5 files changed

+114
-22
lines changed

5 files changed

+114
-22
lines changed

β€Žsrc/getPackageResolution.tsβ€Ž

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ import findWorkspaceRoot from "find-yarn-workspace-root"
77
import { getPackageVersion } from "./getPackageVersion"
88
import { execSync } from "child_process"
99

10-
const isVerbose = false // TODO expose to CLI
10+
const isVerbose = global.patchPackageIsVerbose
11+
const isDebug = global.patchPackageIsDebug
1112

1213
export function getPackageResolution({
1314
packageDetails,
1415
packageManager,
1516
appPath,
17+
appPackageJson,
1618
}: {
1719
packageDetails: PackageDetails
1820
packageManager: PackageManager
1921
appPath: string
22+
appPackageJson: any
2023
}) {
2124
if (packageManager === "yarn") {
2225
let lockFilePath = "yarn.lock"
@@ -76,17 +79,63 @@ export function getPackageResolution({
7679
}
7780
return { version: resolution }
7881
} else if (packageManager === "pnpm") {
82+
// WORKAROUND for pnpm bug? pnpm-lock.yaml says version 1.2.3 for linked packages, not link:../../path/to/package
83+
const declaredVersion =
84+
(appPackageJson.dependencies &&
85+
appPackageJson.dependencies[packageDetails.name]) ||
86+
(appPackageJson.devDependencies &&
87+
appPackageJson.devDependencies[packageDetails.name])
88+
if (isDebug) {
89+
console.log(
90+
`patch-package/getPackageResolution: declaredVersion = ${declaredVersion}`,
91+
)
92+
}
93+
7994
const lockfile = require("js-yaml").load(
8095
require("fs").readFileSync(join(appPath, "pnpm-lock.yaml"), "utf8"),
8196
)
97+
if (isDebug) {
98+
console.log(`patch-package/getPackageResolution: appPath = ${appPath}`)
99+
console.log(`patch-package/getPackageResolution: packageDetails:`)
100+
console.dir(packageDetails)
101+
console.log(
102+
`patch-package/getPackageResolution: packageDetails.name = ${packageDetails.name}`,
103+
)
104+
console.log(
105+
`patch-package/getPackageResolution: lockfile.dependencies[packageDetails.name] = ${
106+
lockfile.dependencies[packageDetails.name]
107+
}`,
108+
)
109+
console.log(
110+
`patch-package/getPackageResolution: lockfile.devDependencies[packageDetails.name] = ${
111+
lockfile.devDependencies[packageDetails.name]
112+
}`,
113+
)
114+
}
82115
let resolvedVersion =
83116
(lockfile.dependencies && lockfile.dependencies[packageDetails.name]) ||
84117
(lockfile.devDependencies &&
85118
lockfile.devDependencies[packageDetails.name])
119+
if (declaredVersion.startsWith("link:")) {
120+
// WORKAROUND
121+
if (isDebug) {
122+
console.log(
123+
`patch-package/getPackageResolution: using declaredVersion ${declaredVersion}, not resolvedVersion ${resolvedVersion}`,
124+
)
125+
}
126+
resolvedVersion = declaredVersion
127+
}
128+
if (isDebug) {
129+
console.log(
130+
`patch-package/getPackageResolution: resolvedVersion = ${resolvedVersion}`,
131+
)
132+
}
86133
if (resolvedVersion.startsWith("link:")) {
87134
const localPath = resolve(resolvedVersion.slice(5))
88135
if (isVerbose) {
89-
console.log(`pnpm installed ${packageDetails.name} from ${localPath}`)
136+
console.log(
137+
`patch-package/getPackageResolution: pnpm installed ${packageDetails.name} from ${localPath}`,
138+
)
90139
}
91140
if (existsSync(localPath + "/.git")) {
92141
// we hope that the originCommit will be available for future downloads
@@ -100,11 +149,12 @@ export function getPackageResolution({
100149
}).trim()
101150
}
102151
const originUrl = exec("git remote get-url origin")
152+
// TODO what if the upstream repo is not called "origin"?
103153
const originCommit = exec("git rev-parse origin/HEAD") // npm needs the long commit hash
104154
resolvedVersion = `git+${originUrl}#${originCommit}`
105155
if (isVerbose) {
106156
console.log(
107-
`using ${packageDetails.name} version ${resolvedVersion} from git origin/HEAD in ${localPath}`,
157+
`patch-package/getPackageResolution: using ${packageDetails.name} version ${resolvedVersion} from git origin/HEAD in ${localPath}`,
108158
)
109159
}
110160
return { version: resolvedVersion, originCommit }
@@ -119,7 +169,9 @@ export function getPackageResolution({
119169
}
120170
}
121171
if (isVerbose) {
122-
console.log(`using ${packageDetails.name} version ${resolvedVersion}`)
172+
console.log(
173+
`patch-package/getPackageResolution: using ${packageDetails.name} version ${resolvedVersion}`,
174+
)
123175
}
124176
return { version: resolvedVersion }
125177
} else {
@@ -158,6 +210,7 @@ if (require.main === module) {
158210
appPath: process.cwd(),
159211
packageDetails,
160212
packageManager: detectPackageManager(process.cwd(), null),
213+
appPackageJson: {}, // TODO?
161214
}),
162215
)
163216
}

β€Žsrc/index.tsβ€Ž

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ import chalk from "chalk"
22
import process from "process"
33
import minimist from "minimist"
44

5-
import { applyPatchesForApp } from "./applyPatches"
65
import { getAppRootPath } from "./getAppRootPath"
7-
import { makePatch } from "./makePatch"
8-
import { makeRegExp } from "./makeRegExp"
9-
import { detectPackageManager } from "./detectPackageManager"
106
import { join } from "./path"
11-
import { normalize, sep } from "path"
12-
import slash = require("slash")
13-
import isCi from "is-ci"
147

158
const appPath = getAppRootPath()
169
const argv = minimist(process.argv.slice(2), {
@@ -33,6 +26,23 @@ console.log(
3326
require(join(__dirname, "../package.json")).version,
3427
)
3528

29+
// used in imported modules
30+
const isDebug = (global.patchPackageIsDebug = Boolean(argv.debug))
31+
global.patchPackageIsVerbose = isDebug || Boolean(argv.verbose)
32+
33+
if (isDebug) {
34+
console.log(`patch-package/index: argv:`)
35+
console.dir(argv)
36+
}
37+
38+
import { applyPatchesForApp } from "./applyPatches"
39+
import { makePatch } from "./makePatch"
40+
import { makeRegExp } from "./makeRegExp"
41+
import { detectPackageManager } from "./detectPackageManager"
42+
import { normalize, sep } from "path"
43+
import slash = require("slash")
44+
import isCi from "is-ci"
45+
3646
if (argv.version || argv.v) {
3747
// noop
3848
} else if (argv.help || argv.h) {

β€Žsrc/makePatch.tsβ€Ž

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import {
3131
openIssueCreationLink,
3232
} from "./createIssue"
3333

34-
const isVerbose = false // TODO expose to CLI
34+
const isVerbose = global.patchPackageIsVerbose
35+
const isDebug = global.patchPackageIsDebug
3536

3637
function printNoPackageFoundError(
3738
packageName: string,
@@ -104,6 +105,7 @@ export function makePatch({
104105
packageDetails,
105106
packageManager,
106107
appPath,
108+
appPackageJson,
107109
})
108110

109111
// make a blank package.json
@@ -122,10 +124,27 @@ export function makePatch({
122124
)
123125

124126
// originCommit is more precise than pkg.version
127+
if (isDebug) {
128+
console.log(
129+
`patch-package/makePatch: resolvedVersion.originCommit = ${resolvedVersion.originCommit}`,
130+
)
131+
}
125132
const packageVersion =
126133
resolvedVersion.originCommit ||
127134
getPackageVersion(join(resolve(packageDetails.path), "package.json"))
128135

136+
if (isDebug) {
137+
console.log(`patch-package/makePatch: packageVersion = ${packageVersion}`)
138+
console.log(
139+
`patch-package/makePatch: package path = ${packageDetails.path}`,
140+
)
141+
console.log(
142+
`patch-package/makePatch: package path resolved = ${resolve(
143+
packageDetails.path,
144+
)}`,
145+
)
146+
}
147+
129148
// copy .npmrc/.yarnrc in case packages are hosted in private registry
130149
// tslint:disable-next-line:align
131150
;[".npmrc", ".yarnrc"].forEach((rcFile) => {
@@ -168,7 +187,9 @@ export function makePatch({
168187
// try first without ignoring scripts in case they are required
169188
// this works in 99.99% of cases
170189
if (isVerbose) {
171-
console.log(`run "${npmCmd} install --force" in ${tmpRepoNpmRoot}`)
190+
console.log(
191+
`patch-package/makePatch: run "${npmCmd} install --force" in ${tmpRepoNpmRoot}`,
192+
)
172193
}
173194
spawnSafeSync(npmCmd, ["install", "--force"], {
174195
cwd: tmpRepoNpmRoot,
@@ -180,7 +201,7 @@ export function makePatch({
180201
// an implicit context which we havn't reproduced
181202
if (isVerbose) {
182203
console.log(
183-
`run "${npmCmd} install --ignore-scripts --force" in ${tmpRepoNpmRoot}`,
204+
`patch-package/makePatch: run "${npmCmd} install --ignore-scripts --force" in ${tmpRepoNpmRoot}`,
184205
)
185206
}
186207
spawnSafeSync(npmCmd, ["install", "--ignore-scripts", "--force"], {
@@ -217,6 +238,7 @@ export function makePatch({
217238
git("config", "--local", "user.email", "[email protected]")
218239

219240
// remove ignored files first
241+
// use CLI options --exclude and --include
220242
removeIgnoredFiles(tmpRepoPackagePath, includePaths, excludePaths)
221243

222244
git("add", "-f", packageDetails.path)
@@ -226,7 +248,11 @@ export function makePatch({
226248
rimraf(tmpRepoPackagePath)
227249

228250
if (isVerbose) {
229-
console.log(`copy ${realpathSync(packagePath)} to ${tmpRepoPackagePath}`)
251+
console.log(
252+
`patch-package/makePatch: copy ${realpathSync(
253+
packagePath,
254+
)} to ${tmpRepoPackagePath}`,
255+
)
230256
}
231257

232258
// pnpm installs packages as symlinks, copySync would copy only the symlink
@@ -243,17 +269,13 @@ export function makePatch({
243269
rimraf(join(tmpRepoPackagePath, ".git"))
244270

245271
// also remove ignored files like before
272+
// use CLI options --exclude and --include
246273
removeIgnoredFiles(tmpRepoPackagePath, includePaths, excludePaths)
247274

248275
// stage all files
249276
git("add", "-f", packageDetails.path)
250277

251-
// TODO allow to add more paths via CLI, to exclude cache files like 'test/stubs*/**'
252-
const ignorePaths = [
253-
"package-lock.json",
254-
"pnpm-lock.yaml",
255-
// 'test/stubs*/**',
256-
]
278+
const ignorePaths = ["package-lock.json", "pnpm-lock.yaml"]
257279

258280
// get diff of changes
259281
const diffResult = git(

β€Žsrc/nodejsGlobal.d.tsβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare namespace NodeJS {
2+
interface Global {
3+
patchPackageIsVerbose: boolean
4+
patchPackageIsDebug: boolean
5+
}
6+
}

β€Žtslint.jsonβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"no-trailing-whitespace": [false],
1818
"object-literal-key-quotes": [false],
1919
"max-line-length": false,
20-
"no-default-export": true
20+
"no-default-export": true,
21+
"no-namespace": false
2122
},
2223
"rulesDirectory": []
2324
}

0 commit comments

Comments
Β (0)