Skip to content

Commit

Permalink
Add a cli to the package
Browse files Browse the repository at this point in the history
Ref #51
  • Loading branch information
sverhoeven committed Oct 11, 2024
1 parent 95c925d commit fd07c6a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ The CLASS web application is available at https://classmodel.github.io/class-web

For more information on CLASS, see https://classmodel.github.io/.

## Command line usage

The class model can be run from the command line.
The argument is the config file that should adhere to the [JSON schema](./packages/class/src/config.json).

```shell
pnpx --package=@classmodel/class class config.json
# Outputs h variable for each timestep
```

(in development use `pnpx tsx src/cli.ts ./config.json`)

To use the reference configuration of a experiment downloaded from the web application use.

```shell
jq .reference < ~/Downloads/class-MyExperiment.json > config.json
```

## Developers

This repository is a so-called monorepo, where multiple packages and application
Expand Down
4 changes: 4 additions & 0 deletions packages/class/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.3",
"exports": {
"./class": "./src/class.ts",
"./cli": "./src/cli.ts",
"./config": "./src/config.ts",
"./config.json": "./src/config.json",
"./runner": "./src/runner.ts",
Expand All @@ -24,6 +25,9 @@
"dependencies": {
"ajv": "^8.17.1"
},
"bin": {
"class": "./src/cli.ts"
},
"engines": {
"node": ">=20.16.0"
}
Expand Down
27 changes: 27 additions & 0 deletions packages/class/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

import fs from "node:fs";
import { runClass } from "./runner";
import { parse } from "./validate";

function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.error("Usage: class <config-file.json>");
process.exit(1);
}

try {
const configFile = args[0];
const configAsString = fs.readFileSync(configFile, "utf-8");
const rawConfig = JSON.parse(configAsString);
const config = parse(rawConfig);
const output = runClass(config);
console.log(JSON.stringify(output, null, 2));
} catch (error) {
console.error("Error running class:", error);
process.exit(1);
}
}

main();

0 comments on commit fd07c6a

Please sign in to comment.