diff --git a/package.json b/package.json index 6d2aea18..b74f8260 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "np": "^7.4.0", "prettier": "^2.2.1", "randomstring": "^1.1.5", + "rimraf": "^2.6.3", "ts-jest": "^24.0.0", "ts-node": "8.0.3", "tslint": "^5.14.0", @@ -75,12 +76,11 @@ "ci-info": "^3.7.0", "cross-spawn": "^7.0.3", "find-yarn-workspace-root": "^2.0.0", - "fs-extra": "^9.0.0", + "fs-extra": "^10.0.0", "json-stable-stringify": "^1.0.2", "klaw-sync": "^6.0.0", "minimist": "^1.2.6", "open": "^7.4.2", - "rimraf": "^2.6.3", "semver": "^7.5.3", "slash": "^2.0.0", "tmp": "^0.0.33", diff --git a/src/createIssue.ts b/src/createIssue.ts index de9e721d..1522e563 100644 --- a/src/createIssue.ts +++ b/src/createIssue.ts @@ -8,9 +8,16 @@ import { join, resolve } from "./path" const repoSpecifier = /^([\w.-]+)\/([\w.-]+)$/ const githubURL = /github.com(:|\/)([\w.-]+\/[\w.-]+?)(.git|\/.*)?$/ -function parseRepoString( - repository: string, -): null | { repo: string; org: string; provider: "GitHub" } { +type VCS = + | { + repo: string + org: string + provider: "GitHub" + } + | null + | undefined + +function parseRepoString(repository: string): VCS { if (repository.startsWith("github:")) { repository = repository.replace(/^github:/, "") } @@ -29,7 +36,7 @@ function parseRepoString( return { org, repo, provider: "GitHub" } } -export function getPackageVCSDetails(packageDetails: PackageDetails) { +export function getPackageVCSDetails(packageDetails: PackageDetails): VCS { const repository = require(resolve(join(packageDetails.path, "package.json"))) .repository as undefined | string | { url: string } @@ -46,6 +53,38 @@ export function getPackageVCSDetails(packageDetails: PackageDetails) { } } +function createIssueUrl({ + vcs, + packageDetails, + packageVersion, + diff, +}: { + vcs: VCS + packageDetails: PackageDetails + packageVersion: string + diff: string +}): string { + return `https://github.com/${vcs?.org}/${vcs?.repo}/issues/new?${stringify({ + title: "", + body: `Hi! 👋 + +Firstly, thanks for your work on this project! 🙂 + +Today I used [patch-package](https://github.com/ds300/patch-package) to patch \`${packageDetails.name}@${packageVersion}\` for the project I'm working on. + + + +Here is the diff that solved my problem: + +\`\`\`diff +${diff} +\`\`\` + +This issue body was [partially generated by patch-package](https://github.com/ds300/patch-package/issues/296). +`, + })}` +} + export function shouldRecommendIssue( vcsDetails: ReturnType, ) { @@ -81,10 +120,12 @@ export function openIssueCreationLink({ packageDetails, patchFileContents, packageVersion, + patchPath, }: { packageDetails: PackageDetails patchFileContents: string packageVersion: string + patchPath: string }) { const vcs = getPackageVCSDetails(packageDetails) @@ -100,25 +141,28 @@ export function openIssueCreationLink({ patchFileContents = patchFileContents.slice(0, -1) } - open( - `https://github.com/${vcs.org}/${vcs.repo}/issues/new?${stringify({ - title: "", - body: `Hi! 👋 - -Firstly, thanks for your work on this project! 🙂 - -Today I used [patch-package](https://github.com/ds300/patch-package) to patch \`${packageDetails.name}@${packageVersion}\` for the project I'm working on. - - - -Here is the diff that solved my problem: + let issueUrl = createIssueUrl({ + vcs, + packageDetails, + packageVersion, + diff: patchFileContents, + }) -\`\`\`diff -${patchFileContents} -\`\`\` + const urlExceedsLimit = patchFileContents.length > 1950 -This issue body was [partially generated by patch-package](https://github.com/ds300/patch-package/issues/296). -`, - })}`, - ) + if (urlExceedsLimit) { + const diffMessage = `` + console.log( + `📋 Copy the contents in [ ${patchPath} ] and paste it in the new issue's diff section.`, + ) + issueUrl = createIssueUrl({ + vcs, + packageDetails, + packageVersion, + diff: diffMessage, + }) + } + open(issueUrl) } diff --git a/src/makePatch.ts b/src/makePatch.ts index 4d040297..7e008eb3 100644 --- a/src/makePatch.ts +++ b/src/makePatch.ts @@ -7,9 +7,9 @@ import { mkdirpSync, mkdirSync, realpathSync, + removeSync, writeFileSync, } from "fs-extra" -import { sync as rimraf } from "rimraf" import { dirSync } from "tmp" import { gzipSync } from "zlib" import { applyPatch } from "./applyPatches" @@ -254,11 +254,11 @@ export function makePatch({ }) // remove nested node_modules just to be safe - rimraf(join(tmpRepoPackagePath, "node_modules")) + removeSync(join(tmpRepoPackagePath, "node_modules")) // remove .git just to be safe - rimraf(join(tmpRepoPackagePath, ".git")) + removeSync(join(tmpRepoPackagePath, ".git")) // remove patch-package state file - rimraf(join(tmpRepoPackagePath, STATE_FILE_NAME)) + removeSync(join(tmpRepoPackagePath, STATE_FILE_NAME)) // commit the package console.info(chalk.grey("•"), "Diffing your files with clean files") @@ -292,17 +292,17 @@ export function makePatch({ git("commit", "--allow-empty", "-m", "init") // replace package with user's version - rimraf(tmpRepoPackagePath) + removeSync(tmpRepoPackagePath) // pnpm installs packages as symlinks, copySync would copy only the symlink copySync(realpathSync(packagePath), tmpRepoPackagePath) // remove nested node_modules just to be safe - rimraf(join(tmpRepoPackagePath, "node_modules")) + removeSync(join(tmpRepoPackagePath, "node_modules")) // remove .git just to be safe - rimraf(join(tmpRepoPackagePath, ".git")) + removeSync(join(tmpRepoPackagePath, ".git")) // remove patch-package state file - rimraf(join(tmpRepoPackagePath, STATE_FILE_NAME)) + removeSync(join(tmpRepoPackagePath, STATE_FILE_NAME)) // also remove ignored files like before removeIgnoredFiles(tmpRepoPackagePath, includePaths, excludePaths) @@ -423,7 +423,7 @@ export function makePatch({ sequenceNumber, }) - const patchPath = join(patchesDir, patchFileName) + const patchPath: string = join(patchesDir, patchFileName) if (!existsSync(dirname(patchPath))) { // scoped package mkdirSync(dirname(patchPath)) @@ -538,6 +538,7 @@ export function makePatch({ packageDetails, patchFileContents: diffResult.stdout.toString(), packageVersion, + patchPath, }) } else { maybePrintIssueCreationPrompt(vcs, packageDetails, packageManager) diff --git a/yarn.lock b/yarn.lock index 6e3eb9f5..f38c3f09 100644 --- a/yarn.lock +++ b/yarn.lock @@ -765,11 +765,6 @@ asynckit@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" -at-least-node@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2" - integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg== - atob@^2.1.1: version "2.1.2" resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9" @@ -1912,12 +1907,11 @@ fragment-cache@^0.2.1: dependencies: map-cache "^0.2.2" -fs-extra@^9.0.0: - version "9.1.0" - resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d" - integrity sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ== +fs-extra@^10.0.0: + version "10.1.0" + resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.1.0.tgz#02873cfbc4084dde127eaa5f9905eef2325d1abf" + integrity sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ== dependencies: - at-least-node "^1.0.0" graceful-fs "^4.2.0" jsonfile "^6.0.1" universalify "^2.0.0"