X Tutup
Skip to content

Commit c0e9dd8

Browse files
committed
Remove the allowOverwriteHandler callback.
1 parent 5428002 commit c0e9dd8

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

apps/rush-lib/src/cli/actions/ChangeAction.ts

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ export class ChangeAction extends BaseRushAction {
177177

178178
this._warnUncommittedChanges();
179179

180+
const promptModule: inquirer.PromptModule = inquirer.createPromptModule();
180181
let changeFileData: Map<string, IChangeFile> = new Map<string, IChangeFile>();
181-
let allowOverwriteHandler: (filePath: string) => Promise<boolean>;
182+
let interactiveMode: boolean = false;
182183
if (this._bulkChangeParameter.value) {
183184
if (
184185
!this._bulkChangeBumpTypeParameter.value ||
@@ -239,19 +240,16 @@ export class ChangeAction extends BaseRushAction {
239240
for (const error of errors) {
240241
console.error(error);
241242
}
243+
242244
throw new AlreadyReportedError();
243245
}
244-
245-
allowOverwriteHandler = async (filePath: string) => Promise.reject(
246-
new Error(`Changefile ${filePath} already exists`)
247-
);
248246
} else if (this._bulkChangeBumpTypeParameter.value || this._bulkChangeMessageParameter.value) {
249247
throw new Error(
250248
`The ${this._bulkChangeParameter.longName} flag must be provided with the ` +
251249
`${this._bulkChangeBumpTypeParameter.longName} and ${this._bulkChangeMessageParameter.longName} parameters.`
252250
);
253251
} else {
254-
const promptModule: inquirer.PromptModule = inquirer.createPromptModule();
252+
interactiveMode = true;
255253

256254
const existingChangeComments: Map<string, string[]> = ChangeFiles.getChangeComments(this._getChangeFiles());
257255
changeFileData = await this._promptForChangeFileData(
@@ -266,31 +264,15 @@ export class ChangeAction extends BaseRushAction {
266264
changeFileData.forEach((changeFile: IChangeFile) => {
267265
changeFile.email = email;
268266
});
269-
270-
allowOverwriteHandler = async (filePath) => {
271-
const overwrite: boolean = await promptModule([
272-
{
273-
name: 'overwrite',
274-
type: 'confirm',
275-
message: `Overwrite ${filePath}?`
276-
}
277-
]);
278-
279-
if (overwrite) {
280-
return true;
281-
} else {
282-
console.log(`Not overwriting ${filePath}`);
283-
return false;
284-
}
285-
};
286-
}
287-
288-
if (this._overwriteFlagParameter.value) {
289-
allowOverwriteHandler = () => Promise.resolve(true);
290267
}
291268

292269
try {
293-
return await this._writeChangeFiles(changeFileData, allowOverwriteHandler);
270+
return await this._writeChangeFiles(
271+
promptModule,
272+
changeFileData,
273+
this._overwriteFlagParameter.value,
274+
interactiveMode
275+
);
294276
} catch (error) {
295277
throw new Error(`There was an error creating a change file: ${error.toString()}`);
296278
}
@@ -609,31 +591,59 @@ export class ChangeAction extends BaseRushAction {
609591
* Writes change files to the common/changes folder. Will prompt for overwrite if file already exists.
610592
*/
611593
private async _writeChangeFiles(
594+
promptModule: inquirer.PromptModule,
612595
changeFileData: Map<string, IChangeFile>,
613-
allowOverwriteHandler: (filePath: string) => Promise<boolean>
596+
overwrite: boolean,
597+
interactiveMode: boolean
614598
): Promise<void> {
615599
await changeFileData.forEach(async (changeFile: IChangeFile) => {
616-
await this._writeChangeFile(changeFile, allowOverwriteHandler);
600+
await this._writeChangeFile(promptModule, changeFile, overwrite, interactiveMode);
617601
});
618602
}
619603

620604
private async _writeChangeFile(
605+
promptModule: inquirer.PromptModule,
621606
changeFileData: IChangeFile,
622-
allowOverwriteHandler: (filePath: string) => Promise<boolean>
607+
overwrite: boolean,
608+
interactiveMode: boolean
623609
): Promise<void> {
624610
const output: string = JSON.stringify(changeFileData, undefined, 2);
625611
const changeFile: ChangeFile = new ChangeFile(changeFileData, this.rushConfiguration);
626612
const filePath: string = changeFile.generatePath();
627613

628614
const fileExists: boolean = FileSystem.exists(filePath);
629-
const shouldWrite: boolean = fileExists
630-
? await allowOverwriteHandler(filePath)
631-
: true;
615+
const shouldWrite: boolean = (
616+
!fileExists ||
617+
overwrite ||
618+
(interactiveMode ? await this._promptForOverwrite(promptModule, filePath) : false)
619+
);
620+
621+
if (!interactiveMode && !overwrite) {
622+
throw new Error(`Changefile ${filePath} already exists`);
623+
}
624+
632625
if (shouldWrite) {
633626
this._writeFile(filePath, output, shouldWrite && fileExists);
634627
}
635628
}
636629

630+
private async _promptForOverwrite(promptModule: inquirer.PromptModule, filePath: string): Promise<boolean> {
631+
const overwrite: boolean = await promptModule([
632+
{
633+
name: 'overwrite',
634+
type: 'confirm',
635+
message: `Overwrite ${filePath}?`
636+
}
637+
]);
638+
639+
if (overwrite) {
640+
return true;
641+
} else {
642+
console.log(`Not overwriting ${filePath}`);
643+
return false;
644+
}
645+
}
646+
637647
/**
638648
* Writes a file to disk, ensuring the directory structure up to that point exists
639649
*/

0 commit comments

Comments
 (0)
X Tutup