Skip to content

Commit 05d727a

Browse files
committed
feat: refactor, add exec
1 parent 9f061ce commit 05d727a

File tree

10 files changed

+398
-320
lines changed

10 files changed

+398
-320
lines changed

.changeset/wise-phones-remember.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperse/exec-program": patch
3+
---
4+
5+
refactor, add `exec`
Lines changed: 305 additions & 305 deletions
Large diffs are not rendered by default.

.yarnrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ enableGlobalCache: false
44

55
nodeLinker: node-modules
66

7-
yarnPath: .yarn/releases/yarn-4.2.2.cjs
7+
yarnPath: .yarn/releases/yarn-4.3.0.cjs

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,45 @@ npm i --save @hyperse/exec-program
3131

3232
### Usage
3333

34+
### runTsScript
35+
36+
```ts
37+
import { dirname, join } from 'node:path';
38+
import { fileURLToPath } from 'node:url';
39+
import { runTsScript } from '@hyperse/exec-program';
40+
41+
const getDirname = (url: string, ...paths: string[]) => {
42+
return join(dirname(fileURLToPath(url)), ...paths);
43+
};
44+
45+
const cliPath = getDirname(import.meta.url, './cli-test.ts');
46+
const { stderr, stdout } = await runTsScript(cliPath);
47+
console.log(stderr, stdout);
48+
```
49+
50+
### exec
51+
52+
import { exec } from '@hyperse/exec-program';
53+
54+
```ts
55+
const { stdout, stderr } = await exec(
56+
'npm',
57+
['i', '--no-save', '--no-package-lock', ...toInstall],
58+
{
59+
cwd: target.directory,
60+
maxBuffer: TEN_MEGA_BYTE,
61+
env: this.options.npmEnv,
62+
}
63+
);
64+
```
65+
66+
```ts
67+
await exec('npm', ['pack', directory], {
68+
cwd: this.uniqueDir,
69+
maxBuffer: TEN_MEGA_BYTE,
70+
});
71+
```
72+
3473
#### unit test
3574

3675
1. config `tsconfig.json`
@@ -83,7 +122,7 @@ const cliPath = getDirname(import.meta.url, './cli-test.ts');
83122

84123
describe('test suites of exec program', () => {
85124
it('should correct invoke cli.ts', async () => {
86-
const { stderr, stdout } = await runTsScript(cliPath, {});
125+
const { stderr, stdout } = await runTsScript(cliPath);
87126
console.log(stderr, stdout);
88127
expect(stderr).toBe('');
89128
expect(stdout).toMatch(/cli.../);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"cz-conventional-changelog": "3.3.0",
7777
"eslint": "^9.4.0",
7878
"husky": "9.0.11",
79-
"lint-staged": "15.2.6",
79+
"lint-staged": "15.2.7",
8080
"npm-run-all": "^4.1.5",
8181
"tsup": "^8.1.0",
8282
"typescript": "^5.4.5",
@@ -89,5 +89,5 @@
8989
"publishConfig": {
9090
"access": "public"
9191
},
92-
"packageManager": "yarn@4.2.2"
92+
"packageManager": "yarn@4.3.0"
9393
}

src/exec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { execa, Options } from 'execa';
2+
3+
/**
4+
* Execute a file with arguments and options
5+
* @example
6+
* ```ts
7+
* const { stdout, stderr } = await exec(
8+
* 'npm',
9+
* ['i', '--no-save', '--no-package-lock', ...toInstall],
10+
* {
11+
* cwd: target.directory,
12+
* maxBuffer: TEN_MEGA_BYTE,
13+
* env: this.options.npmEnv,
14+
* }
15+
* );
16+
* ```
17+
* @example
18+
* ```ts
19+
* await exec('npm', ['pack', directory], {
20+
* cwd: this.uniqueDir,
21+
* maxBuffer: TEN_MEGA_BYTE,
22+
* });
23+
* ```
24+
* @param file - The program/script to execute, as a string or file URL
25+
* @param args - Arguments to pass to `file` on execution.
26+
* @param options - Options to pass to `execa`
27+
* @returns A `ResultPromise` that is both:
28+
*/
29+
export function exec<T extends Options>(
30+
file: string,
31+
args?: readonly string[],
32+
options?: T
33+
) {
34+
return execa(file, args, options);
35+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './exec.js';
12
export * from './runTsScript.js';

src/runTsScript.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import { execa } from 'execa';
99
*/
1010
export const runTsScript = <T extends Options>(
1111
program: string,
12-
options: T,
13-
...args: string[]
12+
args?: readonly string[],
13+
options?: T
1414
) => {
1515
const moduleArgs = [
1616
'--import',
1717
'@hyperse/ts-node-paths/register',
1818
'--no-warnings',
1919
];
20-
return execa('node', moduleArgs.concat(program, ...args), {
21-
...options,
22-
});
20+
return execa('node', moduleArgs.concat(program, ...(args || [])), options);
2321
};

tests/exec.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const cliPath = getDirname(import.meta.url, './cli-test.ts');
1010

1111
describe('test suites of exec program', () => {
1212
it('should correct invoke cli.ts', async () => {
13-
const { stderr, stdout } = await runTsScript(cliPath, {});
13+
const { stderr, stdout } = await runTsScript(cliPath);
1414
console.log(stderr, stdout);
1515
expect(stderr).toBe('');
1616
expect(stdout).toMatch(/cli.../);

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ __metadata:
970970
eslint: "npm:^9.4.0"
971971
execa: "npm:^9.2.0"
972972
husky: "npm:9.0.11"
973-
lint-staged: "npm:15.2.6"
973+
lint-staged: "npm:15.2.7"
974974
npm-run-all: "npm:^4.1.5"
975975
tsup: "npm:^8.1.0"
976976
typescript: "npm:^5.4.5"
@@ -5447,9 +5447,9 @@ __metadata:
54475447
languageName: node
54485448
linkType: hard
54495449

5450-
"lint-staged@npm:15.2.6":
5451-
version: 15.2.6
5452-
resolution: "lint-staged@npm:15.2.6"
5450+
"lint-staged@npm:15.2.7":
5451+
version: 15.2.7
5452+
resolution: "lint-staged@npm:15.2.7"
54535453
dependencies:
54545454
chalk: "npm:~5.3.0"
54555455
commander: "npm:~12.1.0"
@@ -5463,7 +5463,7 @@ __metadata:
54635463
yaml: "npm:~2.4.2"
54645464
bin:
54655465
lint-staged: bin/lint-staged.js
5466-
checksum: 10/d2e7b610603ace80521d80afa7d355864a0f5f574aa2a2edbf5d96486e99ceeebb4d988e733c0b6ad8bfa899092f1c20db5cb02a0cc3e260919aeb92ef6f2d79
5466+
checksum: 10/7557bcf4e8dc0555f2c7e6a8ab6f5dfd7faaaed632a5d9e598768c86f786267ca216f8005068796a8118884d322a1c7f8f93e57c01b3e556475b77297ddad34f
54675467
languageName: node
54685468
linkType: hard
54695469

0 commit comments

Comments
 (0)