UNPKG

2.81 kBJavaScriptView Raw
1import fs from 'fs';
2import chalk from 'chalk';
3import minimist from 'minimist';
4import path from 'path';
5
6export function debug() {
7 if (process.env.DEBUG) {
8 console.log.apply(this, arguments);
9 }
10}
11
12export function isDevEnvironment() {
13 if (process.env.OC_POSTINSTALL_TEST) return true;
14 if (process.env.CI || process.env.CONTINUOUS_INTEGRATION) return false;
15 return (!process.env.NODE_ENV || process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === 'development');
16}
17
18export function isFancyEnvironment() {
19 const npm_config_node_version = process.env.npm_config_node_version;
20 return (isDevEnvironment() && process.stdout.isTTY && process.platform !== 'win32' && (!npm_config_node_version || parseInt(npm_config_node_version.substr(0,npm_config_node_version.indexOf('.')))) >= 5);
21}
22
23export function padding(length) {
24 var padding = '';
25 for (var i=0; i<length; i++) {
26 padding += ' ';
27 }
28 return padding;
29}
30
31export function formatCurrency(amount, currency, precision) {
32 precision = precision || 0;
33 amount = amount/100; // converting cents
34
35 return amount.toLocaleString(currency, {
36 style: 'currency',
37 currency: currency,
38 minimumFractionDigits : precision,
39 maximumFractionDigits : precision
40 });
41}
42
43const argv = minimist(process.argv.slice(2), {
44 alias: {
45 collective: 'c',
46 logo: 'l',
47 help: 'h'
48 }
49});
50
51export function getPackageJSON() {
52 const packageJSONPath = path.resolve('./package.json');
53 debug("Loading ", packageJSONPath);
54 let pkg;
55 try {
56 pkg = JSON.parse(fs.readFileSync(packageJSONPath, "utf8"));
57 return pkg;
58 } catch(e) {
59 debug("error while trying to load ./package.json", "cwd:", process.cwd(), e);
60 }
61}
62
63export function getCollectiveSlug() {
64 debug(">>> argv", argv);
65 if (argv.collective) return argv.collective;
66 if (process.env.npm_package_name) return process.env.npm_package_name;
67 if (argv._[0]) return argv._[0];
68}
69
70export function getCollective() {
71 let pkg;
72 const collective = {};
73 collective.slug = getCollectiveSlug();
74 if (!collective.slug) {
75 pkg = getPackageJSON();
76 if (pkg && pkg.collective && pkg.collective.url) {
77 collective.slug = pkg.collective.url.substr(pkg.collective.url.lastIndexOf('/')+1).toLowerCase();
78 }
79 }
80 collective.url = process.env.npm_package_collective_url || `https://opencollective.com/${collective.slug}`;
81 collective.logo = argv.logo || process.env.npm_package_collective_logo;
82
83 if (!collective.logo) {
84 pkg = pkg || getPackageJSON();
85 if (pkg.collective) {
86 collective.logo = pkg.collective.logo;
87 }
88 }
89
90 debug(">>> collective", collective);
91 return collective;
92}
93
94export function getArgs() {
95 const args = {};
96 for (const i in arguments) {
97 args[arguments[i]] = argv._[i];
98 }
99 debug(">>> args", args);
100 return args;
101}
\No newline at end of file