Skip to content

Commit

Permalink
Add option to commit with generic message (facebook#36421)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#36421

Changelog: [Internal]

Adding an extra choice for commit question, user can now choose between three options:
1. Commit with generic message, no further actions needed
2. Commit with custom message, intercative VIM input will open
3. Not committing anything

Reviewed By: cortinico

Differential Revision: D43943526

fbshipit-source-id: 014215105d192961486b7d1c697f491697492812
  • Loading branch information
hoxyq authored and OlimpiaZurek committed May 22, 2023
1 parent 1da2f62 commit 0cacca0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 25 deletions.
88 changes: 64 additions & 24 deletions scripts/monorepo/bump-all-updated-packages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ const path = require('path');
const {echo, exec, exit} = require('shelljs');
const yargs = require('yargs');

const {PUBLISH_PACKAGES_TAG} = require('../constants');
const {
PUBLISH_PACKAGES_TAG,
GENERIC_COMMIT_MESSAGE,
NO_COMMIT_CHOICE,
COMMIT_WITH_GENERIC_MESSAGE_CHOICE,
COMMIT_WITH_CUSTOM_MESSAGE_CHOICE,
} = require('../constants');
const forEachPackage = require('../for-each-package');
const checkForGitChanges = require('../check-for-git-changes');
const bumpPackageVersion = require('./bump-package-version');
Expand Down Expand Up @@ -156,33 +162,67 @@ const main = async () => {
.prompt([
{
type: 'list',
name: 'shouldSubmitCommit',
name: 'commitChoice',
message: 'Do you want to submit a commit with these changes?',
choices: ['Yes', 'No'],
filter: val => val === 'Yes',
choices: [
{
name: 'Yes, with generic message',
value: COMMIT_WITH_GENERIC_MESSAGE_CHOICE,
},
{
name: 'Yes, with custom message',
value: COMMIT_WITH_CUSTOM_MESSAGE_CHOICE,
},
{
name: 'No',
value: NO_COMMIT_CHOICE,
},
],
},
])
.then(({shouldSubmitCommit}) => {
if (!shouldSubmitCommit) {
echo('Not submitting a commit, but keeping all changes');
return;
.then(({commitChoice}) => {
switch (commitChoice) {
case NO_COMMIT_CHOICE: {
echo('Not submitting a commit, but keeping all changes');

break;
}

case COMMIT_WITH_GENERIC_MESSAGE_CHOICE: {
exec(`git commit -am "${GENERIC_COMMIT_MESSAGE}"`, {
cwd: ROOT_LOCATION,
silent: true,
});

break;
}

case COMMIT_WITH_CUSTOM_MESSAGE_CHOICE: {
// exec from shelljs currently does not support interactive input
// https://github.com/shelljs/shelljs/wiki/FAQ#running-interactive-programs-with-exec
execSync('git commit -a', {cwd: ROOT_LOCATION, stdio: 'inherit'});

const enteredCommitMessage = exec(
'git log -n 1 --format=format:%B',
{
cwd: ROOT_LOCATION,
silent: true,
},
).stdout.trim();
const commitMessageWithTag =
enteredCommitMessage + `\n\n${PUBLISH_PACKAGES_TAG}`;

exec(`git commit --amend -m "${commitMessageWithTag}"`, {
cwd: ROOT_LOCATION,
silent: true,
});

break;
}

default:
throw new Error('');
}

// exec from shelljs currently does not support interactive input
// https://github.com/shelljs/shelljs/wiki/FAQ#running-interactive-programs-with-exec
execSync('git commit -a', {cwd: ROOT_LOCATION, stdio: 'inherit'});

const enteredCommitMessage = exec('git log -n 1 --format=format:%B', {
cwd: ROOT_LOCATION,
silent: true,
}).stdout.trim();
const commitMessageWithTag =
enteredCommitMessage + `\n\n${PUBLISH_PACKAGES_TAG}`;

exec(`git commit --amend -m "${commitMessageWithTag}"`, {
cwd: ROOT_LOCATION,
silent: true,
});
})
.then(() => echo());
}
Expand Down
13 changes: 12 additions & 1 deletion scripts/monorepo/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,16 @@
*/

const PUBLISH_PACKAGES_TAG = '#publish-packages-to-npm';
const GENERIC_COMMIT_MESSAGE = `bumped packages versions\n\n${PUBLISH_PACKAGES_TAG}`;

module.exports = {PUBLISH_PACKAGES_TAG};
const NO_COMMIT_CHOICE = 'NO_COMMIT';
const COMMIT_WITH_GENERIC_MESSAGE_CHOICE = 'COMMIT_WITH_GENERIC_MESSAGE';
const COMMIT_WITH_CUSTOM_MESSAGE_CHOICE = 'COMMIT_WITH_CUSTOM_MESSAGE';

module.exports = {
PUBLISH_PACKAGES_TAG,
GENERIC_COMMIT_MESSAGE,
NO_COMMIT_CHOICE,
COMMIT_WITH_GENERIC_MESSAGE_CHOICE,
COMMIT_WITH_CUSTOM_MESSAGE_CHOICE,
};

0 comments on commit 0cacca0

Please sign in to comment.