@reliverse/rempts 1.7.15 → 1.7.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -79
- package/bin/components/launcher/run-command.js +16 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -662,111 +662,94 @@ export default defineCommand({
|
|
|
662
662
|
});
|
|
663
663
|
```
|
|
664
664
|
|
|
665
|
-
|
|
666
|
-
// example/launcher/app/minimal/cmd.ts
|
|
667
|
-
|
|
668
|
-
import { relinka } from "@reliverse/relinka";
|
|
669
|
-
import { defineArgs, defineCommand } from "@reliverse/rempts";
|
|
670
|
-
|
|
671
|
-
export default defineCommand({
|
|
672
|
-
meta: {
|
|
673
|
-
name: "minimal",
|
|
674
|
-
description: "hello world",
|
|
675
|
-
},
|
|
676
|
-
args: defineArgs({
|
|
677
|
-
name: {
|
|
678
|
-
type: "string",
|
|
679
|
-
description: "your name",
|
|
680
|
-
required: true,
|
|
681
|
-
},
|
|
682
|
-
}),
|
|
683
|
-
run({ args }) {
|
|
684
|
-
relinka("success", `👋 Hello, ${args.name}!`);
|
|
685
|
-
},
|
|
686
|
-
});
|
|
687
|
-
```
|
|
688
|
-
|
|
689
|
-
**Pro Tips & Best Practices**:
|
|
665
|
+
### Loading Commands with `loadCommand`
|
|
690
666
|
|
|
691
|
-
|
|
692
|
-
- You can use any name for the `cmds.ts` file and store it anywhere, but `src/app/cmds.ts` is a good convention you can follow.
|
|
693
|
-
- Use the async function pattern for lazy loading if you have many commands or care about startup performance.
|
|
694
|
-
- Use eager loading (const) for small CLIs or demos where simplicity is preferred.
|
|
667
|
+
The `loadCommand` utility helps you load command files from your filesystem. It automatically handles:
|
|
695
668
|
|
|
696
|
-
|
|
669
|
+
- Relative paths (both `./build` and `build` work the same)
|
|
670
|
+
- Automatic detection of `cmd.{ts,js}` files
|
|
671
|
+
- Clear error messages when files are not found
|
|
697
672
|
|
|
698
673
|
```ts
|
|
699
|
-
|
|
674
|
+
import { loadCommand } from "@reliverse/rempts";
|
|
700
675
|
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
676
|
+
// These are equivalent:
|
|
677
|
+
const cmd1 = await loadCommand("./build"); // Looks for build/cmd.ts or build/cmd.js
|
|
678
|
+
const cmd2 = await loadCommand("build"); // Same as above
|
|
679
|
+
const cmd3 = await loadCommand("./build/cmd"); // Explicit path to cmd file
|
|
704
680
|
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
// ...more commands
|
|
681
|
+
// You can then use the loaded command with runCmd:
|
|
682
|
+
await runCmd(cmd1, ["--some-flag"]);
|
|
710
683
|
```
|
|
711
684
|
|
|
712
|
-
Usage:
|
|
713
|
-
|
|
714
685
|
```ts
|
|
715
|
-
//
|
|
686
|
+
// src/app/cmds.ts
|
|
687
|
+
export const getBuildCmd = async (): Promise<Command> => loadCommand("./build");
|
|
716
688
|
|
|
717
|
-
|
|
689
|
+
// src/cli.ts
|
|
718
690
|
import { runCmd } from "@reliverse/rempts";
|
|
719
|
-
|
|
720
|
-
await runCmd(await
|
|
721
|
-
// OR:
|
|
722
|
-
// const hooksCmd = await cmdHooks();
|
|
723
|
-
// await runCmd(hooksCmd, ["--flag"]);
|
|
691
|
+
import { getBuildCmd } from "./app/cmds";
|
|
692
|
+
await runCmd(await getBuildCmd(), ["--prod"]);
|
|
724
693
|
```
|
|
725
694
|
|
|
726
|
-
**
|
|
727
|
-
|
|
728
|
-
```ts
|
|
729
|
-
// example/launcher/app/cmds.ts
|
|
730
|
-
export const hooksCmd = (await import("./hooks/cmd.js")).default;
|
|
731
|
-
export const fooCmd = (await import("./foo/cmd.js")).default;
|
|
732
|
-
// ...more commands
|
|
733
|
-
```
|
|
695
|
+
**Error Handling:**
|
|
696
|
+
If the command file is not found, you'll get a clear error message:
|
|
734
697
|
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
await runCmd(hooksCmd, ["--flag"]);
|
|
698
|
+
```bash
|
|
699
|
+
No command file found in /path/to/build. Expected to find either:
|
|
700
|
+
- /path/to/build/cmd.ts
|
|
701
|
+
- /path/to/build/cmd.js
|
|
702
|
+
Please ensure one of these files exists and exports a default command.
|
|
742
703
|
```
|
|
743
704
|
|
|
744
|
-
**
|
|
705
|
+
**Best Practices:**
|
|
745
706
|
|
|
746
|
-
|
|
707
|
+
- Use `loadCommand` when you need to load commands from the filesystem
|
|
708
|
+
- Use `runCmd` to execute the loaded command with arguments
|
|
709
|
+
- Keep your command files in a consistent location (e.g., `src/app/yourCmdName/cmd.ts`)
|
|
710
|
+
- Export commands from a central file like `src/app/cmds.ts` for better organization
|
|
747
711
|
|
|
748
712
|
```ts
|
|
749
|
-
|
|
750
|
-
import {
|
|
713
|
+
// example/launcher/app/cmds.ts
|
|
714
|
+
import { loadCommand } from "@reliverse/rempts";
|
|
751
715
|
|
|
752
|
-
|
|
753
|
-
|
|
716
|
+
export async function getBuildCmd() {
|
|
717
|
+
return loadCommand("./build");
|
|
718
|
+
}
|
|
754
719
|
|
|
755
|
-
|
|
720
|
+
export async function getDeployCmd() {
|
|
721
|
+
return loadCommand("./deploy");
|
|
722
|
+
}
|
|
756
723
|
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
await
|
|
724
|
+
// Usage:
|
|
725
|
+
import { getBuildCmd } from "./cmds";
|
|
726
|
+
const buildCmd = await getBuildCmd();
|
|
727
|
+
await runCmd(buildCmd, ["--prod"]);
|
|
760
728
|
```
|
|
761
729
|
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
**Performance Note:**
|
|
730
|
+
```ts
|
|
731
|
+
// example/launcher/app/minimal/cmd.ts
|
|
765
732
|
|
|
766
|
-
|
|
767
|
-
|
|
733
|
+
import { relinka } from "@reliverse/relinka";
|
|
734
|
+
import { defineArgs, defineCommand } from "@reliverse/rempts";
|
|
768
735
|
|
|
769
|
-
|
|
736
|
+
export default defineCommand({
|
|
737
|
+
meta: {
|
|
738
|
+
name: "minimal",
|
|
739
|
+
description: "hello world",
|
|
740
|
+
},
|
|
741
|
+
args: defineArgs({
|
|
742
|
+
name: {
|
|
743
|
+
type: "string",
|
|
744
|
+
description: "your name",
|
|
745
|
+
required: true,
|
|
746
|
+
},
|
|
747
|
+
}),
|
|
748
|
+
run({ args }) {
|
|
749
|
+
relinka("success", `👋 Hello, ${args.name}!`);
|
|
750
|
+
},
|
|
751
|
+
});
|
|
752
|
+
```
|
|
770
753
|
|
|
771
754
|
## Argument Types: Usage Comparison
|
|
772
755
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { resolve } from "@reliverse/pathkit";
|
|
1
|
+
import { resolve, dirname } from "@reliverse/pathkit";
|
|
2
2
|
import fs from "@reliverse/relifso";
|
|
3
3
|
import { relinka } from "@reliverse/relinka";
|
|
4
4
|
import { createJiti } from "jiti";
|
|
5
|
+
import process from "node:process";
|
|
5
6
|
const jiti = createJiti(import.meta.url, {
|
|
6
7
|
debug: process.env.NODE_ENV === "development",
|
|
7
8
|
fsCache: true,
|
|
@@ -9,8 +10,21 @@ const jiti = createJiti(import.meta.url, {
|
|
|
9
10
|
});
|
|
10
11
|
export async function loadCommand(cmdPath) {
|
|
11
12
|
try {
|
|
13
|
+
const err = new Error();
|
|
14
|
+
const stack = err.stack?.split("\n");
|
|
15
|
+
let callerFile;
|
|
16
|
+
if (stack) {
|
|
17
|
+
for (const line of stack) {
|
|
18
|
+
const match = /\((.*):(\d+):(\d+)\)/.exec(line) || /at (.*):(\d+):(\d+)/.exec(line);
|
|
19
|
+
if (match?.[1] && !match[1].includes("run-command")) {
|
|
20
|
+
callerFile = match[1];
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const callerDir = callerFile ? dirname(callerFile) : process.cwd();
|
|
12
26
|
const normalizedPath = cmdPath.replace(/^\.\//, "");
|
|
13
|
-
const resolvedPath =
|
|
27
|
+
const resolvedPath = resolve(callerDir, normalizedPath);
|
|
14
28
|
if (!resolvedPath.endsWith("cmd.ts") && !resolvedPath.endsWith("cmd.js")) {
|
|
15
29
|
const possiblePaths = [
|
|
16
30
|
resolve(resolvedPath, "cmd.ts"),
|