@@ -2,6 +2,7 @@ import * as simplegit from 'simple-git/promise';
2
2
import * as jsonforms from '@jsonforms/core' ;
3
3
import * as cp from 'child_process' ;
4
4
import { writeFile , readFile } from 'fs' ;
5
+ import * as Ajv from 'ajv' ;
5
6
var npm = process . platform === 'win32' ? 'npm.cmd' : 'npm' ;
6
7
7
8
/*
@@ -40,18 +41,53 @@ export function cloneAndInstall(repo: String, path: string, callback: (result: s
40
41
* @param {function } callback forwards the current status to the caller
41
42
*/
42
43
export function generateUISchema ( path : string , callback : ( result : string , type ?: string ) => void ) {
44
+ // Read JSON Schema file
43
45
readFile ( path , 'utf8' , ( err , data ) => {
44
- if ( err ) callback ( err . message , 'err' ) ;
45
- var jsonSchema = JSON . parse ( data ) ;
46
- var jsonUISchema = jsonforms . generateDefaultUISchema ( jsonSchema ) ;
47
- if ( process . platform === 'win32' ) {
48
- var newPath = path . substring ( 0 , path . lastIndexOf ( "\\" ) ) + '\\ui-schema.json' ;
49
- } else {
50
- var newPath = path . substring ( 0 , path . lastIndexOf ( "/" ) ) + '/ui-schema.json' ;
46
+ if ( err ) {
47
+ callback ( err . message , 'err' ) ;
48
+ return ;
51
49
}
52
- writeFile ( newPath , JSON . stringify ( jsonUISchema , null , 2 ) , ( err ) => {
53
- if ( err ) callback ( err . message , 'err' ) ;
54
- callback ( 'Successfully generated UI schema' ) ;
50
+
51
+ var jsonSchema = JSON . parse ( data ) ;
52
+ validateJSONSchema ( jsonSchema , function ( err ?: string ) {
53
+ if ( err ) {
54
+ callback ( err , 'err' ) ;
55
+ return ;
56
+ }
57
+
58
+ var jsonUISchema = jsonforms . generateDefaultUISchema ( jsonSchema ) ;
59
+
60
+ // Check if windows or linux filesystem
61
+ if ( process . platform === 'win32' ) {
62
+ var newPath = path . substring ( 0 , path . lastIndexOf ( "\\" ) ) + '\\ui-schema.json' ;
63
+ } else {
64
+ var newPath = path . substring ( 0 , path . lastIndexOf ( "/" ) ) + '/ui-schema.json' ;
65
+ }
66
+
67
+ // Write UI Schema file
68
+ writeFile ( newPath , JSON . stringify ( jsonUISchema , null , 2 ) , ( err ) => {
69
+ if ( err ) {
70
+ callback ( err . message , 'err' ) ;
71
+ return ;
72
+ }
73
+
74
+ callback ( 'Successfully generated UI schema' ) ;
75
+ } ) ;
55
76
} ) ;
56
77
} ) ;
57
78
}
79
+
80
+ /**
81
+ * Validate a given JSON Schema
82
+ * @param {string } path path to the json schema file
83
+ * @param {function } callback forwards the current status to the caller
84
+ */
85
+ function validateJSONSchema ( schema : Object , callback : ( err ?: string ) => void ) {
86
+ var ajv = new Ajv ( ) ;
87
+ try {
88
+ ajv . compile ( schema ) ;
89
+ callback ( ) ;
90
+ } catch ( error ) {
91
+ callback ( error . message ) ;
92
+ }
93
+ }
0 commit comments