diff --git a/package.json b/package.json index 4354df3a..ae261d97 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "chalk": "^1.1.3", "cross-spawn": "^5.1.0", "fs-extra": "^4.0.1", + "klaw-sync": "^4.0.0", "minimist": "^1.2.0", "rimraf": "^2.6.2", "slash": "^1.0.0", diff --git a/src/makePatch.ts b/src/makePatch.ts index 7763c3f1..cce0deee 100644 --- a/src/makePatch.ts +++ b/src/makePatch.ts @@ -12,6 +12,7 @@ import { getPatchFiles } from "./patchFs" import * as fsExtra from "fs-extra" import { PackageManager } from "./detectPackageManager" import * as slash from "slash" +import * as klawSync from "klaw-sync" function deleteScripts(json: any) { delete json.scripts @@ -120,42 +121,46 @@ export default function makePatch( "!/node_modules\n\n", ) tmpExec("git", ["init"]) - // don't commit package.json though - tmpExec("git", ["add", "-f", slash(path.join("node_modules", packageName))]) - tmpExec("git", ["commit", "-m", "init"]) + klawSync(tmpRepoPackagePath, { nodir: true }) + .map(item => item.path.slice(`${tmpRepoPackagePath}/`.length)) + .filter( + relativePath => + !relativePath.match(includePaths) || relativePath.match(excludePaths), + ) + .forEach(relativePath => + fsExtra.removeSync(slash(path.join(tmpRepoPackagePath, relativePath))), + ) + + tmpExec("git", ["add", "-f", slash(path.join("node_modules", packageName))]) + tmpExec("git", ["commit", "--allow-empty", "-m", "init"]) // replace package with user's version rimraf.sync(tmpRepoPackagePath) - fsExtra.copySync(packagePath, tmpRepoPackagePath, { recursive: true }) + + klawSync(packagePath, { nodir: true }) + .map(item => item.path.slice(`${packagePath}/`.length)) + .filter( + relativePath => + relativePath.match(includePaths) && !relativePath.match(excludePaths), + ) + .forEach(relativePath => + fsExtra.copySync( + slash(path.join(packagePath, relativePath)), + slash(path.join(tmpRepoPackagePath, relativePath)), + ), + ) // 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 => { - const scopedFileName = fileName.slice( - `node_modules/${packageName}/`.length, - ) - if ( - !scopedFileName.match(includePaths) || - scopedFileName.match(excludePaths) - ) { - tmpExec("git", ["reset", "HEAD", fileName]) - } - }) - // get diff of changes const patch = tmpExec("git", [ "diff", "--cached", "--no-color", "--ignore-space-at-eol", - "--no-ext-diff" + "--no-ext-diff", ]).stdout.toString() if (patch.trim() === "") { diff --git a/typings/klaw-sync.d.ts b/typings/klaw-sync.d.ts new file mode 100644 index 00000000..281904be --- /dev/null +++ b/typings/klaw-sync.d.ts @@ -0,0 +1,17 @@ +interface Options { + nodir?: boolean + nofile?: boolean + depthLimit?: number + fs?: object + filter?: (item: Item) => boolean +} + +interface Item { + path: string + stats: object +} + +declare module "klaw-sync" { + const klawSync: (dir: string, opts?: Options, ls?: Item[]) => Item[] + export = klawSync +} diff --git a/yarn.lock b/yarn.lock index a197e4c4..7babcc24 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1949,6 +1949,12 @@ kind-of@^3.0.2: dependencies: is-buffer "^1.1.5" +klaw-sync@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/klaw-sync/-/klaw-sync-4.0.0.tgz#7785692ea1a320ac3dda7a6c0c22b33a30aa3b3f" + dependencies: + graceful-fs "^4.1.11" + latest-version@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/latest-version/-/latest-version-3.1.0.tgz#a205383fea322b33b5ae3b18abee0dc2f356ee15"