Skip to content

Commit

Permalink
Merge pull request #859 from polywrap/commander-refactor
Browse files Browse the repository at this point in the history
feat: use commander as arg-parser
  • Loading branch information
dOrgJelli authored May 26, 2022
2 parents f26f996 + bc6fe4e commit ed91c45
Show file tree
Hide file tree
Showing 44 changed files with 1,429 additions and 1,657 deletions.
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.0.7",
Expand Down
36 changes: 15 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 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

0 comments on commit ed91c45

Please sign in to comment.