Skip to content

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.

7.4 KB
Raw Blame
#!/usr/bin/env node
'use strict';

function fail(message) {
  const error = new Error(message);
  error.code = 'MOBILE_RELEASE_BLOCKED';
  throw error;
}

function main(argv) {
  const {
    selectRelease, resolveProjectRelease, selectProjectRelease, summarizeRelease,
  } = require('./lib/mobile-release');
  const values = {};
  const flags = new Set(['--project-root', '--release', '--host-version', '--template-version', '--default', '--platform', '--base-version', '--base-fingerprint', '--base-runtime-version', '--requirements-only', '--local-development']);
  for (let index = 0; index < argv.length; index += 1) {
    const flag = argv[index];
    if (!flags.has(flag) || Object.hasOwn(values, flag)) fail('Unknown or duplicate release resolver argument.');
    if (flag === '--default' || flag === '--requirements-only' || flag === '--local-development') {
      values[flag] = true;
    } else {
      const value = argv[++index];
      if (!value || value.startsWith('--')) fail('A release resolver argument is missing its value.');
      values[flag] = value;
    }
  }
  if ([values['--release'], values['--host-version'], values['--default']].filter(Boolean).length > 1) {
    fail('Choose one host version selection, not multiple selectors.');
  }
  if (values['--template-version'] && (values['--release'] || values['--host-version'] || values['--default'])) {
    fail('Select the host or the template package, not both.');
  }
  if (!values['--project-root'] && !values['--release'] && !values['--host-version'] && !values['--template-version'] && !values['--default']) {
    fail('Use --project-root DIR, --host-version VERSION, --template-version VERSION, or --default.');
  }
  if (values['--platform'] && !['android', 'ios'].includes(values['--platform'])) fail('Deployment platform must be android or ios.');
  if ((values['--base-version'] || values['--base-fingerprint'] || values['--base-runtime-version']) && !values['--platform']) {
    fail('Checking a deployment base requires --platform android or ios.');
  }
  if (values['--platform'] && (!values['--base-version'] || !values['--base-fingerprint'])) {
    fail('Deployment checks require both --base-version and --base-fingerprint from the actual selected platform base. Version alone does not verify native compatibility.');
  }
  if (values['--platform'] && !values['--project-root']) {
    fail('Deployment-base validation requires --project-root to verify the app metadata, lock and installed native dependencies.');
  }
  if (values['--requirements-only']) {
    if (!values['--project-root']) fail('Read-only --requirements-only planning requires --project-root DIR.');
    if (['--release', '--host-version', '--template-version', '--default', '--platform', '--base-version', '--base-fingerprint', '--base-runtime-version'].some((flag) => values[flag])) {
      fail('Requirements-only planning cannot assert a selected release or check deployment targets. Use strict project resolution for admission and deployment.');
    }
  }
  if (values['--local-development']
    && (!values['--project-root'] || ['--platform', '--requirements-only', '--release', '--host-version', '--template-version', '--default'].some((flag) => values[flag]))) {
    fail('Local-development diagnostics require --project-root and cannot select a release or authorize deployment.');
  }
  if (values['--local-development']) {
    const fs = require('node:fs');
    if (!fs.existsSync(values['--project-root']) || !fs.statSync(values['--project-root']).isDirectory()) {
      fail('Local-development diagnostics require an existing project directory.');
    }
    const summary = {
      validationScope: 'local-development-only', packageCompatibility: 'unverified',
      warnings: ['Native compatibility is not checked; local installation/startup may continue. Use /debug-app if Dev Player fails.'],
    };
    // Read recorded requirements only: local startup must not wait on registry
    // access, inspect the dependency tree, or pretend to admit a native release.
    try {
      const source = selectProjectRelease(values['--project-root']);
      summary.templateVersion = source.templateVersion;
      summary.nativeRuntimeVersions = source.nativeRuntimeVersions;
      summary.hostVersion = source.hostVersion;
    } catch (error) {
      if (error.code !== 'MOBILE_RELEASE_BLOCKED') throw error;
      summary.warnings.unshift(error.message);
    }
    process.stderr.write(`${summary.warnings.map((warning) => `WARNING: ${warning}`).join('\n')}\n`);
    process.stdout.write(`${JSON.stringify(summary, null, 2)}\n`);
    return;
  }

  let selected = null;
  const context = values['--project-root'] ? { cwd: values['--project-root'] } : {};
  if (values['--template-version']) {
    selected = require('./lib/mobile-published-packages').selectTemplateContract(values['--template-version'], context);
  } else if (values['--release'] || values['--host-version'] || values['--default']) {
    const version = values['--host-version'] || values['--release']
      || require('./lib/mobile-published-packages').createSource(context).defaultVersion();
    selected = selectRelease(version, undefined, context);
  }
  let current = null;
  if (values['--project-root']) {
    current = values['--requirements-only']
      ? selectProjectRelease(values['--project-root'])
      : resolveProjectRelease(values['--project-root'], undefined, undefined, {
          templateVersion: values['--template-version'],
        });
  }
  if (selected && current && selected.id !== current.id) fail('The project does not match the exact selected verified release.');
  const release = current || selected;
  if (values['--base-version'] && release.source !== 'npm'
    && release.platforms[values['--platform']].base.version !== values['--base-version']) {
    fail('The deployment base does not match the selected platform in the verified release.');
  }
  if (values['--base-fingerprint'] && release.platforms[values['--platform']].fingerprint !== values['--base-fingerprint']) {
    fail('The deployment base fingerprint does not match the selected platform in the verified release. Do not deploy with an unverified native base.');
  }
  const summary = summarizeRelease(release);
  if (values['--platform'] && release.source === 'npm') {
    const runtime = values['--base-runtime-version'];
    if (!/^[1-9]\d*$/.test(runtime || '') || !Number.isSafeInteger(Number(runtime))
      || Number(runtime) !== release.nativeRuntimeVersions[values['--platform']]) {
      fail('Published package admission also requires the actual selected base runtime version; it must match the host contract.');
    }
    if (!/^[a-zA-Z0-9][a-zA-Z0-9._+-]{0,127}$/.test(values['--base-version'])) fail('Invalid selected base version.');
    summary.deploymentBase = {
      platform: values['--platform'], version: values['--base-version'],
      nativeRuntimeVersion: Number(runtime), fingerprint: values['--base-fingerprint'],
    };
    summary.validationScope = 'app-and-base-native-contract; regional availability must be verified by Wrap';
  }
  if (values['--requirements-only']) summary.validationScope = 'requirements-only';
  process.stdout.write(`${JSON.stringify(summary, null, 2)}\n`);
}

try {
  main(process.argv.slice(2));
} catch (error) {
  const message = error.code === 'MOBILE_RELEASE_BLOCKED'
    ? error.message : 'Unable to verify published package compatibility. Check registry access and the project installation.';
  process.stderr.write(`BLOCKED: ${message}\n`);
  process.exitCode = 2;
}