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.

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

const path = require('node:path');
const { parseArgs, validateFlags, emitResult } = require('./lib/dataverse-auth.js');
const { runNpm } = require('./lib/node-tool.js');
const { booleanFlagError } = require('./lib/pcf-cli-flags.js');
const {
  listTemplates,
  listRecipes,
  planScaffold,
  writeScaffold,
} = require('./lib/pcf-scaffold.js');
const { makeWritesBlocking } = require('./lib/output-redaction.js');

const USAGE = `Usage:
  node scripts/pcf-scaffold.js --list
  node scripts/pcf-scaffold.js --template <id> --namespace <Namespace> --name <ControlName> --out <dir> [--hosts model,pages] [--recipe <id>] [--display-name <text>] [--description <text>] [--install] [--npm-cli <path>]`;

const KNOWN = [
  'template',
  'namespace',
  'name',
  'out',
  'recipe',
  'hosts',
  'display-name',
  'description',
  'install',
  'list',
  'npm-cli',
];
const NEED_VALUE = ['template', 'namespace', 'name', 'out', 'recipe', 'hosts', 'display-name', 'description', 'npm-cli'];

function usageError(message) {
  makeWritesBlocking(process.stdout, process.stderr);
  process.stderr.write(`${USAGE}\n${message}\n`);
  process.exit(1);
}

function main(argv) {
  try {
    return runMain(argv);
  } catch (err) {
    if (err && err.exitCode !== undefined) throw err;
    return emitResult(false, err);
  }
}

function runMain(argv) {
  const parsed = parseArgs(argv);
  const flagError = validateFlags(argv, {
    known: KNOWN,
    needValue: NEED_VALUE,
    hints: {
      template: 'use --list to see available templates',
      hosts: 'comma-separated values: model,pages',
      'npm-cli': 'path to npm-cli.js',
    },
  });
  if (flagError) usageError(flagError);

  const { flags } = parsed;
  const booleanError = booleanFlagError(flags, ['install', 'list']);
  if (booleanError) usageError(booleanError);
  if (flags.list) {
    return emitResult(true, { ok: true, templates: listTemplates(), recipes: listRecipes() });
  }

  for (const required of ['template', 'namespace', 'name', 'out']) {
    if (!flags[required]) usageError(`--${required} is required`);
  }

  const outDir = path.resolve(String(flags.out));
  const recipe = flags.recipe ? String(flags.recipe) : undefined;
  const plan = planScaffold({
    template: String(flags.template),
    recipe,
    namespace: String(flags.namespace),
    name: String(flags.name),
    displayName: flags['display-name'] ? String(flags['display-name']) : undefined,
    description: flags.description ? String(flags.description) : undefined,
    hosts: flags.hosts ? String(flags.hosts).split(',') : ['model'],
  });
  const written = writeScaffold(plan, outDir);

  if (flags.install) {
    // Install into the physical directory. A linked --out would otherwise make npm ci follow
    // the link on its own, after the emptiness check had already used a different path.
    const install = runNpm(['ci'], { cwd: written.resolvedOutDir, npmCli: flags['npm-cli'] ? String(flags['npm-cli']) : undefined });
    if (install.status !== 0) {
      const detail = [install.stderr, install.stdout].filter(Boolean).join('\n').trim();
      throw new Error(`npm ci failed${detail ? `:\n${detail}` : ''}`);
    }
  }

  emitResult(true, {
    ok: true,
    outDir,
    resolvedOutDir: written.resolvedOutDir,
    findings: written.findings,
    template: String(flags.template),
    recipe: recipe || null,
    recipeTitle: plan.recipe ? plan.recipe.title : recipe || null,
    files: written.written,
    next: [
      'node node_modules/pcf-scripts/bin/pcf-scripts.js build --buildMode production',
      'node node_modules/pcf-scripts/bin/pcf-scripts.js lint',
      'node node_modules/jest/bin/jest.js --config jest.config.cjs --runInBand',
    ],
  });
}

if (require.main === module) {
  try {
    main(process.argv.slice(2));
  } catch (err) {
    emitResult(false, err);
  }
}

module.exports = { main };