Skip to content

Commit

Permalink
bun.list: Strip ansi codes from stdout.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Sep 12, 2023
1 parent 59d2846 commit b8c6569
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"semver-utils": "^1.1.4",
"source-map-support": "^0.5.21",
"spawn-please": "^2.0.1",
"strip-ansi": "^7.1.0",
"strip-json-comments": "^5.0.1",
"untildify": "^4.0.0",
"update-notifier": "^6.0.2"
Expand Down Expand Up @@ -138,7 +139,6 @@
"prettier": "^2.8.8",
"should": "^13.2.3",
"sinon": "^15.2.0",
"strip-ansi": "^7.1.0",
"ts-node": "^10.9.1",
"typescript": "5.1.6",
"typescript-json-schema": "0.59.0",
Expand Down
15 changes: 13 additions & 2 deletions src/package-managers/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,31 @@ async function spawnBun(
* (Bun) Fetches the list of all installed packages.
*/
export const list = async (options: Options = {}): Promise<Index<string | undefined>> => {
const { default: stripAnsi } = await import('strip-ansi')

// bun pm ls
const stdout = await spawnBun(
['pm', 'ls'],
{
...(options.global ? { location: 'global' } : null),
...(options.prefix ? { prefix: options.prefix } : null),
},
{
env: {
...process.env,
// Disable color to ensure the output is parsed correctly.
// However, this may be ineffective in some environments (see stripAnsi below).
// https://bun.sh/docs/runtime/configuration#environment-variables
NO_COLOR: '1',
},
...(options.cwd ? { cwd: options.cwd } : null),
rejectOnError: false,
},
)

// parse the output of `bun pm ls` into an object { [name]: version }.
const lines = stdout.split('\n')
// Parse the output of `bun pm ls` into an object { [name]: version }.
// When bun is spawned in the GitHub Actions environment, it outputs ANSI color. Unfortunately, it does not respect the `NO_COLOR` envirionment variable. Therefore, we have to manually strip ansi.
const lines = stripAnsi(stdout).split('\n')
const dependencies = keyValueBy(lines, line => {
const match = line.match(/.* (.*?)@(.+)/)
if (match) {
Expand Down
1 change: 0 additions & 1 deletion test/package-managers/bun/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const describeSkipWindows = os.platform() === 'win32' ? describe.skip : describe
describeSkipWindows('bun', function () {
it('list', async () => {
const result = await bun.list({ cwd: __dirname })
console.log({ result })
result.should.have.property('ncu-test-v2')
})

Expand Down

0 comments on commit b8c6569

Please sign in to comment.