Skip to content

Commit 8c2e326

Browse files
authored
chore: create script runner devtool (#5074)
1 parent e28e7b7 commit 8c2e326

File tree

6 files changed

+121
-20
lines changed

6 files changed

+121
-20
lines changed

CONTRIBUTING.md

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,50 +196,66 @@ the generated code change to your PR. Here's how to generate clients:
196196
clients/client-X> yarn generate:client
197197
```
198198

199-
### CLI dispatch helper
199+
### CLI dispatch helpers
200200

201-
There is an optional CLI helper.
202-
The CLI helper assists in the dispatch of commands to package contexts.
201+
There are optional CLI helpers.
202+
The CLI helpers assist in the dispatch of commands to package or subfolder contexts.
203203

204-
To activate the default alias run:
204+
To activate the default aliases run:
205205

206206
```
207207
. ./scripts/cli-dispatcher/set-alias.sh
208208
```
209209

210210
This enables the command bin/exe
211211

212-
```
213-
b
214-
```
212+
`b` and `r`.
215213

216214
#### General Syntax
217215

218-
```
216+
```sh
219217
b (package name query) - (npm script query)
220218
```
221219

220+
```sh
221+
r (workspace script query)
222+
```
223+
222224
#### Syntax Examples:
223225

224-
Usage examples
226+
Usage examples for `r`:
227+
228+
`r` depends on what files exist in your unversioned `workspace` directory at the repository root.
229+
It will run the first matching `*.js`, `*.mjs`, or `*.ts` file.
225230

231+
```sh
232+
r dyn test
226233
```
227-
b s3 - b
234+
235+
```sh
236+
npx esbuilder-runner ./workspace/dynamodb/test.ts # (if *.ts file)
237+
node ./workspace/dynamodb/test.mjs # (if *.mjs file)
228238
```
229239

230-
yarn **b**uild in clients/client-**s3**
240+
Usage examples for `b`:
231241

242+
```sh
243+
b s3 - b
232244
```
245+
246+
matches to: yarn **b**uild in clients/client-**s3**
247+
248+
```sh
233249
b mar ent - doc
234250
```
235251

236-
yarn build:**doc**s in clients/client-**mar**ketplace-**ent**itlement-service
252+
matches to: yarn build:**doc**s in clients/client-**mar**ketplace-**ent**itlement-service
237253

238-
```
254+
```sh
239255
b m sign - t
240256
```
241257

242-
yarn **t**est in packages/**m**iddleware-**sign**ing
258+
matches to: yarn **t**est in packages/**m**iddleware-**sign**ing
243259

244260
The package name query is used to find the package within clients, lib, or packages, and the npm script query is used to
245261
find a command to execute from within `package.json` `scripts`.

scripts/cli-dispatcher/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ async function main() {
115115
console.info("Location:", target.location);
116116
await spawnProcess("yarn", [script], {
117117
cwd: target.location,
118+
stdio: "inherit",
118119
});
119120
return;
120121
};

scripts/cli-dispatcher/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
## CLI dispatcher
22

3-
This script provides a CLI helper to send shorthand commands to a matching package.
3+
These scripts provides CLI helpers to send shorthand commands to a matching package.
44

55
### Usage
66

77
First, alias the script entry point. An example is provided in `./set-alias.sh`.
88

9-
Then run the script with no arguments to see the help message detailing usage.
9+
Then run the script with the new alias `b` with no arguments to see the help message detailing usage.

scripts/cli-dispatcher/set-alias.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
# Set a command line alias to make running the dispatcher easier.
44

5-
alias b="node ./scripts/cli-dispatcher/index.js"
5+
alias b="node ./scripts/cli-dispatcher/index.js"
6+
alias r="node ./scripts/cli-dispatcher/workspace.js"

scripts/cli-dispatcher/workspace.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* This script runs files by filename in the root/workspace (unversioned) directory.
5+
*
6+
* @example
7+
* r dyn test
8+
*
9+
* would match to a file in /workspace/dynamodb/test.ts and execute it with esbuild runner.
10+
*
11+
* @example
12+
* r ssec
13+
*
14+
* would run "node /workspace/s3/ssec.mjs"
15+
*
16+
* The workspace directory is meant as a place to test short scripts
17+
* that make use of the packages built in the root monorepo workspace.
18+
*/
19+
20+
const path = require("path");
21+
const walk = require("../utils/walk");
22+
23+
const matcher = require("./lib/matcher");
24+
const matchSorter = require("./lib/matchSorter");
25+
26+
const root = path.join(__dirname, "..", "..");
27+
const workspaceFolder = path.join(root, "workspace");
28+
29+
const USE_NODE = 1;
30+
const USE_TYPESCRIPT = 2;
31+
const runnable = { ".js": USE_NODE, ".ts": USE_TYPESCRIPT, ".cjs": USE_NODE, ".mjs": USE_NODE };
32+
33+
const execute = async (cwd, exe, commands) => {
34+
const { spawnProcess } = require("../utils/spawn-process");
35+
await spawnProcess(exe, [...commands], {
36+
cwd,
37+
stdio: "inherit",
38+
});
39+
return;
40+
};
41+
42+
const [node, dispatcher, ...query] = process.argv;
43+
44+
(async () => {
45+
if (query.length === 0) {
46+
console.log("No query given, use `r [substring words]`.");
47+
return;
48+
}
49+
50+
const matches = [];
51+
52+
for await (const f of walk(workspaceFolder, ["node_modules", ".yarn", ".git"])) {
53+
const ext = path.extname(f);
54+
if (ext in runnable) {
55+
if (matcher(f, ...query)) {
56+
matches.push(f);
57+
}
58+
}
59+
}
60+
61+
if (matches.length === 0) {
62+
console.log("No matching workspace scripts.");
63+
return;
64+
}
65+
66+
const selection = matchSorter(matches, ...query)[0];
67+
const ext = path.extname(selection);
68+
69+
console.log("Exec script:", selection);
70+
71+
if (runnable[ext] === USE_NODE) {
72+
await execute(path.dirname(selection), "node", [selection]);
73+
}
74+
if (runnable[ext] === USE_TYPESCRIPT) {
75+
await execute(path.dirname(selection), "npx", ["esbuild-runner", selection]);
76+
}
77+
})();

scripts/utils/walk.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
const fs = require("fs");
22
const path = require("path");
33

4-
module.exports = async function* walk(dir) {
4+
module.exports = async function* walk(dir, ignore = []) {
55
for await (const d of await fs.promises.opendir(dir)) {
66
const entry = path.join(dir, d.name);
7-
if (d.isDirectory()) yield* walk(entry);
8-
else if (d.isFile()) yield entry;
7+
if (ignore.find((ignored) => entry.includes(ignored))) {
8+
continue;
9+
}
10+
if (d.isDirectory()) {
11+
yield* walk(entry, ignore);
12+
} else if (d.isFile()) {
13+
yield entry;
14+
}
915
}
1016
};

0 commit comments

Comments
 (0)