From 2b266c6aea15d1ab32bff6d51459b0a1738941fa Mon Sep 17 00:00:00 2001 From: UnderKoen Date: Thu, 30 May 2024 11:38:58 +0200 Subject: [PATCH] feat: add `_${process.arch}` scripts for example `_x64` or `_arm64` --- README.md | 22 +++++++++++++++++ src/Executor.ts | 65 ++++++++++++++++++++++++++++--------------------- test.js | 7 ++++++ test/bin.ts | 1 + 4 files changed, 67 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 9d2e7bf..753575c 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,28 @@ module.exports = { `bsm example` will execute the `example._win32` on Windows, `example._darwin` on MacOS, and `example._linux` on Linux. When no OS specific script is found, the default script will be executed. +#### Chipset Specified scripts + +You can specify OS specific scripts with `_x64`, `_x32`, and `_arm64` keys. +All Chipsets can be seen [here](https://nodejs.org/api/os.html#os_os_arch). + +```javascript +module.exports = { + scripts: { + example: { + _x64: "echo 64-bit", + _x32: "echo 32-bit", + _arm: "echo ARM", + _arm64: "echo ARM64", + _default: "echo Unknown", + }, + }, +}; +``` + +`bsm example` will execute the `example._win32` on Windows, `example._darwin` on MacOS, and `example._linux` on Linux. +When no OS specific script is found, the default script will be executed. + ### Script arguments All arguments passed to the `bsm` command after `--` will be passed to all specified scripts. diff --git a/src/Executor.ts b/src/Executor.ts index 3fe82f9..d392326 100644 --- a/src/Executor.ts +++ b/src/Executor.ts @@ -14,6 +14,8 @@ type Options = { }; class Executor { + //We test this with bin.ts + /* c8 ignore next 4 */ static async run(script: string): Promise { const config = ConfigLoader.config; await Executor.runScript(config.scripts, script.split("."), [], {}); @@ -64,6 +66,8 @@ class Executor { } if (ConfigLoader.config.config?.defaultHelpBehavior === "interactive") { + /* c8 ignore next 9 */ + //Interactive currently cannot be tested const scripts = await Interactive.selectScript(ConfigLoader.config, { _: [path.join(".")], }); @@ -378,30 +382,37 @@ class Executor { return isCI; } - static async runObject(context: TScripts, path: string[], options: Options) { - const platform = `_${process.platform}`; + static get objectScripts(): string[] { + const scriptNames = [ + `_${process.platform}`, + `_${process.arch}`, + "_default", + ]; + if (Executor._isCI) { + scriptNames.unshift("_ci"); + } - if (Executor._isCI && Object.hasOwn(context, "_ci")) { - await Executor.runScript(context["_ci"], [], [...path, "_ci"], options); - } else if (Object.hasOwn(context, platform)) { - await Executor.runScript( - context[platform], - [], - [...path, platform], - options, - ); - } else if (Object.hasOwn(context, "_default")) { - await Executor.runScript( - context["_default"], - [], - [...path, "_default"], - options, - ); - } else { - await Executor.notFound([...path, "_default"], options, context); + return scriptNames; + } + + static async runObject(context: TScripts, path: string[], options: Options) { + for (const script of Executor.objectScripts) { + if (Object.hasOwn(context, script)) { + await Executor.runScript( + context[script], + [], + [...path, script], + options, + ); + return; + } } + + await Executor.notFound([...path, "_default"], options, context); } + //TODO currently only used in interactive + /* c8 ignore next 15 */ static isExecutable(context: TScript): boolean { if (typeof context === "function") return true; if (typeof context === "string") return true; @@ -409,15 +420,13 @@ class Executor { if (typeof context !== "object") return false; if (Array.isArray(context)) return true; - const platform = process.platform; - - if (Executor._isCI && Object.hasOwn(context, "_ci")) { - return true; - } else if (Object.hasOwn(context, platform)) { - return true; - } else { - return Object.hasOwn(context, "_default"); + for (const script of Executor.objectScripts) { + if (Object.hasOwn(context, script)) { + return true; + } } + + return false; } } diff --git a/test.js b/test.js index 033f112..c26575c 100644 --- a/test.js +++ b/test.js @@ -8,6 +8,13 @@ module.exports = { _win32: "echo Windows", _default: "echo Not Windows", }, + arch: { + _x64: "echo 64-bit", + _x32: "echo 32-bit", + _arm: "echo ARM", + _arm64: "echo ARM64", + _default: "echo Unknown", + }, args: { _pre: "echo pre args", _default: "bsm testing.args.* --", diff --git a/test/bin.ts b/test/bin.ts index 145b367..fba9de1 100644 --- a/test/bin.ts +++ b/test/bin.ts @@ -6,6 +6,7 @@ import fs from "fs"; const commands = [ "testing.default", // "testing.os", //Skip on CI + // "testing.arch", //Skip on CI "testing.args -- WOWOWOW", "testing.array", "testing.hooks",