@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
85 lines • 3.54 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _ContextRemoveCommand_instances, _ContextRemoveCommand_initTelemetry, _ContextRemoveCommand_initOptions;
import fs from 'fs';
import { cli } from '../../../cli/cli.js';
import { CommandError } from '../../../Command.js';
import AnonymousCommand from '../../base/AnonymousCommand.js';
import commands from '../commands.js';
class ContextRemoveCommand extends AnonymousCommand {
get name() {
return commands.REMOVE;
}
get description() {
return 'Removes the CLI for Microsoft 365 context in the current working folder';
}
constructor() {
super();
_ContextRemoveCommand_instances.add(this);
__classPrivateFieldGet(this, _ContextRemoveCommand_instances, "m", _ContextRemoveCommand_initTelemetry).call(this);
__classPrivateFieldGet(this, _ContextRemoveCommand_instances, "m", _ContextRemoveCommand_initOptions).call(this);
}
async commandAction(logger, args) {
if (args.options.force) {
await this.removeContext();
}
else {
const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove the context?` });
if (result) {
await this.removeContext();
}
}
}
removeContext() {
const filePath = '.m365rc.json';
let m365rc = {};
if (fs.existsSync(filePath)) {
try {
const fileContents = fs.readFileSync(filePath, 'utf8');
if (fileContents) {
m365rc = JSON.parse(fileContents);
}
}
catch (e) {
throw new CommandError(`Error reading ${filePath}: ${e}. Please remove context info from ${filePath} manually.`);
}
}
if (!m365rc.context) {
return;
}
const keys = Object.keys(m365rc);
if (keys.length === 1 && keys.indexOf('context') > -1) {
try {
fs.unlinkSync(filePath);
}
catch (e) {
throw new CommandError(`Error removing ${filePath}: ${e}. Please remove ${filePath} manually.`);
}
}
else {
try {
delete m365rc.context;
fs.writeFileSync(filePath, JSON.stringify(m365rc, null, 2));
}
catch (e) {
throw new CommandError(`Error writing ${filePath}: ${e}. Please remove context info from ${filePath} manually.`);
}
}
}
}
_ContextRemoveCommand_instances = new WeakSet(), _ContextRemoveCommand_initTelemetry = function _ContextRemoveCommand_initTelemetry() {
this.telemetry.push((args) => {
Object.assign(this.telemetryProperties, {
force: !!args.options.force
});
});
}, _ContextRemoveCommand_initOptions = function _ContextRemoveCommand_initOptions() {
this.options.unshift({
option: '-f, --force'
});
};
export default new ContextRemoveCommand();
//# sourceMappingURL=context-remove.js.map