Skip to content

Commit

Permalink
fix(hooks): forward git hooks through a variable to avoid breaking sc…
Browse files Browse the repository at this point in the history
…ripts
  • Loading branch information
umbopepato committed Apr 24, 2021
1 parent 96dc42f commit 5ee07e0
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 43 deletions.
4 changes: 2 additions & 2 deletions scripts.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
scripts:

test:
cmd: 'deno test -Ar #'
cmd: deno test -Ar
desc: Runs the tests
gitHook: pre-push

Expand All @@ -15,6 +15,6 @@ scripts:
gitHook: pre-commit

commitlint:
cmd: npx commitlint -x @commitlint/config-conventional -e $1
cmd: echo "Args ${GIT_ARGS[1]}"; npx commitlint -x @commitlint/config-conventional -e ${GIT_ARGS[1]}
desc: Checks commit messages format
gitHook: commit-msg
9 changes: 7 additions & 2 deletions src/cli/commands/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command } from "../../../deps.ts";
import { ConfigData } from "../../load_config.ts";
import { runScript } from "../../run_script.ts";
import { ArgsForwardingMode, runScript } from "../../run_script.ts";
import { checkGitHooks } from "../../git_hooks.ts";
import { validateConfigData } from "../../validate_config_data.ts";

Expand All @@ -17,7 +17,12 @@ export class RunCommand extends Command {
}
validateConfigData(this.configData);
await checkGitHooks(this.configData as ConfigData);
await runScript(this.configData as ConfigData, script, additionalArgs);
await runScript({
configData: this.configData!,
script,
additionalArgs,
argsForwardingMode: ArgsForwardingMode.DIRECT,
});
});
}
}
10 changes: 8 additions & 2 deletions src/cli/commands/run_hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command } from "../../../deps.ts";
import { ConfigData } from "../../load_config.ts";
import { runScript } from "../../run_script.ts";
import { ArgsForwardingMode, runScript } from "../../run_script.ts";
import { VR_HOOKS } from "../../consts.ts";
import { validateConfigData } from "../../validate_config_data.ts";
import { isScriptObject } from "../../util.ts";
Expand All @@ -21,7 +21,13 @@ export class RunHookCommand extends Command {
value.gitHook === hook
);
if (script) {
await runScript(this.configData, script[0], args);
await runScript({
configData: this.configData!,
script: script[0],
prefix: `GIT_ARGS=("$@");`,
additionalArgs: args,
argsForwardingMode: ArgsForwardingMode.INDIRECT,
});
}
}
});
Expand Down
9 changes: 7 additions & 2 deletions src/cli/commands/vr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ScriptIdType } from "../types/script_id_type.ts";
import { ConfigData } from "../../load_config.ts";
import { RunCommand } from "./run.ts";
import { ExportCommand } from "./export.ts";
import { runScript } from "../../run_script.ts";
import { ArgsForwardingMode, runScript } from "../../run_script.ts";
import { RunHookCommand } from "./run_hook.ts";
import { VR_HOOKS, VR_LOG, VR_SHELL } from "../../consts.ts";
import { checkGitHooks } from "../../git_hooks.ts";
Expand Down Expand Up @@ -37,7 +37,12 @@ export class VrCommand extends Command {
.action(async (options, script: string, additionalArgs: string[]) => {
validateConfigData(this.configData);
await checkGitHooks(this.configData as ConfigData);
await runScript(this.configData as ConfigData, script, additionalArgs);
await runScript({
configData: this.configData!,
script,
additionalArgs,
argsForwardingMode: ArgsForwardingMode.DIRECT,
});
})
.command("run", new RunCommand(this.configData))
.command("run-hook", new RunHookCommand(this.configData))
Expand Down
117 changes: 86 additions & 31 deletions src/run_commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { kill } from "../deps.ts";
import { getEnvVars } from "./env.ts";
import { escape, isWindows, OneOrMore } from "./util.ts";
import { escape, isWindows, notNull, OneOrMore } from "./util.ts";
import { log } from "./logger.ts";
import {
Command,
Expand All @@ -9,15 +9,27 @@ import {
ParallelCommands,
} from "./command.ts";
import { buildCommandString } from "./build_command_string.ts";
import { ArgsForwardingMode } from "./run_script.ts";

const runningProcesses: Set<Deno.Process> = new Set();

export async function runCommands(
commands: CompoundCommandItem[],
shell: string,
additionalArgs: string[],
cwd: string,
): Promise<void> {
interface RunCommandsOptions {
shell: string;
cwd: string;
commands: CompoundCommandItem[];
prefix?: string;
additionalArgs?: string[];
argsForwardingMode?: ArgsForwardingMode;
}

export async function runCommands({
shell,
cwd,
commands,
prefix,
additionalArgs,
argsForwardingMode,
}: RunCommandsOptions): Promise<void> {
const _runCommands = async (
commands: OneOrMore<CompoundCommandItem>,
): Promise<unknown> => {
Expand All @@ -30,7 +42,15 @@ export async function runCommands(
if (isParallel(commands)) {
return Promise.all(commands.pll.map((c) => _runCommands(c)));
}
return runCommand(commands, shell, additionalArgs, cwd);
const command = commands as Command;
return runCommand({
shell,
cwd,
command,
prefix,
additionalArgs,
argsForwardingMode,
});
}
};
try {
Expand All @@ -44,23 +64,42 @@ export async function runCommands(
}
}

async function runCommand(
command: Command,
shell: string,
additionalArgs: string[],
cwd: string,
): Promise<void> {
type RunCommandOptions = Omit<RunCommandsOptions, "commands"> & {
command: Command;
};

async function runCommand({
shell,
cwd,
command,
prefix,
additionalArgs,
argsForwardingMode,
}: RunCommandOptions): Promise<void> {
const cmd = buildCommandString(command);
let runOptions: Deno.RunOptions = {
cmd: [shell, ...buildShellArgs(shell, cmd, additionalArgs)],
cmd: [
shell,
...buildShellArgs({
shell,
command: cmd,
prefix,
additionalArgs,
argsForwardingMode,
}),
],
cwd,
env: getEnvVars(command),
};
log.debug(
`Running > ${cmd}${
additionalArgs && additionalArgs.length > 0
? ` -- ${additionalArgs.join(" ")}`
: ""
`Running > ${
[
prefix,
cmd,
additionalArgs && additionalArgs.length > 0
? additionalArgs.join(" ")
: null,
].filter(notNull).join(" ")
}`,
);
const process = Deno.run(runOptions);
Expand All @@ -73,18 +112,34 @@ async function runCommand(
}
}

function buildShellArgs(
shell: string,
command: string,
additionalArgs: string[],
): string[] {
const fullCmd = additionalArgs.length < 1
? command
: `${command} ${
additionalArgs.map((a) => `"${escape(a, '"')}"`).join(" ")
}`;
type BuildShellArgsOptions = Omit<RunCommandOptions, "command" | "cwd"> & {
command: string;
};

function buildShellArgs({
shell,
command,
prefix,
additionalArgs,
argsForwardingMode,
}: BuildShellArgsOptions): string[] {
const cmd = [
prefix,
command,
argsForwardingMode === ArgsForwardingMode.DIRECT && additionalArgs &&
additionalArgs.length > 0
? additionalArgs.map((a) => `"${escape(a, '"')}"`).join(" ")
: null,
].filter(notNull)
.join(" ");
if (isWindows && /^(?:.*\\)?cmd(?:\.exe)?$/i.test(shell)) {
return ["/d", "/s", "/c", fullCmd];
return ["/d", "/s", "/c", cmd];
}
return ["-c", fullCmd];
return [
"-c",
cmd,
...(argsForwardingMode === ArgsForwardingMode.INDIRECT && additionalArgs
? additionalArgs
: []),
];
}
27 changes: 23 additions & 4 deletions src/run_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ import { resolveShell } from "./resolve_shell.ts";
import { runCommands } from "./run_commands.ts";
import { validateScript } from "./validate_script.ts";

export enum ArgsForwardingMode {
DIRECT,
INDIRECT,
}

export interface RunScriptOptions {
configData: ConfigData;
script: string;
prefix?: string;
additionalArgs?: string[];
argsForwardingMode?: ArgsForwardingMode;
}

export async function runScript(
configData: ConfigData,
script: string,
additionalArgs: string[] = [],
{ configData, script, prefix, additionalArgs, argsForwardingMode }:
RunScriptOptions,
) {
const { cwd, config } = configData;
if (script == null || script.length < 1) {
Expand All @@ -23,7 +35,14 @@ export async function runScript(
const commands = normalizeScript(scriptDef, rootConfig);
const shell = resolveShell();
try {
await runCommands(commands, shell, additionalArgs, cwd);
await runCommands({
shell,
cwd,
commands,
prefix,
additionalArgs,
argsForwardingMode,
});
} catch (e) {
log.error(`Failed at the ${bold(script)} script`);
Deno.exit(3);
Expand Down
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ export const isScriptObject = (script: any): script is ScriptObject =>

export const isParallelScripts = (script: any): script is ParallelScripts =>
script instanceof Object && "pll" in script;

export const notNull = (o: any) => o != null;

0 comments on commit 5ee07e0

Please sign in to comment.