Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use commander as arg-parser #859

Merged
merged 30 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c050383
init refactor
dOrgJelli Mar 14, 2022
5e15e79
Merge branch 'prealpha-dev' into codentell-commander-refactor
Mar 15, 2022
aab773e
started working on app commander refactor
Mar 15, 2022
e35da73
add the rest of the options commands for commander.js for refactor
Mar 15, 2022
2d8b730
Merge branch 'prealpha-dev' into codentell-commander-refactor
Mar 21, 2022
59d4ee5
add refactor to app still need to work on the codegenDir
Mar 22, 2022
0a81f22
made some TODO on which of the commands need to be refactored to comm…
Mar 22, 2022
58edd2b
update query refactor with commander
Mar 22, 2022
7fbbae0
fixed alias for commands
Mar 22, 2022
4c1261a
fixed test-env with commander.js
Mar 22, 2022
2cff5e2
add help messages to create command and work on jest testing
Mar 23, 2022
ba2f6c0
add commander to create command fix the jest test in create
Mar 23, 2022
6f8c55c
update jest test and finished app and create refactor
Mar 24, 2022
9d1c52c
refactor app build codegen plugin and query next steps fix e2e test
Mar 24, 2022
ecf0ba5
Merge remote-tracking branch 'upstream/codentell-commander-refactor' …
Niraj-Kamdar May 19, 2022
36cd1c7
Merge remote-tracking branch 'upstream/prealpha-dev' into commander-r…
Niraj-Kamdar May 19, 2022
61c7742
debug: seems like commander parser logic is broken
Niraj-Kamdar May 19, 2022
d8e1636
fix: use manual parsing until commander fixes it
Niraj-Kamdar May 19, 2022
240e25f
fix: add build manifest for the cli codegen tests
Niraj-Kamdar May 19, 2022
818b25a
Merge remote-tracking branch 'upstream/prealpha-dev' into commander-r…
Niraj-Kamdar May 19, 2022
0d82430
fix(cli): tests
Niraj-Kamdar May 19, 2022
2674643
fix(cli): ci issues
Niraj-Kamdar May 20, 2022
af6b74d
Merge remote-tracking branch 'upstream/prealpha-dev' into commander-r…
Niraj-Kamdar May 23, 2022
c896de9
refactor: remove leftover console.log and comments
Niraj-Kamdar May 24, 2022
08b20f0
Merge branch 'prealpha-dev' into commander-refactor
Niraj-Kamdar May 24, 2022
907d3bd
fix(cli): tests
Niraj-Kamdar May 24, 2022
8dab62f
(fix): async client configuration awaiting
namesty May 26, 2022
919df60
fix(cli): json and yaml query output
Niraj-Kamdar May 26, 2022
96fdd99
remove yarn.lock
dOrgJelli May 26, 2022
bc6fe4e
remove duplicate parser functions
dOrgJelli May 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/cli/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"commands_create_options_o": "Output directory for the new project",
"commands_create_options_o_path": "path",
"commands_create_options_projectName": "project-name",
"commands_create_options_recipeScript": "recipe-script",
"commands_query_options_recipeScript": "Path to recipe script",
"commands_create_overwritePrompt": "Do you want to overwrite this directory?",
"commands_create_overwriting": "Overwriting {dir}...",
"commands_create_readyApp": "You are ready to build an app using Web3API",
Expand Down Expand Up @@ -103,6 +103,7 @@
"commands_plugin_options_publish": "Output path for the built schema and manifest (default: {default})",
"commands_plugin_options_codegen": "Output directory for the generated types (default: {default})",
"commands_query_options_configPath": "config-path",
"commands_query_options_outputFilePath": "output-file-path",
"commands_query_options_config": "Add custom configuration to the Web3ApiClient",
"commands_query_options_outputFile": "Output file path for the query result",
"commands_query_options_quiet": "Suppress output",
Expand Down Expand Up @@ -138,6 +139,7 @@
"commands_query_error_missingScript": "Required argument {script} is missing",
"commands_query_error_noApi": "API needs to be initialized",
"commands_query_error_readFail": "Failed to read query {query}",
"commands_query_error_noRecipeScriptFound": "Recipe script not found at path: {path}",
"commands_query_error_noTestEnvFound": "w3 test-env not found, please run 'w3 test-env up'",
"commands_testEnv_description": "Manage a test environment for Web3API",
"commands_testEnv_error_never": "This should never happen...",
Expand Down
11 changes: 6 additions & 5 deletions packages/cli/lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"commands_build_keypressListener_watching": "Watching",
"commands_build_options_h": "Show usage information",
"commands_build_options_m": "Path to the Web3API Build manifest file (default: {default})",

"commands_build_options_o": "Output directory for build results (default: build/)",
"commands_build_options_o_path": "path",
"commands_build_options_options": "options",
Expand Down Expand Up @@ -65,7 +64,7 @@
"commands_create_options_o": "Output directory for the new project",
"commands_create_options_o_path": "path",
"commands_create_options_projectName": "project-name",
"commands_create_options_recipeScript": "recipe-script",
"commands_query_options_recipeScript": "Path to recipe script",
"commands_create_overwritePrompt": "Do you want to overwrite this directory?",
"commands_create_overwriting": "Overwriting {dir}...",
"commands_create_readyApp": "You are ready to build an app using Web3API",
Expand Down Expand Up @@ -104,6 +103,7 @@
"commands_plugin_options_publish": "Output path for the built schema and manifest (default: {default})",
"commands_plugin_options_codegen": "Output directory for the generated types (default: {default})",
"commands_query_options_configPath": "config-path",
"commands_query_options_outputFilePath": "output-file-path",
"commands_query_options_config": "Add custom configuration to the Web3ApiClient",
"commands_query_options_outputFile": "Output file path for the query result",
"commands_query_options_quiet": "Suppress output",
Expand Down Expand Up @@ -139,6 +139,7 @@
"commands_query_error_missingScript": "Required argument {script} is missing",
"commands_query_error_noApi": "API needs to be initialized",
"commands_query_error_readFail": "Failed to read query {query}",
"commands_query_error_noRecipeScriptFound": "Recipe script not found at path: {path}",
"commands_query_error_noTestEnvFound": "w3 test-env not found, please run 'w3 test-env up'",
"commands_testEnv_description": "Manage a test environment for Web3API",
"commands_testEnv_error_never": "This should never happen...",
Expand Down Expand Up @@ -194,14 +195,14 @@
"lib_helpers_manifest_loadText": "Manifest loaded from {path}",
"lib_helpers_manifest_loadWarning": "Warnings loading manifest from {path}",
"lib_helpers_deployManifestExt_loadError": "Failed to load deploy manifest extension from {path}",
"lib_helpers_deployManifestExt_loadText": "Deploy manifest extension loaded from {path}",
"lib_helpers_deployManifestExt_loadWarning": "No deploy manifest extension found in {path}",
"lib_helpers_deployManifestExt_loadText": "Load manifest extension from {path}",
"lib_helpers_deployManifestExt_loadWarning": "No manifest extension found in {path}",
"lib_helpers_manifest_outputError": "Failed to output manifest to {path}",
"lib_helpers_manifest_outputText": "Manifest written to {path}",
"lib_helpers_manifest_outputWarning": "Warnings writing manifest to {path}",
"lib_helpers_manifest_unableToDump": "Unable to dump manifest: {manifest}",
"lib_helpers_manifest_unableToLoad": "Unable to load manifest: {path}",
"lib_helpers_docker_copyText": "Artifacts written to {path} from the image {image}",
"lib_helpers_docker_copyText": "Artifacts written to {path} from the image `{image}`",
"lib_helpers_docker_copyError": "Failed to write build artifacts to {path} from the image `{image}`",
"lib_helpers_docker_copyWarning": "Warnings write build artifacts to {path} from the image `{image}`",
"lib_helpers_docker_buildText": "Building source image `{image}` using dockerfile `{dockerfile}` in context `{context}`",
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"build:envs": "copyfiles ./src/lib/build-envs/**/**/* ./build/lib/build-envs/ -u 3",
"build:deployers": "copyfiles ./src/lib/deployers/**/web3api.deploy.ext.json ./build/lib/deployers -u 3",
"prebuild": "ts-node ./scripts/generateIntlTypes.ts",
"build:fast": "rimraf ./build && tsc --project tsconfig.build.json && yarn build:envs",
"lint": "eslint --color -c ../../.eslintrc.js .",
"test": "cross-env TEST=true jest --passWithNoTests --runInBand --verbose",
"test:ci": "cross-env TEST=true jest --passWithNoTests --runInBand --verbose",
Expand All @@ -46,6 +47,7 @@
"axios": "0.21.2",
"chalk": "4.1.0",
"chokidar": "3.5.1",
"commander": "9.2.0",
"content-hash": "2.5.2",
"copyfiles": "2.4.1",
"ethers": "5.6.2",
Expand Down
39 changes: 18 additions & 21 deletions packages/cli/src/__tests__/e2e/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import { GetPathToCliTestFiles } from "@web3api/test-cases";
import path from "path";
import fs from "fs";

const HELP = `
w3 app command [options]

Commands:
codegen Generate code for the app
const HELP = `Usage: w3 app|a [options] [command]

Build/generate types for your app

Options:
-h, --help Show usage information
-m, --manifest-file <path> Path to the Web3API App manifest file (default: web3api.app.yaml | web3api.app.yml)
-c, --codegen-dir <path> Output directory for the generated code (default: ./src/w3)
-i, --ipfs [<node>] IPFS node to load external schemas (default: ipfs.io & localhost)
-e, --ens [<address>] ENS address to lookup external schemas (default: 0x0000...2e1e)
-h, --help display help for command

`;
Commands:
codegen [options] Generate code for the app
help [command] display help for command
`

const CODEGEN_SUCCESS = `- Manifest loaded from ./web3api.app.yaml
✔ Manifest loaded from ./web3api.app.yaml
Expand Down Expand Up @@ -60,9 +58,8 @@ describe("e2e tests for app command", () => {
);

expect(code).toEqual(1);
expect(error).toBe("");
expect(clearStyle(output)).toEqual(`Please provide a command
${HELP}`);
expect(error).toBe("error: unknown option '--output-dir'\n");
expect(output).toEqual(``);
});

test("Should throw error for invalid params - codegen-dir", async () => {
Expand All @@ -75,10 +72,8 @@ ${HELP}`);
);

expect(code).toEqual(1);
expect(error).toBe("");
expect(clearStyle(output))
.toEqual(`--codegen-dir option missing <path> argument
${HELP}`);
expect(error).toBe(`error: option '-c, --codegen-dir <path>' argument missing\n`);
expect(output).toEqual(``);
});

test("Should throw error for invalid params - ens", async () => {
Expand All @@ -91,10 +86,9 @@ ${HELP}`);
);

expect(code).toEqual(1);
expect(error).toBe("");
expect(clearStyle(output))
.toEqual(`--ens option missing [<address>] argument
${HELP}`);
expect(error).toBe("error: option '-e, --ens [<address>]' argument missing\n");
expect(output)
.toEqual(``);
});

describe("test-cases", () => {
Expand All @@ -111,6 +105,9 @@ ${HELP}`);
},
);

console.log(output);
Niraj-Kamdar marked this conversation as resolved.
Show resolved Hide resolved
console.log(error);

expect(error).toBe("");
expect(code).toEqual(0);
expect(clearStyle(output)).toEqual(CODEGEN_SUCCESS);
Expand Down
25 changes: 13 additions & 12 deletions packages/cli/src/__tests__/e2e/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import { GetPathToCliTestFiles } from "@web3api/test-cases";
import fs from "fs";
import path from "path";

const HELP = `
w3 build [options]
const HELP = `Usage: w3 build|b [options]

Options:
-h, --help Show usage information
-m, --manifest-file <path> Path to the Web3API Build manifest file (default: web3api.yaml | web3api.yml)
-o, --output-dir <path> Output directory for build results (default: build/)
-w, --watch Automatically rebuild when changes are made (default: false)
-v, --verbose Verbose output (default: false)
Builds a Web3API

Options:
-m, --manifest-file <path> Path to the Web3API Build manifest file (default:
web3api.yaml | web3api.yml)
-o, --output-dir <path> Output directory for build results (default:
build/)
-w, --watch Automatically rebuild when changes are made
(default: false)
-v, --verbose Verbose output (default: false)
-h, --help display help for command
`;

jest.setTimeout(500000);
Expand Down Expand Up @@ -53,10 +56,8 @@ describe("e2e tests for build command", () => {
);

expect(code).toEqual(1);
expect(error).toBe("");
expect(clearStyle(output))
.toEqual(`--output-dir option missing <path> argument
${HELP}`);
expect(error).toContain("error: option '-o, --output-dir <path>' argument missing");
expect(output).toBe("")
});

test("Adds uuid-v4 suffix to build-env image if no build manifest specified", async () => {
Expand Down
103 changes: 63 additions & 40 deletions packages/cli/src/__tests__/e2e/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@ import { runCLI } from "@web3api/test-env-js";
import { GetPathToCliTestFiles } from "@web3api/test-cases";
import path from "path";
import fs from "fs";
import rimraf from "rimraf";

const HELP = `
w3 codegen [options]
const HELP = `Usage: w3 codegen|g [options]

Options:
-h, --help Show usage information
-m, --manifest-file <path> Path to the Web3API manifest file (default: ${defaultWeb3ApiManifest.join(
" | "
)})
-c, --codegen-dir <path> Output directory for the generated code (default: ./w3)
-s, --script <path> Path to a custom generation script (JavaScript | TypeScript)
-i, --ipfs [<node>] IPFS node to load external schemas (default: ipfs.io & localhost)
-e, --ens [<address>] ENS address to lookup external schemas (default: 0x0000...2e1e)
Auto-generate API Types

Options:
-m, --manifest-file <path> Path to the Web3API manifest file (default:
${defaultWeb3ApiManifest.join(" | ")})
-c, --codegen-dir <path> Output directory for the generated code
(default: ./w3)
-s, --script <path> Path to a custom generation script (JavaScript |
TypeScript)
-i, --ipfs [<node>] IPFS node to load external schemas (default:
ipfs.io & localhost)
-e, --ens [<address>] ENS address to lookup external schemas (default:
0x0000...2e1e)
-h, --help display help for command
`;

describe("e2e tests for codegen command", () => {
const testCaseRoot = path.join(GetPathToCliTestFiles(), "api/codegen");
const testCases =
fs.readdirSync(testCaseRoot, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const testCases = fs
.readdirSync(testCaseRoot, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const getTestCaseDir = (index: number) =>
path.join(testCaseRoot, testCases[index]);

Expand All @@ -42,55 +46,75 @@ describe("e2e tests for codegen command", () => {
expect(clearStyle(output)).toEqual(HELP);
});

test("Should throw error for invalid params - script", async () => {
test("Should throw error for invalid params - ens", async () => {
const { exitCode: code, stdout: output, stderr: error } = await runCLI({
args: ["codegen", "--script"],
args: ["codegen", "--ens"],
cwd: getTestCaseDir(0),
cli: w3Cli,
});

expect(code).toEqual(1);
expect(error).toBe("");
expect(clearStyle(output))
.toEqual(`--script option missing <path> argument
${HELP}`);
expect(error).toBe(
"error: option '-e, --ens [<address>]' argument missing\n"
);
expect(clearStyle(output)).toEqual(``);
});

test("Should throw error for invalid params - ens", async () => {
test("Should throw error for invalid generation file - wrong file", async () => {
const { exitCode: code, stdout: output, stderr: error } = await runCLI({
args: ["codegen", "--ens"],
args: ["codegen", "--script", `web3api-invalid.gen.js`],
cwd: getTestCaseDir(0),
cli: w3Cli,
});

const genFile = path.normalize(
`${getTestCaseDir(0)}/web3api-invalid.gen.js`
);

expect(code).toEqual(1);
expect(error).toBe("");
expect(clearStyle(output))
.toEqual(`--ens option missing [<address>] argument
${HELP}`);
expect(clearStyle(output)).toContain(
`Failed to generate types: Cannot find module '${genFile}'`
);
});

test("Should throw error for invalid generation file - wrong file", async () => {
test("Should throw error for invalid generation file - no run() method", async () => {
const { exitCode: code, stdout: output, stderr: error } = await runCLI({
args: ["codegen", "--script", `web3api-invalid.gen.js`],
args: ["codegen", "--script", `web3api-norun.gen.js`],
cwd: getTestCaseDir(0),
cli: w3Cli,
});

const genFile = path.normalize(`${getTestCaseDir(0)}/web3api-invalid.gen.js`);

expect(code).toEqual(1);
expect(error).toBe("");
expect(clearStyle(output)).toContain(`Failed to generate types: Cannot find module '${genFile}'`);
expect(clearStyle(output)).toContain(
`Failed to generate types: The generation file provided doesn't have the 'generateBinding' method.`
);
});

test("Should successfully generate types", async () => {
rimraf.sync(`${getTestCaseDir(0)}/types`);

const { exitCode: code, stdout: output, stderr: error } = await runCLI({
args: ["codegen"],
cwd: getTestCaseDir(0),
cli: w3Cli,
});

expect(code).toEqual(0);
expect(error).toBe("");
expect(clearStyle(output)).toContain(
`🔥 Types were generated successfully 🔥`
);

rimraf.sync(`${getTestCaseDir(0)}/types`);
});
describe("test-cases", () => {
for (let i = 0; i < testCases.length; ++i) {
const testCaseName = testCases[i];
const testCaseDir = getTestCaseDir(i);

test(testCaseName, async () => {

let cmdArgs = [];
let cmdFile = path.join(testCaseDir, "cmd.json");
if (fs.existsSync(cmdFile)) {
Expand All @@ -100,20 +124,19 @@ ${HELP}`);
}
}

let { exitCode, stdout, stderr } = await runCLI(
{
args: ["codegen", ...cmdArgs],
cwd: testCaseDir,
cli: w3Cli,
},
);
let { exitCode, stdout, stderr } = await runCLI({
args: ["codegen", ...cmdArgs],
cwd: testCaseDir,
cli: w3Cli,
});

stdout = clearStyle(stdout);
stderr = clearStyle(stderr);

const expected = JSON.parse(
fs.readFileSync(
path.join(testCaseDir, "expected/output.json"), "utf-8"
path.join(testCaseDir, "expected/output.json"),
"utf-8"
)
);

Expand Down
Loading