-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(docs): add docs on inferred actions
- Loading branch information
Showing
4 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]; | ||
} |