mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Pull parsing logic into distinct file and remove invalid continue
This commit is contained in:
parent
98a1ba7d10
commit
08ae1ef732
3 changed files with 122 additions and 78 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import { runRushCommand } from "./runRushCommand.js";
|
import { runRushCommand } from "./runRushCommand.js";
|
||||||
import { resolvePackageVersion } from "../../api/npmApi.js";
|
import { resolvePackageVersion } from "../../api/npmApi.js";
|
||||||
|
import { parsePackagesFromRushAddArgs } from "./parsing/parsePackagesFromRushAddArgs.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {import("../currentPackageManager.js").PackageManager}
|
* @returns {import("../currentPackageManager.js").PackageManager}
|
||||||
|
|
@ -22,9 +23,7 @@ async function scanRushAddCommand(args) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsedSpecs = extractRushAddPackageSpecs(args)
|
const parsedSpecs = parsePackagesFromRushAddArgs(args.slice(1));
|
||||||
.map((spec) => parsePackageSpec(spec))
|
|
||||||
.filter((spec) => spec !== null);
|
|
||||||
|
|
||||||
const resolvedVersions = await Promise.all(
|
const resolvedVersions = await Promise.all(
|
||||||
parsedSpecs.map(async (parsed) => {
|
parsedSpecs.map(async (parsed) => {
|
||||||
|
|
@ -63,78 +62,3 @@ function getRushCommand(args) {
|
||||||
|
|
||||||
return args[0]?.toLowerCase();
|
return args[0]?.toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string[]} args
|
|
||||||
* @returns {string[]}
|
|
||||||
*/
|
|
||||||
function extractRushAddPackageSpecs(args) {
|
|
||||||
const packageSpecs = [];
|
|
||||||
|
|
||||||
for (let i = 1; i < args.length; i++) {
|
|
||||||
const arg = args[i];
|
|
||||||
if (!arg) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arg === "--package" || arg === "-p") {
|
|
||||||
const next = args[i + 1];
|
|
||||||
if (next && !next.startsWith("-")) {
|
|
||||||
packageSpecs.push(next);
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arg.startsWith("--package=")) {
|
|
||||||
const value = arg.slice("--package=".length);
|
|
||||||
if (value) {
|
|
||||||
packageSpecs.push(value);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!arg.startsWith("-")) {
|
|
||||||
packageSpecs.push(arg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return packageSpecs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} spec
|
|
||||||
* @returns {{name: string, version: string | null} | null}
|
|
||||||
*/
|
|
||||||
function parsePackageSpec(spec) {
|
|
||||||
const value = removeAlias(spec.trim());
|
|
||||||
if (!value) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const lastAtIndex = value.lastIndexOf("@");
|
|
||||||
if (lastAtIndex > 0) {
|
|
||||||
return {
|
|
||||||
name: value.slice(0, lastAtIndex),
|
|
||||||
version: value.slice(lastAtIndex + 1),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
name: value,
|
|
||||||
version: null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} spec
|
|
||||||
* @returns {string}
|
|
||||||
*/
|
|
||||||
function removeAlias(spec) {
|
|
||||||
const aliasIndex = spec.indexOf("@npm:");
|
|
||||||
if (aliasIndex !== -1) {
|
|
||||||
return spec.slice(aliasIndex + 5);
|
|
||||||
}
|
|
||||||
|
|
||||||
return spec;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
/**
|
||||||
|
* @param {string[]} args
|
||||||
|
* @returns {{name: string, version: string | null}[]}
|
||||||
|
*/
|
||||||
|
export function parsePackagesFromRushAddArgs(args) {
|
||||||
|
const packageSpecs = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < args.length; i++) {
|
||||||
|
const arg = args[i];
|
||||||
|
if (!arg) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg === "--package" || arg === "-p") {
|
||||||
|
const next = args[i + 1];
|
||||||
|
if (next && !next.startsWith("-")) {
|
||||||
|
packageSpecs.push(next);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg.startsWith("--package=")) {
|
||||||
|
const value = arg.slice("--package=".length);
|
||||||
|
if (value) {
|
||||||
|
packageSpecs.push(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return packageSpecs
|
||||||
|
.map((spec) => parsePackageSpec(spec))
|
||||||
|
.filter((spec) => spec !== null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} spec
|
||||||
|
* @returns {{name: string, version: string | null} | null}
|
||||||
|
*/
|
||||||
|
function parsePackageSpec(spec) {
|
||||||
|
const value = removeAlias(spec.trim());
|
||||||
|
if (!value) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastAtIndex = value.lastIndexOf("@");
|
||||||
|
if (lastAtIndex > 0) {
|
||||||
|
return {
|
||||||
|
name: value.slice(0, lastAtIndex),
|
||||||
|
version: value.slice(lastAtIndex + 1),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: value,
|
||||||
|
version: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} spec
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
function removeAlias(spec) {
|
||||||
|
const aliasIndex = spec.indexOf("@npm:");
|
||||||
|
if (aliasIndex !== -1) {
|
||||||
|
return spec.slice(aliasIndex + 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
return spec;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
import { describe, it } from "node:test";
|
||||||
|
import assert from "node:assert";
|
||||||
|
import { parsePackagesFromRushAddArgs } from "./parsePackagesFromRushAddArgs.js";
|
||||||
|
|
||||||
|
describe("parsePackagesFromRushAddArgs", () => {
|
||||||
|
it("returns an empty array when no packages are provided", () => {
|
||||||
|
const result = parsePackagesFromRushAddArgs([]);
|
||||||
|
|
||||||
|
assert.deepEqual(result, []);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses packages from --package arguments", () => {
|
||||||
|
const result = parsePackagesFromRushAddArgs([
|
||||||
|
"--package",
|
||||||
|
"axios@1.9.0",
|
||||||
|
"--package",
|
||||||
|
"@scope/tool@2.0.0",
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.deepEqual(result, [
|
||||||
|
{ name: "axios", version: "1.9.0" },
|
||||||
|
{ name: "@scope/tool", version: "2.0.0" },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses packages from -p arguments", () => {
|
||||||
|
const result = parsePackagesFromRushAddArgs(["-p", "axios"]);
|
||||||
|
|
||||||
|
assert.deepEqual(result, [{ name: "axios", version: null }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses packages from --package=value arguments", () => {
|
||||||
|
const result = parsePackagesFromRushAddArgs(["--package=axios@^1.9.0"]);
|
||||||
|
|
||||||
|
assert.deepEqual(result, [{ name: "axios", version: "^1.9.0" }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores positional packages because rush add requires --package", () => {
|
||||||
|
const result = parsePackagesFromRushAddArgs(["axios", "--dev"]);
|
||||||
|
|
||||||
|
assert.deepEqual(result, []);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses aliases", () => {
|
||||||
|
const result = parsePackagesFromRushAddArgs(["--package", "server@npm:axios@1.9.0"]);
|
||||||
|
|
||||||
|
assert.deepEqual(result, [{ name: "axios", version: "1.9.0" }]);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue