diff --git a/src/commands/check.js b/src/commands/check.js index e72c0bd..9b2f17b 100644 --- a/src/commands/check.js +++ b/src/commands/check.js @@ -1,4 +1,4 @@ -import {writeFileSync} from 'fs'; +import {writeFileSync, existsSync} from 'fs'; import _ from 'lodash'; import {flow, map, partition} from 'lodash/fp'; @@ -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'; const pkg = require('../../package.json'); @@ -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'}, {name: 'No', value: false}, // Don't show this option if we couldn't find module's changelog url (changelogUrl !== null) && @@ -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` + + createUpdatedModulesTable(updatedModules) + + '\n' + ); + + const {indent} = detectIndent(packageSource); + writeFileSync( + packageFile, + // Adding newline to the end of file + `${JSON.stringify(packageJson, null, indent)}\n` + ); + + try { + if (existsSync('yarn.lock')) { + // 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'}); + 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); + } catch(err) { + console.error(err) + } + break; + case true: updatedModules.push(outdatedModule); setModuleVersion(name, to, packageJson);