Skip to content

Commit b984bfe

Browse files
committed
Updated schemas to include connection check
1 parent dda271c commit b984bfe

11 files changed

+137
-58
lines changed

package-lock.json

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"homepage": "https://github.com/maxbilbow/angularjs2react-cli#readme",
3434
"devDependencies": {
35-
"@ng2react/core": "^1.5.0",
35+
"@ng2react/core": "^1.6.0",
3636
"@types/jest": "^29.5.2",
3737
"@types/lodash": "^4.14.195",
3838
"@types/node": "^18.16.16",

schemas/CliArgs.json

+81-30
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,99 @@
11
{
22
"$id": "https://raw.githubusercontent.com/ng2react/cli/main/schemas/CliArgs.json",
33
"$schema": "https://json-schema.org/draft/2020-12/schema",
4-
"title": "Ng2rConvertOptions",
5-
"extends": {
6-
"$ref": "#/$defs/test"
7-
},
4+
"title": "ignorethistype",
5+
"description": "@deprecated",
86
"type": "object",
97
"additionalProperties": false,
10-
"required": [
11-
"componentName",
12-
"sourceRoot"
13-
],
148
"properties": {
15-
"componentName": {
16-
"type": "string"
9+
"test": {
10+
"$ref": "#/$defs/test"
1711
},
18-
"sourceRoot": {
19-
"type": "string",
20-
"description": "The root directory of the project. If not specified, the directory below that of the\nnearest package.json file will be used."
12+
"convert": {
13+
"$ref": "#/$defs/convert"
2114
},
22-
"customPrompt": {
23-
"type": "string",
24-
"description": "Custom rules (Markdown) that will be used instead of the default rules regarding pattern conversion."
15+
"search": {
16+
"$ref": "#/$defs/search"
17+
},
18+
"check": {
19+
"$ref": "#/$defs/check"
2520
}
2621
},
2722
"$defs": {
28-
"common": {
29-
"$schema": "https://json-schema.org/draft/2020-12/schema",
30-
"title": "Ng2rCommonOptions",
23+
"convert": {
24+
"title": "Ng2rConvertOptions",
25+
"extends": {
26+
"$ref": "#/$defs/gpt"
27+
},
3128
"type": "object",
3229
"additionalProperties": false,
3330
"required": [
34-
"file",
35-
"cwd"
31+
"componentName",
32+
"sourceRoot"
3633
],
3734
"properties": {
38-
"cwd": {
35+
"componentName": {
3936
"type": "string"
4037
},
41-
"file": {
42-
"type": "string"
38+
"sourceRoot": {
39+
"type": "string",
40+
"description": "The root directory of the project. If not specified, the directory below that of the\nnearest package.json file will be used."
41+
},
42+
"customPrompt": {
43+
"type": "string",
44+
"description": "Custom rules (Markdown) that will be used instead of the default rules regarding pattern conversion."
4345
}
4446
}
4547
},
4648
"test": {
4749
"title": "Ng2rTestGenerationOptions",
4850
"type": "object",
4951
"extends": {
50-
"$ref": "#/$defs/common"
52+
"$ref": "#/$defs/gpt"
5153
},
54+
"additionalProperties": false
55+
},
56+
"model": {
57+
"type": "string",
58+
"enum": [
59+
"gpt-4",
60+
"gpt-3-turbo"
61+
]
62+
},
63+
"search": {
64+
"title": "Ng2rSearchOptions",
65+
"type": "object",
66+
"additionalProperties": false,
5267
"required": [
53-
"apiKey"
68+
"file"
5469
],
70+
"properties": {
71+
"file": {
72+
"description": "File path relative to cwd",
73+
"type": "string"
74+
}
75+
}
76+
},
77+
"gpt": {
78+
"title": "Ng2rGptOptions",
79+
"type": "object",
5580
"additionalProperties": false,
81+
"required": [
82+
"file",
83+
"apiKey",
84+
"model",
85+
"temperature"
86+
],
5687
"properties": {
88+
"file": {
89+
"description": "Absolute path to the file to be converted",
90+
"type": "string"
91+
},
5792
"apiKey": {
5893
"type": "string"
5994
},
6095
"model": {
61-
"type": "string",
62-
"enum": [
63-
"gpt-4"
64-
],
96+
"$ref": "#/$defs/model",
6597
"default": "gpt-4"
6698
},
6799
"organization": {
@@ -71,6 +103,7 @@
71103
"type": "number",
72104
"minimum": 0,
73105
"maximum": 2,
106+
"default": 0.2,
74107
"description": "The temperature to use when generating completions. Higher values result in more random completions.\nMust be between 0 and 2."
75108
},
76109
"targetLanguage": {
@@ -81,6 +114,24 @@
81114
]
82115
}
83116
}
117+
},
118+
"check": {
119+
"title": "Ng2rCheckOptions",
120+
"type": "object",
121+
"additionalProperties": false,
122+
"required": [
123+
"apiKey",
124+
"model"
125+
],
126+
"properties": {
127+
"apiKey": {
128+
"type": "string"
129+
},
130+
"model": {
131+
"$ref": "#/$defs/model",
132+
"default": "gpt-3-turbo"
133+
}
134+
}
84135
}
85136
}
86137
}

schemas/Commands.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"$schema": "https://json-schema.org/draft/2020-12/schema",
44
"title": "Ng2rCommand",
55
"type": "string",
6-
"enum": ["search", "convert", "generateReactTest"]
6+
"enum": ["search", "convert", "generateReactTest", "checkConnection"]
77
}

src/lib/cliArgs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type {ConvertOptions} from '@ng2react/core'
22
import {ReactTestOptions} from '@ng2react/core/src/lib/modules/openai-conversion/react-test-gen'
3-
import {Ng2RCommonOptions, Ng2RConvertOptions, Ng2RTestGenerationOptions} from './generated/CliArgs'
3+
import { Ng2RConvertOptions, Ng2RTestGenerationOptions} from './generated/CliArgs'
44
import {Ng2ROption} from './generated/Options'
55

6-
export interface CliArgs extends Ng2RCommonOptions {
6+
export interface CliArgs {
77
verbose?: true
88
quiet?: true
99
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {checkConnection} from '@ng2react/core'
2+
import {onComplete, onError} from '../io/writeOutput'
3+
import {Ng2RCheckOptions} from '../generated/CliArgs'
4+
5+
export default async function checkConnectionCmd(options: Ng2RCheckOptions) {
6+
try {
7+
const response = await checkConnection(options)
8+
onComplete(response)
9+
} catch (error) {
10+
onError(error)
11+
}
12+
}

src/lib/commands/convertCmd.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import {convert} from '@ng2react/core'
22
import {ConvertComponentArgs} from '../cliArgs'
3-
import path from 'path'
43
import {onComplete} from '../io/writeOutput'
54
import fs from 'fs'
65
import {Ng2RGenerationResponse} from '../generated/GenerationResponse'
76

8-
export default async function convertCmd({cwd, ...ng2ReactArgs}: ConvertComponentArgs) {
9-
const absoluteFilePath = path.join(cwd, ng2ReactArgs.file)
10-
const fileContent = fs.readFileSync(absoluteFilePath, 'utf-8')
11-
const {prompt, results} = await convert(fileContent, ng2ReactArgs)
7+
export default async function convertCmd(opt: ConvertComponentArgs) {
8+
const fileContent = fs.readFileSync(opt.file, 'utf-8')
9+
const {prompt, results} = await convert(fileContent, opt)
1210

1311
onComplete({prompt, result: results} satisfies Ng2RGenerationResponse)
1412
}
+3-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import {generateReactTest} from '@ng2react/core'
22
import {GenerateTestArgs} from '../cliArgs'
3-
import path from 'path'
43
import {onComplete} from '../io/writeOutput'
54
import fs from 'fs'
65
import {Ng2RGenerationResponse} from '../generated/GenerationResponse'
76

8-
export default async function generateReactTestCmd({cwd, file, ...ng2ReactArgs}: GenerateTestArgs) {
9-
const absoluteFilePath = path.join(cwd, file)
10-
const fileContent = fs.readFileSync(absoluteFilePath, 'utf-8')
11-
const {prompt, results} = await generateReactTest(fileContent, ng2ReactArgs)
7+
export default async function generateReactTestCmd(opt: GenerateTestArgs) {
8+
const fileContent = fs.readFileSync(opt.file, 'utf-8')
9+
const {prompt, results} = await generateReactTest(fileContent, opt)
1210

1311
onComplete({prompt, result: results} satisfies Ng2RGenerationResponse)
1412
}

src/lib/commands/searchCmd.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { search } from '@ng2react/core'
2-
import { CliArgs } from '../cliArgs'
3-
import path from 'path'
4-
import { onComplete } from '../io/writeOutput'
1+
import {search} from '@ng2react/core'
2+
import {onComplete} from '../io/writeOutput'
53
import fs from 'fs'
6-
import type {Ng2RComponent} from '../generated/SearchResult'
4+
import type {Ng2RComponent, Ng2RSearchResult} from '../generated/SearchResult'
5+
import {Ng2RSearchOptions} from '../generated/CliArgs'
76

8-
export default function searchCmd({ file, cwd }: CliArgs): void {
9-
const fileContent = fs.readFileSync(path.join(cwd, file), 'utf-8')
10-
const components = search(fileContent, { file })
7+
export default function searchCmd({file}: Ng2RSearchOptions): void {
8+
const fileContent = fs.readFileSync(file, 'utf-8')
9+
const components = search(fileContent, {file})
1110
if (!components) {
1211
throw Error(`Could not find components in ${file}`)
1312
}
@@ -23,5 +22,5 @@ export default function searchCmd({ file, cwd }: CliArgs): void {
2322
} satisfies Ng2RComponent)
2423
)
2524

26-
onComplete({ result })
25+
onComplete({result} satisfies Ng2RSearchResult)
2726
}

src/lib/io/writeOutput.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import {Ng2RSearchResult} from '../generated/SearchResult'
22
import {Ng2RGenerationResponse} from '../generated/GenerationResponse'
33

4-
export function onComplete(output: Ng2RSearchResult | Ng2RGenerationResponse): void {
4+
export function onComplete(output: Ng2RSearchResult | Ng2RGenerationResponse | string): void {
5+
if (typeof output === 'string') {
6+
process.stdout.write(output)
7+
return
8+
}
59
// For json output, we want to write the full object to stdout
610
const json = JSON.stringify(output)
711
process.stdout.write(json)

src/lib/ng2r.ts

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import middleware from './middleware'
88
import generateReactTestCmd from './commands/generateReactTestCmd'
99
import {Ng2RCommand} from './generated/Commands'
1010
import type yargs from 'yargs'
11+
import checkConnectionCmd from './commands/checkConnectionCmd'
12+
import {Ng2RCheckOptions} from './generated/CliArgs'
1113

1214
process.on('unhandledRejection', (reason) => {
1315
onError(reason)
@@ -117,6 +119,21 @@ process.on('uncaughtException', (error) => {
117119
}),
118120
generateReactTestCmd
119121
)
122+
.command<Ng2RCheckOptions>('checkConnection' satisfies Ng2RCommand,
123+
'Checks the connection to the openai api',
124+
yargs => yargs
125+
.option('apiKey' satisfies OptionName, {
126+
describe: 'The openai api key',
127+
type: 'string',
128+
default: process.env.OPENAI_API_KEY,
129+
})
130+
.option('model' satisfies OptionName, {
131+
describe: 'The openai model to use',
132+
type: 'string',
133+
default: process.env.OPENAI_MODEL ?? 'gpt-3-turbo',
134+
}),
135+
checkConnectionCmd
136+
)
120137
.option('cwd' satisfies OptionName, {
121138
describe: 'The current working directory',
122139
type: 'string',

0 commit comments

Comments
 (0)