Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@rushstack/rig-package

microsoft9.5mMIT0.5.3TypeScript support: included

A system for sharing tool configurations between projects without duplicating config files.

readme

@rushstack/rig-package

The config/rig.json file is a system that Node.js build tools can adopt, in order to eliminate duplication of config files when many projects share a common configuration. This is particularly valuable in a setup where hundreds of projects may be built using a small set of reusable recipes.

Motivation

For a concrete example, consider the API Extractor tool which reads its configuration from <projectFolder>/config/api-extractor.json. Suppose that we have three separate projects that all share the exact same configuration:

project1/package.json
project1/config/api-extractor.json
project1/config/other-tool2.json
project1/config/other-tool3.json
project1/src/index.ts

project2/package.json
project2/config/api-extractor.json
project2/config/other-tool2.json
project2/config/other-tool3.json
project2/src/index.ts

project3/package.json
project3/config/api-extractor.json
project3/config/other-tool2.json
project3/config/other-tool3.json
project3/src/index.ts

It seems wasteful to copy and paste the api-extractor.json file with all those settings. If we later need to tune the configuration, we'd have to find and update each file. For a large organization, there could be hundreds of such projects.

The "extends" setting provides a basic way to centralize the configuration in a "rig package". For this example, we'll call our NPM package example-rig:

example-rig/package.json
example-rig/profile/node-library/config/api-extractor.json
example-rig/profile/web-library/config/api-extractor.json

project1/package.json
project1/config/api-extractor.json
project1/config/other-tool2.json
project1/config/other-tool3.json
project1/src/index.ts

project2/package.json
project2/config/api-extractor.json
project2/config/other-tool2.json
project2/config/other-tool3.json
project2/src/index.ts

project3/package.json
project3/config/api-extractor.json
project3/config/other-tool2.json
project3/config/other-tool3.json
project3/src/index.ts

To make things interesting, above we've introduced two "profiles":

  • node-library is for libraries that target the Node.js runtime
  • web-library is for libraries that target a web browser

NOTE: The node-library and web-library names are hypothetical examples. The names and purposes of rig profiles are user-defined. If you only need one profile, then call it default.

If project1 and project2 are Node.js libraries, then their api-extractor.json now reduces to this:

project1/config/api-extractor.json

{
  "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
  "extends": "example-rig/profile/node-library/config/api-extractor.json"
}

Whereas if project3 is a web browser library, then it might look like this:

project3/config/api-extractor.json

{
  "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
  "extends": "example-rig/profile/web-library/config/api-extractor.json"
}

Using "extends" definitely made the config file shorter! But imagine that we have a large monorepo with 100 projects. And each project has 5 config files like api-extactor.json. We still have to copy+paste 100 x 5 = 500 config files across all our project folders.

Can we do better?

rig.json eliminates files entirely

The idea is to replace config/api-extractor.json and config/other-tool2.json (and any other such files) with a single file config/rig.json that delegates everything to the rig package:

project3/config/rig.json

{
  "$schema": "https://developer.microsoft.com/json-schemas/rig-package/rig.schema.json",

  /**
   * (Required) The name of the rig package to inherit from.
   * It should be an NPM package name with the "-rig" suffix.
   */
  "rigPackageName": "example-rig",

  /**
   * (Optional) Selects a config profile from the rig package.  The name must consist of
   * lowercase alphanumeric words separated by hyphens, for example "sample-profile".
   * If omitted, then the "default" profile will be used."
   */
  "rigProfile": "web-library"
}

Using rig.json eliminates the "extends" stub files entirely. A tool that implements the rig.json system would probe for its config file (<targetFile>.json) using the following procedure:

  1. First check for config/<targetFile>.json in the project folder; if found, use that file. OTHERWISE...
  2. Check for config/rig.json; if found, then this project is using a rig package. Read the <rigPackageName> and <rigProfile> settings from that file.
  3. Use Node.js module resolution to find the <rigPackageName> package folder (let's call that <rigPackageFolder>)
  4. Check for <rigPackageFolder>/profile/<rigProfile>/<targetFile>.json; if found, use that file. OTHERWISE...
  5. If the <targetFile>.json cannot be found in either of these places, the behavior is left to the tool. For example, it could report an error, or proceed using defaults.

In cases where we need a project-specific customization, the "extends" field is still supported. For example, project1 can still add a local override like this:

project1/config/api-extractor.json

{
  "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
  "extends": "example-rig/profile/node-library/config/api-extractor.json",

  // Local override:
  "mainEntryPointFilePath": "<projectFolder>/lib/custom.d.ts",
}

The result is a much shorter inventory of files:

example-rig/package.json

example-rig/profile/node-library/config/api-extractor.json
example-rig/profile/node-library/config/other-tool2.json
example-rig/profile/node-library/config/other-tool3.json

example-rig/profile/web-library/config/api-extractor.json
example-rig/profile/web-library/config/other-tool2.json
example-rig/profile/web-library/config/other-tool3.json

project1/package.json
project1/config/rig.json
project1/config/api-extractor.json  <-- local override shown above
project1/src/index.ts

project2/package.json
project2/config/rig.json
project2/src/index.ts

project3/package.json
project3/config/rig.json
project3/src/index.ts

The @rushstack/rig-package API

The @rushstack/rig-package library provides an API for loading the rig.json file and performing lookups. It is a lightweight NPM package, intended to be easy for tool projects to accept as a dependency. The package also includes the JSON schema file rig.schema.json.

Example usage of the API:

import { RigConfig } from '@rushstack/rig-package';

// Probe for the rig.json file and load it if found
const rigConfig: RigConfig = RigConfig.loadForProjectFolder({
  // Specify a  project folder (i.e. where its package.json file is located)
  projectFolderPath: testProjectFolder
});

if (rigConfig.rigFound) {
  // We found a config/rig.json file
  //
  // Prints "/path/to/project3/config/rig.json"
  console.log('Found rig.json: ' + rigConfig.filePath);

  // Prints "example-rig"
  console.log('The rig package is: ' + rigConfig.rigPackageName);

  // Resolve the rig package
  //
  // Prints "/path/to/project3/node_modules/example-rig/profile/web-library"
  console.log('Profile folder: ' + rigConfig.getResolvedProfileFolder());

  // Look up a config file.  These file paths will be tested:
  //
  //   /path/to/project3/folder/file.json
  //   /path/to/project3/node_modules/example-rig/profile/web-library/folder/file.json
  //
  // The result will be the first path that exists, or undefined if the config file was not found.
  console.log('Resolved config file: ' + rigConfig.tryResolveConfigFilePath('folder/file.json'));
}

Note that there are also async variants of the functions that access the filesystem.

@rushstack/rig-package is part of the Rush Stack family of projects.

changelog

Change Log - @rushstack/rig-package

This log was last generated on Sat, 27 Jul 2024 00:10:27 GMT and should not be manually modified.

0.5.3

Sat, 27 Jul 2024 00:10:27 GMT

Patches

  • Include CHANGELOG.md in published releases again

0.5.2

Sat, 17 Feb 2024 06:24:35 GMT

Patches

  • Fix broken link to API documentation

0.5.1

Tue, 26 Sep 2023 09:30:33 GMT

Patches

  • Update type-only imports to include the type modifier.

0.5.0

Fri, 15 Sep 2023 00:36:58 GMT

Minor changes

  • Update @types/node from 14 to 18

0.4.1

Tue, 08 Aug 2023 07:10:40 GMT

Version update only

0.4.0

Mon, 19 Jun 2023 22:40:21 GMT

Minor changes

  • Expose an IRigConfig interface that RigConfig implements.

0.3.21

Thu, 15 Jun 2023 00:21:01 GMT

Version update only

0.3.20

Wed, 07 Jun 2023 22:45:16 GMT

Version update only

0.3.19

Mon, 22 May 2023 06:34:33 GMT

Version update only

0.3.18

Fri, 10 Feb 2023 01:18:50 GMT

Version update only

0.3.17

Mon, 10 Oct 2022 15:23:44 GMT

Version update only

0.3.16

Thu, 29 Sep 2022 07:13:06 GMT

Version update only

0.3.15

Thu, 15 Sep 2022 00:18:51 GMT

Version update only

0.3.14

Wed, 03 Aug 2022 18:40:35 GMT

Version update only

0.3.13

Tue, 28 Jun 2022 00:23:32 GMT

Version update only

0.3.12

Fri, 17 Jun 2022 00:16:18 GMT

Version update only

0.3.11

Sat, 23 Apr 2022 02:13:07 GMT

Version update only

0.3.10

Fri, 15 Apr 2022 00:12:36 GMT

Version update only

0.3.9

Sat, 09 Apr 2022 02:24:26 GMT

Patches

  • Rename the "master" branch to "main".

0.3.8

Tue, 15 Mar 2022 19:15:53 GMT

Version update only

0.3.7

Mon, 27 Dec 2021 16:10:40 GMT

Version update only

0.3.6

Mon, 06 Dec 2021 16:08:32 GMT

Version update only

0.3.5

Fri, 05 Nov 2021 15:09:18 GMT

Version update only

0.3.4

Wed, 27 Oct 2021 00:08:15 GMT

Patches

  • Update the package.json repository field to include the directory property.

0.3.3

Wed, 13 Oct 2021 15:09:54 GMT

Version update only

0.3.2

Thu, 07 Oct 2021 07:13:35 GMT

Version update only

0.3.1

Thu, 23 Sep 2021 00:10:41 GMT

Patches

  • Upgrade the @types/node dependency to version to version 12.

0.3.0

Fri, 27 Aug 2021 00:07:25 GMT

Minor changes

  • Cache rig.json reads

0.2.13

Mon, 12 Jul 2021 23:08:26 GMT

Version update only

0.2.12

Mon, 12 Apr 2021 15:10:28 GMT

Version update only

0.2.11

Tue, 06 Apr 2021 15:14:22 GMT

Version update only

0.2.10

Thu, 04 Mar 2021 01:11:31 GMT

Patches

  • Eliminate dependency on @types/node

0.2.9

Thu, 10 Dec 2020 23:25:49 GMT

Version update only

0.2.8

Wed, 11 Nov 2020 01:08:59 GMT

Version update only

0.2.7

Fri, 30 Oct 2020 06:38:39 GMT

Version update only

0.2.6

Fri, 30 Oct 2020 00:10:14 GMT

Version update only

0.2.5

Wed, 28 Oct 2020 01:18:03 GMT

Version update only

0.2.4

Tue, 06 Oct 2020 00:24:06 GMT

Version update only

0.2.3

Mon, 05 Oct 2020 22:36:57 GMT

Version update only

0.2.2

Mon, 05 Oct 2020 15:10:42 GMT

Patches

  • Fix minor mistake in README.md

0.2.1

Wed, 30 Sep 2020 18:39:17 GMT

Patches

  • Update to build with @rushstack/heft-node-rig

0.2.0

Wed, 30 Sep 2020 06:53:53 GMT

Minor changes

  • Update the rig package guidance to place tool configuration files that would normally be in a "config" folder in a "config" folder inside the rig package as well.
  • Add ILoadForProjectFolderOptions.overrideRigJsonObject
  • Add RigConfig.tryResolveConfigFilePath()
  • Upgrade compiler; the API now requires TypeScript 3.9 or newer

Patches

  • Report an error if the specified "rigProfile" is not defined by the rig package
  • Update README.md

0.1.0

Fri, 25 Sep 2020 08:13:01 GMT

Minor changes

  • Initial release