Skip to content

Commit dac2629

Browse files
committed
Added JSON schemas to aid with CLI implementations
1 parent 9f15347 commit dac2629

13 files changed

+442
-29
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/build
55
**/.DS_Store
66
/.idea
7-
/coverage
7+
/coverage
8+
generated

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ coverage
77
*.md
88
package-lock.json
99
package.json
10+
generated

package-lock.json

+326
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
},
1010
"scripts": {
1111
"prebuild": "rimraf dist",
12-
"build": "tsc --build ./src/lib/tsconfig.json",
12+
"build": "npm run schemas && tsc --build ./src/lib/tsconfig.json",
13+
"schemas": "json2ts -i schemas/ -o src/lib/generated/",
1314
"preversion": "npm run lint && npm run test && npm run build",
1415
"prepublishOnly": "npm run build",
1516
"test": "jest",
@@ -43,6 +44,7 @@
4344
"eslint": "^8.36.0",
4445
"jest": "^29.5.0",
4546
"jest-environment-node": "^29.5.0",
47+
"json-schema-to-typescript": "^13.0.1",
4648
"prettier": "^2.8.8",
4749
"rimraf": "^4.4.0",
4850
"ts-jest": "^29.1.0"

schemas/GenerationResponse.json

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"$id": "https://raw.githubusercontent.com/main/schemas/GenerationResponse.json",
3+
"$schema": "https://json-schema.org/draft/2020-12/schema",
4+
"title": "Ng2rGenerationResponse",
5+
"type": "object",
6+
"additionalProperties": false,
7+
"properties": {
8+
"prompt": {
9+
"type": "string",
10+
"description": "The prompted used to generate this response"
11+
},
12+
"result": {
13+
"type": "array",
14+
"items": {
15+
"$ref": "#/$defs/GeneratedCode"
16+
}
17+
}
18+
},
19+
"required": [
20+
"prompt",
21+
"result"
22+
],
23+
"$defs": {
24+
"GeneratedCode": {
25+
"type": "object",
26+
"title": "Ng2rGeneratedCode",
27+
"additionalProperties": false,
28+
"properties": {
29+
"jsx": {
30+
"type": "string",
31+
"description": "The generated JSX or TSX"
32+
},
33+
"markdown": {
34+
"type": "string",
35+
"description": "The full response in Markdown format"
36+
}
37+
},
38+
"required": [
39+
"jsx",
40+
"markdown"
41+
]
42+
}
43+
}
44+
}

schemas/SearchResult.json

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"$id": "https://raw.githubusercontent.com/main/schemas/SearchResponse.json",
3+
"$schema": "https://json-schema.org/draft/2020-12/schema",
4+
"title": "Ng2rSearchResult",
5+
"type": "object",
6+
"additionalProperties": false,
7+
"required": ["result"],
8+
"properties": {
9+
"result": {
10+
"type": "array",
11+
"items": {
12+
"$ref": "#/$defs/ComponentInfo"
13+
}
14+
}
15+
},
16+
"$defs": {
17+
"ComponentInfo": {
18+
"type": "object",
19+
"title": "Ng2rComponent",
20+
"additionalProperties": false,
21+
"required": [
22+
"name",
23+
"file",
24+
"location"
25+
],
26+
"properties": {
27+
"name": {
28+
"type": "string",
29+
"description": "The name of the AngularJS component"
30+
},
31+
"file": {
32+
"type": "string",
33+
"description": "The name of the file"
34+
},
35+
"location": {
36+
"type": "object",
37+
"title": "Location",
38+
"required": [
39+
"start",
40+
"end"
41+
],
42+
"description": "the location of the component within the file",
43+
"additionalProperties": false,
44+
"properties": {
45+
"start": {
46+
"type": "integer"
47+
},
48+
"end": {
49+
"type": "integer"
50+
}
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}

src/.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
sourceType: 'module',
99
tsconfigRootDir: __dirname,
1010
},
11-
ignorePatterns: ['node_modules', 'dist', 'coverage', '.eslintrc.cjs', 'templates', 'test_data'],
11+
ignorePatterns: ['node_modules', 'dist', 'coverage', '.eslintrc.cjs', 'templates', 'test_data', 'generated'],
1212
rules: {
1313
quotes: ['error', 'single'],
1414
semi: ['error', 'never'],

src/lib/commands/convertCmd.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import {ConvertComponentArgs} from '../cliArgs'
33
import path from 'path'
44
import {onComplete} from '../io/writeOutput'
55
import fs from 'fs'
6+
import {Ng2RGenerationResponse} from '../generated/GenerationResponse'
67

78
export default async function convertCmd({cwd, ...ng2ReactArgs}: ConvertComponentArgs) {
89
const absoluteFilePath = path.join(cwd, ng2ReactArgs.file)
910
const fileContent = fs.readFileSync(absoluteFilePath, 'utf-8')
1011
const {prompt, results} = await convert(fileContent, ng2ReactArgs)
1112

12-
onComplete({prompt, result: results})
13+
onComplete({prompt, result: results} satisfies Ng2RGenerationResponse)
1314
}

src/lib/commands/generateReactTestCmd.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import {GenerateTestArgs} from '../cliArgs'
33
import path from 'path'
44
import {onComplete} from '../io/writeOutput'
55
import fs from 'fs'
6+
import {Ng2RGenerationResponse} from '../generated/GenerationResponse'
67

78
export default async function generateReactTestCmd({cwd, file, ...ng2ReactArgs}: GenerateTestArgs) {
89
const absoluteFilePath = path.join(cwd, file)
910
const fileContent = fs.readFileSync(absoluteFilePath, 'utf-8')
1011
const {prompt, results} = await generateReactTest(fileContent, ng2ReactArgs)
1112

12-
onComplete({prompt, result: results})
13+
onComplete({prompt, result: results} satisfies Ng2RGenerationResponse)
1314
}

src/lib/commands/searchCmd.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { search } from '@ng2react/core'
22
import { CliArgs } from '../cliArgs'
33
import path from 'path'
44
import { onComplete } from '../io/writeOutput'
5-
import { ComponentInfo } from '../model/SearchResult'
65
import fs from 'fs'
6+
import type {Ng2RComponent} from '../generated/SearchResult'
77

88
export default function searchCmd({ file, cwd }: CliArgs): void {
99
const fileContent = fs.readFileSync(path.join(cwd, file), 'utf-8')
@@ -20,7 +20,7 @@ export default function searchCmd({ file, cwd }: CliArgs): void {
2020
start: c.node.getStart(),
2121
end: c.node.getEnd(),
2222
},
23-
} satisfies ComponentInfo)
23+
} satisfies Ng2RComponent)
2424
)
2525

2626
onComplete({ result })

src/lib/io/writeOutput.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {SearchResult} from '../model/SearchResult'
2-
import {ConvertResult} from '../model/ConvertResult'
1+
import {Ng2RSearchResult} from '../generated/SearchResult'
2+
import {Ng2RGenerationResponse} from '../generated/GenerationResponse'
33

4-
export function onComplete(output: SearchResult | ConvertResult): void {
4+
export function onComplete(output: Ng2RSearchResult | Ng2RGenerationResponse): void {
55
// For json output, we want to write the full object to stdout
66
const json = JSON.stringify(output)
77
process.stdout.write(json)

src/lib/model/ConvertResult.ts

-7
This file was deleted.

src/lib/model/SearchResult.ts

-12
This file was deleted.

0 commit comments

Comments
 (0)