@pnp/cli-microsoft365
Version:
Manage Microsoft 365 and SharePoint Framework projects on any platform
161 lines • 7.53 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 _EntraAppGetCommand_instances, _EntraAppGetCommand_initTelemetry, _EntraAppGetCommand_initOptions, _EntraAppGetCommand_initValidators, _EntraAppGetCommand_initOptionSets;
import fs from 'fs';
import request from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
import { validation } from '../../../../utils/validation.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { cli } from '../../../../cli/cli.js';
import { entraApp } from '../../../../utils/entraApp.js';
class EntraAppGetCommand extends GraphCommand {
get name() {
return commands.APP_GET;
}
get description() {
return 'Gets an Entra app registration';
}
constructor() {
super();
_EntraAppGetCommand_instances.add(this);
__classPrivateFieldGet(this, _EntraAppGetCommand_instances, "m", _EntraAppGetCommand_initTelemetry).call(this);
__classPrivateFieldGet(this, _EntraAppGetCommand_instances, "m", _EntraAppGetCommand_initOptions).call(this);
__classPrivateFieldGet(this, _EntraAppGetCommand_instances, "m", _EntraAppGetCommand_initValidators).call(this);
__classPrivateFieldGet(this, _EntraAppGetCommand_instances, "m", _EntraAppGetCommand_initOptionSets).call(this);
}
async commandAction(logger, args) {
try {
const appObjectId = await this.getAppObjectId(args, logger);
const appInfo = await this.getAppInfo(appObjectId, args.options.properties);
const res = await this.saveAppInfo(args, appInfo, logger);
await logger.log(res);
}
catch (err) {
this.handleRejectedODataJsonPromise(err);
}
}
async getAppObjectId(args, logger) {
if (args.options.objectId) {
return args.options.objectId;
}
const { appId, name } = args.options;
if (this.verbose) {
await logger.logToStderr(`Retrieving information about Microsoft Entra app ${appId ? appId : name}...`);
}
if (appId) {
const app = await entraApp.getAppRegistrationByAppId(appId, ["id"]);
return app.id;
}
else {
const requestOptions = {
url: `${this.resource}/v1.0/myorganization/applications?$filter=displayName eq '${formatting.encodeQueryParameter(name)}'&$select=id`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};
const res = await request.get(requestOptions);
if (res.value.length === 1) {
return res.value[0].id;
}
if (res.value.length === 0) {
throw `No Microsoft Entra application registration with name ${name} found`;
}
const resultAsKeyValuePair = formatting.convertArrayToHashTable('id', res.value);
const result = await cli.handleMultipleResultsFound(`Multiple Microsoft Entra application registrations with name '${name}' found.`, resultAsKeyValuePair);
return result.id;
}
}
async getAppInfo(appObjectId, properties) {
const queryParameters = [];
if (properties) {
const allProperties = properties.split(',');
const selectProperties = allProperties.filter(prop => !prop.includes('/'));
if (selectProperties.length > 0) {
queryParameters.push(`$select=${selectProperties}`);
}
}
const queryString = queryParameters.length > 0
? `?${queryParameters.join('&')}`
: '';
const requestOptions = {
url: `${this.resource}/v1.0/myorganization/applications/${appObjectId}${queryString}`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};
return request.get(requestOptions);
}
async saveAppInfo(args, appInfo, logger) {
if (!args.options.save) {
return appInfo;
}
const filePath = '.m365rc.json';
if (this.verbose) {
await logger.logToStderr(`Saving Microsoft Entra app registration information to the ${filePath} file...`);
}
let m365rc = {};
if (fs.existsSync(filePath)) {
if (this.debug) {
await logger.logToStderr(`Reading existing ${filePath}...`);
}
try {
const fileContents = fs.readFileSync(filePath, 'utf8');
if (fileContents) {
m365rc = JSON.parse(fileContents);
}
}
catch (e) {
await logger.logToStderr(`Error reading ${filePath}: ${e}. Please add app info to ${filePath} manually.`);
return Promise.resolve(appInfo);
}
}
if (!m365rc.apps) {
m365rc.apps = [];
}
if (!m365rc.apps.some(a => a.appId === appInfo.appId)) {
m365rc.apps.push({
appId: appInfo.appId,
name: appInfo.displayName
});
try {
fs.writeFileSync(filePath, JSON.stringify(m365rc, null, 2));
}
catch (e) {
await logger.logToStderr(`Error writing ${filePath}: ${e}. Please add app info to ${filePath} manually.`);
}
}
return appInfo;
}
}
_EntraAppGetCommand_instances = new WeakSet(), _EntraAppGetCommand_initTelemetry = function _EntraAppGetCommand_initTelemetry() {
this.telemetry.push((args) => {
Object.assign(this.telemetryProperties, {
appId: typeof args.options.appId !== 'undefined',
objectId: typeof args.options.objectId !== 'undefined',
name: typeof args.options.name !== 'undefined',
properties: typeof args.options.properties !== 'undefined'
});
});
}, _EntraAppGetCommand_initOptions = function _EntraAppGetCommand_initOptions() {
this.options.unshift({ option: '--appId [appId]' }, { option: '--objectId [objectId]' }, { option: '--name [name]' }, { option: '--save' }, { option: '-p, --properties [properties]' });
}, _EntraAppGetCommand_initValidators = function _EntraAppGetCommand_initValidators() {
this.validators.push(async (args) => {
if (args.options.appId && !validation.isValidGuid(args.options.appId)) {
return `${args.options.appId} is not a valid GUID`;
}
if (args.options.objectId && !validation.isValidGuid(args.options.objectId)) {
return `${args.options.objectId} is not a valid GUID`;
}
return true;
});
}, _EntraAppGetCommand_initOptionSets = function _EntraAppGetCommand_initOptionSets() {
this.optionSets.push({ options: ['appId', 'objectId', 'name'] });
};
export default new EntraAppGetCommand();
//# sourceMappingURL=app-get.js.map