diff --git a/packages/cli/cli.js b/packages/cli/cli.js index 8e3b64e..d84c33a 100755 --- a/packages/cli/cli.js +++ b/packages/cli/cli.js @@ -1,2 +1,5 @@ #!/usr/bin/env node -import "./dist/index.js"; +import { argv } from "node:process"; +import run from "./dist/index.js"; + +run(argv.slice(2).join(" ")); diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index e18f058..0f9d9bb 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -18,38 +18,52 @@ import { WatchCommand, } from "./commands/index.js"; -const pkg: { name: string; description: string; version: string } = JSON.parse( - readFileSync(new URL("../package.json", import.meta.url), "utf8"), -); +/** + * Programmatic entry point for the Allure 3 CLI + * @param argv the array of arguments which already doesn't includes the node binary and the script path (the first two elements) + * @example + * ```js + * import allure from "allure"; + * + * allure("run -- npm test"]); // equivalent to `allure run -- npm test` + * allure("generate ./allure-results"); // equivalent to `allure generate ./allure-results` + * ``` + */ +const run = (argv: string) => { + const pkg: { name: string; description: string; version: string } = JSON.parse( + readFileSync(new URL("../package.json", import.meta.url), "utf8"), + ); + const cli = cac(pkg.name).usage(pkg.description).help().version(pkg.version); + const commands = [ + ClassicCommand, + AwesomeCommand, + CsvCommand, + GenerateCommand, + HistoryCommand, + KnownIssueCommand, + LogCommand, + OpenCommand, + QualityGateCommand, + RunCommand, + SlackCommand, + TestPlanCommand, + WatchCommand, + ]; -const cli = cac(pkg.name).usage(pkg.description).help().version(pkg.version); -const commands = [ - ClassicCommand, - AwesomeCommand, - CsvCommand, - GenerateCommand, - HistoryCommand, - KnownIssueCommand, - LogCommand, - OpenCommand, - QualityGateCommand, - RunCommand, - SlackCommand, - TestPlanCommand, - WatchCommand, -]; - -commands.forEach((command) => { - command(cli); -}); + commands.forEach((command) => { + command(cli); + }); -cli.on("command:*", () => { - console.error("Invalid command: %s", cli.args.join(" ")); - process.exit(1); -}); + cli.on("command:*", () => { + console.error("Invalid command: %s", cli.args.join(" ")); + process.exit(1); + }); -console.log(cwd()); + console.log(cwd()); -cli.parse(); + cli.parse(["", "", ...argv.split(" ")]); +}; export { defineConfig } from "@allurereport/plugin-api"; + +export default run; diff --git a/packages/cli/src/utils/commands.ts b/packages/cli/src/utils/commands.ts index f50591a..164dc58 100644 --- a/packages/cli/src/utils/commands.ts +++ b/packages/cli/src/utils/commands.ts @@ -22,6 +22,7 @@ export const createCommand = (payload: CreateCommandOptions) => { if (!payload.name) { throw new Error("Command name is not provided!"); } + if (!payload.action) { throw new Error("Command action is not provided!"); }