@@ -10,6 +10,7 @@ import {
1010 unlinkSync ,
1111 mkdirpSync ,
1212 realpathSync ,
13+ renameSync ,
1314} from "fs-extra"
1415import { sync as rimraf } from "rimraf"
1516import { copySync } from "fs-extra"
@@ -30,6 +31,8 @@ import {
3031 openIssueCreationLink ,
3132} from "./createIssue"
3233
34+ const isVerbose = true // TODO expose to CLI
35+
3336function printNoPackageFoundError (
3437 packageName : string ,
3538 packageJsonPath : string ,
@@ -87,17 +90,19 @@ export function makePatch({
8790
8891 console . info ( chalk . grey ( "•" ) , "Creating temporary folder" )
8992
93+ const resolvedVersion = getPackageResolution ( {
94+ packageDetails,
95+ packageManager,
96+ appPath,
97+ } )
98+
9099 // make a blank package.json
91100 mkdirpSync ( tmpRepoNpmRoot )
92101 writeFileSync (
93102 tmpRepoPackageJsonPath ,
94103 JSON . stringify ( {
95104 dependencies : {
96- [ packageDetails . name ] : getPackageResolution ( {
97- packageDetails,
98- packageManager,
99- appPath,
100- } ) ,
105+ [ packageDetails . name ] : resolvedVersion . version ,
101106 } ,
102107 resolutions : resolveRelativeFileDependencies (
103108 appPath ,
@@ -106,9 +111,10 @@ export function makePatch({
106111 } ) ,
107112 )
108113
109- const packageVersion = getPackageVersion (
110- join ( resolve ( packageDetails . path ) , "package.json" ) ,
111- )
114+ // originCommit is more precise than pkg.version
115+ const packageVersion =
116+ resolvedVersion . originCommit ||
117+ getPackageVersion ( join ( resolve ( packageDetails . path ) , "package.json" ) )
112118
113119 // copy .npmrc/.yarnrc in case packages are hosted in private registry
114120 // tslint:disable-next-line:align
@@ -143,26 +149,42 @@ export function makePatch({
143149 )
144150 }
145151 } else {
152+ const npmCmd = packageManager === "pnpm" ? "pnpm" : "npm"
146153 console . info (
147154 chalk . grey ( "•" ) ,
148- `Installing ${ packageDetails . name } @${ packageVersion } with npm ` ,
155+ `Installing ${ packageDetails . name } @${ packageVersion } with ${ npmCmd } ` ,
149156 )
150157 try {
151158 // try first without ignoring scripts in case they are required
152159 // this works in 99.99% of cases
153- spawnSafeSync ( `npm` , [ "i" , "--force" ] , {
160+ if ( isVerbose ) {
161+ console . log ( `run "${ npmCmd } install --force" in ${ tmpRepoNpmRoot } ` )
162+ }
163+ spawnSafeSync ( npmCmd , [ "install" , "--force" ] , {
154164 cwd : tmpRepoNpmRoot ,
155165 logStdErrOnError : false ,
156- stdio : "ignore" ,
166+ stdio : isVerbose ? "inherit" : "ignore" ,
157167 } )
158168 } catch ( e ) {
159169 // try again while ignoring scripts in case the script depends on
160170 // an implicit context which we havn't reproduced
161- spawnSafeSync ( `npm` , [ "i" , "--ignore-scripts" , "--force" ] , {
171+ if ( isVerbose ) {
172+ console . log (
173+ `run "${ npmCmd } install --ignore-scripts --force" in ${ tmpRepoNpmRoot } ` ,
174+ )
175+ }
176+ spawnSafeSync ( npmCmd , [ "install" , "--ignore-scripts" , "--force" ] , {
162177 cwd : tmpRepoNpmRoot ,
163- stdio : "ignore" ,
178+ stdio : isVerbose ? "inherit" : "ignore" ,
164179 } )
165180 }
181+ if ( packageManager === "pnpm" ) {
182+ // workaround for `git diff`: replace symlink with hardlink
183+ const pkgPath = tmpRepoNpmRoot + "/node_modules/" + packageDetails . name
184+ const realPath = realpathSync ( pkgPath )
185+ unlinkSync ( pkgPath ) // rm symlink
186+ renameSync ( realPath , pkgPath )
187+ }
166188 }
167189
168190 const git = ( ...args : string [ ] ) =>
@@ -193,8 +215,16 @@ export function makePatch({
193215 // replace package with user's version
194216 rimraf ( tmpRepoPackagePath )
195217
218+ if ( isVerbose ) {
219+ console . log ( `copy ${ realpathSync ( packagePath ) } to ${ tmpRepoPackagePath } ` )
220+ }
221+
196222 // pnpm installs packages as symlinks, copySync would copy only the symlink
197- copySync ( realpathSync ( packagePath ) , tmpRepoPackagePath )
223+ copySync ( realpathSync ( packagePath ) , tmpRepoPackagePath , {
224+ filter : ( path ) => {
225+ return path . indexOf ( "/node_modules/" ) === - 1
226+ } ,
227+ } )
198228
199229 // remove nested node_modules just to be safe
200230 rimraf ( join ( tmpRepoPackagePath , "node_modules" ) )
@@ -207,13 +237,23 @@ export function makePatch({
207237 // stage all files
208238 git ( "add" , "-f" , packageDetails . path )
209239
240+ // TODO allow to add more paths via CLI, to exclude cache files like 'test/stubs*/**'
241+ const ignorePaths = [
242+ "package-lock.json" ,
243+ "pnpm-lock.yaml" ,
244+ // 'test/stubs*/**',
245+ ]
246+
210247 // get diff of changes
211248 const diffResult = git (
212249 "diff" ,
213250 "--cached" ,
214251 "--no-color" ,
215252 "--ignore-space-at-eol" ,
216253 "--no-ext-diff" ,
254+ ...ignorePaths . map (
255+ ( path ) => `:(exclude,top)${ packageDetails . path } /${ path } ` ,
256+ ) ,
217257 )
218258
219259 if ( diffResult . stdout . length === 0 ) {
0 commit comments