Skip to content

Commit

Permalink
feat: add $alias option
Browse files Browse the repository at this point in the history
can be used to add shorthand names to long scripts
  • Loading branch information
UnderKoen committed Jun 18, 2024
1 parent 11bf001 commit 906f2d2
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@types/sinon": "^17.0.3",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"@under_koen/bsm": "^1.3.3",
"@under_koen/bsm": "^1.4.0",
"c8": "^9.1.0",
"ci-info": "^4.0.0",
"cpy-cli": "^5.0.0",
Expand Down
1 change: 1 addition & 0 deletions package.scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
prettier: "prettier --check .",
},
test: {
$alias: "t",
$env: {
TEST: "TRUE",
NODE_ENV: "test",
Expand Down
39 changes: 38 additions & 1 deletion src/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,17 @@ class Executor {
options,
);
} else {
return await Executor.notFound([...path, sub], options, context);
const alias = Executor.subscriptWithAlias(context, sub);
if (alias) {
await Executor.runScript(
alias[1],
script.slice(1),
[...path, alias[0]],
options,
);
} else {
return await Executor.notFound([...path, sub], options, context);
}
}
}

Expand All @@ -284,6 +294,33 @@ class Executor {
}
}

static subscriptWithAlias(
context: TScripts | TScript[],
alias: string,
): [string, TScript] | undefined {
for (const entry of Object.entries(context)) {
const script = entry[1];
if (typeof script !== "object") continue;
if (Array.isArray(script)) continue;

if (Object.hasOwn(script, "$alias")) {
const aliases = script["$alias"];

if (typeof aliases === "string") {
if (aliases === alias) {
return entry;
}
} else if (Array.isArray(aliases)) {
if (aliases.includes(alias)) {
return entry;
}
}
}
}

return undefined;
}

static getEnv(context: TScript): Record<string, string> {
if (typeof context === "string") {
if (context === "") {
Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ module.exports = {
scripts: "bsm --debug scripts",
extends: "bsm --debug extends",
},
alias: {
_default: "bsm ~.n ~.l ~.ls ~.d.n",
normal: {
$alias: "n",
_default: "echo normal",
},
list: {
$alias: ["l", "ls"],
_default: "echo list",
},
deep: {
$alias: "d",
normal: {
$alias: "n",
_default: "echo deep normal",
},
},
},
},
},
};
116 changes: 116 additions & 0 deletions test/Executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,122 @@ envSuite("should not override environment variables", async () => {
envSuite.run();
// endregion

// region executeObject() - $alias
const aliasSuite = suite("executeObject() - $alias");

aliasSuite("should find script with alias", async () => {
// Arrange
const runScript = sinon.stub(Executor, "runScript");

// Act
await Executor.executeObject(
{
test: {
$alias: "alias",
},
not: "not",
other: {
$alias: "other",
},
},
["alias"],
[],
{},
);

// Assert
assert.equal(runScript.callCount, 1);
assert.equal(runScript.args[0][0], {
$alias: "alias",
});
assert.equal(runScript.args[0][2], ["test"]);
});

aliasSuite("should find script with multiple aliases", async () => {
// Arrange
const runScript = sinon.stub(Executor, "runScript");

// Act
await Executor.executeObject(
{
test: {
$alias: ["alias", "alias2"],
},
not: "not",
other: {
$alias: "other",
},
},
["alias2"],
[],
{},
);

// Assert
assert.equal(runScript.callCount, 1);
assert.equal(runScript.args[0][0], {
$alias: ["alias", "alias2"],
});
assert.equal(runScript.args[0][2], ["test"]);
});

aliasSuite("should use not use alias if normal script is found", async () => {
// Arrange
const runScript = sinon.stub(Executor, "runScript");

// Act
await Executor.executeObject(
{
test: {
$alias: "alias",
},
alias: "not",
other: {
$alias: "other",
},
},
["alias"],
[],
{},
);

// Assert
assert.equal(runScript.callCount, 1);
assert.equal(runScript.args[0][0], "not");
assert.equal(runScript.args[0][2], ["alias"]);
});

aliasSuite("should use first alias if multiple scripts are found", async () => {
// Arrange
const runScript = sinon.stub(Executor, "runScript");

// Act
await Executor.executeObject(
{
list: ["1"],
test: {
$alias: ["alias", "alias2"],
},
other: {
$alias: "alias",
},
},
["alias"],
[],
{},
);

// Assert
assert.equal(runScript.callCount, 1);
assert.equal(runScript.args[0][0], {
$alias: ["alias", "alias2"],
});
assert.equal(runScript.args[0][2], ["test"]);
});

aliasSuite.run();
// endregion

// endregion

// region runScript()
Expand Down
1 change: 1 addition & 0 deletions test/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const commands = [
"testing.env.overrides",
"testing.env.file",
"testing.debug",
"testing.alias",
];

for (const command of commands) {
Expand Down
11 changes: 11 additions & 0 deletions test/snapshots/testing.alias.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exitcode: 0

> bsm ~.n ~.l ~.ls ~.d.n (testing.alias._default)
> echo normal (testing.alias.normal._default)
normal
> echo list (testing.alias.list._default)
list
> echo list (testing.alias.list._default)
list
> echo deep normal (testing.alias.deep.normal._default)
deep normal
4 changes: 4 additions & 0 deletions test/snapshots/testing.debug.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bsm ~._notFound
bsm ~.*
bsm --debug scripts
bsm --debug extends
bsm ~.n ~.l ~.ls ~.d.n
echo hooks1
echo hooks2
echo pre test
Expand All @@ -49,7 +50,10 @@ echo onError
exit 3
echo finally
echo pre test
echo normal
echo list
echo $BSM_PATH
echo %BSM_PATH%
echo testing.debug
echo deep normal
> bsm --debug extends (testing.debug.extends)
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true
}
"esModuleInterop": true,
},
}

0 comments on commit 906f2d2

Please sign in to comment.