feat(dev-infra): support command line arguments for restore-commit-message (#39739)
Allowing command line arguments to provide the file and source values to the restore-commit-message command will assist in the the process of upgrading to husky@5. PR Close #39739
This commit is contained in:
parent
3a344d4e0e
commit
d1dca8ba92
@ -13,38 +13,52 @@ import {CommitMsgSource} from '../commit-message-source';
|
|||||||
import {restoreCommitMessage} from './restore-commit-message';
|
import {restoreCommitMessage} from './restore-commit-message';
|
||||||
|
|
||||||
export interface RestoreCommitMessageOptions {
|
export interface RestoreCommitMessageOptions {
|
||||||
fileEnvVariable: string[];
|
file?: string;
|
||||||
|
source?: string;
|
||||||
|
fileEnvVariable?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Builds the command. */
|
/** Builds the command. */
|
||||||
function builder(yargs: Argv) {
|
function builder(yargs: Argv) {
|
||||||
return yargs.option('file-env-variable' as 'fileEnvVariable', {
|
return yargs
|
||||||
type: 'string',
|
.option('file-env-variable' as 'fileEnvVariable', {
|
||||||
array: true,
|
type: 'string',
|
||||||
demandOption: true,
|
description: 'The key for the environment variable which holds the arguments for the\n' +
|
||||||
description: 'The key for the environment variable which holds the arguments for the\n' +
|
'prepare-commit-msg hook as described here:\n' +
|
||||||
'prepare-commit-msg hook as described here:\n' +
|
'https://git-scm.com/docs/githooks#_prepare_commit_msg'
|
||||||
'https://git-scm.com/docs/githooks#_prepare_commit_msg',
|
})
|
||||||
coerce: arg => {
|
.positional('file', {type: 'string'})
|
||||||
const [file, source] = (process.env[arg] || '').split(' ');
|
.positional('source', {type: 'string'});
|
||||||
if (!file) {
|
|
||||||
throw new Error(`Provided environment variable "${arg}" was not found.`);
|
|
||||||
}
|
|
||||||
return [file, source];
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handles the command. */
|
/** Handles the command. */
|
||||||
async function handler({fileEnvVariable}: Arguments<RestoreCommitMessageOptions>) {
|
async function handler({fileEnvVariable, file, source}: Arguments<RestoreCommitMessageOptions>) {
|
||||||
restoreCommitMessage(fileEnvVariable[0], fileEnvVariable[1] as CommitMsgSource);
|
// File and source are provided as command line parameters
|
||||||
|
if (file !== undefined) {
|
||||||
|
restoreCommitMessage(file, source as CommitMsgSource);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// File and source are provided as values held in an environment variable.
|
||||||
|
if (fileEnvVariable !== undefined) {
|
||||||
|
const [fileFromEnv, sourceFromEnv] = (process.env[fileEnvVariable!] || '').split(' ');
|
||||||
|
if (!fileFromEnv) {
|
||||||
|
throw new Error(`Provided environment variable "${fileEnvVariable}" was not found.`);
|
||||||
|
}
|
||||||
|
restoreCommitMessage(fileFromEnv, sourceFromEnv as CommitMsgSource);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(
|
||||||
|
'No file path and commit message source provide. Provide values via positional command ' +
|
||||||
|
'arguments, or via the --file-env-variable flag');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** yargs command module describing the command. */
|
/** yargs command module describing the command. */
|
||||||
export const RestoreCommitMessageModule: CommandModule<{}, RestoreCommitMessageOptions> = {
|
export const RestoreCommitMessageModule: CommandModule<{}, RestoreCommitMessageOptions> = {
|
||||||
handler,
|
handler,
|
||||||
builder,
|
builder,
|
||||||
command: 'restore-commit-message-draft',
|
command: 'restore-commit-message-draft [file] [source]',
|
||||||
// Description: Restore a commit message draft if one has been saved from a failed commit attempt.
|
// Description: Restore a commit message draft if one has been saved from a failed commit attempt.
|
||||||
// No describe is defiend to hide the command from the --help.
|
// No describe is defiend to hide the command from the --help.
|
||||||
describe: false,
|
describe: false,
|
||||||
|
@ -1604,33 +1604,42 @@ function restoreCommitMessage(filePath, source) {
|
|||||||
*/
|
*/
|
||||||
/** Builds the command. */
|
/** Builds the command. */
|
||||||
function builder$1(yargs) {
|
function builder$1(yargs) {
|
||||||
return yargs.option('file-env-variable', {
|
return yargs
|
||||||
|
.option('file-env-variable', {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
array: true,
|
|
||||||
demandOption: true,
|
|
||||||
description: 'The key for the environment variable which holds the arguments for the\n' +
|
description: 'The key for the environment variable which holds the arguments for the\n' +
|
||||||
'prepare-commit-msg hook as described here:\n' +
|
'prepare-commit-msg hook as described here:\n' +
|
||||||
'https://git-scm.com/docs/githooks#_prepare_commit_msg',
|
'https://git-scm.com/docs/githooks#_prepare_commit_msg'
|
||||||
coerce: arg => {
|
})
|
||||||
const [file, source] = (process.env[arg] || '').split(' ');
|
.positional('file', { type: 'string' })
|
||||||
if (!file) {
|
.positional('source', { type: 'string' });
|
||||||
throw new Error(`Provided environment variable "${arg}" was not found.`);
|
|
||||||
}
|
|
||||||
return [file, source];
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
/** Handles the command. */
|
/** Handles the command. */
|
||||||
function handler$1({ fileEnvVariable }) {
|
function handler$1({ fileEnvVariable, file, source }) {
|
||||||
return tslib.__awaiter(this, void 0, void 0, function* () {
|
return tslib.__awaiter(this, void 0, void 0, function* () {
|
||||||
restoreCommitMessage(fileEnvVariable[0], fileEnvVariable[1]);
|
// File and source are provided as command line parameters
|
||||||
|
if (file !== undefined) {
|
||||||
|
restoreCommitMessage(file, source);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// File and source are provided as values held in an environment variable.
|
||||||
|
if (fileEnvVariable !== undefined) {
|
||||||
|
const [fileFromEnv, sourceFromEnv] = (process.env[fileEnvVariable] || '').split(' ');
|
||||||
|
if (!fileFromEnv) {
|
||||||
|
throw new Error(`Provided environment variable "${fileEnvVariable}" was not found.`);
|
||||||
|
}
|
||||||
|
restoreCommitMessage(fileFromEnv, sourceFromEnv);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new Error('No file path and commit message source provide. Provide values via positional command ' +
|
||||||
|
'arguments, or via the --file-env-variable flag');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/** yargs command module describing the command. */
|
/** yargs command module describing the command. */
|
||||||
const RestoreCommitMessageModule = {
|
const RestoreCommitMessageModule = {
|
||||||
handler: handler$1,
|
handler: handler$1,
|
||||||
builder: builder$1,
|
builder: builder$1,
|
||||||
command: 'restore-commit-message-draft',
|
command: 'restore-commit-message-draft [file] [source]',
|
||||||
// Description: Restore a commit message draft if one has been saved from a failed commit attempt.
|
// Description: Restore a commit message draft if one has been saved from a failed commit attempt.
|
||||||
// No describe is defiend to hide the command from the --help.
|
// No describe is defiend to hide the command from the --help.
|
||||||
describe: false,
|
describe: false,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user