Skip to content

An open source design system that's fully customizable and agent ready

TypeScript
3.1 KB
Raw Blame
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
 * @file @astryxdesign/cli postinstall — nudge to run `astryx init`.
 *
 * Enforcement layer 2 of making `astryx init` foolproof: when the CLI is
 * installed as a project dependency and the project hasn't run init yet, print a
 * one-line next-step so agents/humans discover it.
 *
 * Reuses the ONE setup check (isAstryxPromptInReach) from the dependency-free leaf
 * ../foundation/agent-docs/agent-doc-state.mjs — node builtins only, so it is
 * genuinely safe to load at install time (importing agent-docs.mjs would drag in
 * the full CLI module graph). Non-interactive, never fails the install, and
 * stays quiet:
 *   - in the monorepo/source build (not under node_modules),
 *   - during npx's transient fetch (npx runs the bin — likely `init` — right
 *     after, so nudging here would double up), and
 *   - once setup is already done.
 */

import {fileURLToPath, pathToFileURL} from 'node:url';
// The ONE setup contract, shared with core's postinstall (which ships a
// generated byte-for-byte copy). Node builtins only, and in this package, so it
// is safe to load at install time.
import {
  shouldNudge,
  setupNudge,
} from '../foundation/agent-docs/agent-doc-state.mjs';

const HERE = fileURLToPath(import.meta.url);

/**
 * Re-exported so this layer's tests, and anything already importing it from
 * here, keep working. The implementation lives in the shared contract.
 */
export {shouldNudge};

/** @param {string} root @returns {Promise<boolean>} */
async function projectIsSetUp(root) {
  try {
    // Dep-free leaf (node builtins only) — safe to load mid-install. NOT
    // agent-docs.mjs, whose static graph (component discovery, package-manager
    // detection, …) isn't guaranteed importable yet during this postinstall.
    const {isAstryxPromptInReach} = await import(
      '../foundation/agent-docs/agent-doc-state.mjs'
    );
    return isAstryxPromptInReach(root);
  } catch {
    return false; // best-effort — if the check can't load, fall through and nudge
  }
}

/**
 * The stem the CLI's picker prints for this project. At install time no
 * integration is loaded, so this is the package-manager guess. Falls back to
 * the contract's scoped form when the picker cannot load.
 * @param {string} root
 * @returns {Promise<string | undefined>}
 */
export async function cliInvocation(root) {
  try {
    // Node builtins only, like the contract.
    const {getCliInvocation} = await import(
      '../foundation/env/package-manager.mjs'
    );
    return getCliInvocation(root);
  } catch {
    return undefined;
  }
}

async function main() {
  const root = process.env.INIT_CWD || process.cwd();
  const nudge = shouldNudge({
    scriptPath: HERE,
    npmCommand: process.env.npm_command,
    isSetUp: await projectIsSetUp(root),
  });
  if (nudge) {
    process.stdout.write(setupNudge(await cliInvocation(root)));
  }
}

// Run only when executed directly (`node scripts/postinstall.mjs`), never when
// imported by tests.
if (import.meta.url === pathToFileURL(process.argv[1] || '').href) {
  main()
    .catch(() => {})
    .finally(() => process.exit(0));
}