DocsIntegratingGetting started

Getting Started

Learn how to integrate your CLI directly with Fig to have specs always up-to date and versioned.

Steps

Integrating with the autocomplete repo requires to automatically generate new spec versions, merge or diff them with the currently available spec and then push it to the autocomplete repo. We set up some tools that allow to do all this stuff by writing almost zero code.

Case of use (@withfig/autocomplete-tools)

In this example we examine how we integrated @withfig/autocomplete-tools CLI with the autocomplete repo:

  1. We set up spec generation:

    • We install @fig/complete-commander integration to be used along with the Commander.js CLI framework:
    import { Command } from "commander";
    import { generateSpec } from "@fig/complete-commander";
    
    const program = new Command();
    
    // ... autocomplete-tools commands
    
    program.command("generate-fig-spec").action(() => {
      console.log(generateSpec(program));
    });
    
    program.parse(process.argv);
    
    • We also create a new npm script capable of generating the spec on the CI:
    "scripts": {
      "generate-fig-spec": "npm run build && node build/index.js generate-fig-spec > generated/autocomplete-tools-spec.ts"
    }
    
    • We add generated/autocomplete-tools-spec.ts to .gitignore
    node_modules
    ...
    generated/autocomplete-tools-spec.ts
    
  2. Now that we added the ability to autogenerate new specs we can use the push-to-autocomplete-action to automatically open PRs against the autocomplete repo:

name: 'Update @withfig/autocomplete-tools spec'
on:
  push:
    tags:        
      - '@withfig/autocomplete-tools@*' # Only run the action on new tags
  workflow_dispatch:

jobs:
  push-to-fig-autocomplete:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v2
      - run: | # Use the command created previously to generate the new spec
          npm ci
          npm run generate-spec
      - name: 'Create Autocomplete PR'
        uses: withfig/push-to-fig-autocomplete-action@v1
        with:
          token: ${{ secrets.FIG_BOT_TOKEN }}
          autocomplete-spec-name: '@withfig/autocomplete-tools'
          spec-path: generated/autocomplete-tools-spec.ts
          integration: commander # NOTE: If we specify which integration was used to autogenerate the new spec, the merge tool does a better job!

Refer to the push-to-autocomplete-action docs to learn more about available options and to see more examples.