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
// provision-entities CLI: provision Dataverse entities via the shared core.
// Validates the input, builds SDK clients, runs the shared provision functions, and prints
// a structured JSON result. Dry-run by default; --apply writes. --sample-data opt-in.
//
// Usage:
// node provision-entities.js --env <orgUrl> --input @<path> [--apply] [--sample-data] [--language-code|--languageCode <lcid>]
const fs = require('node:fs');
const path = require('node:path');
const { validateProvisionInput } = require('./lib/provision-input.js');
const { makeRunner, provisionSolution, provisionDataModel, provisionSampleData, relationshipsInCreateOrder, checkLocalizedLabelLanguages } = require('./lib/entity-provision.js');
const { quickCreateEnabledFor, normalizeLanguageCode, askedPrimaryNameLength, keyWaitsForNameLength } = require('./lib/app-spec.js');
const { createAzHttpClient } = require('./lib/sdk-http-client.js');
const { parseArgs, validateFlags, readAliasedFlag, readJsonArg, emitResult, readProvisionedLanguages } = require('./lib/dataverse-auth.js');
const { makeTempWorkspace } = require('./lib/temp-workspace.js');
const { makeWritesBlocking } = require('./lib/output-redaction.js');
// Construct the SDK against the vendored bundle + an az-token HttpClient. Two clients:
// sdk — carries solutionUniqueName (metadata + record writes auto-join the
// solution via the MSCRM.SolutionUniqueName header).
// provision — header-less; used for discovery reads (findTables/findColumns/
// fetchEntityMetadata/queryRecords).
async function makeSdk(env, input) {
const { createMakerSdk, createNodeWorkspaceStorage } = require('./lib/maker-sdk.js');
const httpClient = createAzHttpClient(env);
const sdkTempDir = makeTempWorkspace('provision-');
const provisionTempDir = makeTempWorkspace('provision-');
const cleanup = () => {
fs.rmSync(sdkTempDir, { recursive: true, force: true });
fs.rmSync(provisionTempDir, { recursive: true, force: true });
};
// Everything fallible after the directories exist runs INSIDE this guard, because the caller's
// `finally { cleanup() }` only becomes reachable once this function RETURNS — so anything that
// throws before the return strands both temp directories for the life of the machine.
//
// That deliberately includes the `createMakerSdk` CONSTRUCTORS, not just `initWorkspace`: the
// constructor now builds the injected-storage adapter (`createNodeWorkspaceStorage`), so it
// touches the filesystem and can fail on its own. Guarding only the init left the first
// construction outside the net. Matches provision-solution.js and ai-preflight.js, which already
// keep construction inside their protected region for this exact reason.
let sdk;
let provision;
try {
sdk = createMakerSdk({
workspaceStorage: createNodeWorkspaceStorage(sdkTempDir),
instanceUrl: env,
httpClient,
solutionUniqueName: input.solution && input.solution.uniqueName,
});
await sdk.initWorkspace();
provision = createMakerSdk({ workspaceStorage: createNodeWorkspaceStorage(provisionTempDir), instanceUrl: env, httpClient });
await provision.initWorkspace();
} catch (err) {
cleanup();
throw err;
}
return { sdk, provision, cleanup };
}
// Count the total steps in the plan for makeRunner's [n/total] narration.
function computeTotal(input, opts) {
let total = 1; // solution
// entities + columns + status reasons + alternate keys
for (const e of input.entities || []) {
total += 1; // table
// primary name column length step — MUST match provisionDataModel's condition (entity-provision.js) and buildPlan below:
// one narrated step (run, or skipped with the reason) whenever the input gives the name column a length.
if (askedPrimaryNameLength(e) !== undefined) total += 1;
// quick-create enable step — MUST match provisionDataModel's condition (entity-provision.js) and
// buildPlan below, or [n/total] drifts. This CLI's input has no forms[], so quickCreateEnabledFor
// fires only on an explicit entities[].quickCreate === true.
if (quickCreateEnabledFor(input, e)) total += 1;
total += (e.columns || []).filter((c) => c.type !== 'Lookup').length; // columns (Lookup comes from relationships)
total += (e.statusReasons || []).length; // status reasons
total += (e.alternateKeys || []).length; // alternate keys
}
// relationships
total += (input.relationships || []).length;
// global choices
total += (input.globalChoices || []).length;
// sample data (if requested)
if (opts.sampleData && input.sampleData) {
total += Object.keys(input.sampleData).length;
}
return total;
}
// Build a dry-run plan (list of steps that would be executed).
function buildPlan(input, opts) {
const plan = [];
plan.push(`solution ${input.solution.uniqueName}`);
for (const gc of input.globalChoices || []) {
plan.push(`global choice ${gc.name}`);
}
for (const e of input.entities || []) {
plan.push(`table ${e.schemaName}`);
if (quickCreateEnabledFor(input, e)) plan.push(`enable quick create on ${String(e.schemaName).toLowerCase()}`);
for (const c of e.columns || []) {
if (c.type !== 'Lookup') {
plan.push(`column ${e.schemaName}.${c.schemaName} (${c.type || 'Text'})`);
}
}
for (const sr of e.statusReasons || []) {
plan.push(`status reason ${e.schemaName}: ${sr.label}`);
}
for (const k of e.alternateKeys || []) {
if (!keyWaitsForNameLength(e, k)) plan.push(`alt key ${e.schemaName}.${k.schemaName}`);
}
}
// In the order the apply creates them — a lookup labelled in several languages goes first — so the plan lists
// the steps in the order the run takes them (lib/entity-provision.js relationshipsInCreateOrder).
for (const rel of relationshipsInCreateOrder(input)) {
if (rel.type === 'OneToMany') {
plan.push(`relationship 1:N ${rel.referenced}->${rel.referencing}`);
} else if (rel.type === 'ManyToMany') {
plan.push(`relationship N:N ${rel.entity1}<->${rel.entity2}`);
}
}
// After the relationships: where provisionDataModel sets the length of a table it creates (computeTotal counts the step too).
for (const e of input.entities || []) {
if (askedPrimaryNameLength(e) !== undefined) plan.push(`primary column length ${e.schemaName}.${e.primaryAttribute.schemaName}`);
}
// Then the alternate keys over a name column whose length is set: the apply makes such a key once that length is right
// (keyWaitsForNameLength, which depends on what the key's label is sent as and not on the language), where a table it creates has it.
// A key labelled in several languages that the environment made the name column too long to fit beside is made there too, which only the
// environment can say, so the plan lists it with its table and the apply makes it after the length step. computeTotal counts them with the other keys.
for (const e of input.entities || []) {
for (const k of e.alternateKeys || []) {
if (keyWaitsForNameLength(e, k)) plan.push(`alt key ${e.schemaName}.${k.schemaName}`);
}
}
if (opts.sampleData && input.sampleData) {
for (const [entityKey, records] of Object.entries(input.sampleData)) {
if (Array.isArray(records) && records.length > 0) {
plan.push(`${records.length} record(s) -> ${entityKey}`);
}
}
}
return plan;
}
// The testable core: validates, computes plan, runs the shared provision functions.
// `deps` injects { sdk, provision, emit, log, warn } so tests never touch the network.
async function provisionEntities(input, opts = {}, deps = {}) {
// 1. Validation gate first — if invalid, return errors with ZERO SDK writes.
const v = validateProvisionInput(input);
if (!v.ok) {
return { ok: false, errors: v.errors };
}
// 2. Dry-run default: without --apply, return a plan with NO SDK writes.
if (!opts.apply) {
const plan = buildPlan(input, opts);
return { ok: true, dryRun: true, plan };
}
// 3. Apply: run the shared core (solution → data-model → sample-data-if-requested).
const log = deps.log || (() => undefined);
const emit = deps.emit || (() => undefined);
const warn = deps.warn || (() => undefined);
const sdk = deps.sdk;
const provision = deps.provision;
const total = computeTotal(input, opts);
const runner = makeRunner({ emit, total });
// Convert input to spec shape expected by the shared core
const spec = {
solution: input.solution,
entities: input.entities,
relationships: input.relationships || [],
globalChoices: input.globalChoices || [],
sampleData: input.sampleData,
// Carried through so a caller can pin the label LCID here exactly as an App Spec can. Without
// it the field would be silently dropped on this path while working on the build path.
...(input.languageCode !== undefined ? { languageCode: input.languageCode } : {}),
};
// A localized label in a language the organization has not provisioned stops here, before the publisher and the
// solution are created, rather than in the data-model phase after them (same check and halt as there; an
// unreadable language list does not stop the build). The data-model phase is told, so the list is read once.
await checkLocalizedLabelLanguages(spec, deps.provisionedLanguages);
// Provision solution
await provisionSolution({ sdk, provision, runner, solution: spec.solution });
// Provision data model (entities, columns, relationships)
// `warn` is threaded so a fallback to the default label LCID is REPORTED here too. This CLI is the
// genpage create flow's data-model path, so without it a non-English org would hit the same opaque
// "language code 1033 is not a valid language for this organization" that #447 describes, with no
// diagnostic — the build path would explain itself and this one would not.
const dataModel = await provisionDataModel({
sdk,
provision,
runner,
spec,
apply: true,
languageCode: opts.languageCode,
// Same guard as the build path: an explicit override the org has not provisioned fails here,
// before any write, rather than half-succeeding (labels silently coerced to the base language,
// then a hard 400 on the first DateTime/Memo column).
provisionedLanguages: deps.provisionedLanguages,
localizedLabelsChecked: true,
warn,
});
// Provision sample data (if requested)
let sampleResult = { records: {} };
if (opts.sampleData && spec.sampleData) {
sampleResult = await provisionSampleData({
sdk,
provision,
runner,
spec,
dataModel,
});
}
// 4. Assemble the structured result from the captured maps.
// dataModel.entities is { [schemaName]: { logicalName, entitySetName, metadataId? } }
// We need to convert to the array shape: [{ schemaName, logicalName, entitySetName, metadataId? }]
const entities = Object.entries(dataModel.entities || {}).map(([schemaName, data]) => ({
schemaName,
logicalName: data.logicalName,
entitySetName: data.entitySetName,
metadataId: data.metadataId,
}));
// Build columns array from dataModel.columns (real captured values) with fallback to input-derived
const columns = [];
for (const e of spec.entities) {
const capturedCols = dataModel.columns[e.schemaName] || [];
const capturedMap = new Map(capturedCols.map((c) => [c.schemaName, c]));
for (const c of e.columns || []) {
if (c.type !== 'Lookup') {
const captured = capturedMap.get(c.schemaName);
if (captured) {
columns.push({
table: e.schemaName.toLowerCase(),
schemaName: c.schemaName,
logicalName: captured.logicalName,
metadataId: captured.metadataId,
});
} else {
// Fallback for existing/uncaptured columns
columns.push({
table: e.schemaName.toLowerCase(),
schemaName: c.schemaName,
logicalName: c.schemaName.toLowerCase(),
});
}
}
}
}
// Build relationships array from dataModel.relationships (real captured values) with fallback to input-derived
const relationships = [];
const capturedRelMap = new Map(dataModel.relationships.map((r) => [r.schemaName.toLowerCase(), r]));
for (const r of spec.relationships || []) {
let schema;
if (r.type === 'OneToMany') {
schema = r.schemaName || `${r.referenced}_${r.referencing}`;
const captured = capturedRelMap.get(schema.toLowerCase());
if (captured) {
relationships.push({
kind: '1n',
schemaName: captured.schemaName,
metadataId: captured.metadataId,
lookupLogicalName: captured.lookupLogicalName,
});
} else {
// Fallback for existing/uncaptured relationships
relationships.push({
kind: '1n',
schemaName: schema,
});
}
} else if (r.type === 'ManyToMany') {
schema = r.schemaName || `${r.entity1}_${r.entity2}`;
const captured = capturedRelMap.get(schema.toLowerCase());
if (captured) {
relationships.push({
kind: 'nn',
schemaName: captured.schemaName,
metadataId: captured.metadataId,
});
} else {
// Fallback for existing/uncaptured relationships
relationships.push({
kind: 'nn',
schemaName: schema,
});
}
}
}
return {
ok: true,
solution: spec.solution.uniqueName,
entities,
columns,
relationships,
records: sampleResult.records || {},
};
}
async function main() {
const argv = process.argv.slice(2);
const { positional, flags } = parseArgs(argv);
const USAGE = 'Usage: node provision-entities.js --env <url> --input @<path> [--apply] [--sample-data] [--language-code|--languageCode <lcid>]';
// A value-less `--language-code` must never fall through to the org default, and an unknown flag
// must never be dropped while swallowing the token after it.
const flagError = validateFlags(argv, {
known: ['env', 'input', 'apply', 'sample-data', 'language-code', 'languageCode'],
needValue: ['env', 'input', 'language-code', 'languageCode'],
});
if (flagError) {
makeWritesBlocking(process.stdout, process.stderr);
process.stderr.write(`✗ ${flagError}\n${USAGE}\n`);
process.exit(1);
}
const env = flags.env;
const inputArg = flags.input || positional[0];
if (!env || !inputArg) {
process.stderr.write(USAGE + '\n');
process.exit(1);
}
const inputPath = path.resolve(typeof inputArg === 'string' && inputArg.startsWith('@') ? inputArg.slice(1) : inputArg);
const input = readJsonArg('@' + inputPath);
const rawLang = readAliasedFlag(flags, 'language-code', 'languageCode');
let languageCode;
if (rawLang !== undefined) {
languageCode = normalizeLanguageCode(rawLang);
if (languageCode === null) {
makeWritesBlocking(process.stdout, process.stderr);
process.stderr.write(`✗ --language-code / --languageCode must be digits only, a positive integer LCID up to 65535 (got '${rawLang}')\n`);
process.exit(1);
}
}
const opts = {
apply: flags.apply === true,
sampleData: flags['sample-data'] === true,
languageCode,
};
// Construct SDK clients (offline until first call)
const { sdk, provision, cleanup } = await makeSdk(env, input);
let r;
try {
const deps = {
log: (m) => process.stderr.write(m + '\n'),
warn: (m) => process.stderr.write(`⚠ ${m}\n`),
sdk,
provision,
// Only consulted for an explicit override, and any failure resolves to null so a build never
// breaks on the diagnostic itself.
provisionedLanguages: () => readProvisionedLanguages(env),
};
r = await provisionEntities(input, opts, deps);
} finally {
cleanup();
}
// emitResult() calls process.exit(), so emit AFTER cleanup() has run.
emitResult(r.ok, r);
}
if (require.main === module) {
main().catch((err) => emitResult(false, err));
}
module.exports = { provisionEntities };