-
Notifications
You must be signed in to change notification settings - Fork 1
feat: section library validate command #1297
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d5d2c29
validate command
benlife5 24fefe5
Automated update to THIRD-PARTY-NOTICES from github action's 3rd part…
github-actions[bot] e908d62
Update component screenshots for visual-editor
github-actions[bot] 6f9cc67
rabbit
benlife5 2fba838
increase test timeout for main section library plugin
benlife5 df8b6e7
Update component screenshots for visual-editor
github-actions[bot] 3964bb5
remove todo
benlife5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,53 @@ | ||
| import { parseArgs } from "node:util"; | ||
| import { | ||
| createValidationContext, | ||
| validateSectionLibrary, | ||
| } from "../../internal/sectionLibraryValidation/validateSectionLibrary.ts"; | ||
| import type { ValidationStage } from "../../internal/sectionLibraryValidation/types.ts"; | ||
| import { renderValidationResult } from "../output.ts"; | ||
|
|
||
| type ValidateCommandIo = { | ||
| stdout: Pick<NodeJS.WriteStream, "write" | "isTTY">; | ||
| }; | ||
|
|
||
| export const runValidateCommand = ( | ||
| args: string[], | ||
| io: ValidateCommandIo, | ||
| rootDir: string | ||
| ): number => { | ||
| const { values } = parseArgs({ | ||
| args, | ||
| strict: true, | ||
| allowPositionals: false, | ||
| options: { | ||
| yextCI: { type: "boolean" }, | ||
| "skip-api-check": { type: "boolean" }, | ||
| "skip-repo-structure-check": { type: "boolean" }, | ||
| "skip-code-check": { type: "boolean" }, | ||
| }, | ||
| }); | ||
|
|
||
| const skippedStages = new Set<ValidationStage>(); | ||
| if (values["skip-api-check"]) { | ||
| skippedStages.add("api"); | ||
| } | ||
| if (values["skip-repo-structure-check"]) { | ||
| skippedStages.add("structure"); | ||
| } | ||
| if (values["skip-code-check"]) { | ||
| skippedStages.add("code"); | ||
| } | ||
|
|
||
| const context = createValidationContext(rootDir, { | ||
| yextCI: values.yextCI, | ||
| skippedStages, | ||
| }); | ||
|
|
||
| const result = validateSectionLibrary(context); | ||
|
|
||
| io.stdout.write( | ||
| renderValidationResult(result, !!io.stdout.isTTY && !context.yextCI) | ||
| ); | ||
|
|
||
| return result.issues.length === 0 ? 0 : 1; | ||
| }; | ||
This file contains hidden or 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,52 @@ | ||
| import pc from "picocolors"; | ||
| import { formatValidationIssue } from "../internal/sectionLibraryValidation/validationError.ts"; | ||
| import type { | ||
| ValidationResult, | ||
| ValidationStage, | ||
| } from "../internal/sectionLibraryValidation/types.ts"; | ||
|
|
||
| const stages: { stage: ValidationStage; label: string }[] = [ | ||
| { stage: "api", label: "Library metadata" }, | ||
| { stage: "structure", label: "Repository structure" }, | ||
| { stage: "code", label: "Code checks" }, | ||
| ]; | ||
|
|
||
| export const renderValidationResult = ( | ||
| result: ValidationResult, | ||
| colorEnabled: boolean | ||
| ): string => { | ||
| const colors = pc.createColors(colorEnabled && pc.isColorSupported); | ||
| const lines: string[] = []; | ||
|
|
||
| for (const { stage, label } of stages) { | ||
| if (result.context.skippedStages.has(stage)) { | ||
| lines.push(`${label}: ${colors.yellow("skipped")}`); | ||
| continue; | ||
| } | ||
|
|
||
| const issues = result.issues.filter((issue) => issue.category === stage); | ||
|
|
||
| if (issues.length === 0) { | ||
| lines.push(`${label}: ${colors.green("passed")}`); | ||
| continue; | ||
| } | ||
|
|
||
| lines.push( | ||
| `${label}: ${colors.red( | ||
| `failed (${issues.length} ${issues.length === 1 ? "error" : "errors"})` | ||
| )}` | ||
| ); | ||
|
|
||
| lines.push(...issues.map((issue) => ` ${formatValidationIssue(issue)}`)); | ||
| } | ||
|
|
||
| lines.push(""); | ||
| lines.push( | ||
| result.issues.length === 0 | ||
| ? colors.green("Validation passed. 0 errors.") | ||
| : colors.red( | ||
| `Validation failed. ${result.issues.length} ${result.issues.length === 1 ? "error" : "errors"}.` | ||
| ) | ||
| ); | ||
| return `${lines.join("\n")}\n`; | ||
| }; |
This file contains hidden or 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,92 @@ | ||
| import path from "node:path"; | ||
| import fs from "fs-extra"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { createTempRoot } from "../internal/sectionLibraryValidation/testUtils.ts"; | ||
| import { runCli } from "./yextve.ts"; | ||
| import packageJson from "../../package.json" with { type: "json" }; | ||
|
|
||
| describe("yextve", () => { | ||
| it("prints help and version", () => { | ||
| const help = invoke(["--help"]); | ||
| expect(help.exitCode).toBe(0); | ||
| expect(help.stdout).toContain( | ||
| "npx --package=@yext/visual-editor@latest yextve validate" | ||
| ); | ||
| expect(help.stdout).not.toContain("scripts"); | ||
| }); | ||
|
|
||
| it("prints version", () => { | ||
| const version = invoke(["--version"]); | ||
| expect(version).toEqual({ | ||
| exitCode: 0, | ||
| stdout: `${packageJson.version}\n`, | ||
| stderr: "", | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { args: [] }, | ||
| { args: ["unknown"] }, | ||
| { args: ["validate", "project-path"] }, | ||
| { args: ["validate", "--unknown"] }, | ||
| ])("returns usage exit 2 for invalid arguments: $args", ({ args }) => { | ||
| const result = invoke(args); | ||
| expect(result.exitCode).toBe(2); | ||
| expect(result.stderr).toContain("Usage:"); | ||
| }); | ||
|
|
||
| it("renders all skipped stages and succeeds", () => { | ||
| const result = invoke([ | ||
| "validate", | ||
| "--skip-api-check", | ||
| "--skip-repo-structure-check", | ||
| "--skip-code-check", | ||
| ]); | ||
|
|
||
| expect(result.exitCode).toBe(0); | ||
| expect(result.stdout.match(/skipped/g)).toHaveLength(3); | ||
| expect(result.stdout).toContain("Validation passed. 0 errors."); | ||
| expect(result.stdout).not.toContain("\u001b["); | ||
| }); | ||
|
|
||
| it("skips only API validation in Yext CI", () => { | ||
| const rootDir = createTempRoot(); | ||
| fs.outputFileSync( | ||
| path.join(rootDir, "src", "library", "Unsafe.ts"), | ||
| "eval(code);" | ||
| ); | ||
|
|
||
| const result = invoke(["validate", "--yextCI"], rootDir); | ||
|
|
||
| expect(result.exitCode).toBe(1); | ||
| expect(result.stdout).toContain("Library metadata: skipped"); | ||
| expect(result.stdout).toContain("Repository structure: failed"); | ||
| expect(result.stdout).toContain("Code checks: failed"); | ||
| }); | ||
| }); | ||
|
|
||
| // invoke calls the cli and records stdout and stderr | ||
| const invoke = (args: string[], rootDir: string = createTempRoot()) => { | ||
| let stdout = ""; | ||
| let stderr = ""; | ||
| const exitCode = runCli( | ||
| args, | ||
| { | ||
| stdout: { | ||
| isTTY: false, | ||
| write: (value) => { | ||
| stdout += value; | ||
| return true; | ||
| }, | ||
| }, | ||
| stderr: { | ||
| write: (value) => { | ||
| stderr += value; | ||
| return true; | ||
| }, | ||
| }, | ||
| }, | ||
| rootDir | ||
| ); | ||
| return { exitCode, stdout, stderr }; | ||
| }; |
This file contains hidden or 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,90 @@ | ||
| #!/usr/bin/env node | ||
| import fs from "node:fs"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import packageJson from "../../package.json" with { type: "json" }; | ||
| import { runValidateCommand } from "./commands/validate.ts"; | ||
|
|
||
| const usage = `Usage: | ||
| yextve validate [--skip-api-check] [--skip-repo-structure-check] [--skip-code-check] | ||
| yextve --help | ||
| yextve --version | ||
|
|
||
| Validate the Section Library in the current working directory. | ||
|
|
||
| Options: | ||
| --skip-api-check Skip library.json metadata validation. | ||
| --skip-repo-structure-check Skip repository structure validation. | ||
| --skip-code-check Skip import and code-safety validation. | ||
| --help Show this help. | ||
| --version Show the package version. | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| Examples: | ||
| npx --package=@yext/visual-editor@latest yextve validate | ||
| `; | ||
|
|
||
| type CliIo = { | ||
| stdout: Pick<NodeJS.WriteStream, "write" | "isTTY">; | ||
| stderr: Pick<NodeJS.WriteStream, "write">; | ||
| }; | ||
|
|
||
| export const runCli = ( | ||
| args: string[], | ||
| io: CliIo = { stdout: process.stdout, stderr: process.stderr }, | ||
| rootDir: string = process.cwd() | ||
| ): number => { | ||
| try { | ||
| // handle help | ||
| if (args.length === 1 && args[0] === "--help") { | ||
| io.stdout.write(usage); | ||
| return 0; | ||
| } | ||
|
|
||
| // handle invalid help calls | ||
| if (args.slice(1).includes("--help")) { | ||
| if (args.length === 2) { | ||
| io.stdout.write(usage); | ||
| return 0; | ||
| } | ||
| io.stderr.write(usage); | ||
| return 2; | ||
| } | ||
|
|
||
| // handle version | ||
| if (args.length === 1 && args[0] === "--version") { | ||
| io.stdout.write(`${packageJson.version}\n`); | ||
| return 0; | ||
| } | ||
|
|
||
| // handle invalid args | ||
| if (args[0] !== "validate") { | ||
| io.stderr.write(usage); | ||
| return 2; | ||
| } | ||
|
|
||
| // run command | ||
| return runValidateCommand(args.slice(1), io, rootDir); | ||
| } catch (error) { | ||
| if ( | ||
| error instanceof TypeError && | ||
| (error as TypeError & { code?: string }).code?.startsWith( | ||
| "ERR_PARSE_ARGS" | ||
| ) | ||
| ) { | ||
| io.stderr.write(`${error.message}\n\n${usage}`); | ||
| return 2; | ||
| } | ||
|
|
||
| io.stderr.write( | ||
| `Validation could not be completed: ${error instanceof Error ? error.message : String(error)}\n` | ||
| ); | ||
| return 2; | ||
| } | ||
| }; | ||
|
|
||
| if ( | ||
| process.argv[1] && | ||
| fs.realpathSync(process.argv[1]) === | ||
| fs.realpathSync(fileURLToPath(import.meta.url)) | ||
| ) { | ||
| process.exitCode = runCli(process.argv.slice(2)); | ||
| } | ||
Binary file modified
BIN
-9.26 KB
(32%)
...nshots/NearbyLocationsSection/[desktop] version 36 with no nearby locations.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-5.94 KB
(29%)
...hots/NearbyLocationsSection/[tablet] default props with no nearby locations.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.