Skip to content

Commit

Permalink
Unified entrypoint 🤝
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrieanKhisbe committed Apr 4, 2024
1 parent d07b5c2 commit e66a622
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 247 deletions.
45 changes: 28 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ Just use `npm install -g bundle-phobia-cli` and you're good to go!

## Usage

Once installed you will have access to different executables:
- `bundle-phobia`: to query package size.
Just invoke it with a list of package names and some options.
- `bundle-phobia-install`: to conditionally install package if weight constraint are respected. This is a wrapper on `npm install`
Once installed you will have access a single executable `bundle-phobia` (aliased to `bundle-phobia-cli` for `npx usage`), with two commands:
- `stats` to query package size. This is the default command, just invoke it with a list of package names and some options.
```
$ bundle-phobia lodash react
```
- `install`: to conditionally install package if weight constraint are respected. This is a wrapper on `npm install`
```
$ bundle-phobia install lodash react
```

Note that you can specify a version along with the package range such as an
instance exact version `lodash@4.12.0` or range version `ora@^3.0.0`.
Expand Down Expand Up @@ -63,18 +68,20 @@ Usage: bundle-phobia <package-name> [other-package-names...]
Options:
--version Show version number [boolean]
--package, -p Provide a package.json to read dependencies [string]
--range, -r Get a range of version (0 for all, 8 by default) [number]
--json, -j Output json rather than a formater string [boolean]
--size, -s Output just the module size [boolean]
--gzip-size, -g Output just the module gzip size [boolean]
--dependencies, -d Output just the number of dependencies [boolean]
--self Output bundle-phobia stats [boolean]
-p, --package Provide a package.json to read dependencies [string]
-r, --range Get a range of version (0 for all, 8 by default) [number]
-j, --json Output json rather than a formater string [boolean]
-s, --size Output just the module size [boolean]
-g, --gzip-size Output just the module gzip size [boolean]
-d, --dependencies Output just the number of dependencies [boolean]
-x, --fail-fast Stop on first error [boolean]
-1, --serial Run requests serially [boolean]
--self Output bundle-phobia stats [boolean]
-h, --help Show help [boolean]
```
#### `bundle-phobia-install`
#### `bundle-phobia install`

`bundle-phobia-install` offer three kind of flags:
`bundle-phobia install` offer three kind of flags:
- flags to specify the size constraints
- flags to specify behavior when constraints are not respected
- npm install flags to control it's behavior
Expand Down Expand Up @@ -108,11 +115,15 @@ Usage: bundle-phobia-install <package-name> [other-package-names...]
Options:
--version Show version number [boolean]
--warn, -w Install despite of negative check but warn about
-w, --warn Install despite of negative check but warn about
predicate violation [boolean]
--interactive, -i Ask for override in case of predicate violation [boolean]
--max-size, -m Size threeshold of individual library to install [string]
--max-gzip-size, -M Gzip Size threeshold of individual library to install
-i, --interactive Ask for override in case of predicate violation [boolean]
-m, --max-size Size threeshold of individual library to install [string]
-M, --max-gzip-size Gzip Size threeshold of individual library to install
[string]
-o, --max-overall-size Overall size threeshold of dependencies [string]
-O, --max-overall-gzip-size Overall Gzip size threeshold of dependencies
[string]
-x, --fail-fast Stop on first error [boolean]
-h, --help Show help [boolean]
```
68 changes: 0 additions & 68 deletions index-install.js

This file was deleted.

55 changes: 0 additions & 55 deletions index.js

This file was deleted.

140 changes: 140 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/usr/bin/env node

const updateNotifier = require('update-notifier');
const c = require('chalk');
const Yargs = require('yargs');
const yargs = require('yargs');

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.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',
'Perform install if specified size constraints are met',
yargs => yargs.options(INSTALL_OPTIONS),
argv =>
require('./src/install')
.main({argv})
.catch(err => {
console.log(c.red.bold(`bundle-phobia-install failed: `) + err.message);
yargs.exit(1, err);
})
)
.command(
['$0', 'stats', 'package-stats'],
'Get the stats of given package from bundlephobia.com',
yargs => yargs.options(STATS_OPTIONS),
argv =>
require('./src/core')
.main({argv})
.catch(err => {
console.log(c.red.bold(`bundle-phobia failed: `) + err.message);
yargs.exit(1, err);
})
)
.showHelpOnFail(false)
.alias('h', 'help')
.pkgConf('bundle-phobia');

if (!module.parent) {
yargsParser.parse(process.argv.slice(2)).argv;
const pkg = require('./package');
updateNotifier({pkg}).notify();
}

0 comments on commit e66a622

Please sign in to comment.