UNPKG

5.98 kBJavaScriptView Raw
1'use strict';
2
3var _ = require('lodash');
4var inquirer = require('inquirer');
5var chalk = require('chalk');
6var table = require('text-table');
7var installPackages = require('./install-packages');
8var emoji = require('./emoji');
9var stripAnsi = require('strip-ansi');
10
11var UI_GROUPS = [{
12 title: chalk.bold.underline.green('Update package.json to match version installed.'),
13 filter: { mismatch: true, bump: null }
14}, {
15 title: chalk.bold.underline.green('Missing.') + ' ' + chalk.green('You probably want these.'),
16 filter: { notInstalled: true, bump: null }
17}, {
18 title: chalk.bold.underline.green('Patch Update') + ' ' + chalk.green('Backwards-compatible bug fixes.'),
19 filter: { bump: 'patch' }
20}, {
21 title: chalk.yellow.underline.bold('Minor Update') + ' ' + chalk.yellow('New backwards-compatible features.'),
22 bgColor: 'yellow',
23 filter: { bump: 'minor' }
24}, {
25 title: chalk.red.underline.bold('Major Update') + ' ' + chalk.red('Potentially breaking API changes. Use caution.'),
26 filter: { bump: 'major' }
27}, {
28 title: chalk.magenta.underline.bold('Non-Semver') + ' ' + chalk.magenta('Versions less than 1.0.0, caution.'),
29 filter: { bump: 'nonSemver' }
30}];
31
32function label(pkg) {
33 var bumpInstalled = pkg.bump ? pkg.installed : '';
34 var installed = pkg.mismatch ? pkg.packageJson : bumpInstalled;
35 var name = chalk.yellow(pkg.moduleName);
36 var type = pkg.devDependency ? chalk.green(' devDep') : '';
37 var missing = pkg.notInstalled ? chalk.red(' missing') : '';
38 var homepage = pkg.homepage ? chalk.blue.underline(pkg.homepage) : '';
39 return [name + type + missing, installed, installed && '❯', chalk.bold(pkg.latest || ''), pkg.latest ? homepage : pkg.regError || pkg.pkgError];
40}
41
42function short(pkg) {
43 return pkg.moduleName + '@' + pkg.latest;
44}
45
46function choice(pkg) {
47 if (!pkg.mismatch && !pkg.bump && !pkg.notInstalled) {
48 return false;
49 }
50
51 return {
52 value: pkg,
53 name: label(pkg),
54 short: short(pkg)
55 };
56}
57
58function unselectable(options) {
59 return new inquirer.Separator(chalk.reset(options ? options.title : ' '));
60}
61
62function createChoices(packages, options) {
63 var filteredChoices = _.filter(packages, options.filter);
64
65 var choices = filteredChoices.map(choice).filter(Boolean);
66
67 var choicesAsATable = table(_.map(choices, 'name'), {
68 align: ['l', 'l', 'l'],
69 stringLength: function stringLength(str) {
70 return stripAnsi(str).length;
71 }
72 }).split('\n');
73
74 var choicesWithTableFormating = _.map(choices, function (choice, i) {
75 choice.name = choicesAsATable[i];
76 return choice;
77 });
78
79 if (choicesWithTableFormating.length) {
80 choices.unshift(unselectable(options));
81 choices.unshift(unselectable());
82 return choices;
83 }
84}
85
86function buildPackageToUpdate(moduleName, version, isYarn, saveExact) {
87 // handle adding ^ for yarn, npm seems to handle this if not exact
88 return isYarn && !saveExact ? moduleName + '@^' + version : moduleName + '@' + version;
89}
90
91function interactive(currentState) {
92 var packages = currentState.get('packages');
93
94 if (currentState.get('debug')) {
95 console.log('packages', packages);
96 }
97
98 var choicesGrouped = UI_GROUPS.map(function (group) {
99 return createChoices(packages, group);
100 }).filter(Boolean);
101
102 var choices = _.flatten(choicesGrouped);
103
104 if (!choices.length) {
105 console.log(emoji(':heart: ') + 'Your modules look ' + chalk.bold('amazing') + '. Keep up the great work.' + emoji(' :heart:'));
106 return;
107 }
108
109 choices.push(unselectable());
110 choices.push(unselectable({ title: 'Space to select. Enter to start upgrading. Control-C to cancel.' }));
111
112 var questions = [{
113 name: 'packages',
114 message: 'Choose which packages to update.',
115 type: 'checkbox',
116 choices: choices.concat(unselectable()),
117 pageSize: process.stdout.rows - 2
118 }];
119
120 return inquirer.prompt(questions).then(function (answers) {
121 var packagesToUpdate = answers.packages;
122 var isYarn = currentState.get('installer') === 'yarn';
123 var saveExact = currentState.get('saveExact');
124
125 if (!packagesToUpdate || !packagesToUpdate.length) {
126 console.log('No packages selected for update.');
127 return false;
128 }
129
130 var saveDependencies = packagesToUpdate.filter(function (pkg) {
131 return !pkg.devDependency;
132 }).map(function (pkg) {
133 return buildPackageToUpdate(pkg.moduleName, pkg.latest, isYarn, saveExact);
134 });
135
136 var saveDevDependencies = packagesToUpdate.filter(function (pkg) {
137 return pkg.devDependency;
138 }).map(function (pkg) {
139 return buildPackageToUpdate(pkg.moduleName, pkg.latest, isYarn, saveExact);
140 });
141
142 var updatedPackages = packagesToUpdate.map(function (pkg) {
143 return buildPackageToUpdate(pkg.moduleName, pkg.latest, isYarn, saveExact);
144 }).join(', ');
145
146 if (!currentState.get('global')) {
147 if (saveDependencies.length) {
148 !isYarn && saveDependencies.push('--save');
149 }
150
151 if (saveDevDependencies.length) {
152 isYarn ? saveDevDependencies.push('--dev') : saveDevDependencies.push('--save-dev');
153 }
154 }
155
156 return installPackages(saveDependencies, currentState).then(function (currentState) {
157 return installPackages(saveDevDependencies, currentState);
158 }).then(function (currentState) {
159 console.log('');
160 console.log(chalk.green('[npm-check] Update complete!'));
161 console.log(chalk.green('[npm-check] ' + updatedPackages));
162 console.log(chalk.green('[npm-check] You should re-run your tests to make sure everything works with the updates.'));
163 return currentState;
164 });
165 });
166}
167
168module.exports = interactive;
\No newline at end of file