A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.
JavaScript claude-code-pluginclaude-code-skillscode-appsgithub-copilot-pluginpower-appspower-pagespower-platform
#!/usr/bin/env node
'use strict';
const path = require('node:path');
const { parseArgs, validateFlags } = require('./lib/dataverse-auth.js');
const { booleanFlagError } = require('./lib/pcf-cli-flags.js');
const { makeWritesBlocking } = require('./lib/output-redaction.js');
const {
WHERE_USED_CAVEAT,
makePcfSdk,
findCustomControl,
listCustomControls,
dependentsOf,
} = require('./lib/pcf-dataverse.js');
const USAGE = `Usage:
node scripts/pcf-inventory.js --env <url> [--control <name>] [--include-managed] [--where-used] [--workspace <dir>]`;
const KNOWN = ['env', 'control', 'include-managed', 'where-used', 'workspace'];
const NEED_VALUE = ['env', 'control', 'workspace'];
function usageError(message) {
makeWritesBlocking(process.stdout, process.stderr);
process.stderr.write(`${USAGE}\n${message}\n`);
process.exit(1);
}
async function main(argv = process.argv.slice(2)) {
const parsed = parseArgs(argv);
const flagError = validateFlags(argv, {
known: KNOWN,
needValue: NEED_VALUE,
hints: {
control: 'registered name, usually <publisherPrefix>_<namespace>.<constructor>',
workspace: 'directory for Dataverse SDK metadata cache',
},
});
if (flagError) usageError(flagError);
const { flags } = parsed;
if (!flags.env) usageError('--env is required');
const env = String(flags.env);
const workspace = flags.workspace ? String(flags.workspace) : path.resolve('.maker-workspace', 'pcf-inventory');
const booleanError = booleanFlagError(flags, ['include-managed', 'where-used']);
if (booleanError) usageError(booleanError);
const includeManaged = Boolean(flags['include-managed']);
const withWhereUsed = Boolean(flags['where-used']);
const output = { ok: true, controls: [] };
if (withWhereUsed) output.whereUsedCaveat = WHERE_USED_CAVEAT;
try {
const sdk = await makePcfSdk(env, workspace);
const controls = flags.control
? await readOneControl(sdk, String(flags.control))
: await readControlList(sdk, includeManaged);
if (!controls) {
const controlName = String(flags.control);
writeResult(false, {
...output,
ok: false,
error: missingControlMessage(controlName),
});
return;
}
for (const control of controls) {
const entry = inventoryRow(control);
if (withWhereUsed) {
entry.whereUsed = control.id
? await dependentsOf(sdk, control.id)
: { ok: false, reason: 'customcontrolid was missing from the customcontrol row' };
}
output.controls.push(entry);
}
} catch (err) {
if (err && err.exitCode !== undefined) throw err;
writeResult(false, { ...output, ok: false, error: errorMessage(err) });
return;
}
writeResult(true, output);
}
async function readOneControl(sdk, controlName) {
const row = await findCustomControl(sdk, controlName);
return row ? [row] : null;
}
async function readControlList(sdk, includeManaged) {
const rows = await listCustomControls(sdk, { includeManaged });
return includeManaged ? rows : rows.filter(isDefaultInventoryControl);
}
function inventoryRow(control) {
return {
name: control.name,
version: control.version,
isManaged: control.isManaged,
componentState: control.componentState,
};
}
function isDefaultInventoryControl(control) {
const name = String(control && control.name || '');
// First-party platform controls exist in every org and drown out the controls a maker usually
// owns; keep them hidden with the managed-solution rows unless --include-managed asks for the
// complete platform inventory.
return control && !control.isManaged && !name.startsWith('MscrmControls.') && !name.startsWith('Microsoft.');
}
function missingControlMessage(controlName) {
const expected = controlName.includes('_') ? '<publisherPrefix>_<namespace>.<constructor>' : `<publisherPrefix>_${controlName}`;
return `No PCF custom control named '${controlName}' was found. Registered controls use the prefixed form '${expected}' (for example, <publisherPrefix>_<namespace>.<constructor>), not just the manifest namespace and constructor.`;
}
function writeResult(ok, payload) {
// A listing of an org's controls can be megabytes, and the process exits right after printing it: blocking writes put the whole
// text in the pipe first (on Linux and macOS a pipe's writes are queued, and process.exit does not wait for them — lib/output-redaction.js).
makeWritesBlocking(process.stdout, process.stderr);
if (ok) {
process.stdout.write(JSON.stringify(payload) + '\n');
process.exit(0);
}
if (payload instanceof Error) {
const errorPayload = { ok: false, error: payload.message || String(payload) };
process.stdout.write(JSON.stringify(errorPayload) + '\n');
process.stderr.write(errorPayload.error + '\n');
} else if (payload !== null && typeof payload === 'object') {
process.stdout.write(JSON.stringify(payload) + '\n');
if (typeof payload.error === 'string' && payload.error.trim()) process.stderr.write(payload.error.trim() + '\n');
else process.stderr.write('Operation failed with an unstructured error; see stdout JSON\n');
} else {
process.stderr.write(String(payload) + '\n');
}
process.exit(1);
}
function errorMessage(err) {
return err && err.message ? String(err.message) : String(err);
}
if (require.main === module) {
main().catch((err) => writeResult(false, err));
}
module.exports = { main, isDefaultInventoryControl };