Skip to content
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

chalk.js doesn't seem to work on Windows when running inside a hook #350

Closed
fancyboynet opened this issue Sep 18, 2018 · 11 comments
Closed

Comments

@fancyboynet
Copy link

The script source( ./script/chalk.js)

const chalk = require('chalk')
const shell = require('shelljs')
console.log(chalk.blue('11111111'))
shell.exit(1)

The config

"husky": {
    "hooks": {
      "pre-commit": "node ./script/chalk.js"
    }
  }

If I run the script directly, the color will show.Anyone knows why? Thx.

@ezpuzz
Copy link

ezpuzz commented Oct 4, 2018

same issue here with output of yarn outdated not being in color but having color when executed directly.

@NickyMeuleman
Copy link

NickyMeuleman commented Oct 5, 2018

See #32 for related context. I agree that output from other sources than husky should retain their colors.

@typicode
Copy link
Owner

typicode commented Nov 8, 2018

Can't seem to reproduce on macOS with the Terminal.

Here's how I've tested:

mkdir husky-chalk && cd husky-chalk
git init
npm init -y
npm install chalk husky
// package.json
"husky": {
  "hooks": {
    "pre-commit": "node index.js"
  }
}
// index.js
const chalk = require('chalk')
console.log(chalk.green('hello world'))
process.exit(1) // make the commit fail so it can be tested multiple times
husky-chalk$ git commit -m test

screen

What's your OS? Which terminal are you using?

@fancyboynet
Copy link
Author

@typicode Thanks for reply. I am using windows 10 with the system terminal

@typicode typicode changed the title chalk.js color seems can not show chalk.js doesn't seem to work on Windows when running inside a hook Nov 12, 2018
@typicode
Copy link
Owner

Can try adding FORCE_COLOR=1 to ~/.huskyrc (requires Husky v1.2.0)

# ~/.huskyrc
export FORCE_COLOR=1

@cinderblock
Copy link

Chalk auto detects if color support is available in the current terminal.

You can force it on. @typicode showed one way. They are all enumerated here: https://github.com/chalk/chalk#chalklevel

Husky should still probably be improved to properly preserve the terminal type so that chalk can detect it correctly. :)

@fancyboynet
Copy link
Author

@typicode Can I config that in the package.json file?

@cinderblock
Copy link

cinderblock commented Nov 26, 2018

@fancyboynet try this:

"husky": {
  "hooks": {
    "pre-commit": "env FORCE_COLOR=1 node ./script/chalk.js"
  }
}

@fancyboynet
Copy link
Author

@typicode @cinderblock It works! Thanks for your work.

@typicode
Copy link
Owner

Husky should still probably be improved to properly preserve the terminal type so that chalk can detect it correctly. :)

@cinderblock Not sure how it could be done :/ Seems to be specifi to Git on Windows.

For example, if I create a pre-commit script without Husky:

#!/usr/bin/env node
// .git/hooks/pre-commit
const chalk = require('chalk')
console.log(chalk.green('hello world'))
process.exit(1)
git commit -m test

It will show colors on macOS and Linux but not on Windows.

Can I config that in the package.json file?

@fancyboynet as shown, yes it's possible.

One downside though is that all your repo users will have colors forced. So if people clone your repo and use a Git client that doesn't support colors, they may get something like \e[32mhello world\e[0m instead of just hello world.

But if you're the main developer, or are working in a team and know the tools that are used, forcing it may be fine.

@soulehshaikh99
Copy link

This worked for me, Set an environment variable like this in Windows:

Chalk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants