Skip to content

Commit 3bfac24

Browse files
CoveMBericglau
andauthored
Run command utils (#454)
Co-authored-by: Eric Lau <[email protected]>
1 parent 6f1fee7 commit 3bfac24

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Install dependencies with `yarn install`.
2020

2121
You'll need to supply your own environment variables if you want to enable Wizard AI Assistant (OPENAI_API_KEY) and/or logging (REDIS_URL, REDIS_TOKEN).
2222

23+
You can run yarn commands directly into `core/{language}` folders with `yarn run:core`.
24+
For example, running `yarn run:core cairo test` from the root directory will run tests for Cairo.
25+
2326
## Embedding
2427

2528
To embed Contracts Wizard on your site, first include the script tag:

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"name": "root",
33
"private": true,
44
"scripts": {
5-
"prepare": "wsrun -m prepare"
5+
"prepare": "wsrun -m prepare",
6+
"dev:ui": "yarn --cwd ./packages/ui dev",
7+
"run:core": "node ./scripts/run-command.mjs"
68
},
79
"workspaces": {
810
"packages": [
@@ -17,4 +19,4 @@
1719
"devDependencies": {
1820
"wsrun": "^5.2.4"
1921
}
20-
}
22+
}

scripts/command-input.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function getCommandAndFlagsInput() {
2+
const [, , _languageFolderName, ...commandAndFlags] = process.argv;
3+
4+
if (commandAndFlags) return commandAndFlags.join(" ");
5+
6+
console.error("Please provide a command.");
7+
process.exit(1);
8+
}

scripts/execute-command.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { execSync } from "child_process";
2+
3+
export function runYarnCommandForLanguage(languageInput, commandAndFlagsInput) {
4+
try {
5+
execSync(
6+
`yarn --cwd ./packages/core/${languageInput} ${commandAndFlagsInput}`,
7+
{
8+
stdio: "inherit",
9+
}
10+
);
11+
} catch (error) {
12+
console.error(
13+
`Error running ${commandAndFlagsInput} in ${languageInput}:`,
14+
error
15+
);
16+
process.exit(1);
17+
}
18+
}

scripts/language-input.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { readdirSync, statSync } from "fs";
2+
import { join } from "path";
3+
4+
const coreSubfolderPath = "./packages/core";
5+
6+
function getSupportedLanguageInCoreSubfolder() {
7+
return readdirSync(coreSubfolderPath).filter((subfolder) => {
8+
const subfolderPath = join(coreSubfolderPath, subfolder);
9+
return statSync(subfolderPath).isDirectory();
10+
});
11+
}
12+
13+
function getLanguageInput() {
14+
const [, , languageFolderName] = process.argv;
15+
16+
if (languageFolderName) return languageFolderName;
17+
18+
console.error("Please provide a language name.");
19+
process.exit(1);
20+
}
21+
22+
export function getAndCheckLanguageInput() {
23+
const languageFolderName = getLanguageInput();
24+
25+
const supportedLanguages = getSupportedLanguageInCoreSubfolder();
26+
27+
if (supportedLanguages.includes(languageFolderName))
28+
return languageFolderName;
29+
30+
console.error(
31+
`Invalid language name ${languageFolderName}. Supported languages: ${supportedLanguages.join(", ")}`
32+
);
33+
process.exit(1);
34+
}

scripts/run-command.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getCommandAndFlagsInput } from "./command-input.mjs";
2+
import { runYarnCommandForLanguage } from "./execute-command.mjs";
3+
import { getAndCheckLanguageInput } from "./language-input.mjs";
4+
5+
runYarnCommandForLanguage(
6+
getAndCheckLanguageInput(),
7+
getCommandAndFlagsInput()
8+
);

0 commit comments

Comments
 (0)