UNPKG

2.77 kBJavaScriptView Raw
1import { execSync } from 'child_process';
2import chalk from 'chalk';
3import { padding, formatCurrency } from '../lib/utils';
4
5const collective_suggested_donation_amount = process.env.npm_package_collective_suggested_donation_amount;
6const collective_suggested_donation_interval = process.env.npm_package_collective_suggested_donation_interval;
7const user_agent = process.env.npm_config_user_agent;
8
9export function getDonateURL(collective) {
10 var donate_url = collective.url;
11 if (collective_suggested_donation_amount) {
12 donate_url += `/donate/${collective_suggested_donation_amount}`;
13 if (collective_suggested_donation_interval) {
14 donate_url += `/${collective_suggested_donation_interval}`;
15 }
16 donate_url += (npm_config_user_agent.match(/yarn/)) ? '/yarn' : '/npm';
17 } else {
18 donate_url += '/donate';
19 }
20 return donate_url;
21}
22
23export function print(str, opts) {
24 opts = opts || { color: null, align: 'center'};
25 if (opts.plain) {
26 opts.color = null;
27 }
28 str = str || '';
29 opts.align = opts.align || 'center';
30 const terminalCols = process.platform === 'win32' ? 80 : parseInt(execSync(`tput cols`).toString());
31 const strLength = str.replace(/\u001b\[[0-9]{2}m/g,'').length;
32 const leftPaddingLength = (opts.align === 'center') ? Math.floor((terminalCols - strLength) / 2) : 2;
33 const leftPadding = padding(leftPaddingLength);
34 if (opts.color) {
35 str = chalk[opts.color](str);
36 }
37
38 console.log(leftPadding, str);
39}
40
41export function printStats(stats, opts) {
42 if (!stats) return;
43 print(`Number of contributors: ${stats.contributorsCount}`, opts);
44 print(`Number of backers: ${stats.backersCount}`, opts);
45 print(`Annual budget: ${formatCurrency(stats.yearlyIncome, stats.currency)}`, opts);
46 print(`Current balance: ${formatCurrency(stats.balance, stats.currency)}`, Object.assign({}, { color: 'bold' }, opts));
47}
48
49export function printLogo(logotxt) {
50 if (!logotxt) return;
51 logotxt.split('\n').forEach(function(line) {
52 return print(line, { color: 'blue' });
53 });
54}
55
56/**
57 * Only show emoji on OSx (Windows shell doesn't like them that much ¯\_(ツ)_/¯ )
58 * @param {*} emoji
59 */
60export function emoji(emoji) {
61 if (process.stdout.isTTY && process.platform === 'darwin') {
62 return emoji;
63 } else {
64 return '';
65 }
66}
67
68export function printFooter(collective) {
69 console.log("");
70 print(`Thanks for installing ${collective.slug} ${emoji('🙏')}`, { color: 'yellow' });
71 print(`Please consider donating to our open collective`, { color: 'dim' });
72 print(`to help us maintain this package.`, { color: 'dim' });
73 console.log("");
74 printStats(collective.stats);
75 console.log("");
76 print(`${chalk.bold(`${emoji('👉 ')} Donate:`)} ${chalk.underline(getDonateURL(collective))}`);
77 console.log("");
78}
\No newline at end of file