yarn-frozen-dependencies
Version:
A yarn plugin who freeze the workspace dependencies to prevent undesired updates.
36 lines (28 loc) • 1.51 kB
text/typescript
import {createInterface} from "readline";
import {ReportError, MessageName, formatUtils} from '@yarnpkg/core';
import {OperationReporter} from "./operation-reporter";
export class ConfirmOperationReporter extends OperationReporter {
async reportOperation(): Promise<void> {
const {operation, target, formattedFields, workspace: {project: {configuration}}} = this;
const confirmText = formatUtils.pretty(configuration, formatUtils.applyStyle(configuration, 'Confirm:', 2), 'yellow');
const message = formatUtils.pretty(configuration, `${confirmText} you will ${operation} ${formattedFields.dependency} but ${target} are indicated as sealed in ${formattedFields.workspace}. Are you sure? [y/N]`, 'blue');
if (!await this.askConfirmation(message)) {
this.abortOperation();
}
}
protected askConfirmation = async (message: string): Promise<boolean> => {
const readlineInterface = createInterface({input: process.stdin, output: process.stdout, terminal: true});
const confirmation: boolean = await new Promise(resolve => {
readlineInterface.question(message, (answer) => {
resolve(/^(yes|y|1)$/i.test(answer.trim()));
});
});
readlineInterface.close();
return confirmation;
}
protected abortOperation = (): void => {
const error = new ReportError(MessageName.UNNAMED, 'Operation aborted!');
error.stack = undefined;
throw error;
}
}