Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 4x 4x 1x 7x 7x 7x 7x 6x 6x 6x 1x 7x 7x 7x 7x 6x 6x 6x 12x 1x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 4x 4x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 4x 1x 1x 1x 1x 1x 1x 3x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x | const c = require('chalk'); const _ = require('lodash/fp'); const ora = require('ora'); const pMap = require('p-map'); const shelljs = require('shelljs'); const inquirer = require('inquirer'); const readPkgUp = require('read-pkg-up'); const {fetchPackageStats, fetchPackageJsonStats} = require('./fetch-package-stats'); const fakeSpinner = require('./fake-spinner'); const { sizePredicate, gzipSizePredicate, globalSizePredicate, globalGzipSizePredicate } = require('./install-predicates'); const DEFAULT_MAX_SIZE = '100kB'; const BUNLE_PHOBIA_ARGS = [ 'i', 'interactive', '$0', 'warn', 'w', '_', 'm', 'M', 'max-size', 'max-gzip-size', 'o', 'O', 'max-overall-size', 'max-overall-gzip-size' ]; const npmOptionsFromArgv = argv => { return _.pipe( _.omit(BUNLE_PHOBIA_ARGS), _.toPairs, _.map(([key, value]) => { const val = _.isBoolean(value) ? '' : ` ${value}`; // FIXME: check handling of false const leadingDash = _.size(key) === 1 ? '-' : '--'; return leadingDash + key + val; }), _.join(' ') )(argv); }; const installCommand = argv => { const options = npmOptionsFromArgv(argv); return `npm install ${argv._.join(' ')}${(options && ` ${options}`) || ''}`; }; const getSizePredicate = (argv, defaultSize, packageConfig) => { Iif (argv['max-size']) return sizePredicate(argv['max-size'], 'argv'); Iif (argv['max-gzip-size']) return gzipSizePredicate(argv['max-gzip-size'], 'argv'); const maxSizeConfig = _.get(['bundle-phobia', 'max-size'], packageConfig); if (maxSizeConfig) return sizePredicate(maxSizeConfig, 'package-config'); const maxGzipSizeConfig = _.get(['bundle-phobia', 'max-gzip-size'], packageConfig); Iif (maxGzipSizeConfig) return gzipSizePredicate(maxGzipSizeConfig, 'package-config'); return sizePredicate(defaultSize, 'default'); }; const getGlobalSizePredicate = (argv, packageConfig) => { Iif (argv['max-overall-size']) return globalSizePredicate(argv['max-overall-size'], 'argv'); Iif (argv['max-overall-gzip-size']) return globalGzipSizePredicate(argv['max-overall-gzip-size'], 'argv'); const maxGlobalSizeConfig = _.get(['bundle-phobia', 'max-overall-size'], packageConfig); if (maxGlobalSizeConfig) return globalSizePredicate(maxGlobalSizeConfig, 'package-config'); const maxGlobalGzipSizeConfig = _.get(['bundle-phobia', 'max-overall-gzip-size'], packageConfig); Iif (maxGlobalGzipSizeConfig) return globalGzipSizePredicate(maxGlobalGzipSizeConfig, 'package-config'); return () => ({canInstall: true}); }; const aggregateStats = statsList => ({ size: _.sumBy('size', statsList), gzip: _.sumBy('gzip', statsList), dependencyCount: _.sumBy('dependencyCount', statsList) }); const main = async ({ argv, stream = process.stdout, noOra = false, exec = shelljs.exec, prompt = inquirer.prompt, defaultMaxSize = DEFAULT_MAX_SIZE, readPkg = () => _.get('pkg', readPkgUp.sync()) }) => { const noSpin = noOra; const Spinner = noSpin ? fakeSpinner : ora; const spinner = Spinner({stream}); const handleError = (paquage, forceThrow) => err => { Eif (!noSpin) spinner.fail(c.red(`resolving ${c.bold.underline(paquage)} failed: `) + err.message); Eif (forceThrow || noSpin) { const wrapError = new Error(`${paquage}: ${err.message}`); wrapError.error = err; throw wrapError; } }; const currentPkg = readPkg(); const packages = argv._; Iif (_.isEmpty(packages)) throw new Error('No packages to install was given'); const pluralSuffix = _.size(packages) > 1 ? 's' : ''; const performInstall = () => { const res = exec(installCommand(argv)); Iif (res.code !== 0) throw new Error(`npm install returned with status code ${res.code}`); }; const predicate = getSizePredicate(argv, defaultMaxSize, currentPkg); const globalPredicate = getGlobalSizePredicate(argv, currentPkg); spinner.text = `Fetching stats for package${pluralSuffix} ${packages.join(', ')}`; spinner.info( `Applying a ${c.underline(predicate.description)} from ${predicate.source}${ globalPredicate.description ? ` and ${c.underline(globalPredicate.description)} from ${globalPredicate.source}` : '' }\n` ); spinner.start(); const statuses = await pMap(packages, async paquage => { const stats = await fetchPackageStats(paquage).catch(handleError(paquage, true)); return _.defaultsAll([{package: paquage}, stats, predicate(stats)]); }); const toInstallStats = aggregateStats(statuses); spinner.text = 'Fetching stats for already installed packages'; spinner.color = 'blue'; const allStats = await fetchPackageJsonStats(currentPkg); const installedStats = aggregateStats(allStats); const globalStatus = globalPredicate(installedStats, toInstallStats); spinner.clear(); if (globalStatus.canInstall && _.every({canInstall: true}, statuses)) { spinner.info( `Proceed to installation of package${pluralSuffix} ${c.bold.dim(packages.join(', '))}` ); await performInstall(); // §TODO: handle failure exit. and eventually add status message .succeed } else if (argv.warn) { spinner.warn( `Proceed to installation of packages ${packages .map(p => c.bold.dim(p)) .join(', ')} despite following warnings:` ); _.filter({canInstall: false}, statuses).forEach(status => { spinner.warn( `${c.red.yellow(status.package)}: ${status.reason}${ status.details ? ` (${c.dim(status.details)})` : '' }` ); }); Iif (!globalStatus.canInstall) spinner.warn( `${c.yellow.bold('global constraint')} is not respected: ${globalStatus.reason} (${c.dim( globalStatus.details )})` ); await performInstall(); } else if (argv.interactive) { spinner.warn( `Packages ${packages.map(p => c.bold.dim(p)).join(', ')} raised following ${c.yellow.bold( 'warnings' )}:` ); _.filter({canInstall: false}, statuses).forEach(status => { spinner.warn( `${c.red.yellow(status.package)}: ${status.reason}${ status.details ? ` (${c.dim(status.details)})` : '' }` ); }); Iif (!globalStatus.canInstall) spinner.warn( `${c.yellow.bold('global constraint')} is not respected: ${globalStatus.reason} (${c.dim( globalStatus.details )})` ); const {proceed} = await prompt([ { type: 'confirm', name: 'proceed', message: 'Do you still want to proceed with installation?', default: 'N' } ]); if (proceed) { spinner.succeed('Proceeding with installation as you requested'); await performInstall(); } else { spinner.fail('Installation is canceled on your demand'); } } else { spinner.fail(c.bold('Could not install for following reasons:')); _.forEach(status => { Iif (status.canInstall) spinner.succeed( `${c.green.bold(status.package)}: was individually ${c.bold('ok')} to install` ); else spinner.fail( `${c.red.bold(status.package)}: ${status.reason}${ status.details ? ` (${c.dim(status.details)})` : '' }` ); }, statuses); Eif (globalStatus.canInstall) spinner.succeed(`${c.green.bold('global constraint')} is respected`); else spinner.fail( `${c.red.bold('global constraint')} is not respected: ${globalStatus.reason} (${c.dim( globalStatus.details )})` ); throw new Error('Install was canceled.'); } spinner.stop(); }; module.exports = { main, npmOptionsFromArgv, installCommand, getGlobalSizePredicate, getSizePredicate }; |