UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

62 lines 2.49 kB
import fs from 'fs'; import { z } from 'zod'; import { CommandError, globalOptionsZod } from '../../../../Command.js'; import ContextCommand from '../../../base/ContextCommand.js'; import commands from '../../commands.js'; export const options = z.strictObject({ ...globalOptionsZod.shape, name: z.string().alias('n'), value: z.string().alias('v') }); class ContextOptionSetCommand extends ContextCommand { get name() { return commands.OPTION_SET; } get description() { return 'Allows to add a new name for the option and value to the local context file.'; } get schema() { return options; } async commandAction(logger, args) { const filePath = '.m365rc.json'; if (this.verbose) { await logger.logToStderr(`Saving ${args.options.name} with value ${args.options.value} to the ${filePath} file...`); } let m365rc = {}; if (fs.existsSync(filePath)) { try { if (this.verbose) { await logger.logToStderr(`Reading existing ${filePath}...`); } const fileContents = fs.readFileSync(filePath, 'utf8'); if (fileContents) { m365rc = JSON.parse(fileContents); } } catch (e) { throw new CommandError(`Error reading ${filePath}: ${e}. Please add ${args.options.name} to ${filePath} manually.`); } } if (m365rc.context) { m365rc.context[args.options.name] = args.options.value; try { if (this.verbose) { await logger.logToStderr(`Creating option ${args.options.name} with value ${args.options.value} in existing context...`); } fs.writeFileSync(filePath, JSON.stringify(m365rc, null, 2)); } catch (e) { throw new CommandError(`Error writing ${filePath}: ${e}. Please add ${args.options.name} to ${filePath} manually.`); } } else { if (this.verbose) { await logger.logToStderr(`Context doesn't exist. Initializing the context and creating option ${args.options.name} with value ${args.options.value}...`); } this.saveContextInfo({ [args.options.name]: args.options.value }); } } } export default new ContextOptionSetCommand(); //# sourceMappingURL=option-set.js.map