-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.js
More file actions
144 lines (139 loc) Β· 3.63 KB
/
main.js
File metadata and controls
144 lines (139 loc) Β· 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
import updateNotifier from 'update-notifier';
import c from 'chalk';
import Yargs from 'yargs';
// eslint-disable-next-line import/no-unresolved -- yargs/helpers is a valid subpath export
import {hideBin} from 'yargs/helpers';
import {createRequire} from 'module';
// TODO: Replace with `import pkg from './package.json' with {type: 'json'};` once ESLint 9+ parser is used
const require = createRequire(import.meta.url);
const pkg = require('./package.json');
const STATS_OPTIONS = {
package: {
alias: 'p',
string: true,
describe: 'Provide a package.json to read dependencies'
},
range: {
alias: 'r',
number: true,
describe: 'Get a range of version (0 for all, 8 by default)'
},
json: {
alias: 'j',
boolean: true,
describe: 'Output json rather than a formater string'
},
size: {
alias: 's',
boolean: true,
describe: 'Output just the module size'
},
'gzip-size': {
alias: 'g',
boolean: true,
describe: 'Output just the module gzip size'
},
dependencies: {
alias: 'd',
boolean: true,
describe: 'Output just the number of dependencies'
},
'fail-fast': {
alias: 'x',
boolean: true,
default: false,
describe: 'Stop on first error'
},
serial: {
alias: '1',
boolean: true,
describe: 'Run requests serially'
},
self: {
boolean: true,
describe: 'Output bundle-phobia stats'
},
help: {
alias: 'h',
describe: 'Show help'
}
};
const INSTALL_OPTIONS = {
warn: {
describe: 'Install despite of negative check but warn about predicate violation',
alias: 'w',
boolean: true
},
interactive: {
describe: 'Ask for override in case of predicate violation',
alias: 'i',
boolean: true
},
'max-size': {
describe: 'Size threeshold of individual library to install',
alias: 'm',
string: true
},
'max-gzip-size': {
describe: 'Gzip Size threeshold of individual library to install',
alias: 'M',
string: true
},
'max-overall-size': {
describe: 'Overall size threeshold of dependencies',
alias: 'o',
string: true
},
'max-overall-gzip-size': {
describe: 'Overall Gzip size threeshold of dependencies',
alias: 'O',
string: true
},
'fail-fast': {
describe: 'Stop on first error',
alias: 'x',
boolean: true
}
};
const yargsParser = Yargs(hideBin(process.argv))
.scriptName('bundle-phobia')
.parserConfiguration({
'short-option-groups': true,
'camel-case-expansion': false,
'dot-notation': false,
'parse-numbers': true,
'boolean-negation': false
})
.usage('Bundle Phobia: Find the cost of adding a npm package to your bundle')
.example('$0 lodash chalk', 'Get stats for a list of packages')
.example('$0 install lodash chalk', 'Conditionaly install packages')
.command(
'install <packages..>',
'Perform install if specified size constraints are met',
yargs => yargs.options(INSTALL_OPTIONS),
async argv => {
const {main} = await import('./src/install.js');
main({argv}).catch(err => {
console.log(c.red.bold(`bundle-phobia-install failed: `) + err.message);
process.exit(1);
});
}
)
.command(
'$0 [packages..]',
'Get the stats of given package from bundlephobia.com',
yargs => yargs.options(STATS_OPTIONS),
async argv => {
const {main} = await import('./src/core.js');
main({argv}).catch(err => {
console.log(c.red.bold(`bundle-phobia failed: `) + err.message);
process.exit(1);
});
}
)
.showHelpOnFail(false)
.alias('h', 'help')
.pkgConf('bundle-phobia');
yargsParser.parse();
updateNotifier({pkg}).notify();