UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

87 lines 3.98 kB
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 _ContextOptionRemoveCommand_instances, _ContextOptionRemoveCommand_initTelemetry, _ContextOptionRemoveCommand_initOptions; import fs from 'fs'; import { cli } from '../../../../cli/cli.js'; import { CommandError } from '../../../../Command.js'; import ContextCommand from '../../../base/ContextCommand.js'; import commands from '../../commands.js'; class ContextOptionRemoveCommand extends ContextCommand { get name() { return commands.OPTION_REMOVE; } get description() { return 'Removes an already available name from local context file.'; } constructor() { super(); _ContextOptionRemoveCommand_instances.add(this); __classPrivateFieldGet(this, _ContextOptionRemoveCommand_instances, "m", _ContextOptionRemoveCommand_initOptions).call(this); __classPrivateFieldGet(this, _ContextOptionRemoveCommand_instances, "m", _ContextOptionRemoveCommand_initTelemetry).call(this); } async commandAction(logger, args) { if (this.verbose) { await logger.logToStderr(`Removing context option '${args.options.name}'...`); } if (args.options.force) { await this.removeContextOption(args.options.name, logger); } else { const result = await cli.promptForConfirmation({ message: `Are you sure you want to remove the context option ${args.options.name}?` }); if (result) { await this.removeContextOption(args.options.name, logger); } } } async removeContextOption(name, logger) { const filePath = '.m365rc.json'; let m365rc = {}; if (fs.existsSync(filePath)) { try { if (this.verbose) { await logger.logToStderr(`Reading context file...`); } const fileContents = fs.readFileSync(filePath, 'utf8'); if (fileContents) { m365rc = JSON.parse(fileContents); } } catch (e) { throw new CommandError(`Error reading ${filePath}: ${e}. Please remove context option ${name} from ${filePath} manually.`); } } if (!m365rc.context || !m365rc.context[name]) { throw new CommandError(`There is no option ${name} in the context info`); } else { try { if (this.verbose) { await logger.logToStderr(`Removing context option ${name} from the context file...`); } delete m365rc.context[name]; fs.writeFileSync(filePath, JSON.stringify(m365rc, null, 2)); } catch (e) { throw new CommandError(`Error writing ${filePath}: ${e}. Please remove context option ${name} from ${filePath} manually.`); } } } } _ContextOptionRemoveCommand_instances = new WeakSet(), _ContextOptionRemoveCommand_initTelemetry = function _ContextOptionRemoveCommand_initTelemetry() { this.telemetry.push((args) => { Object.assign(this.telemetryProperties, { force: !!args.options.force }); }); }, _ContextOptionRemoveCommand_initOptions = function _ContextOptionRemoveCommand_initOptions() { this.options.unshift({ option: '-n, --name <name>' }, { option: '-f, --force' }); }; export default new ContextOptionRemoveCommand(); //# sourceMappingURL=option-remove.js.map