11import { green } from "chalk"
22import * as fs from "fs"
3- import * as path from "./path"
3+ import { join , dirname , relative } from "./path"
44import * as rimraf from "rimraf"
55import * as tmp from "tmp"
66import {
@@ -18,16 +18,27 @@ function deleteScripts(json: any) {
1818 return json
1919}
2020
21- export default function makePatch (
21+ function printNoPackageFoundError (
22+ packageName : string ,
23+ packageJsonPath : string ,
24+ ) {
25+ console . error (
26+ `No such package ${ packageName }
27+
28+ File not found: ${ packageJsonPath } ` ,
29+ )
30+ }
31+
32+ export const makePatch = (
2233 packageName : string ,
2334 appPath : string ,
2435 packageManager : PackageManager ,
2536 includePaths : RegExp ,
2637 excludePaths : RegExp ,
27- ) {
28- const nodeModulesPath = path . join ( appPath , "node_modules" )
29- const packagePath = path . join ( nodeModulesPath , packageName )
30- const packageJsonPath = path . join ( packagePath , "package.json" )
38+ ) => {
39+ const nodeModulesPath = join ( appPath , "node_modules" )
40+ const packagePath = join ( nodeModulesPath , packageName )
41+ const packageJsonPath = join ( packagePath , "package.json" )
3142 if ( ! fs . existsSync ( packageJsonPath ) ) {
3243 printNoPackageFoundError ( packageName , packageJsonPath )
3344 process . exit ( 1 )
@@ -36,12 +47,12 @@ export default function makePatch(
3647 const packageVersion = require ( packageJsonPath ) . version
3748
3849 const tmpRepo = tmp . dirSync ( { unsafeCleanup : true } )
39- const tmpRepoNodeModulesPath = path . join ( tmpRepo . name , "node_modules" )
40- const tmpRepoPackageJsonPath = path . join ( tmpRepo . name , "package.json" )
41- const tmpRepoPackagePath = path . join ( tmpRepoNodeModulesPath , packageName )
50+ const tmpRepoNodeModulesPath = join ( tmpRepo . name , "node_modules" )
51+ const tmpRepoPackageJsonPath = join ( tmpRepo . name , "package.json" )
52+ const tmpRepoPackagePath = join ( tmpRepoNodeModulesPath , packageName )
4253
4354 try {
44- const patchesDir = path . join ( appPath , "patches" )
55+ const patchesDir = join ( appPath , "patches" )
4556
4657 if ( ! fs . existsSync ( patchesDir ) ) {
4758 fs . mkdirSync ( patchesDir )
@@ -55,9 +66,9 @@ export default function makePatch(
5566 console . info (
5667 green ( "☑" ) ,
5768 "Removing existing" ,
58- path . relative ( process . cwd ( ) , path . join ( patchesDir , fileName ) ) ,
69+ relative ( process . cwd ( ) , join ( patchesDir , fileName ) ) ,
5970 )
60- fs . unlinkSync ( path . join ( patchesDir , fileName ) )
71+ fs . unlinkSync ( join ( patchesDir , fileName ) )
6172 }
6273 } )
6374 }
@@ -68,8 +79,8 @@ export default function makePatch(
6879 spawnSafeSync ( command , args , { cwd : tmpRepo . name } )
6980 // reinstall a clean version of the user's node_modules in our tmp location
7081 fsExtra . copySync (
71- path . join ( appPath , "package.json" ) ,
72- path . join ( tmpRepo . name , "package.json" ) ,
82+ join ( appPath , "package.json" ) ,
83+ join ( tmpRepo . name , "package.json" ) ,
7384 )
7485 // resolve relative file paths in package.json
7586 // also delete scripts
@@ -79,16 +90,16 @@ export default function makePatch(
7990 deleteScripts (
8091 resolveRelativeFileDependenciesInPackageJson (
8192 appPath ,
82- require ( path . join ( tmpRepo . name , "package.json" ) ) ,
93+ require ( join ( tmpRepo . name , "package.json" ) ) ,
8394 ) ,
8495 ) ,
8596 ) ,
8697 )
8798
8899 if ( packageManager === "yarn" ) {
89100 fsExtra . copySync (
90- path . join ( appPath , "yarn.lock" ) ,
91- path . join ( tmpRepo . name , "yarn.lock" ) ,
101+ join ( appPath , "yarn.lock" ) ,
102+ join ( tmpRepo . name , "yarn.lock" ) ,
92103 )
93104 console . info ( green ( "☑" ) , "Building clean node_modules with yarn" )
94105 tmpExec ( `yarn` )
@@ -99,14 +110,14 @@ export default function makePatch(
99110 : "package-lock.json"
100111
101112 const lockFileContents = JSON . parse (
102- fsExtra . readFileSync ( path . join ( appPath , lockFileName ) ) . toString ( ) ,
113+ fsExtra . readFileSync ( join ( appPath , lockFileName ) ) . toString ( ) ,
103114 )
104115 const resolvedLockFileContents = resolveRelativeFileDependenciesInPackageLock (
105116 appPath ,
106117 lockFileContents ,
107118 )
108119 fs . writeFileSync (
109- path . join ( tmpRepo . name , lockFileName ) ,
120+ join ( tmpRepo . name , lockFileName ) ,
110121 JSON . stringify ( resolvedLockFileContents ) ,
111122 )
112123 console . info ( green ( "☑" ) , "Building clean node_modules with npm" )
@@ -115,13 +126,10 @@ export default function makePatch(
115126
116127 // commit the package
117128 console . info ( green ( "☑" ) , "Diffing your files with clean files" )
118- fs . writeFileSync (
119- path . join ( tmpRepo . name , ".gitignore" ) ,
120- "!/node_modules\n\n" ,
121- )
129+ fs . writeFileSync ( join ( tmpRepo . name , ".gitignore" ) , "!/node_modules\n\n" )
122130 tmpExec ( "git" , [ "init" ] )
123131 // don't commit package.json though
124- tmpExec ( "git" , [ "add" , "-f" , slash ( path . join ( "node_modules" , packageName ) ) ] )
132+ tmpExec ( "git" , [ "add" , "-f" , slash ( join ( "node_modules" , packageName ) ) ] )
125133
126134 tmpExec ( "git" , [ "commit" , "-m" , "init" ] )
127135
@@ -130,7 +138,7 @@ export default function makePatch(
130138 fsExtra . copySync ( packagePath , tmpRepoPackagePath , { recursive : true } )
131139
132140 // stage all files
133- tmpExec ( "git" , [ "add" , "-f" , slash ( path . join ( "node_modules" , packageName ) ) ] )
141+ tmpExec ( "git" , [ "add" , "-f" , slash ( join ( "node_modules" , packageName ) ) ] )
134142
135143 // unstage any ignored files so they don't show up in the diff
136144 tmpExec ( "git" , [ "diff" , "--cached" , "--name-only" ] )
@@ -155,7 +163,7 @@ export default function makePatch(
155163 "--cached" ,
156164 "--no-color" ,
157165 "--ignore-space-at-eol" ,
158- "--no-ext-diff"
166+ "--no-ext-diff" ,
159167 ] ) . stdout . toString ( )
160168
161169 if ( patch . trim ( ) === "" ) {
@@ -164,10 +172,10 @@ export default function makePatch(
164172 process . exit ( 1 )
165173 } else {
166174 const patchFileName = `${ packageName } +${ packageVersion } .patch`
167- const patchPath = path . join ( patchesDir , patchFileName )
168- if ( ! fs . existsSync ( path . dirname ( patchPath ) ) ) {
175+ const patchPath = join ( patchesDir , patchFileName )
176+ if ( ! fs . existsSync ( dirname ( patchPath ) ) ) {
169177 // scoped package
170- fs . mkdirSync ( path . dirname ( patchPath ) )
178+ fs . mkdirSync ( dirname ( patchPath ) )
171179 }
172180 fs . writeFileSync ( patchPath , patch )
173181 console . log ( `${ green ( "✔" ) } Created file patches/${ patchFileName } ` )
@@ -179,14 +187,3 @@ export default function makePatch(
179187 tmpRepo . removeCallback ( )
180188 }
181189}
182-
183- function printNoPackageFoundError (
184- packageName : string ,
185- packageJsonPath : string ,
186- ) {
187- console . error (
188- `No such package ${ packageName }
189-
190- File not found: ${ packageJsonPath } ` ,
191- )
192- }
0 commit comments