1 | 'use strict';
|
2 |
|
3 | var _promise = require('babel-runtime/core-js/promise');
|
4 |
|
5 | var _promise2 = _interopRequireDefault(_promise);
|
6 |
|
7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
8 |
|
9 | var _ = require('lodash');
|
10 | var inquirer = require('inquirer');
|
11 | var chalk = require('chalk');
|
12 | var table = require('text-table');
|
13 | var installPackages = require('./install-packages');
|
14 | var emoji = require('./emoji');
|
15 |
|
16 | var UI_GROUPS = [{
|
17 | title: chalk.bold.underline.green('Update package.json to match version installed.'),
|
18 | filter: { mismatch: true, bump: null }
|
19 | }, {
|
20 | title: chalk.bold.underline.green('Missing.') + ' ' + chalk.green('You probably want these.'),
|
21 | filter: { notInstalled: true, bump: null }
|
22 | }, {
|
23 | title: chalk.bold.underline.green('Patch Update') + ' ' + chalk.green('Backwards-compatible bug fixes.'),
|
24 | filter: { bump: 'patch' }
|
25 | }, {
|
26 | title: chalk.yellow.underline.bold('Minor Update') + ' ' + chalk.yellow('New backwards-compatible features.'),
|
27 | bgColor: 'yellow',
|
28 | filter: { bump: 'minor' }
|
29 | }, {
|
30 | title: chalk.red.underline.bold('Major Update') + ' ' + chalk.red('Potentially breaking API changes. Use caution.'),
|
31 | filter: { bump: 'major' }
|
32 | }, {
|
33 | title: chalk.magenta.underline.bold('Non-Semver') + ' ' + chalk.magenta('Versions less than 1.0.0, caution.'),
|
34 | filter: { bump: 'nonSemver' }
|
35 | }];
|
36 |
|
37 | function label(pkg) {
|
38 | var bumpInstalled = pkg.bump ? pkg.installed : '';
|
39 | var installed = pkg.mismatch ? pkg.packageJson : bumpInstalled;
|
40 | var name = chalk.yellow(pkg.moduleName);
|
41 | var type = pkg.devDependency ? chalk.green(' devDep') : '';
|
42 | var missing = pkg.notInstalled ? chalk.red(' missing') : '';
|
43 | var homepage = pkg.homepage ? chalk.blue.underline(pkg.homepage) : '';
|
44 | return [name + type + missing, installed, installed && '❯', chalk.bold(pkg.latest || ''), pkg.latest ? homepage : pkg.regError || pkg.pkgError];
|
45 | }
|
46 |
|
47 | function short(pkg) {
|
48 | return pkg.moduleName + '@' + pkg.latest;
|
49 | }
|
50 |
|
51 | function choice(pkg) {
|
52 | if (!pkg.mismatch && !pkg.bump && !pkg.notInstalled) {
|
53 | return false;
|
54 | }
|
55 |
|
56 | return {
|
57 | value: pkg,
|
58 | name: label(pkg),
|
59 | short: short(pkg)
|
60 | };
|
61 | }
|
62 |
|
63 | function unselectable(options) {
|
64 | return new inquirer.Separator(chalk.reset(options ? options.title : ' '));
|
65 | }
|
66 |
|
67 | function createChoices(packages, options) {
|
68 | var filteredChoices = _.filter(packages, options.filter);
|
69 |
|
70 | var choices = filteredChoices.map(choice).filter(Boolean);
|
71 |
|
72 | var choicesAsATable = table(_.map(choices, 'name'), {
|
73 | align: ['l', 'l', 'l'],
|
74 | stringLength: function stringLength(str) {
|
75 | return chalk.stripColor(str).length;
|
76 | }
|
77 | }).split('\n');
|
78 |
|
79 | var choicesWithTableFormating = _.map(choices, function (choice, i) {
|
80 | choice.name = choicesAsATable[i];
|
81 | return choice;
|
82 | });
|
83 |
|
84 | if (choicesWithTableFormating.length) {
|
85 | choices.unshift(unselectable(options));
|
86 | choices.unshift(unselectable());
|
87 | return choices;
|
88 | }
|
89 | }
|
90 |
|
91 | function 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 new _promise2.default(function (resolve) {
|
121 | return inquirer.prompt(questions, resolve);
|
122 | }).then(function (answers) {
|
123 | var packagesToUpdate = answers.packages;
|
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 pkg.moduleName + '@' + pkg.latest;
|
134 | });
|
135 |
|
136 | var saveDevDependencies = packagesToUpdate.filter(function (pkg) {
|
137 | return pkg.devDependency;
|
138 | }).map(function (pkg) {
|
139 | return pkg.moduleName + '@' + pkg.latest;
|
140 | });
|
141 |
|
142 | var updatedPackages = packagesToUpdate.map(function (pkg) {
|
143 | return pkg.moduleName + '@' + pkg.latest;
|
144 | }).join(', ');
|
145 |
|
146 | if (!currentState.get('global')) {
|
147 | if (saveDependencies.length) {
|
148 | saveDependencies.unshift('--save');
|
149 | }
|
150 |
|
151 | if (saveDevDependencies.length) {
|
152 | saveDevDependencies.unshift('--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 |
|
168 | module.exports = interactive; |
\ | No newline at end of file |