1
1
// tslint:disable:no-use-before-declare
2
2
3
3
import { generateDefaultUISchema } from '@jsonforms/core' ;
4
- import { sep } from 'path' ;
4
+ import { dirname , join } from 'path' ;
5
5
import { existsSync } from 'fs' ;
6
6
7
- import { MessageType , readFileWithPromise , showMessage , writeFileWithPromise } from './utils' ;
7
+ import { MessageType , readFileWithPromise , showMessage , validateUiSchema , writeFileWithPromise } from './utils' ;
8
8
9
9
/**
10
10
* Generates the default UI Schema from a json schema
11
11
* @param {any } editorInstance the instance of the editor
12
- * @param {string } path the path to the schema file
12
+ * @param {any } path the path to the schema file
13
13
*/
14
- export const generateUISchema = async ( editorInstance : any , path : string ) => {
14
+ export const generateUISchema = async ( editorInstance : any , path : any ) => {
15
15
if ( ! path ) {
16
16
let fileUri = null ;
17
17
try {
@@ -23,23 +23,47 @@ export const generateUISchema = async (editorInstance: any, path: string) => {
23
23
filters : {
24
24
'Json Files' : [ 'json' ] ,
25
25
} ,
26
+ id : 'selectSchema' ,
26
27
} ) ;
27
28
if ( fileUri && fileUri [ 0 ] . fsPath ) {
28
29
path = fileUri [ 0 ] . fsPath ;
29
30
} else {
30
- showMessage ( editorInstance , 'Please select a json schema file' , MessageType . Error ) ;
31
- return ;
31
+ throw new Error ( 'Please select a json schema file' ) ;
32
32
}
33
33
} catch ( err ) {
34
- return ;
34
+ showMessage ( editorInstance , err . message , MessageType . Error ) ;
35
+ return err ;
35
36
}
36
37
}
37
38
39
+ // Read JSON Schema file
40
+ let jsonContent = null ;
41
+ try {
42
+ const content = await readFileWithPromise ( path , 'utf8' ) ;
43
+ jsonContent = JSON . parse ( content ) ;
44
+ } catch ( err ) {
45
+ showMessage ( editorInstance , err . message , MessageType . Error ) ;
46
+ return err ;
47
+ }
48
+
49
+ // Check if file is already a uischema file and throw an error
50
+ try {
51
+ const validUiSchema = await validateUiSchema ( jsonContent ) ;
52
+ if ( validUiSchema ) {
53
+ throw new Error ( 'It seems you selected a uischema. This functions does only work with a'
54
+ + ' schema file' ) ;
55
+ }
56
+ } catch ( err ) {
57
+ showMessage ( editorInstance , err . message , MessageType . Error ) ;
58
+ return err ;
59
+ }
60
+
38
61
let fileName = '' ;
39
62
try {
40
63
fileName = await editorInstance . window . showInputBox ( editorInstance . InputBoxOptions = {
41
64
prompt : 'Label: ' ,
42
65
placeHolder : 'Enter a filename for your UI Schema (default: uischema.json)' ,
66
+ id : 'fileName' ,
43
67
} ) ;
44
68
if ( fileName === undefined ) {
45
69
throw new Error ( 'UI schema generation canceled' ) ;
@@ -49,46 +73,43 @@ export const generateUISchema = async (editorInstance: any, path: string) => {
49
73
}
50
74
} catch ( err ) {
51
75
showMessage ( editorInstance , err . message , MessageType . Error ) ;
52
- return ;
76
+ return err ;
53
77
}
54
78
55
79
// Check if file already exist and ask user if it should be overwritten
56
- const newPath = path . substring ( 0 , path . lastIndexOf ( sep ) ) + sep + fileName ;
80
+ const newPath = join ( dirname ( path ) , fileName ) ;
57
81
if ( existsSync ( newPath ) ) {
58
82
let decision = 'No' ;
59
83
try {
60
84
decision = await editorInstance . window . showQuickPick ( [ 'Yes' , 'No' ] , editorInstance . QuickPickOptions = {
61
85
canSelectMany : false ,
62
- placeHolder : `This file ${ fileName } does already exist. Should it be overwritten?`
86
+ placeHolder : `This file ${ fileName } does already exist. Should it be overwritten?` ,
87
+ id : 'overwrite' ,
63
88
} ) ;
64
89
if ( decision !== 'Yes' ) {
65
90
throw new Error ( 'UI schema generation canceled' ) ;
66
91
}
67
92
} catch ( err ) {
68
93
showMessage ( editorInstance , err . message , MessageType . Error ) ;
69
- return ;
94
+ return err ;
70
95
}
71
96
}
72
97
73
- // Read JSON Schema file
74
- let jsonContent = null ;
98
+ // Generate the default UI schema
99
+ let jsonUISchema = null ;
75
100
try {
76
- const content = await readFileWithPromise ( path , 'utf8' ) ;
77
- jsonContent = JSON . parse ( content ) ;
101
+ jsonUISchema = await generateDefaultUISchema ( jsonContent ) ;
78
102
} catch ( err ) {
79
103
showMessage ( editorInstance , err . message , MessageType . Error ) ;
80
104
return ;
81
105
}
82
106
83
- // Generate the default UI schema
84
- const jsonUISchema = generateDefaultUISchema ( jsonContent ) ;
85
-
86
107
// Write UI Schema file
87
108
try {
88
109
await writeFileWithPromise ( newPath , JSON . stringify ( jsonUISchema , null , 2 ) ) ;
89
110
} catch ( err ) {
90
111
showMessage ( editorInstance , err . message , MessageType . Error ) ;
91
- return ;
112
+ return err ;
92
113
}
93
114
showMessage ( editorInstance , 'Successfully generated UI schema' ) ;
94
115
return jsonUISchema ;
0 commit comments