Skip to content

Reduce installation size #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 1, 2025
8 changes: 8 additions & 0 deletions .changeset/slow-llamas-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@unts/patch-package": minor
---

feat: reduce installation size by replacing several dependencies:

- `chalk` -> `picocolors`
- `json-stable-stringify` -> `@nolyfill/json-stable-stringify`
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@
"@types/cross-spawn": "^6.0.0",
"@types/fs-extra": "^9.0.0",
"@types/jest": "^24.0.11",
"@types/json-stable-stringify": "^1.0.34",
"@types/klaw-sync": "^6.0.5",
"@types/minimist": "^1.2.2",
"@types/node": "^12.0.0",
"@types/randomstring": "^1.3.0",
"@types/rimraf": "^2.0.2",
"@types/semver": "^7.5.0",
"@types/tmp": "^0.0.34",
Expand All @@ -78,18 +79,18 @@
"typescript": "^4.2.2"
},
"dependencies": {
"@nolyfill/json-stable-stringify": "^1.0.30",
"@yarnpkg/lockfile": "^1.1.0",
"chalk": "^4.1.2",
"ci-info": "^3.7.0",
"cross-spawn": "^7.0.3",
"find-yarn-workspace-root": "^2.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",
"picocolors": "^1.1.1",
"semver": "^7.5.3",
"slash": "^2.0.0",
"slash": "^3.0.0",
"tmp": "^0.0.33",
"yaml": "^2.2.2"
},
Expand Down
60 changes: 26 additions & 34 deletions src/applyPatches.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import chalk from "chalk"
import { writeFileSync } from "fs"
import { existsSync } from "fs-extra"
import { existsSync, writeFileSync } from "fs"
import { posix } from "path"
import { blue, bold, cyan, green, red, yellow } from "picocolors"
import semver from "semver"
import { hashFile } from "./hash"
import { logPatchSequenceError } from "./makePatch"
Expand Down Expand Up @@ -45,7 +44,7 @@ function getInstalledPackageVersion({
}

let err =
`${chalk.red("Error:")} Patch file found for package ${posix.basename(
`${red("Error:")} Patch file found for package ${posix.basename(
pathSpecifier,
)}` + ` which is not present at ${relative(".", packageDir)}`

Expand All @@ -54,7 +53,7 @@ function getInstalledPackageVersion({

If this package is a dev dependency, rename the patch file to

${chalk.bold(patchFilename.replace(".patch", ".dev.patch"))}
${bold(patchFilename.replace(".patch", ".dev.patch"))}
`
}
throw new PatchApplicationError(err)
Expand All @@ -65,7 +64,7 @@ function getInstalledPackageVersion({
const result = semver.valid(version)
if (result === null) {
throw new PatchApplicationError(
`${chalk.red(
`${red(
"Error:",
)} Version string '${version}' cannot be parsed from ${join(
packageDir,
Expand All @@ -85,9 +84,9 @@ function logPatchApplication(patchDetails: PatchedPackageDetails) {
})`
: ""
console.log(
`${chalk.bold(patchDetails.pathSpecifier)}@${
`${bold(patchDetails.pathSpecifier)}@${
patchDetails.version
}${sequenceString} ${chalk.green("✔")}`,
}${sequenceString} ${green("✔")}`,
)
}

Expand All @@ -110,7 +109,7 @@ export function applyPatchesForApp({
const groupedPatches = getGroupedPatches(patchesDirectory)

if (groupedPatches.numPatchFiles === 0) {
console.log(chalk.blueBright("No patch files found"))
console.log(blue("No patch files found"))
return
}

Expand Down Expand Up @@ -140,10 +139,10 @@ export function applyPatchesForApp({

const problemsSummary = []
if (warnings.length) {
problemsSummary.push(chalk.yellow(`${warnings.length} warning(s)`))
problemsSummary.push(yellow(`${warnings.length} warning(s)`))
}
if (errors.length) {
problemsSummary.push(chalk.red(`${errors.length} error(s)`))
problemsSummary.push(red(`${errors.length} error(s)`))
}

if (problemsSummary.length) {
Expand Down Expand Up @@ -201,8 +200,8 @@ export function applyPatchesForPackage({
appliedPatches.push(unappliedPatches.shift()!)
} else {
console.log(
chalk.red("Error:"),
`The patches for ${chalk.bold(pathSpecifier)} have changed.`,
red("Error:"),
`The patches for ${bold(pathSpecifier)} have changed.`,
`You should reinstall your node_modules folder to make sure the package is up to date`,
)
process.exit(1)
Expand Down Expand Up @@ -249,9 +248,7 @@ export function applyPatchesForPackage({
if (!installedPackageVersion) {
// it's ok we're in production mode and this is a dev only package
console.log(
`Skipping dev-only ${chalk.bold(
pathSpecifier,
)}@${version} ${chalk.blue("✔")}`,
`Skipping dev-only ${bold(pathSpecifier)}@${version} ${blue("✔")}`,
)
continue
}
Expand Down Expand Up @@ -427,10 +424,7 @@ export function applyPatch({
const errors: string[] | undefined = bestEffort ? [] : undefined
executeEffects(forward, { dryRun: false, cwd, bestEffort, errors })
if (errors?.length) {
console.log(
"Saving errors to",
chalk.cyan.bold("./patch-package-errors.log"),
)
console.log("Saving errors to", cyan(bold("./patch-package-errors.log")))
writeFileSync("patch-package-errors.log", errors.join("\n\n"))
process.exit(0)
}
Expand Down Expand Up @@ -464,18 +458,18 @@ function createVersionMismatchWarning({
path: string
}) {
return `
${chalk.yellow("Warning:")} patch-package detected a patch file version mismatch
${yellow("Warning:")} patch-package detected a patch file version mismatch

Don't worry! This is probably fine. The patch was still applied
successfully. Here's the deets:

Patch file created for

${packageName}@${chalk.bold(originalVersion)}
${packageName}@${bold(originalVersion)}

applied to

${packageName}@${chalk.bold(actualVersion)}
${packageName}@${bold(actualVersion)}

At path

Expand All @@ -485,7 +479,7 @@ ${chalk.yellow("Warning:")} patch-package detected a patch file version mismatch
breakage even though the patch was applied successfully. Make sure the package
still behaves like you expect (you wrote tests, right?) and then run

${chalk.bold(`patch-package ${pathSpecifier}`)}
${bold(`patch-package ${pathSpecifier}`)}

to update the version in the patch file name and make this warning go away.
`
Expand All @@ -503,8 +497,8 @@ function createBrokenPatchFileError({
pathSpecifier: string
}) {
return `
${chalk.red.bold("**ERROR**")} ${chalk.red(
`Failed to apply patch for package ${chalk.bold(packageName)} at path`,
${red(bold("**ERROR**"))} ${red(
`Failed to apply patch for package ${bold(packageName)} at path`,
)}

${path}
Expand Down Expand Up @@ -543,13 +537,13 @@ function createPatchApplicationFailureError({
pathSpecifier: string
}) {
return `
${chalk.red.bold("**ERROR**")} ${chalk.red(
`Failed to apply patch for package ${chalk.bold(packageName)} at path`,
${red(bold("**ERROR**"))} ${red(
`Failed to apply patch for package ${bold(packageName)} at path`,
)}

${path}

This error was caused because ${chalk.bold(packageName)} has changed since you
This error was caused because ${bold(packageName)} has changed since you
made the patch file for it. This introduced conflicts with your patch,
just like a merge conflict in Git when separate incompatible changes are
made to the same piece of code.
Expand All @@ -568,8 +562,8 @@ ${chalk.red.bold("**ERROR**")} ${chalk.red(

Info:
Patch file: patches/${patchFilename}
Patch was made for version: ${chalk.green.bold(originalVersion)}
Installed version: ${chalk.red.bold(actualVersion)}
Patch was made for version: ${green(bold(originalVersion))}
Installed version: ${red(bold(actualVersion))}
`
}

Expand All @@ -581,9 +575,7 @@ function createUnexpectedError({
error: Error
}) {
return `
${chalk.red.bold("**ERROR**")} ${chalk.red(
`Failed to apply patch file ${chalk.bold(filename)}`,
)}
${red(bold("**ERROR**"))} ${red(`Failed to apply patch file ${bold(filename)}`)}

${error.stack}

Expand Down
4 changes: 2 additions & 2 deletions src/createIssue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from "chalk"
import open from "open"
import { bold } from "picocolors"
import { stringify } from "querystring"
import { PackageManager } from "./detectPackageManager"
import { PackageDetails } from "./PackageDetails"
Expand Down Expand Up @@ -105,7 +105,7 @@ export function maybePrintIssueCreationPrompt(
packageManager: PackageManager,
) {
if (vcs) {
console.log(`💡 ${chalk.bold(packageDetails.name)} is on ${
console.log(`💡 ${bold(packageDetails.name)} is on ${
vcs.provider
}! To draft an issue based on your patch run

Expand Down
18 changes: 8 additions & 10 deletions src/detectPackageManager.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import fs from "fs-extra"
import { join } from "./path"
import chalk from "chalk"
import process from "process"
import findWorkspaceRoot from "find-yarn-workspace-root"
import fs from "fs"
import { bold, red } from "picocolors"
import process from "process"
import { join } from "./path"

export type PackageManager = "yarn" | "npm" | "npm-shrinkwrap"

function printNoYarnLockfileError() {
console.log(`
${chalk.red.bold("**ERROR**")} ${chalk.red(
${red(bold("**ERROR**"))} ${red(
`The --use-yarn option was specified but there is no yarn.lock file`,
)}
`)
}

function printNoLockfilesError() {
console.log(`
${chalk.red.bold("**ERROR**")} ${chalk.red(
${red(bold("**ERROR**"))} ${red(
`No package-lock.json, npm-shrinkwrap.json, or yarn.lock file.

You must use either npm@>=5, yarn, or npm-shrinkwrap to manage this project's
Expand All @@ -27,10 +27,8 @@ dependencies.`,

function printSelectingDefaultMessage() {
console.info(
`${chalk.bold(
"patch-package",
)}: you have both yarn.lock and package-lock.json
Defaulting to using ${chalk.bold("npm")}
`${bold("patch-package")}: you have both yarn.lock and package-lock.json
Defaulting to using ${bold("npm")}
You can override this setting by passing --use-yarn or deleting
package-lock.json if you don't need it
`,
Expand Down
2 changes: 1 addition & 1 deletion src/getAppRootPath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join, resolve } from "./path"
import process from "process"
import { existsSync } from "fs-extra"
import { existsSync } from "fs"

export const getAppRootPath = (): string => {
let cwd = process.cwd()
Expand Down
2 changes: 1 addition & 1 deletion src/getPackageResolution.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join, resolve } from "./path"
import { PackageDetails, getPatchDetailsFromCliString } from "./PackageDetails"
import { PackageManager, detectPackageManager } from "./detectPackageManager"
import { readFileSync, existsSync } from "fs-extra"
import { readFileSync, existsSync } from "fs"
import { parse as parseYarnLockFile } from "@yarnpkg/lockfile"
import yaml from "yaml"
import findWorkspaceRoot from "find-yarn-workspace-root"
Expand Down
Loading
Loading