mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Add --safe-chain-skip-minimum-package-age cli flag
This commit is contained in:
parent
f64ee3bccf
commit
752504dcc8
3 changed files with 103 additions and 3 deletions
|
|
@ -1,8 +1,9 @@
|
||||||
/**
|
/**
|
||||||
* @type {{loggingLevel: string | undefined}}
|
* @type {{loggingLevel: string | undefined, skipMinimumPackageAge: boolean | undefined}}
|
||||||
*/
|
*/
|
||||||
const state = {
|
const state = {
|
||||||
loggingLevel: undefined,
|
loggingLevel: undefined,
|
||||||
|
skipMinimumPackageAge: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
const SAFE_CHAIN_ARG_PREFIX = "--safe-chain-";
|
const SAFE_CHAIN_ARG_PREFIX = "--safe-chain-";
|
||||||
|
|
@ -14,6 +15,7 @@ const SAFE_CHAIN_ARG_PREFIX = "--safe-chain-";
|
||||||
export function initializeCliArguments(args) {
|
export function initializeCliArguments(args) {
|
||||||
// Reset state on each call
|
// Reset state on each call
|
||||||
state.loggingLevel = undefined;
|
state.loggingLevel = undefined;
|
||||||
|
state.skipMinimumPackageAge = undefined;
|
||||||
|
|
||||||
const safeChainArgs = [];
|
const safeChainArgs = [];
|
||||||
const remainingArgs = [];
|
const remainingArgs = [];
|
||||||
|
|
@ -27,6 +29,7 @@ export function initializeCliArguments(args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
setLoggingLevel(safeChainArgs);
|
setLoggingLevel(safeChainArgs);
|
||||||
|
setSkipMinimumPackageAge(safeChainArgs);
|
||||||
|
|
||||||
return remainingArgs;
|
return remainingArgs;
|
||||||
}
|
}
|
||||||
|
|
@ -47,6 +50,20 @@ function getLastArgEqualsValue(args, prefix) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string[]} args
|
||||||
|
* @param {string} flagName
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
function hasFlagArg(args, flagName) {
|
||||||
|
for (const arg of args) {
|
||||||
|
if (arg.toLowerCase() === flagName.toLowerCase()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string[]} args
|
* @param {string[]} args
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
|
|
@ -64,3 +81,19 @@ function setLoggingLevel(args) {
|
||||||
export function getLoggingLevel() {
|
export function getLoggingLevel() {
|
||||||
return state.loggingLevel;
|
return state.loggingLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string[]} args
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
function setSkipMinimumPackageAge(args) {
|
||||||
|
const flagName = SAFE_CHAIN_ARG_PREFIX + "skip-minimum-package-age";
|
||||||
|
|
||||||
|
if (hasFlagArg(args, flagName)) {
|
||||||
|
state.skipMinimumPackageAge = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSkipMinimumPackageAge() {
|
||||||
|
return state.skipMinimumPackageAge;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
import { describe, it } from "node:test";
|
import { describe, it } from "node:test";
|
||||||
import assert from "node:assert";
|
import assert from "node:assert";
|
||||||
import { initializeCliArguments, getLoggingLevel } from "./cliArguments.js";
|
import {
|
||||||
|
initializeCliArguments,
|
||||||
|
getLoggingLevel,
|
||||||
|
getSkipMinimumPackageAge,
|
||||||
|
} from "./cliArguments.js";
|
||||||
|
|
||||||
describe("initializeCliArguments", () => {
|
describe("initializeCliArguments", () => {
|
||||||
it("should return all args when no safe-chain args are present", () => {
|
it("should return all args when no safe-chain args are present", () => {
|
||||||
|
|
@ -118,4 +122,60 @@ describe("initializeCliArguments", () => {
|
||||||
assert.deepEqual(result, ["install"]);
|
assert.deepEqual(result, ["install"]);
|
||||||
assert.strictEqual(getLoggingLevel(), "silent");
|
assert.strictEqual(getLoggingLevel(), "silent");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should not set skipMinimumPackageAge when flag is absent", () => {
|
||||||
|
const args = ["install", "express", "--save"];
|
||||||
|
initializeCliArguments(args);
|
||||||
|
|
||||||
|
assert.strictEqual(getSkipMinimumPackageAge(), undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should set skipMinimumPackageAge to true when flag is present", () => {
|
||||||
|
const args = ["--safe-chain-skip-minimum-package-age", "install", "lodash"];
|
||||||
|
const result = initializeCliArguments(args);
|
||||||
|
|
||||||
|
assert.deepEqual(result, ["install", "lodash"]);
|
||||||
|
assert.strictEqual(getSkipMinimumPackageAge(), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle skip-minimum-package-age flag case-insensitively", () => {
|
||||||
|
const args = ["--SAFE-CHAIN-SKIP-MINIMUM-PACKAGE-AGE", "install"];
|
||||||
|
initializeCliArguments(args);
|
||||||
|
|
||||||
|
assert.strictEqual(getSkipMinimumPackageAge(), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should filter out skip-minimum-package-age flag from returned args", () => {
|
||||||
|
const args = [
|
||||||
|
"install",
|
||||||
|
"--safe-chain-skip-minimum-package-age",
|
||||||
|
"express",
|
||||||
|
"--save",
|
||||||
|
];
|
||||||
|
const result = initializeCliArguments(args);
|
||||||
|
|
||||||
|
assert.deepEqual(result, ["install", "express", "--save"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle skip-minimum-package-age with other safe-chain arguments", () => {
|
||||||
|
const args = [
|
||||||
|
"--safe-chain-logging=verbose",
|
||||||
|
"--safe-chain-skip-minimum-package-age",
|
||||||
|
"install",
|
||||||
|
"lodash",
|
||||||
|
];
|
||||||
|
const result = initializeCliArguments(args);
|
||||||
|
|
||||||
|
assert.deepEqual(result, ["install", "lodash"]);
|
||||||
|
assert.strictEqual(getLoggingLevel(), "verbose");
|
||||||
|
assert.strictEqual(getSkipMinimumPackageAge(), true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle skip-minimum-package-age flag in different positions", () => {
|
||||||
|
const args = ["install", "lodash", "--safe-chain-skip-minimum-package-age"];
|
||||||
|
const result = initializeCliArguments(args);
|
||||||
|
|
||||||
|
assert.deepEqual(result, ["install", "lodash"]);
|
||||||
|
assert.strictEqual(getSkipMinimumPackageAge(), true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,13 @@ export function getMinimumPackageAgeHours() {
|
||||||
return defaultMinimumPackageAge;
|
return defaultMinimumPackageAge;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultSkipMinimumPackageAge = false;
|
||||||
export function skipMinimumPackageAge() {
|
export function skipMinimumPackageAge() {
|
||||||
return false;
|
const cliValue = cliArguments.getSkipMinimumPackageAge();
|
||||||
|
|
||||||
|
if (cliValue === true) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultSkipMinimumPackageAge;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue