Skip to content

Commit 3fb5f0c

Browse files
v2 (#49)
* Update to @raycast/api@1.94.0 * make it tree-shakeable * remove deprecated MutableRefObject * abort requests when mutating * complete tree shaking * add changelog * add runPowerShellScript * add docs * fix types * update workflow * use React.JSX
1 parent 4478c23 commit 3fb5f0c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+14758
-4139
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ jobs:
2020
steps:
2121
- name: Checkout
2222
uses: raycast/github-actions/git-checkout@v1.13.0
23-
- name: Setup node
24-
uses: raycast/github-actions/setup-node@v1.13.0
23+
- name: Setup Node
24+
uses: actions/setup-node@v4
25+
with:
26+
node-version: 22.14.0
2527
- name: Npm Install
2628
run: npm ci
2729
- name: Npm Lint

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
dist
3+
.parcel-cache

docs/utils-reference/functions/runAppleScript.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Function that executes an AppleScript script.
44

5+
{% hint style="info" %}
6+
Only available on macOS
7+
{% endhint %}
8+
59
## Signature
610

711
There are two ways to use the function.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# `runPowerShellScript`
2+
3+
Function that executes an PowerShell script.
4+
5+
{% hint style="info" %}
6+
Only available on Windows
7+
{% endhint %}
8+
9+
## Signature
10+
11+
```ts
12+
function runPowerShellScript<T>(
13+
script: string,
14+
options?: {
15+
signal?: AbortSignal;
16+
timeout?: number;
17+
parseOutput?: ParseExecOutputHandler<T>;
18+
},
19+
): Promise<T>;
20+
```
21+
22+
### Arguments
23+
24+
- `script` is the script to execute.
25+
26+
With a few options:
27+
28+
- `options.signal` is a Signal object that allows you to abort the request if required via an AbortController object.
29+
- `options.timeout` is a number. If greater than `0`, the parent will send the signal `SIGTERM` if the script runs longer than timeout milliseconds. By default, the execution will timeout after 10000ms (eg. 10s).
30+
- `options.parseOutput` is a function that accepts the output of the script as an argument and returns the data the hooks will return - see [ParseExecOutputHandler](#parseexecoutputhandler). By default, the function will return `stdout` as a string.
31+
32+
### Return
33+
34+
Returns a Promise which resolves to a string by default. You can control what it returns by passing `options.parseOutput`.
35+
36+
## Example
37+
38+
```tsx
39+
import { showHUD } from "@raycast/api";
40+
import { runPowerShellScript } from "@raycast/utils";
41+
42+
export default async function () {
43+
const res = await runPowerShellScript(
44+
`
45+
Write-Host "hello, world."
46+
`,
47+
);
48+
await showHUD(res);
49+
}
50+
```
51+
52+
## Types
53+
54+
### ParseExecOutputHandler
55+
56+
A function that accepts the output of the script as an argument and returns the data the function will return.
57+
58+
```ts
59+
export type ParseExecOutputHandler<T> = (args: {
60+
/** The output of the script on stdout. */
61+
stdout: string;
62+
/** The output of the script on stderr. */
63+
stderr: string;
64+
error?: Error | undefined;
65+
/** The numeric exit code of the process that was run. */
66+
exitCode: number | null;
67+
/** The name of the signal that was used to terminate the process. For example, SIGFPE. */
68+
signal: NodeJS.Signals | null;
69+
/** Whether the process timed out. */
70+
timedOut: boolean;
71+
/** The command that was run, for logging purposes. */
72+
command: string;
73+
/** The options passed to the script, for logging purposes. */
74+
options?: ExecOptions | undefined;
75+
}) => T;
76+
```

docs/utils-reference/getting-started.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ npm install --save @raycast/utils
1616

1717
## Changelog
1818

19+
### v2.0.0
20+
21+
- The library can now be tree-shaken, reducing its size considerably.
22+
- When using `usePromise` and mutating the data with an optimistic update before it is fetched, the current fetch will be aborted to avoid a race condition.
23+
- Add a new [`runPowerShellScript`](./functions/runPowerShellScript.md) function.
24+
1925
### v1.19.1
2026

2127
- Fixed an issue where arguments weren't passed to `withCache`.

docs/utils-reference/react-hooks/useCachedPromise.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function useCachedPromise<T, U>(
2222
options?: {
2323
initialData?: U;
2424
keepPreviousData?: boolean;
25-
abortable?: MutableRefObject<AbortController | null | undefined>;
25+
abortable?: RefObject<AbortController | null | undefined>;
2626
execute?: boolean;
2727
onError?: (error: Error) => void;
2828
onData?: (data: Result<T>) => void;

docs/utils-reference/react-hooks/usePromise.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function usePromise<T>(
1515
fn: T,
1616
args?: Parameters<T>,
1717
options?: {
18-
abortable?: MutableRefObject<AbortController | null | undefined>;
18+
abortable?: RefObject<AbortController | null | undefined>;
1919
execute?: boolean;
2020
onError?: (error: Error) => void;
2121
onData?: (data: Result<T>) => void;

docs/utils-reference/react-hooks/useStreamJSON.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import { useCallback, useState } from "react";
7171

7272
type Formula = { name: string; desc?: string };
7373

74-
export default function Main(): JSX.Element {
74+
export default function Main(): React.JSX.Element {
7575
const [searchText, setSearchText] = useState("");
7676

7777
const formulaFilter = useCallback(
@@ -122,7 +122,7 @@ import { setTimeout } from "timers/promises";
122122

123123
type Formula = { name: string; desc?: string };
124124

125-
export default function Main(): JSX.Element {
125+
export default function Main(): React.JSX.Element {
126126
const [searchText, setSearchText] = useState("");
127127

128128
const formulaFilter = useCallback(

0 commit comments

Comments
 (0)