Skip to content

Commit

Permalink
feat: add _${process.arch} scripts
Browse files Browse the repository at this point in the history
for example `_x64` or `_arm64`
  • Loading branch information
UnderKoen committed May 30, 2024
1 parent d93b4db commit 3f79ef1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 28 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
65 changes: 37 additions & 28 deletions src/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Options = {
};

class Executor {
//We test this with bin.ts
/* c8 ignore next 4 */
static async run(script: string): Promise<void> {
const config = ConfigLoader.config;
await Executor.runScript(config.scripts, script.split("."), [], {});
Expand Down Expand Up @@ -64,6 +66,8 @@ class Executor {
}

if (ConfigLoader.config.config?.defaultHelpBehavior === "interactive") {
/* c8 ignore next 8 */
//Interactive currently cannot be tested
const scripts = await Interactive.selectScript(ConfigLoader.config, {
_: [path.join(".")],
});
Expand Down Expand Up @@ -378,46 +382,51 @@ 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;
// Unknown type
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;
}
}

Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.* --",
Expand Down
1 change: 1 addition & 0 deletions test/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 3f79ef1

Please sign in to comment.