@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
77 lines • 3.62 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 _AppCommand_instances, _AppCommand_initOptions, _AppCommand_initValidators;
import fs from 'fs';
import { cli } from '../../cli/cli.js';
import Command, { CommandError } from '../../Command.js';
import { validation } from '../../utils/validation.js';
import { formatting } from '../../utils/formatting.js';
class AppCommand extends Command {
get resource() {
return 'https://graph.microsoft.com';
}
constructor() {
super();
_AppCommand_instances.add(this);
__classPrivateFieldGet(this, _AppCommand_instances, "m", _AppCommand_initOptions).call(this);
__classPrivateFieldGet(this, _AppCommand_instances, "m", _AppCommand_initValidators).call(this);
}
async action(logger, args) {
const m365rcJsonPath = '.m365rc.json';
if (!fs.existsSync(m365rcJsonPath)) {
throw new CommandError(`Could not find file: ${m365rcJsonPath}`);
}
try {
const m365rcJsonContents = fs.readFileSync(m365rcJsonPath, 'utf8');
if (!m365rcJsonContents) {
throw new CommandError(`File ${m365rcJsonPath} is empty`);
}
this.m365rcJson = JSON.parse(m365rcJsonContents);
}
catch (err) {
if (err instanceof CommandError) {
throw err;
}
throw new CommandError(`Could not parse file: ${m365rcJsonPath}`);
}
if (!this.m365rcJson.apps ||
this.m365rcJson.apps.length === 0) {
throw new CommandError(`No Entra apps found in ${m365rcJsonPath}`);
}
if (args.options.appId) {
if (!this.m365rcJson.apps.some(app => app.appId === args.options.appId)) {
throw new CommandError(`App ${args.options.appId} not found in ${m365rcJsonPath}`);
}
this.appId = args.options.appId;
return super.action(logger, args);
}
if (this.m365rcJson.apps.length === 1) {
this.appId = this.m365rcJson.apps[0].appId;
return super.action(logger, args);
}
if (this.m365rcJson.apps.length > 1) {
this.m365rcJson.apps.forEach((app, index) => {
app.appIdIndex = index;
});
const resultAsKeyValuePair = formatting.convertArrayToHashTable('appId', this.m365rcJson.apps);
const result = await cli.handleMultipleResultsFound(`Multiple Entra apps found in ${m365rcJsonPath}.`, resultAsKeyValuePair);
this.appId = result.appId;
await super.action(logger, args);
}
}
}
_AppCommand_instances = new WeakSet(), _AppCommand_initOptions = function _AppCommand_initOptions() {
this.options.unshift({ option: '--appId [appId]' });
}, _AppCommand_initValidators = function _AppCommand_initValidators() {
this.validators.push(async (args) => {
if (args.options.appId && !validation.isValidGuid(args.options.appId)) {
return `${args.options.appId} is not a valid GUID`;
}
return true;
});
};
export default AppCommand;
//# sourceMappingURL=AppCommand.js.map