-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: create script runner devtool (#5074)
- Loading branch information
Showing
6 changed files
with
121 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
## CLI dispatcher | ||
|
||
This script provides a CLI helper to send shorthand commands to a matching package. | ||
These scripts provides CLI helpers to send shorthand commands to a matching package. | ||
|
||
### Usage | ||
|
||
First, alias the script entry point. An example is provided in `./set-alias.sh`. | ||
|
||
Then run the script with no arguments to see the help message detailing usage. | ||
Then run the script with the new alias `b` with no arguments to see the help message detailing usage. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* This script runs files by filename in the root/workspace (unversioned) directory. | ||
* | ||
* @example | ||
* r dyn test | ||
* | ||
* would match to a file in /workspace/dynamodb/test.ts and execute it with esbuild runner. | ||
* | ||
* @example | ||
* r ssec | ||
* | ||
* would run "node /workspace/s3/ssec.mjs" | ||
* | ||
* The workspace directory is meant as a place to test short scripts | ||
* that make use of the packages built in the root monorepo workspace. | ||
*/ | ||
|
||
const path = require("path"); | ||
const walk = require("../utils/walk"); | ||
|
||
const matcher = require("./lib/matcher"); | ||
const matchSorter = require("./lib/matchSorter"); | ||
|
||
const root = path.join(__dirname, "..", ".."); | ||
const workspaceFolder = path.join(root, "workspace"); | ||
|
||
const USE_NODE = 1; | ||
const USE_TYPESCRIPT = 2; | ||
const runnable = { ".js": USE_NODE, ".ts": USE_TYPESCRIPT, ".cjs": USE_NODE, ".mjs": USE_NODE }; | ||
|
||
const execute = async (cwd, exe, commands) => { | ||
const { spawnProcess } = require("../utils/spawn-process"); | ||
await spawnProcess(exe, [...commands], { | ||
cwd, | ||
stdio: "inherit", | ||
}); | ||
return; | ||
}; | ||
|
||
const [node, dispatcher, ...query] = process.argv; | ||
|
||
(async () => { | ||
if (query.length === 0) { | ||
console.log("No query given, use `r [substring words]`."); | ||
return; | ||
} | ||
|
||
const matches = []; | ||
|
||
for await (const f of walk(workspaceFolder, ["node_modules", ".yarn", ".git"])) { | ||
const ext = path.extname(f); | ||
if (ext in runnable) { | ||
if (matcher(f, ...query)) { | ||
matches.push(f); | ||
} | ||
} | ||
} | ||
|
||
if (matches.length === 0) { | ||
console.log("No matching workspace scripts."); | ||
return; | ||
} | ||
|
||
const selection = matchSorter(matches, ...query)[0]; | ||
const ext = path.extname(selection); | ||
|
||
console.log("Exec script:", selection); | ||
|
||
if (runnable[ext] === USE_NODE) { | ||
await execute(path.dirname(selection), "node", [selection]); | ||
} | ||
if (runnable[ext] === USE_TYPESCRIPT) { | ||
await execute(path.dirname(selection), "npx", ["esbuild-runner", selection]); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
module.exports = async function* walk(dir) { | ||
module.exports = async function* walk(dir, ignore = []) { | ||
for await (const d of await fs.promises.opendir(dir)) { | ||
const entry = path.join(dir, d.name); | ||
if (d.isDirectory()) yield* walk(entry); | ||
else if (d.isFile()) yield entry; | ||
if (ignore.find((ignored) => entry.includes(ignored))) { | ||
continue; | ||
} | ||
if (d.isDirectory()) { | ||
yield* walk(entry, ignore); | ||
} else if (d.isFile()) { | ||
yield entry; | ||
} | ||
} | ||
}; |