Skip to content

Commit

Permalink
disable showing based on npm loglevel (#14)
Browse files Browse the repository at this point in the history
- Do not show if npm's loglevel is set to warn, error or silent
- Update Readme with instructions
- Add Jest testing
  • Loading branch information
eddiemonge authored and xdamman committed Oct 20, 2018
1 parent f65bb3f commit 3d3d074
Show file tree
Hide file tree
Showing 5 changed files with 5,568 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -25,5 +25,7 @@ And in your `package.json` add:
## Disabling this message
In some places (e.g. CI) you may want to disable this output. You can do this by setting the environment variable `DISABLE_OPENCOLLECTIVE=true`.

It will not be shown if npm's log level is set to silent (`--silent`), warn (`--quiet`), or error (`--loglevel error`).

Note: This is a lightweight alternative to the [opencollective-cli](https://github.com/opencollective/opencollective-cli) that offers a more complete postinstall message with the current balance and ASCII logo of the collective.

20 changes: 11 additions & 9 deletions index.js
@@ -1,11 +1,13 @@
#!/usr/bin/env node
if (process.env.DISABLE_OPENCOLLECTIVE && process.env.DISABLE_OPENCOLLECTIVE !== '0' && process.env.DISABLE_OPENCOLLECTIVE.toLowerCase() !== 'false') {
return;
}
var pkg = require(require('path').resolve('./package.json'));
if (pkg.collective) {
console.log(`\u001b[96m\u001b[1mThank you for using ${pkg.name}!\u001b[96m\u001b[1m`);
console.log(`\u001b[0m\u001b[96mIf you rely on this package, please consider supporting our open collective:\u001b[22m\u001b[39m`);
console.log(`> \u001b[94m${pkg.collective.url}/donate\u001b[0m\n`);
}
var envDisable = Boolean(process.env.DISABLE_OPENCOLLECTIVE);
var logLevel = process.env.npm_config_loglevel;
var logLevelDisplay = ['silent', 'error', 'warn'].indexOf(logLevel) > -1;

if (!envDisable && !logLevelDisplay) {
var pkg = require(require('path').resolve('./package.json'));
if (pkg.collective) {
console.log(`\u001b[96m\u001b[1mThank you for using ${pkg.name}!\u001b[96m\u001b[1m`);
console.log(`\u001b[0m\u001b[96mIf you rely on this package, please consider supporting our open collective:\u001b[22m\u001b[39m`);
console.log(`> \u001b[94m${pkg.collective.url}/donate\u001b[0m\n`);
}
}
69 changes: 69 additions & 0 deletions index.test.js
@@ -0,0 +1,69 @@
jest.mock('./package.json', () => ({
name: 'testpkg2',
collective: {url: 'testurl'}
}), {virtual: true});

describe('test all the things', () => {
const env = global.process.env;
const console = global.console;
beforeEach(() => {
global.console = {log: jest.fn(), warn: global.console.warn};
});
afterEach(() => {
global.process.env = env;
global.console.log.mockReset();
global.console = console;
jest.resetModules();
});

describe('outputs the correct values', () => {
it('when called without args', () =>{
expect(global.console.log).not.toHaveBeenCalled();
require('./index');
expect(global.console.log).toHaveBeenCalledTimes(3);
expect(global.console.log).toHaveBeenNthCalledWith(1,
`\u001b[96m\u001b[1mThank you for using testpkg2!\u001b[96m\u001b[1m`
);
expect(global.console.log).toHaveBeenNthCalledWith(3,
`> \u001b[94mtesturl/donate\u001b[0m\n`
);
});

[0, false].forEach(falsy => {
it(`when Disable is set to ${falsy}`, () => {
global.process.env = {DISABLE_OPENCOLLECTIVE: falsy};
expect(global.console.log).not.toHaveBeenCalled();
require('./index');
expect(global.console.log).toHaveBeenCalledTimes(3);
});
});

['notice', 'http', 'timing', 'info', 'verbose', 'silly'].forEach(log => {
it(`when config_loglevel is set to ${log}`, () => {
global.process.env = {npm_config_loglevel: log}
expect(global.console.log).not.toHaveBeenCalled();
require('./index');
expect(global.console.log).toHaveBeenCalledTimes(3);
});
});
});

describe('does not show output', () => {
[1, true].forEach(truthy => {
it(`when Disable is set to ${truthy}`, () => {
global.process.env = {DISABLE_OPENCOLLECTIVE: truthy};
expect(global.console.log).not.toHaveBeenCalled();
require('./index');
expect(global.console.log).not.toHaveBeenCalled();
});
});

['warn', 'error', 'silent'].forEach(log => {
it(`when npm_config_loglevel is set to ${log}`, () => {
global.process.env = {npm_config_loglevel: log}
require('./index');
expect(global.console.log).not.toHaveBeenCalled();
});
});
});
});

0 comments on commit 3d3d074

Please sign in to comment.