Skip to content

Commit

Permalink
generalize CLI utility to be multi-command
Browse files Browse the repository at this point in the history
  • Loading branch information
dslmeinte committed Aug 29, 2023
1 parent 13ad90f commit 1b7a96c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The following is a list of links to potential starting points:
* [Models](models/) - various models in their serialized formats (the LIonWeb JSON format, or Ecore XML); see the [specific README](models/README.md).
* [Schemas](schemas/) - various JSON Schema files for validating models serialized in the LIonWeb JSON format against; see the [specific README](schemas/README.md).
* [Source](src/) - all TypeScript source to be exported as part of the NPM/Deno package.
* [Auxiliary](src-aux/) - all TypeScript source that's not core to the NPM/Deno package but still useful, e.g. for testing.
* [Command-line interface](src-cli/) - TypeScript source that implements a single-entrypoint CLI for utilities around the LIonCore functionality, such as: JSON Schema and diagram generation, textual syntax, extractors for the deserialization format, Ecore import, etc.
* [Scripts](src-build) - a `build-npm.ts` Deno script to package the source as an NPM package using [`dnt`](https://github.com/denoland/dnt).
* [Test sources](src-test/) - all TypeScript sources with/for (unit) tests.
Tests are located in files with names ending with `.test.ts`.
Expand Down Expand Up @@ -133,12 +133,12 @@ Run
deno task compile-cli
```

to produce a binary executable `lib/extract-serialization` for your platform.
to produce a binary executable `lib/lioncore-cli` for your platform.

This you can then call to make "extractions" from a serialization ("chunk"), as follows (e.g.):

```shell
lib/extract-serialization models/meta/lioncore.json
lib/lioncore-cli extract models/meta/lioncore.json
```

This is meant as a way to inspect, reason about, and compare serialization because the format is rather verbose.
Expand Down
8 changes: 4 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"tasks": {
"run-tests": "deno test --allow-read --allow-write src-test/",
"watch-tests": "deno test --watch --allow-write --allow-read src-test/",
"lint": "deno lint src/ src-test/ src-aux/",
"lock-deps": "deno cache --reload --lock=deno.lock --lock-write src/deps.ts src-aux/deps.ts src-build/build-npm.ts src-test/deps.ts",
"compile-cli": "deno compile --allow-read --allow-write src-aux/extract-serialization.ts && mv extract-serialization lib/"
"lint": "deno lint src/ src-test/ src-cli/",
"lock-deps": "deno cache --reload --lock=deno.lock --lock-write src/deps.ts src-cli/deps.ts src-build/build-npm.ts src-test/deps.ts",
"compile-cli": "deno compile --allow-read --allow-write src-cli/lioncore-cli.ts && mv lioncore-cli lib/"
},
"lint": {
"include": [
"src/",
"src-aux/",
"src-build/",
"src-cli/",
"src-test/"
],
"rules": {
Expand Down
File renamed without changes.
38 changes: 38 additions & 0 deletions src-cli/lioncore-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { extractFromSerialization } from "./serialization-extractor.ts"


const main = (args: string[])=> {
if (args.length === 0) {
console.log(
`lioncore-cli is a LIonWeb utility around LIonCore-TypeScript
Usage: $ lioncore-cli <command> <arguments>
Commands are:
extract
`
)
return
}

const command = args[0]
switch (command) {
case "extract": {
if (args.length === 1) {
console.log(
`The extract command extracts the following from a serialization chunk in the form of files: a sorted JSON, and a shortened JSON.
If the chunk is the serialization of a LIonCore Language/M2, then a textual rendering is already output.`
)
} else {
args.slice(1).forEach(extractFromSerialization)
}
return
}
default: {
console.error(`command "${command}" is not recognized`)
}
}
}
main(Deno.args)

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const isSerializedLanguage = (json: unknown): boolean =>
&& json["languages"].some((language) => isRecord(language) && language["key"] === lioncoreQName)


const verbose = async (path: string) => {
export const extractFromSerialization = async (path: string) => {
try {
const json = JSON.parse(await Deno.readTextFile(path))
const extlessPath = path.substring(0, path.length - extensionOfPath(path).length)
Expand All @@ -33,9 +33,3 @@ const verbose = async (path: string) => {
}
}


const main = (args: string[])=> {
args.forEach(verbose)
}
main(Deno.args)

File renamed without changes.

0 comments on commit 1b7a96c

Please sign in to comment.