-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
run.js
95 lines (77 loc) · 2.3 KB
/
run.js
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
let { resolve } = require('path')
let chokidar = require('chokidar')
let spinner = require('mico-spinner')
let SizeLimitError = require('./size-limit-error')
let createReporter = require('./create-reporter')
let loadPlugins = require('./load-plugins')
let createHelp = require('./create-help')
let readPkgUp = require('./read-pkg-up')
let getConfig = require('./get-config')
let parseArgs = require('./parse-args')
let debug = require('./debug')
let calc = require('./calc')
function throttle(fn) {
let next, running
// istanbul ignore next
return () => {
clearTimeout(next)
next = setTimeout(async () => {
await running
running = fn()
}, 200)
}
}
async function findPlugins(parentPkg) {
let plugins = loadPlugins(parentPkg)
if (!parentPkg || !plugins.isEmpty) return plugins
let cwd = resolve(parentPkg.path, '..', '..')
let pkg = await readPkgUp(cwd)
return findPlugins(pkg)
}
module.exports = async process => {
function hasArg(arg) {
return process.argv.includes(arg)
}
let isJsonOutput = hasArg('--json')
let isSilentMode = hasArg('--silent')
let reporter = createReporter(process, isJsonOutput, isSilentMode)
let help = createHelp(process)
let config, args
try {
if (hasArg('--version')) {
return help.showVersion()
}
let pkg = await readPkgUp(process.cwd())
let plugins = await findPlugins(pkg)
if (hasArg('--help')) {
return help.showHelp(plugins)
}
if (!pkg || !pkg.packageJson) {
throw new SizeLimitError('noPackage')
}
args = parseArgs(plugins, process.argv)
if (plugins.isEmpty) {
help.showMigrationGuide(pkg)
return process.exit(1)
}
config = await getConfig(plugins, process, args, pkg)
let calcAndShow = async () => {
let outputFunc = isJsonOutput ? null : spinner
await calc(plugins, config, outputFunc)
debug.results(process, args, config)
reporter.results(plugins, config)
}
await calcAndShow()
if (hasArg('--watch')) {
let watcher = chokidar.watch(['**/*'], {
ignored: '**/node_modules/**'
})
watcher.on('change', throttle(calcAndShow))
}
if ((config.failed || config.missed) && !args.why) process.exit(1)
} catch (e) {
debug.error(process, args, config)
reporter.error(e)
process.exit(1)
}
}