Skip to content

Commit

Permalink
chore(docs): add docs on inferred actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkdev98 committed Sep 8, 2023
1 parent 89e1f9c commit 471ebe7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
31 changes: 26 additions & 5 deletions docs/docs/actions-and-defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ By default, Compas will watch your package.json files and determine if it needs
to reinstall your dependencies. This way your local environment always matches
the required dependency versions.

Other actions, like automatically spinning up development dependencies in Docker
need to be configured explicitly. See the respective integrations for how this
works.

## Configuration

Compas is configurable via a JSON file at `config/compas.json`. An empty config
Expand All @@ -36,4 +32,29 @@ and return back to the menu. When a process is running, you can restart it with
'R' or kill the running process by pressing 'K'.

The configuration file is automatically reloaded on changes, assuming that the
syntax is correct.
syntax is correct. Allowing you to iterate on it and expand your
[workspace](/docs/workspaces.html).

## Inferred actions

Compas tries to give you a good experience without any configuration. This is
why Compas automatically infers some standard actions based on your project
setup.

These inferred actions are only added when no action with the same or similar
name is defined. For example, 'Lint' is only added if both 'Lint' and 'Format'
are not configured.

- 'Dev' is automatically populated from the package.json scripts.
- 'Lint' defaults to the 'format' script defined in your package.json OR the
'lint' script defined in your package.json OR to `compas lint` if the
`@compas/cli` package is installed.
- 'Test' is based on the 'test' script in your package.json OR to `compas test`
if the `@compas/cli` package is installed.

## Other integrations

Compas supports much more. Like automatically starting Docker containers for
services that your development environment needs, or running the code generators
automatically on changes (not yet implemented). For more information, checkout
the [integrations](/docs/integrations/docker.md).
5 changes: 5 additions & 0 deletions docs/docs/workspaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ that Compas executes are now also done relative to that directory. You can
create another config in `$root/packages/shared/config/compas.json` to define
custom actions for that specific project and to add even more nested projects.

Navigation to subprojects is automatically added:

![Navigation from the root project](/workspace-navigation-home.png)

Compas also allows projects to be in a sibling directory:

```json [config/compas.json]
Expand All @@ -47,3 +51,4 @@ you are currently working on.
truth. So the most efficient way of developing is to always start Compas from
the same project and to navigate inside Compas to other projects when
necessary.
- Only 9 subprojects are supported currently.
Binary file added docs/public/workspace-navigation-home.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions packages/compas/src/shared/inferred-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { readFile } from "node:fs/promises";
import { pathJoin } from "@compas/stdlib";

/**
* Try to infer the lint command for a specific directory
*
* @param {string} rootDirectory
* @returns {Promise<string[]|undefined>}
*/
export async function inferActionLint(rootDirectory = "") {
const usesCompas = await inferUsesLegacyCompasCli(rootDirectory);

if (usesCompas) {
// TODO: use package manager
return ["npx", "compas", "lint"];
}

const packageJsonFile = pathJoin(rootDirectory, "package.json");

if (!packageJsonFile) {
return undefined;
}

const file = JSON.parse(await readFile(packageJsonFile, "utf-8"));
const command = file.scripts?.format ?? file.scripts?.lint;

if (!command) {
return undefined;
}

return command.split(" ");
}

/**
* Check if the project uses the legacy Compas CLI provided by @compas/cli.
*
* This is done based on the following rules:
*
* - @compas/cli is installed in the project
* - No lint or format script in the package.json
*
* @param {string} rootDirectory
* @returns {Promise<boolean>}
*/
export async function inferUsesLegacyCompasCli(rootDirectory = "") {
const packageJsonFile = pathJoin(rootDirectory, "package.json");

if (!packageJsonFile) {
return false;
}

const file = JSON.parse(await readFile(packageJsonFile, "utf-8"));

if (
!file.dependencies["@compas/cli"] &&
!file.devDependencies["@compas/cli"]
) {
return false;
}

return !file.scripts["lint"] && !file.scripts["format"];
}

0 comments on commit 471ebe7

Please sign in to comment.