Эх сурвалжийг харах

Use partitionArguments on Rust formatting scripts

Loris Leiva 1 жил өмнө
parent
commit
9ea44b4c99

+ 4 - 2
template/base/scripts/program/format.mjs

@@ -4,6 +4,7 @@ import {
   cliArguments,
   getProgramFolders,
   getToolchainArgument,
+  partitionArguments,
   popArgument,
   workingDirectory,
 } from '../utils.mjs';
@@ -13,6 +14,7 @@ import {
 const formatArgs = cliArguments();
 
 const fix = popArgument(formatArgs, '--fix');
+const [cargoArgs, fmtArgs] = partitionArguments(formatArgs, '--');
 const toolchain = getToolchainArgument('format');
 
 // Format the programs.
@@ -21,9 +23,9 @@ await Promise.all(
     const manifestPath = path.join(workingDirectory, folder, 'Cargo.toml');
 
     if (fix) {
-      await $`cargo ${toolchain} fmt --manifest-path ${manifestPath} -- ${formatArgs}`;
+      await $`cargo ${toolchain} fmt --manifest-path ${manifestPath} ${cargoArgs} -- ${fmtArgs}`;
     } else {
-      await $`cargo ${toolchain} fmt --manifest-path ${manifestPath} -- --check ${formatArgs}`;
+      await $`cargo ${toolchain} fmt --manifest-path ${manifestPath} ${cargoArgs} -- --check ${fmtArgs}`;
     }
   })
 );

+ 7 - 0
template/base/scripts/utils.mjs

@@ -104,3 +104,10 @@ export function popArgument(args, arg) {
   }
   return index >= 0;
 }
+
+export function partitionArguments(args, delimiter) {
+  const index = args.indexOf(delimiter);
+  return index >= 0
+    ? [args.slice(0, index), args.slice(index + 1)]
+    : [args, []];
+}

+ 4 - 2
template/clients/rust/scripts/client/format-rust.mjs

@@ -3,6 +3,7 @@ import 'zx/globals';
 import {
   cliArguments,
   getToolchainArgument,
+  partitionArguments,
   popArgument,
   workingDirectory,
 } from '../utils.mjs';
@@ -12,6 +13,7 @@ import {
 const formatArgs = cliArguments();
 
 const fix = popArgument(formatArgs, '--fix');
+const [cargoArgs, fmtArgs] = partitionArguments(formatArgs, '--');
 const toolchain = getToolchainArgument('format');
 const manifestPath = path.join(
   workingDirectory,
@@ -22,7 +24,7 @@ const manifestPath = path.join(
 
 // Format the client.
 if (fix) {
-  await $`cargo ${toolchain} fmt --manifest-path ${manifestPath} -- ${formatArgs}`;
+  await $`cargo ${toolchain} fmt --manifest-path ${manifestPath} ${cargoArgs} -- ${fmtArgs}`;
 } else {
-  await $`cargo ${toolchain} fmt --manifest-path ${manifestPath} -- --check ${formatArgs}`;
+  await $`cargo ${toolchain} fmt --manifest-path ${manifestPath} ${cargoArgs} -- --check ${fmtArgs}`;
 }