Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Create separate commits per upgraded package #39

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
45 changes: 44 additions & 1 deletion src/commands/check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {writeFileSync} from 'fs';
import {writeFileSync, existsSync} from 'fs';

import _ from 'lodash';
import {flow, map, partition} from 'lodash/fp';
Expand All @@ -18,6 +18,7 @@ import askUser from '../askUser';
import {toSentence} from '../stringUtils';
import {askIgnoreFields} from './ignore';
import Config from '../Config';
import { execSync } from 'child_process';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move it below import {writeFileSync, existsSync} from 'fs'


const pkg = require('../../package.json');

Expand Down Expand Up @@ -154,6 +155,7 @@ export const handler = catchAsyncError(async opts => {
`from ${from} to ${colorizeDiff(from, to)}?`,
choices: _.compact([
{name: 'Yes', value: true},
{name: 'Update immediately and create a separate commit', value: 'commit'},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's better move it below No option

{name: 'No', value: false},
// Don't show this option if we couldn't find module's changelog url
(changelogUrl !== null) &&
Expand Down Expand Up @@ -218,6 +220,47 @@ export const handler = catchAsyncError(async opts => {
isUpdateFinished = true;
break;

case 'commit':
updatedModules.push(outdatedModule);
setModuleVersion(name, to, packageJson);
delete config.ignore[name];

// Showing the list of modules that are going to be updated
console.log(
`\n${strong('These packages will be updated:')}\n\n` +
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will always show a single package, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, should update the text

createUpdatedModulesTable(updatedModules) +
'\n'
);

const {indent} = detectIndent(packageSource);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this block of code into a separate function e.g. updatePackageJson

writeFileSync(
packageFile,
// Adding newline to the end of file
`${JSON.stringify(packageJson, null, indent)}\n`
);

try {
if (existsSync('yarn.lock')) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to make this check every time, right? It can be done only once.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

// Assume the user wants to use yarn
execSync('yarn', {stdio: 'inherit'});
execSync('git add package.json yarn.lock', {stdio: 'inherit'});
} else if (existsSync('package-lock.json')) {
// Use npm and package-lock.json
execSync('npm install', {stdio: 'inherit'});
execSync('git add package.json package-lock.json', {stdio: 'inherit'});
} else {
// Default to only using npm install and not including a lock file
execSync('npm install', {stdio: 'inherit'});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's right to generate a package-lock.json if it wasn't there before. There is an npm flag to not generate it during npm install. Shall we use it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

execSync('git add package.json', {stdio: 'inherit'});
}
execSync(`git commit -m "Upgrade ${name} from ${from} to ${to}"`,{stdio: 'inherit'});
// Clean the list of packages to be updated after updating and commiting
updatedModules.splice(0, updatedModules.length);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just updatedModules = []?

} catch(err) {
console.error(err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to show more user-friendly errors e.g. console.error(`Error upgrading ${name} module: ${err.message}`)

}
break;

case true:
updatedModules.push(outdatedModule);
setModuleVersion(name, to, packageJson);
Expand Down