Skip to content

Commit

Permalink
feat: NodeJS WASM CLI parity with native binary
Browse files Browse the repository at this point in the history
  • Loading branch information
bcheidemann committed Feb 21, 2025
1 parent 2f8be1d commit c455989
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 116 deletions.
60 changes: 31 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ license = "GPL-3.0-only"
homepage = "https://github.com/mpalmer/action-validator"
repository = "https://github.com/mpalmer/action-validator"
include = [
"/LICENCE",
"/src/*.rs",
"/src/schemastore/src/schemas/json/github-workflow.json",
"/src/schemastore/src/schemas/json/github-action.json"
"/LICENCE",
"/src/*.rs",
"/src/schemastore/src/schemas/json/github-workflow.json",
"/src/schemastore/src/schemas/json/github-action.json",
]
version = "0.0.0-git"
authors = ["Matt Palmer <[email protected]>"]
Expand All @@ -31,18 +31,19 @@ serde_yaml = "0.9.17"
# Install 4.x once published to crates.io
valico = "4.0.0-rc.0"

wasm-bindgen = "0.2.63"
wasm-bindgen = "0.2.84"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }

serde-wasm-bindgen = "0.4.5"
js-sys = "0.3.77"

[dev-dependencies]
wasm-bindgen-test = "0.3.13"
wasm-bindgen-test = "0.3.34"

[profile.release]
# Tell `rustc` to optimize for small code size.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"version": "0.0.0-git",
"license": "GPL-3.0-only",
"scripts": {
"build": "npx wasm-pack build --out-dir target/wasm-pack/build --no-typescript --target nodejs --features js && cp target/wasm-pack/build/action_validator_bg.wasm packages/core/ && cp target/wasm-pack/build/action_validator.js packages/core/ && cp target/wasm-pack/build/action_validator.js packages/core/",
"build": "./scripts/build-wasm.sh",
"build:dev": "./scripts/build-wasm.sh",
"test": "node test/run.mjs",
"lint": "prettier --check .",
"format": "prettier --write ."
Expand Down
52 changes: 1 addition & 51 deletions packages/cli/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,56 +1,6 @@
#!/usr/bin/env node
// @ts-check

import chalk from "chalk";
import fs from "fs";
import * as actionValidator from "@action-validator/core";

function usage(exitCode = 0) {
console.log(
[
`${chalk.underline("Usage:")} action-validator <path_to_action_yaml>`,
"",
`${chalk.underline("Arguments:")}`,
" <path_to_action_yaml> Input file",
].join("\n")
);

process.exit(exitCode);
}

const args = process.argv.slice(2);
if (args.length !== 1) {
usage(1);
}

if (args[0] === "--help" || args[0] === "-h") {
usage();
}

const pathToActionYaml = args[0];

if (!fs.existsSync(pathToActionYaml)) {
console.error(`File not found: ${pathToActionYaml}`);
process.exit(1);
}

const contents = fs.readFileSync(pathToActionYaml, "utf8");
const actionType =
pathToActionYaml.endsWith("action.yml") ||
pathToActionYaml.endsWith("action.yaml")
? "action"
: "workflow";

const result = (() => {
switch (actionType) {
case "action":
return actionValidator.validateAction(contents);
case "workflow":
return actionValidator.validateWorkflow(contents);
}
})();

if (result.errors.length > 0) {
console.error(JSON.stringify(result, null, 2));
process.exit(1);
}
actionValidator.main(process.argv.slice(1));
4 changes: 1 addition & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
"lint": "prettier --check .",
"format": "prettier --write ."
},
"dependencies": {
"chalk": "5.2.0"
},
"dependencies": {},
"devDependencies": {
"@action-validator/core": "file:../core"
},
Expand Down
1 change: 1 addition & 0 deletions packages/core/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
/snippets
action_validator_bg.wasm
action_validator.js
1 change: 1 addition & 0 deletions packages/core/action_validator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export type ValidationState = {
errors: ValidationError[];
};

export function main(args: string[]): never;
export function validateAction(src: string): ValidationState;
export function validateWorkflow(src: string): ValidationState;
9 changes: 9 additions & 0 deletions scripts/build-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -euo pipefail

npx wasm-pack build --out-dir target/wasm-pack/build --no-typescript --target nodejs --features js
rm -rf packages/core/snippets
cp -R target/wasm-pack/build/snippets packages/core/snippets
cp target/wasm-pack/build/action_validator_bg.wasm packages/core/
cp target/wasm-pack/build/action_validator.js packages/core/
7 changes: 7 additions & 0 deletions src/js/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const console = require('node:console');
const fs = require('node:fs');
const process = require('node:process');

module.exports.console = console;
module.exports.fs = fs;
module.exports.process = process;
Loading

0 comments on commit c455989

Please sign in to comment.