@@ -2,10 +2,24 @@ const fs = require('fs');
2
2
const tsj = require ( 'ts-json-schema-generator' ) ;
3
3
const path = require ( 'path' ) ;
4
4
5
- console . log ( 'Building settings schemas\n' ) ;
6
-
7
5
const providersDir = 'src/default_providers' ;
8
6
7
+ const error = [ ] ;
8
+ let generate = false ;
9
+ if ( process . argv . length >= 3 ) {
10
+ if ( process . argv [ 2 ] === '--generate' ) {
11
+ generate = true ;
12
+ } else {
13
+ throw Error ( `Argument '${ process . argv [ 2 ] } ' is not valid.` )
14
+ }
15
+ }
16
+
17
+ if ( generate ) {
18
+ console . log ( 'Building settings schemas\n' ) ;
19
+ } else {
20
+ console . log ( 'Checking settings schemas\n' ) ;
21
+ }
22
+
9
23
// Build the langchain BaseLanguageModelParams object
10
24
const configBase = {
11
25
path : 'node_modules/@langchain/core/dist/language_models/base.d.ts' ,
@@ -50,9 +64,11 @@ const providers = {
50
64
51
65
Object . entries ( providers ) . forEach ( ( [ name , desc ] , index ) => {
52
66
const outputDir = path . join ( providersDir , name ) ;
53
- if ( ! fs . existsSync ( outputDir ) ) {
54
- fs . mkdirSync ( outputDir ) ;
67
+ const outputPath = path . join ( outputDir , 'settings-schema.json' ) ;
68
+ if ( ! generate && ! fs . existsSync ( outputPath ) ) {
69
+ throw Error ( `${ outputPath } does not exist` ) ;
55
70
}
71
+
56
72
// The configuration doesn't include functions, which may probably not be filled
57
73
// from the settings panel.
58
74
const config = {
@@ -63,8 +79,6 @@ Object.entries(providers).forEach(([name, desc], index) => {
63
79
topRef : false
64
80
} ;
65
81
66
- const outputPath = path . join ( outputDir , 'settings-schema.json' ) ;
67
-
68
82
const generator = tsj . createGenerator ( config ) ;
69
83
let schema ;
70
84
@@ -130,38 +144,33 @@ Object.entries(providers).forEach(([name, desc], index) => {
130
144
}
131
145
} ) ;
132
146
133
- // Write JSON file.
134
- const schemaString = JSON . stringify ( schema , null , 2 ) ;
135
- fs . writeFile ( outputPath , schemaString , err => {
136
- if ( err ) {
137
- throw err ;
147
+ let schemaString = JSON . stringify ( schema , null , 2 ) ;
148
+ schemaString += '\n' ;
149
+ if ( generate ) {
150
+ if ( ! fs . existsSync ( outputDir ) ) {
151
+ fs . mkdirSync ( outputDir ) ;
138
152
}
139
- } ) ;
153
+ // Write JSON file.
154
+ fs . writeFile ( outputPath , schemaString , err => {
155
+ if ( err ) {
156
+ throw err ;
157
+ }
158
+ } ) ;
159
+ } else {
160
+ const currentContent = fs . readFileSync ( outputPath , { encoding : 'utf-8' } ) ;
161
+ if ( currentContent !== schemaString ) {
162
+ error . push ( `The content of ${ name } settings does not match with the generated one.` )
163
+ }
164
+ }
140
165
} ) ;
141
166
142
- // // Build the index.ts file
143
- // const indexContent = ["import { IDict } from '../../tokens';", ''];
144
- // Object.keys(providers).forEach(name => {
145
- // indexContent.push(`import ${name} from './_generated/${name}.json';`);
146
- // });
147
-
148
- // indexContent.push('', 'const ProviderSettings: IDict<any> = {');
149
-
150
- // Object.keys(providers).forEach((name, index) => {
151
- // indexContent.push(
152
- // ` ${name}` + (index < Object.keys(providers).length - 1 ? ',' : '')
153
- // );
154
- // });
155
- // indexContent.push(' };', '', 'export { ProviderSettings };', '');
156
- // fs.writeFile(
157
- // path.join(providersDir, 'index.ts'),
158
- // indexContent.join('\n'),
159
- // err => {
160
- // if (err) {
161
- // throw err;
162
- // }
163
- // }
164
- // );
165
-
166
- console . log ( 'Settings schemas built\n' ) ;
167
- console . log ( '=====================\n' ) ;
167
+ if ( generate ) {
168
+ console . log ( 'Settings schemas built\n' ) ;
169
+ console . log ( '=====================\n' ) ;
170
+ } else if ( error . length ) {
171
+ console . error ( error . join ( '\n' ) ) ;
172
+ console . error ( 'Please run "jlpm settings:build" to fix it' )
173
+ throw Error ( 'Errors in settings schemas' ) ;
174
+ } else {
175
+ console . log ( 'Settings schemas checked successfully\n' ) ;
176
+ }
0 commit comments