Skip to content

Commit

Permalink
Merge partition arguments helper (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
febo authored Dec 19, 2024
1 parent bac0950 commit 6185b40
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
28 changes: 14 additions & 14 deletions scripts/helpers/utils.mts
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,18 @@ export function popArgument(args: string[], arg: string) {

export function partitionArguments(
args: string[],
delimiter: string
delimiter: string,
defaultArgs?: string[]
): [string[], string[]] {
const index = args.indexOf(delimiter);
return index >= 0
? [args.slice(0, index), args.slice(index + 1)]
: [args, []];
}
const [providedCargoArgs, providedCommandArgs] =
index >= 0 ? [args.slice(0, index), args.slice(index + 1)] : [args, []];

export function partitionArgumentsWithDefaultArgs(
args: string[],
delimiter: string,
defaultArgs?: string[],
): [string[], string[]] {
const [providedCargoArgs, providedCommandArgs] = partitionArguments(args, delimiter);
if (defaultArgs) {
const [defaultCargoArgs, defaultCommandArgs] = partitionArguments(defaultArgs, delimiter);
const [defaultCargoArgs, defaultCommandArgs] = partitionArguments(
defaultArgs,
delimiter
);
return [
[...defaultCargoArgs, ...providedCargoArgs],
[...defaultCommandArgs, ...providedCommandArgs],
Expand All @@ -144,10 +140,14 @@ export async function getInstalledSolanaVersion(): Promise<string | undefined> {
}
}

export function parseCliArguments(): { command: string, libraryPath: string; args: string[] } {
export function parseCliArguments(): {
command: string;
libraryPath: string;
args: string[];
} {
const command = process.argv[2];
const args = process.argv.slice(3);

// Extract the relative crate directory from the command-line arguments. This
// is the only required argument.
const relativePath = args.shift();
Expand Down
4 changes: 2 additions & 2 deletions scripts/js.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'zx/globals';
import {
parseCliArguments,
partitionArgumentsWithDefaultArgs,
partitionArguments,
} from './helpers/utils.mts';

enum Command {
Expand All @@ -21,7 +21,7 @@ async function pnpm(
command: string,
build = false,
) {
const [pnpmArgs, commandArgs] = partitionArgumentsWithDefaultArgs(args, '--');
const [pnpmArgs, commandArgs] = partitionArguments(args, '--');
cd(libraryPath);
await $`pnpm install`;
if (build) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/rust.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getCargo,
getToolchainArgument,
parseCliArguments,
partitionArgumentsWithDefaultArgs,
partitionArguments,
popArgument,
workingDirectory,
} from './helpers/utils.mts';
Expand All @@ -32,7 +32,7 @@ async function cargo(
defaultArgs?: string[],
variables?: [string, string][],
) {
const [cargoArgs, commandArgs] = partitionArgumentsWithDefaultArgs(args, '--', defaultArgs);
const [cargoArgs, commandArgs] = partitionArguments(args, '--', defaultArgs);
variables?.forEach(([k, v]) => $.env[k] = v);
await $`cargo ${toolchain} ${command} --manifest-path ${manifestPath} ${cargoArgs} -- ${commandArgs}`;
}
Expand Down

0 comments on commit 6185b40

Please sign in to comment.