Skip to content

Commit

Permalink
refactor: Remove dependency on strip-ansi and embed the tiny function…
Browse files Browse the repository at this point in the history
… instead (#2499 by @jamonholmgren)

[skip ci]
* Remove dependency on strip-ansi and embed the tiny function instead

* Reset yarn.lock back to where it was before

* Reset Podfile
  • Loading branch information
jamonholmgren authored Sep 28, 2023
1 parent 3e86d93 commit b7c2453
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"jest": "^27.0.6",
"prettier": "2.8.8",
"semantic-release": "^17.4.2",
"strip-ansi": "^6.0.0",
"ts-jest": "^27.0.4",
"ts-node": "^10.2.0",
"typescript": "^5.0.4"
Expand Down
2 changes: 1 addition & 1 deletion src/commands/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
prettyPrompt,
prefix,
} from "../tools/pretty"
const stripANSI = require("strip-ansi")
import { stripANSI } from "../tools/strip-ansi"

const isMac = process.platform === "darwin"

Expand Down
20 changes: 20 additions & 0 deletions src/tools/strip-ansi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// https://github.com/chalk/ansi-regex/blob/02fa893d619d3da85411acc8fd4e2eea0e95a9d9/index.js
function ansiRegex({ onlyFirst = false } = {}) {
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
].join("|")

return new RegExp(pattern, onlyFirst ? undefined : "g")
}

export function stripANSI(string) {
if (typeof string !== "string") {
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``)
}

// Even though the regex is global, we don't need to reset the `.lastIndex`
// because unlike `.exec()` and `.test()`, `.replace()` does it automatically
// and doing it manually has a performance penalty.
return string.replace(ansiRegex(), "")
}
2 changes: 1 addition & 1 deletion test/_test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { system, filesystem } from "gluegun"
const stripANSI = require("strip-ansi") // why...
import { stripANSI } from "../src/tools/strip-ansi"

const IGNITE = "node " + filesystem.path(__dirname, "..", "bin", "ignite")
const shellOpts = { stdio: "inherit" }
Expand Down

0 comments on commit b7c2453

Please sign in to comment.