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.

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

const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { createHash } = require('node:crypto');
const { isDeepStrictEqual } = require('node:util');
const { runCliSync, CliLaunchError } = require('./lib/process-runner');
const { withNpmContext } = require('./lib/npm-context');
const { planPublishedUpdate, readSourceRequirements } = require('./lib/mobile-published-packages');
const {
  selectRelease,
  summarizeRelease,
  dependencySections,
  inspectInstalledPackages,
  isRuntimePackage,
} = require('./lib/mobile-release');

const HOST = '@microsoft/power-apps-native-host';
const TEMPLATE = '@microsoft/power-apps-native-template';

function readJson(file) {
  try {
    return JSON.parse(fs.readFileSync(file, 'utf8'));
  } catch {
    throw new Error(`Cannot read valid ${path.basename(file)}; restore it before continuing`);
  }
}

function assertEmptyDestination(destination) {
  const stat = fs.lstatSync(destination, { throwIfNoEntry: false });
  if (stat && (!stat.isDirectory() || stat.isSymbolicLink() || fs.readdirSync(destination).length)) {
    throw new Error('Creation requires a new or empty real directory; existing apps are never overwritten');
  }
}

function run(command, args, cwd, { env = process.env, failureMessage } = {}) {
  const isNpm = command === 'npm';
  const configuredTimeoutMs = Number.parseInt(process.env.MOBILE_TEMPLATE_NPM_TIMEOUT_MS ?? '', 10);
  const timeoutMs = Number.isFinite(configuredTimeoutMs) && configuredTimeoutMs > 0
    ? configuredTimeoutMs
    : 10 * 60 * 1000;
  try {
    return runCliSync(command, args, {
      cwd,
      encoding: 'utf8',
      timeout: timeoutMs,
      maxBuffer: 4 * 1024 * 1024,
      env: { ...env, npm_config_ignore_scripts: 'true' },
    });
  } catch (error) {
    if (!(error instanceof CliLaunchError) && !(Number.isInteger(error.status) && error.status > 0)) throw error;
    throw new Error(failureMessage || `${isNpm ? 'Template package installation' : 'Template CLI'} failed; check registry access, slow network conditions, package support locally, or increase MOBILE_TEMPLATE_NPM_TIMEOUT_MS if the command is timing out (raw output withheld)`);
  }
}

function assertTemplateManifest(manifest, release) {
  if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest)
    || manifest.schemaVersion !== 1
    || manifest.templatePackage?.name !== TEMPLATE
    || manifest.templatePackage?.version !== release.template.version
    || manifest.templateVersion !== release.templateVersion
    || ['android', 'ios'].some((platform) => (
      manifest.nativeRuntimeVersions?.[platform] !== release.nativeRuntimeVersions[platform]
    ))) {
    throw new Error('Packaged template manifest does not match the reviewed release');
  }
}

function assertSelfContainedTemplate(installed, lock, packageRoot) {
  if (['dependencies', 'optionalDependencies', 'peerDependencies'].some(section => (
    Object.keys(installed[section] || {}).length > 0
  )) || installed.bundleDependencies === true || installed.bundledDependencies === true
    || installed.bundleDependencies?.length || installed.bundledDependencies?.length
    || Object.keys(lock.packages || {}).some(name => name && name !== `node_modules/${TEMPLATE}`)
    || fs.existsSync(path.join(packageRoot, 'node_modules'))) {
    throw new Error('Template creation requires a self-contained CLI with no acquired or bundled dependencies.');
  }
  const modules = path.dirname(path.dirname(packageRoot));
  if (fs.readdirSync(modules).some(name => !['.bin', '.package-lock.json', '@microsoft'].includes(name))
    || fs.readdirSync(path.join(modules, '@microsoft')).some(name => name !== 'power-apps-native-template')) {
    throw new Error('The template tooling installation contains unexpected packages; no creation CLI was executed.');
  }
}

function inspectDestination(destination) {
  const target = path.resolve(destination);
  assertEmptyDestination(target);
  let parent = path.dirname(target);
  while (!fs.lstatSync(parent, { throwIfNoEntry: false })) parent = path.dirname(parent);
  if (!fs.statSync(parent).isDirectory()) throw new Error('Creation requires an existing directory ancestor.');
  const canonicalParent = fs.realpathSync.native(parent);
  const resolved = path.resolve(canonicalParent, path.relative(parent, target));
  assertEmptyDestination(resolved);
  return { parent: canonicalParent, destination: resolved, exists: fs.existsSync(resolved) };
}

function acquireTemplate(destination, releaseId, options = {}) {
  const release = options.catalog ? selectRelease(releaseId, options.catalog)
    : require('./lib/mobile-published-packages').selectTemplateContract(releaseId, options);
  const target = path.resolve(destination);
  assertEmptyDestination(target);
  const execute = options.run || run;
  const staging = fs.mkdtempSync(path.join(os.tmpdir(), 'mobile-template-package-'));
  fs.chmodSync(staging, 0o700);
  try {
    fs.writeFileSync(path.join(staging, 'package.json'), '{"name":"template-acquisition","private":true}\n');
    withNpmContext(options.cwd || process.cwd(), staging, (env, cwd) => execute('npm', [
      'install', '--ignore-scripts', '--no-audit', '--no-fund', '--save-exact',
      `${TEMPLATE}@${release.template.version}`, '--prefix', staging,
    ], cwd, { env }));
    const packageRoot = path.join(staging, 'node_modules', TEMPLATE);
    if (!fs.lstatSync(packageRoot).isDirectory() || fs.lstatSync(packageRoot).isSymbolicLink()) {
      throw new Error('Template must be an installed registry package, not a symbolic link');
    }
    const installed = readJson(path.join(packageRoot, 'package.json'));
    const lock = readJson(path.join(staging, 'package-lock.json'));
    const locked = lock.packages?.[`node_modules/${TEMPLATE}`];
    if (installed.name !== TEMPLATE || installed.version !== release.template.version
      || locked?.version !== release.template.version || locked?.integrity !== release.template.integrity) {
      throw new Error('Installed template version/integrity does not match the reviewed release');
    }
    assertSelfContainedTemplate(installed, lock, packageRoot);
    const bin = installed.bin?.['create-power-app-native'];
    if (typeof bin !== 'string' || path.isAbsolute(bin) || bin.split(/[\\/]/).includes('..')) {
      throw new Error('Template package does not expose the supported creation CLI');
    }
    const executable = path.join(packageRoot, bin);
    const relative = path.relative(fs.realpathSync(packageRoot), fs.realpathSync(executable));
    if (relative.startsWith('..') || path.isAbsolute(relative) || !fs.lstatSync(executable).isFile()) {
      throw new Error('Template CLI must be a regular file inside its package');
    }
    let manifest;
    try {
      manifest = JSON.parse(execute(process.execPath, [executable, '--manifest'], staging));
    } catch {
      throw new Error('Template CLI inspection failed or returned an unsupported manifest');
    }
    assertTemplateManifest(manifest, release);
    assertEmptyDestination(target);
    execute(process.execPath, [executable, target], staging);
    const created = readJson(path.join(target, 'app.json')).expo?.extra?.powerappsNative;
    assertTemplateManifest({ ...created, templatePackage: manifest.templatePackage }, release);
    return { release: summarizeRelease(release), installedDependencies: false };
  } finally {
    // This is only the unique temporary package install, never the customer's target.
    fs.rmSync(staging, { recursive: true, force: true });
  }
}

function acquireHost(destination, version, options = {}) {
  const target = path.resolve(destination);
  assertEmptyDestination(target);
  const published = require('./lib/mobile-published-packages');
  const source = published.createSource(options);
  const contract = published.selectPublishedContract(version, {
    ...options, readPackage: source.read, versions: source.versions,
  });
  const profile = source.read(HOST, version).profile;
  const execute = options.run || run;
  assertEmptyDestination(target);
  fs.mkdirSync(target, { recursive: true });
  withNpmContext(options.cwd || process.cwd(), target, (env, cwd) => {
    fs.writeFileSync(path.join(target, 'package.json'), `${JSON.stringify({
      name: 'mobile-upgrade-tooling', private: true,
      ...contract.managedDependencies,
      dependencies: { ...contract.managedDependencies.dependencies, [HOST]: version },
      ...(profile.managedOverrides ? { overrides: profile.managedOverrides } : {}),
    }, null, 2)}\n`, { flag: 'wx' });
    return execute('npm', [
      'install', '--ignore-scripts', '--strict-peer-deps', '--no-legacy-peer-deps', '--no-force', '--no-audit', '--no-fund',
      '--prefix', target,
    ], cwd, { env });
  });
  const hostRoot = path.join(target, 'node_modules', HOST);
  const installed = readJson(path.join(hostRoot, 'package.json'));
  const locked = readJson(path.join(target, 'package-lock.json')).packages?.[`node_modules/${HOST}`];
  if (installed.name !== HOST || installed.version !== version || locked?.version !== version
    || locked.integrity !== contract.host.integrity || locked.link
    || !fs.lstatSync(hostRoot).isDirectory() || fs.lstatSync(hostRoot).isSymbolicLink()) {
    throw new Error('The tooling installation does not match the selected published host integrity');
  }
  const executable = installed.bin?.['upgrade-template'];
  if (typeof executable !== 'string' || path.isAbsolute(executable) || executable.split(/[\\/]/).includes('..')) {
    throw new Error('The published host does not expose a contained upgrade-template CLI');
  }
  const relative = path.relative(fs.realpathSync(hostRoot), fs.realpathSync(path.join(hostRoot, executable)));
  if (relative.startsWith('..') || path.isAbsolute(relative) || !fs.lstatSync(path.join(hostRoot, executable)).isFile()) {
    throw new Error('The upgrade CLI must be a regular file inside the published host package');
  }
  return {
    target: summarizeRelease(contract), installedDependencies: true, lifecycleScriptsExecuted: false,
    upgradeCli: `node_modules/${HOST}/${executable.replaceAll('\\', '/')}`,
  };
}

function restorePreviewDependencies(projectRoot, previewRoot, options = {}) {
  const source = fs.realpathSync.native(path.resolve(projectRoot));
  const preview = fs.realpathSync.native(path.resolve(previewRoot));
  const related = (parent, child) => {
    const relative = path.relative(parent, child);
    return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative));
  };
  if (related(source, preview) || related(preview, source)) {
    throw new Error('Source and preview must be separate, non-nested directories; source dependencies cannot be restored during preview.');
  }
  const modules = path.join(preview, 'node_modules');
  if (fs.lstatSync(modules, { throwIfNoEntry: false })?.isSymbolicLink()) {
    throw new Error('Preview node_modules must not be a symbolic link.');
  }
  const snapshots = new Map();
  for (const name of ['package.json', 'package-lock.json', 'app.json', '.npmrc']) {
    const sourcePath = path.join(source, name);
    const previewPath = path.join(preview, name);
    if (name === '.npmrc' && !fs.existsSync(sourcePath) && !fs.existsSync(previewPath)) continue;
    for (const file of [sourcePath, previewPath]) {
      if (!fs.lstatSync(file).isFile() || fs.lstatSync(file).isSymbolicLink()) {
        throw new Error('Source and preview inputs must be regular files.');
      }
    }
    const original = fs.readFileSync(sourcePath);
    if (!original.equals(fs.readFileSync(previewPath))) {
      throw new Error(`Preview ${name} differs from the source; create and review a fresh snapshot.`);
    }
    snapshots.set(name, original);
  }
  const manifest = readJson(path.join(source, 'package.json'));
  const lock = readJson(path.join(source, 'package-lock.json'));
  if (manifest.workspaces || fs.existsSync(path.join(source, 'npm-shrinkwrap.json'))
    || (manifest.packageManager && !manifest.packageManager.startsWith('npm@'))
    || ![2, 3].includes(lock.lockfileVersion) || !lock.packages?.['']) {
    throw new Error('Preview dependency restore requires a standalone npm package-lock project.');
  }
  const execute = options.run || run;
  try {
    withNpmContext(source, preview, (env, cwd) => execute('npm', [
      'ci', '--ignore-scripts', '--no-legacy-peer-deps', '--no-force', '--no-audit', '--no-fund',
      '--prefix', preview,
    ], cwd, { env }));
  } finally {
    for (const [name, content] of snapshots) {
      if (!content.equals(fs.readFileSync(path.join(source, name)))
        || !content.equals(fs.readFileSync(path.join(preview, name)))) {
        throw new Error(`Preview dependency restore changed ${name}; preserve the snapshot and investigate before continuing.`);
      }
    }
  }
  return { previewRoot: preview, installedDependencies: true, sourceModified: false, lifecycleScriptsExecuted: false };
}

function checkSdk(projectRoot, options = {}) {
  const root = fs.realpathSync.native(path.resolve(projectRoot));
  const expoRoot = fs.realpathSync.native(path.join(root, 'node_modules/expo'));
  const expo = readJson(path.join(expoRoot, 'package.json'));
  const bin = typeof expo.bin === 'string' ? expo.bin : expo.bin?.expo;
  if (expo.name !== 'expo' || typeof bin !== 'string' || !bin || path.isAbsolute(bin)) {
    throw new Error('SDK validation requires the project\'s installed Expo CLI; automatic acquisition is not allowed.');
  }
  const cli = fs.realpathSync.native(path.resolve(expoRoot, bin));
  const relative = path.relative(expoRoot, cli);
  if (!relative || relative.startsWith(`..${path.sep}`) || relative === '..' || path.isAbsolute(relative)
    || !fs.statSync(cli).isFile()) {
    throw new Error('The installed Expo CLI must be contained in its package.');
  }
  const output = (options.run || run)(process.execPath, [cli, 'install', '--check'], root, {
    env: { ...process.env, EXPO_OFFLINE: '1' },
    failureMessage: 'Installed Expo SDK validation failed. Preserve the release pins and investigate the local compatibility mismatch; do not install newer online recommendations (raw output withheld).',
  });
  return { projectRoot: root, expoVersion: expo.version, versionSource: 'installed-expo', output: output.trim() };
}

function snapshotNativeDependencies(projectRoot) {
  const root = fs.realpathSync.native(path.resolve(projectRoot));
  const requirements = readSourceRequirements(root);
  const installed = inspectInstalledPackages(root);
  const declarations = dependencySections(readJson(path.join(root, 'package.json')));
  const nativeDeclarations = {};
  for (const [section, dependencies] of Object.entries(declarations)) {
    nativeDeclarations[section] = Object.fromEntries(Object.entries(dependencies).filter(([name]) => {
      const actual = installed.get(`node_modules/${name}`);
      if (isRuntimePackage(name) && !actual) {
        throw new Error('Restore the missing native/runtime dependency before preparing a JavaScript-only update.');
      }
      return isRuntimePackage(name) || actual?.native;
    }));
    if (!Object.keys(nativeDeclarations[section]).length) delete nativeDeclarations[section];
  }
  return {
    schemaVersion: 1,
    projectRoot: root,
    requirements,
    nativeDeclarations,
    nativePackages: Object.fromEntries([...installed].filter(([, item]) => item.native)
      .map(([installPath, item]) => [installPath, {
        name: item.canonicalName,
        version: item.version,
        manifestSha256: createHash('sha256').update(JSON.stringify(item.pkg)).digest('hex'),
      }])),
  };
}

function checkNativeDependencies(projectRoot, baselinePath) {
  if (!fs.lstatSync(baselinePath).isFile() || fs.lstatSync(baselinePath).isSymbolicLink()) {
    throw new Error('The JavaScript update baseline must be a regular snapshot file.');
  }
  const baseline = readJson(baselinePath);
  const current = snapshotNativeDependencies(projectRoot);
  if (baseline?.schemaVersion !== 1 || baseline.projectRoot !== current.projectRoot) {
    throw new Error('The native dependency snapshot is invalid or belongs to another app.');
  }
  if (!isDeepStrictEqual(baseline, current)) {
    throw new Error('JavaScript-only update changed native/runtime dependencies or app requirements. Restore the package snapshot and its installation before continuing.');
  }
  return { nativeDependenciesUnchanged: true };
}

function main(argv) {
  const [action, ...args] = argv;
  const values = {};
  const allowed = action === 'check-sdk' ? ['--project-root']
    : ['snapshot-native', 'check-native'].includes(action) ? ['--project-root', '--baseline']
    : action === 'restore-preview' ? ['--project-root', '--preview-root']
    : action === 'destination' ? ['--destination'] : action === 'acquire'
    ? ['--destination', '--release', '--host-version', '--template-version']
    : ['--project-root', '--release', '--host-version'];
  if (!['acquire', 'plan', 'destination', 'restore-preview', 'check-sdk', 'snapshot-native', 'check-native'].includes(action)) throw new Error('Use acquire, plan, destination, restore-preview, check-sdk, snapshot-native or check-native with explicit arguments; the published host CLI owns migration writes');
  for (let index = 0; index < args.length; index += 2) {
    const key = args[index];
    if (!allowed.includes(key) || values[key] !== undefined || !args[index + 1]
      || args[index + 1].startsWith('--')) throw new Error('Unknown, duplicate, or missing lifecycle argument');
    values[key] = args[index + 1];
  }
  if (action === 'snapshot-native' || action === 'check-native') {
    if (!values['--project-root'] || !values['--baseline']) {
      throw new Error('Required: --project-root and --baseline for the JavaScript-only update snapshot.');
    }
    if (action === 'check-native') return checkNativeDependencies(values['--project-root'], values['--baseline']);
    const snapshot = snapshotNativeDependencies(values['--project-root']);
    const baseline = path.resolve(values['--baseline']);
    const relative = path.relative(snapshot.projectRoot, fs.realpathSync.native(path.dirname(baseline)));
    if (relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
      throw new Error('Save the native dependency snapshot inside this app recovery directory.');
    }
    fs.writeFileSync(baseline, `${JSON.stringify(snapshot, null, 2)}\n`, { flag: 'wx', mode: 0o600 });
    return { snapshotCreated: true };
  }
  if (action === 'check-sdk') {
    if (!values['--project-root']) throw new Error('Required: --project-root for installed SDK validation.');
    return checkSdk(values['--project-root']);
  }
  if (action === 'restore-preview') {
    if (!values['--project-root'] || !values['--preview-root']) {
      throw new Error('Required: --project-root and --preview-root for an approved isolated snapshot.');
    }
    return restorePreviewDependencies(values['--project-root'], values['--preview-root']);
  }
  if (action === 'destination') {
    if (!values['--destination']) throw new Error('Required: --destination for read-only creation inspection');
    return inspectDestination(values['--destination']);
  }
  if (!values[allowed[0]] || ['--release', '--host-version', '--template-version']
    .filter((key) => values[key]).length !== 1) {
    throw new Error(`Required: ${allowed[0]} and one exact host/template version`);
  }
  if (action === 'plan') return planPublishedUpdate(path.resolve(values['--project-root']), values['--host-version'] || values['--release']);
  return values['--host-version']
    ? acquireHost(values['--destination'], values['--host-version'])
    : acquireTemplate(values['--destination'], values['--template-version'] || values['--release']);
}

if (require.main === module) {
  try {
    process.stdout.write(`${JSON.stringify(main(process.argv.slice(2)), null, 2)}\n`);
  } catch (error) {
    process.stderr.write(`BLOCKED: ${error.message}\n`);
    process.exitCode = 2;
  }
}

module.exports = {
  acquireTemplate, acquireHost, assertEmptyDestination, assertTemplateManifest, checkSdk,
  snapshotNativeDependencies, checkNativeDependencies, inspectDestination, main, restorePreviewDependencies,
};