1 | let { bold: b, yellow: y, red } = require('colorette')
|
2 | let { existsSync } = require('fs')
|
3 | let { join } = require('path')
|
4 |
|
5 | let ownPackage = require('./package.json')
|
6 |
|
7 | function npmCommands(pkg) {
|
8 | let add = 'npm install --save-dev '
|
9 | let rm = 'npm remove '
|
10 | if (existsSync(join(pkg.path, '..', 'yarn.lock'))) {
|
11 | add = 'yarn add --dev '
|
12 | rm = 'yarn remove '
|
13 | }
|
14 | return { add, rm }
|
15 | }
|
16 |
|
17 | module.exports = process => {
|
18 | function print(...lines) {
|
19 | process.stdout.write(lines.join('\n') + '\n')
|
20 | }
|
21 | function printError(...lines) {
|
22 | process.stderr.write(lines.join('\n') + '\n')
|
23 | }
|
24 |
|
25 | function showHelp(plugins) {
|
26 | print(
|
27 | y('size-limit [OPTION]… [FILE]…'),
|
28 | 'Check the real performance cost of your front-end project to users',
|
29 | '',
|
30 | b('Core options:'),
|
31 | ` ${y('--limit LIMIT')} Set size or running time limit for files`,
|
32 | ` ${y('--json')} Show results in JSON format`,
|
33 | ` ${y('--hide-passed')} Print only failed files`,
|
34 | ` ${y('--highlight-less')} Highlight files with less than limit size`,
|
35 | ` ${y('--help')} Display this help`,
|
36 | ` ${y('--watch')} Runs in watch mode`,
|
37 | ` ${y('--silent')} Show only failed limits`,
|
38 | ` ${y('--debug')} Show internal configs for issue report`,
|
39 | ` ${y('--version')} Display version`
|
40 | )
|
41 | if (plugins.has('webpack')) {
|
42 | print(
|
43 | '',
|
44 | b('Webpack options:'),
|
45 | ` ${y('--why')} Show package content`,
|
46 | ` ${y('--save-bundle DIR')} Put build files to check them by hand`,
|
47 | ` ${y('--clean-dir')} Remove build files folder before start`
|
48 | )
|
49 | }
|
50 | print(
|
51 | '',
|
52 | b('Examples:'),
|
53 | ' ' + y('size-limit'),
|
54 | ` Read configuration from ${b('package.json')} or ` +
|
55 | `${b('.size-limit.json')} and check limit`,
|
56 | y(' size-limit index.js')
|
57 | )
|
58 | if (plugins.has('webpack')) {
|
59 | print(
|
60 | ' Show the size of specific files with all file dependencies',
|
61 | ' ' + y('size-limit --why'),
|
62 | ' Show reasons why project have this size'
|
63 | )
|
64 | } else {
|
65 | print(' Show the size of specific files')
|
66 | }
|
67 | }
|
68 |
|
69 | function showVersion() {
|
70 | print(`size-limit ${ownPackage.version}`)
|
71 | }
|
72 |
|
73 | function showMigrationGuide(pkg) {
|
74 | let { add } = npmCommands(pkg)
|
75 | printError(
|
76 | red('Install Size Limit preset depends on type of the project'),
|
77 | '',
|
78 | 'For application, where you send JS bundle directly to users',
|
79 | ' ' + y(add + '@size-limit/preset-app'),
|
80 | '',
|
81 | 'For frameworks, components and big libraries',
|
82 | ' ' + y(add + '@size-limit/preset-big-lib'),
|
83 | '',
|
84 | 'For small (< 10 kB) libraries',
|
85 | ' ' + y(add + '@size-limit/preset-small-lib'),
|
86 | '',
|
87 | 'Check out docs for more complicated cases',
|
88 | ' ' + y('https://github.com/ai/size-limit/')
|
89 | )
|
90 | let devDependencies = pkg.packageJson.devDependencies
|
91 | if (devDependencies && !devDependencies['size-limit']) {
|
92 | printError(
|
93 | '',
|
94 | `You need to add size-limit dependency: ${y(add + 'size-limit')}`
|
95 | )
|
96 | }
|
97 | }
|
98 |
|
99 | return { showVersion, showHelp, showMigrationGuide }
|
100 | }
|