Skip to content

Commit 950fa9e

Browse files
committed
error -> warning: found wildcard version
1 parent bc54703 commit 950fa9e

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

src/makePatch.ts

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import {
1212
mkdirpSync,
1313
realpathSync,
1414
renameSync,
15+
/*
16+
lstatSync,
17+
readlinkSync,
18+
*/
1519
} from "fs-extra"
1620
import { sync as rimraf } from "rimraf"
1721
import { copySync } from "fs-extra"
@@ -150,9 +154,31 @@ export function makePatch({
150154
var protocol = m[1]
151155
var location = m[2]
152156
var isGit = protocol.startsWith('git')
153-
var gitCommit = isGit ? location.split('#').slice(-1)[0] : null
157+
var gitCommit = (isGit && location.includes("#")) ? location.split('#').slice(-1)[0] : null
158+
if (isDebug) {
159+
console.dir({ loc: 'get declaredVersion', isGit, gitCommit });
160+
}
154161
if (isGit && !gitCommit) {
155-
throw new Error(`error: found wildcard git version ${v}. package.json must pin the exact version of ${packageDetails.name} in the format <protocol>:<packagePath>#<commitHash>`)
162+
var error = new Error(`found wildcard git version ${v}. \
163+
package.json must pin the exact version of ${packageDetails.name} \
164+
in the format <protocol>:<packagePath>#<commitHash>. \
165+
commitHash is the full hash with 40 chars.`)
166+
delete error.stack
167+
throw error
168+
/* too complex
169+
// guess commit hash of installed package
170+
var stats = lstatSync(packageDetails.path)
171+
if (stats.isSymbolicLink()) {
172+
var linkTarget = readlinkSync(packageDetails.path)
173+
if (linkTarget.startsWith(".pnpm")) {
174+
var match = linkTarget.match(/^\.pnpm\/[^/]+@([0-9a-f]{10})_[0-9a-f]{32}\//)
175+
if (match) {
176+
gitCommit = match[1]
177+
if (isDebug) console.log(`parsed gitCommit ${gitCommit} from pnpm symlink`)
178+
}
179+
}
180+
}
181+
*/
156182
}
157183
if (isGit) {
158184
return { full: v, protocol, location, gitCommit }
@@ -163,17 +189,32 @@ export function makePatch({
163189
// -> use version number from package's package.json
164190
var version = getPackageVersion(join(resolve(packageDetails.path), "package.json"))
165191
if (isVerbose) {
166-
console.log(`patch-package/makePatch: warning: using version ${version} from ${packageDetails.name}/package.json`)
192+
console.log(`patch-package/makePatch: using version ${version} from ${packageDetails.name}/package.json`)
167193
}
168194
return { version }
169195
}
170196
}
171-
if (!v.match(/^[0-9]+\.[0-9]+\.[0-9]+/)) {
172-
throw new Error(`error: found wildcard version. package.json must pin the exact version of ${packageDetails.name} in the format <package>@<major>.<minor>.<patch>`)
197+
// https://docs.npmjs.com/about-semantic-versioning
198+
if (!v.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
199+
var exactPart = v.match(/^[^~]([0-9]+\.[0-9]+\.[0-9]+)$/)
200+
var exampleVersion = '1.2.3'
201+
if (exactPart) {
202+
exampleVersion = exactPart[1]
203+
}
204+
console.warn(`patch-package/makePatch: warning: found wildcard version. \
205+
to ensure successful patching, package.json should pin the exact version of ${packageDetails.name} \
206+
in the format <major>.<minor>.<patch>, for example: "${packageDetails.name}": "${exampleVersion}"`)
173207
}
174208
return { full: v, version: v }
175209
})()
176210

211+
if (isDebug) {
212+
//console.log(`patch-package/makePatch: resolvedVersion.originCommit = ${resolvedVersion.originCommit}`)
213+
console.log(`patch-package/makePatch: declaredVersion.version = ${declaredVersion.version}`)
214+
console.log(`patch-package/makePatch: declaredVersion.gitCommit = ${declaredVersion.gitCommit}`)
215+
console.log(`patch-package/makePatch: declaredVersion.full = ${declaredVersion.full}`)
216+
}
217+
177218
const packageVersion = (
178219
declaredVersion.version || declaredVersion.gitCommit || declaredVersion.full
179220
)
@@ -207,9 +248,7 @@ export function makePatch({
207248
*/
208249

209250
if (isDebug) {
210-
console.log(`patch-package/makePatch: resolvedVersion.version = ${resolvedVersion.version}`)
211251
console.log(`patch-package/makePatch: getPackageVersion -> ${getPackageVersion(join(resolve(packageDetails.path), "package.json"))}`)
212-
console.log(`patch-package/makePatch: packageVersion = ${packageVersion}`)
213252
console.log(`patch-package/makePatch: package path = ${packageDetails.path}`)
214253
console.log(`patch-package/makePatch: package path resolved = ${resolve(packageDetails.path)}`)
215254
}
@@ -480,8 +519,7 @@ export function makePatch({
480519

481520
const patchPath = join(patchesDir, patchFileName)
482521
if (!existsSync(dirname(patchPath))) {
483-
// scoped package
484-
mkdirSync(dirname(patchPath))
522+
mkdirSync(dirname(patchPath), { recursive: true })
485523
}
486524
writeFileSync(patchPath, diffHeader + diffResult.stdout)
487525
console.log(
@@ -497,7 +535,7 @@ export function makePatch({
497535
maybePrintIssueCreationPrompt(packageDetails, packageManager)
498536
}
499537
} catch (e: any) {
500-
console.error(e)
538+
//console.error(e)
501539
throw e
502540
} finally {
503541
cleanup()

0 commit comments

Comments
 (0)