-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add CLI package for parsing schema files #1286
Draft
tim-evans
wants to merge
1
commit into
main
Choose a base branch
from
cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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,16 @@ | ||
{ | ||
"name": "@atjson/cli", | ||
"version": "0.1.0", | ||
"description": "CLI (command line interface) for building and maintaining atjson schemas", | ||
"main": "dist/commonjs/index.js", | ||
"module": "dist/modules/index.js", | ||
"types": "dist/commonjs/index.d.ts", | ||
"license": "Apache-2.0", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"bin": "./dist/commonjs/cli.js", | ||
"dependencies": { | ||
"yaml": "^2.1.1" | ||
} | ||
} |
This file contains 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,31 @@ | ||
#!/usr/bin/env node | ||
import minimist from "minimist"; | ||
import { generateTypes } from "./commands/generate-types"; | ||
import { validate } from "./commands/validate"; | ||
|
||
let args = minimist(process.argv.slice(2)); | ||
|
||
if (args._[0] === "generate-types") { | ||
if ( | ||
typeof args._[1] === "string" && | ||
typeof args.out === "string" && | ||
(args.format === "javascript" || args.format === "typescript") | ||
) { | ||
generateTypes({ | ||
path: args._[1], | ||
out: args.out, | ||
format: args.format as "javascript" | "typescript", | ||
dryRun: "d" in args || "dry" in args, | ||
}).then(() => { | ||
process.exit(); | ||
}); | ||
} | ||
} else if (args._[0] === "validate") { | ||
if (typeof args._[1] === "string") { | ||
validate({ | ||
path: args._[1], | ||
}).then(() => { | ||
process.exit(); | ||
}); | ||
} | ||
} |
This file contains 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,114 @@ | ||
import { parse } from "yaml"; | ||
import path from "path"; | ||
import fs from "fs/promises"; | ||
import { format } from "prettier"; | ||
|
||
function classify(text: string) { | ||
return text | ||
.split("-") | ||
.map((part) => `${part[0].toUpperCase()}${part.slice(1)}`) | ||
.join(""); | ||
} | ||
|
||
function dasherize(text: string) { | ||
return text | ||
.replace(/([a-z])([A-Z])/g, (...args) => `${args[1]}-${args[2]}`) | ||
.split(/\s+/g) | ||
.join("-") | ||
.toLowerCase(); | ||
} | ||
|
||
const TYPE_MAP = { | ||
string: "string", | ||
int: "number", | ||
float: "number", | ||
boolean: "boolean", | ||
date: "Date", | ||
}; | ||
|
||
export async function generateTypes(options: { | ||
path: string; | ||
out: string; | ||
format: "javascript" | "typescript"; | ||
dryRun: boolean; | ||
}) { | ||
let file = await fs.readFile( | ||
path.join(process.cwd(), ...options.path.split("/")), | ||
"utf-8" | ||
); | ||
let yaml = parse(file.toString()); | ||
|
||
for (let key in yaml.schema) { | ||
let annotation = yaml.schema[key]; | ||
let type = | ||
annotation.type === "mark" | ||
? "InlineAnnotation" | ||
: annotation.type === "block" | ||
? "BlockAnnotation" | ||
: "ObjectAnnotation"; | ||
|
||
let lines = [`import { ${type} } from "@atjson/document";`, ``]; | ||
if (annotation.description) { | ||
lines.push(`/**`, ` * ${annotation.description}`, ` */`); | ||
} | ||
let name = annotation.name ?? annotation.key; | ||
lines.push(`export class ${classify(name)} extends ${type}`); | ||
|
||
if (annotation.data) { | ||
if (options.format === "javascript") { | ||
continue; | ||
} | ||
lines.push(`<{`); | ||
|
||
for (let key in annotation.data) { | ||
let attribute = annotation.data[key]; | ||
let typeName = attribute.type; | ||
let isArray = typeName.endsWith("[]"); | ||
if (isArray) { | ||
typeName = typeName.slice(0, typeName.length - 2); | ||
} | ||
if (!(typeName in TYPE_MAP)) { | ||
console.error(`${typeName} is not a valid attribute type.`); | ||
return; | ||
} | ||
if (!attribute.required) { | ||
key = `${key}?`; | ||
} | ||
let type = TYPE_MAP[typeName as keyof typeof TYPE_MAP]; | ||
if (isArray) { | ||
type = `${type}[]`; | ||
} | ||
if (attribute.description) { | ||
lines.push(`/**`, `* ${attribute.description}`, `*/`); | ||
} | ||
lines.push(`${key}: ${type}`); | ||
} | ||
lines.push(`}>`); | ||
} | ||
|
||
lines.push( | ||
`{`, | ||
` static vendorPrefix = "${dasherize(yaml.name)}";`, | ||
` static type = "${key}";`, | ||
`}` | ||
); | ||
|
||
let output = format(lines.join("\n"), { | ||
parser: options.format === "javascript" ? "babel" : "typescript", | ||
}); | ||
let filename = `${annotation.name}.${ | ||
options.format === "javascript" ? "js" : "ts" | ||
}`; | ||
|
||
if (options.dryRun) { | ||
console.log(output); | ||
} else { | ||
await fs.writeFile( | ||
path.join(process.cwd(), ...options.out.split("/"), filename), | ||
output | ||
); | ||
} | ||
} | ||
|
||
return yaml; | ||
} |
This file contains 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,26 @@ | ||
import { parseDocument } from "yaml"; | ||
import path from "path"; | ||
import fs from "fs/promises"; | ||
|
||
export async function validate(options: { path: string }) { | ||
let file = await fs.readFile( | ||
path.join(process.cwd(), ...options.path.split("/")), | ||
"utf-8" | ||
); | ||
let yaml = parseDocument(file.toString()); | ||
let errors = { | ||
missing: [] as string[], | ||
}; | ||
if (!yaml.has("name")) { | ||
errors.missing.push("name"); | ||
} | ||
if (!yaml.has("version")) { | ||
errors.missing.push("version"); | ||
} | ||
if (!yaml.has("schema")) { | ||
errors.missing.push("schema"); | ||
} | ||
console.log(errors); | ||
|
||
return errors; | ||
} |
This file contains 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,2 @@ | ||
export * from "./commands/generate-types"; | ||
export * from "./commands/validate"; |
This file contains 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,10 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist/commonjs", | ||
"rootDir": "./src", | ||
"module": "Node16", | ||
"target": "ES3" | ||
}, | ||
"include": ["src/**/*"] | ||
} |
This file contains 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,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist/modules", | ||
"isolatedModules": true, | ||
"module": "esnext", | ||
"target": "ES2018" | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this....right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops lol