-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_deno.ts
More file actions
141 lines (126 loc) · 4.02 KB
/
_deno.ts
File metadata and controls
141 lines (126 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Shared helper for Deno subcommand execution
*
* Used by fmt, lint, and check commands to run corresponding deno commands
* on discovered scenario files.
*
* @module
*/
import { parseArgs } from "@std/cli";
import { resolve } from "@std/path";
import { getLogger, type LogLevel } from "@logtape/logtape";
import { discoverScenarioFiles } from "@probitas/discover";
import { EXIT_CODE } from "../constants.ts";
import { findProbitasConfigFile, loadConfig } from "../config.ts";
import { createDiscoveryProgress } from "../progress.ts";
import { configureLogging, readAsset } from "../utils.ts";
const logger = getLogger(["probitas", "cli", "deno"]);
/**
* Options for running a Deno subcommand
*/
export interface DenoSubcommandOptions {
/** Asset file name for help text */
usageAsset: string;
/** Extra arguments to pass to the deno command */
extraArgs?: readonly string[];
/** Enable -r/--reload option support */
supportReload?: boolean;
/** Use deno.json/deno.lock config (default: false, adds --no-config) */
useConfig?: boolean;
}
/**
* Run a Deno subcommand on discovered scenario files
*
* @param subcommand - Deno subcommand to run (fmt, lint, check)
* @param args - Command-line arguments
* @param cwd - Current working directory
* @param options - Subcommand options
* @returns Exit code from the deno command
*/
export async function runDenoSubcommand(
subcommand: string,
args: string[],
cwd: string,
options: DenoSubcommandOptions,
): Promise<number> {
try {
const parsed = parseArgs(args, {
string: ["config", "include", "exclude"],
boolean: ["help", "quiet", "verbose", "debug", "reload"],
collect: ["include", "exclude"],
alias: {
h: "help",
v: "verbose",
q: "quiet",
d: "debug",
r: "reload",
},
default: {
include: undefined,
exclude: undefined,
},
});
if (parsed.help) {
const helpText = await readAsset(options.usageAsset);
console.log(helpText);
return EXIT_CODE.SUCCESS;
}
// Determine log level
const logLevel: LogLevel = parsed.debug
? "debug"
: parsed.verbose
? "info"
: parsed.quiet
? "fatal"
: "warning";
try {
await configureLogging(logLevel);
} catch {
// Ignore - logging may already be configured
}
// Load probitas config (NOT deno.json)
const configPath = parsed.config ??
await findProbitasConfigFile(cwd, { parentLookup: true });
const config = configPath ? await loadConfig(configPath) : {};
// CLI > config
const includes = parsed.include ?? config?.includes;
const excludes = parsed.exclude ?? config?.excludes;
// Discover scenario files
const paths = parsed._.map(String).map((p) => resolve(cwd, p));
const discoveryProgress = parsed.quiet ? null : createDiscoveryProgress();
const scenarioFiles = await discoverScenarioFiles(
paths.length ? paths : [cwd],
{ includes, excludes, onProgress: discoveryProgress?.onProgress },
);
discoveryProgress?.complete(scenarioFiles.length);
if (scenarioFiles.length === 0) {
console.warn("No scenario files found");
return EXIT_CODE.NOT_FOUND;
}
logger.debug(`Running deno ${subcommand}`, {
fileCount: scenarioFiles.length,
});
// Run deno command (with --no-config unless useConfig is true)
const denoArgs = [
subcommand,
...(options.useConfig ? [] : ["--no-config"]),
...(options.supportReload && parsed.reload ? ["--reload"] : []),
...(options.extraArgs ?? []),
...scenarioFiles,
];
const command = new Deno.Command("deno", {
args: denoArgs,
cwd,
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
});
const { code } = await command.output();
return code;
} catch (err: unknown) {
logger.error(`deno ${subcommand} failed`, { error: err });
const m = err instanceof Error ? err.message : String(err);
console.error(`Error: ${m}`);
return EXIT_CODE.FAILURE;
}
}