Skip to content

Commit

Permalink
refactor: export collect as function for programmatic use
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Jun 19, 2023
2 parents 938bea8 + af6d04d commit 83390d1
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 37 deletions.
9 changes: 9 additions & 0 deletions packages/cli/src/lib/commands/assert/command-impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {RcJsonAsArgv} from "../../types";

import {logVerbose} from "../../core/loggin";
import {readBudgets} from "./utils/budgets";

export async function runAssertCommand(argv: RcJsonAsArgv) {
logVerbose(readBudgets());
return Promise.resolve();
}
4 changes: 0 additions & 4 deletions packages/cli/src/lib/commands/assert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,3 @@ export const assertCommand: YargsCommandObject = {
}
};

export async function run(argv: Partial<RcJson>): Promise<void> {
logVerbose(readBudgets());
return Promise.resolve();
}
19 changes: 19 additions & 0 deletions packages/cli/src/lib/commands/collect/command-impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {RcJson, RcJsonAsArgv} from "../../types";
import {getCollectCommandOptionsFromArgv} from "./utils/params";
import {logVerbose} from "../../core/loggin";
import {run} from "../../core/processing/behaviors";
import {collectRcJson} from "../init/processes/collect-rc-json";
import {startServerIfNeededAndExecute} from "./utils/serve-command";
import {collectReports} from "./processes/collect-reports";

export async function runCollectCommand(argv: RcJsonAsArgv) {
const cfg = getCollectCommandOptionsFromArgv(argv);
logVerbose('Collect options: ', cfg);
await run([
collectRcJson,
(cfg: RcJson) =>
startServerIfNeededAndExecute(() => collectReports(cfg)
.then()
, cfg.collect)
])(cfg);
}
23 changes: 5 additions & 18 deletions packages/cli/src/lib/commands/collect/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { YargsCommandObject } from '../../core/yargs/types';
import { logVerbose } from '../../core/loggin/index';
import { COLLECT_OPTIONS } from './options';
import { startServerIfNeededAndExecute } from './utils/serve-command';
import { collectRcJson } from '../init/processes/collect-rc-json';
import { run } from '../../core/processing/behaviors';
import { collectReports } from './processes/collect-reports';
import { RcJson } from '../../types';
import { getCollectCommandOptionsFromArgv } from './utils/params';
import {YargsCommandObject} from '../../core/yargs/types';
import {logVerbose} from '../../core/loggin/index';
import {COLLECT_OPTIONS} from './options';
import {runCollectCommand} from "./command-impl";

export const collectUserFlowsCommand: YargsCommandObject = {
command: 'collect',
Expand All @@ -15,15 +10,7 @@ export const collectUserFlowsCommand: YargsCommandObject = {
module: {
handler: async (argv: any) => {
logVerbose(`run "collect" as a yargs command with args:`);
const cfg = getCollectCommandOptionsFromArgv(argv);
logVerbose('Collect options: ', cfg);
await run([
collectRcJson,
(cfg: RcJson) =>
startServerIfNeededAndExecute(() => collectReports(cfg)
.then()
, cfg.collect)
])(cfg);
await runCollectCommand(argv);
}
}
};
29 changes: 29 additions & 0 deletions packages/cli/src/lib/commands/init/command-impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {RcJsonAsArgv} from "../../types";

import {log, logVerbose} from "../../core/loggin";
import {run} from "../../core/processing/behaviors";
import {collectRcJson} from "../init/processes/collect-rc-json";
import {getGlobalOptionsFromArgv} from "../../global/utils";
import {getInitCommandOptionsFromArgv} from "./utils";
import {updateRcJson} from "./processes/update-rc-json";
import {handleFlowGeneration} from "./processes/generate-userflow";
import {handleGhWorkflowGeneration} from "./processes/generate-workflow";
import {handleBudgetsGeneration} from "./processes/generate-lh-budgets";
import {SETUP_CONFIRM_MESSAGE} from "./constants";

export async function runInitCommand(argv: RcJsonAsArgv) {
const { interactive } = getGlobalOptionsFromArgv(argv);
const { generateFlow, generateGhWorkflow, generateBudgets, lhr, ...cfg } = getInitCommandOptionsFromArgv(argv);
logVerbose('Init options: ', { interactive, generateFlow, generateGhWorkflow, generateBudgets,lhr, ...cfg });

await run([
collectRcJson,
updateRcJson,
handleFlowGeneration({ interactive: !!interactive, generateFlow }),
handleGhWorkflowGeneration({ generateGhWorkflow }),
handleBudgetsGeneration({ generateBudgets, lhr }),
])(cfg );
log(SETUP_CONFIRM_MESSAGE);
// @TODO move to constants
log('To execute a user flow run `npx user-flow` or `npx user-flow collect`');
}
16 changes: 2 additions & 14 deletions packages/cli/src/lib/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { handleFlowGeneration } from './processes/generate-userflow';
import { getGlobalOptionsFromArgv } from '../../global/utils';
import { handleGhWorkflowGeneration } from './processes/generate-workflow';
import { handleBudgetsGeneration } from './processes/generate-lh-budgets';
import {runInitCommand} from "./command-impl";

export const initCommand: YargsCommandObject = {
command: 'init',
Expand All @@ -18,20 +19,7 @@ export const initCommand: YargsCommandObject = {
module: {
handler: async (argv: any) => {
logVerbose(`run "init" as a yargs command`);
const { interactive } = getGlobalOptionsFromArgv(argv);
const { generateFlow, generateGhWorkflow, generateBudgets, lhr, ...cfg } = getInitCommandOptionsFromArgv(argv);
logVerbose('Init options: ', { interactive, generateFlow, generateGhWorkflow, generateBudgets,lhr, ...cfg });

await run([
collectRcJson,
updateRcJson,
handleFlowGeneration({ interactive: !!interactive, generateFlow }),
handleGhWorkflowGeneration({ generateGhWorkflow }),
handleBudgetsGeneration({ generateBudgets, lhr }),
])(cfg );
log(SETUP_CONFIRM_MESSAGE);
// @TODO move to constants
log('To execute a user flow run `npx user-flow` or `npx user-flow collect`');
await runInitCommand(argv);
}
}
};
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ export { DEFAULT_COLLECT_UF_PATH } from './commands/collect/options/ufPath.const
export { DEFAULT_PERSIST_OUT_PATH } from './commands/collect/options/outPath.constant';
export { ReportFormat } from './commands/collect/options/types';
export { createReducedReport } from './commands/collect/utils/report/utils';
export { enrichReducedReportWithBaseline } from './commands/collect/utils/report/utils';
export {enrichReducedReportWithBaseline} from './commands/collect/utils/report/utils';
export {runInitCommand} from './commands/init/command-impl';
export {runCollectCommand} from './commands/collect/command-impl';
export {runAssertCommand} from './commands/assert/command-impl';

0 comments on commit 83390d1

Please sign in to comment.