@@ -5,6 +5,25 @@ const inquirer = require('inquirer');
55const yaml = require ( 'js-yaml' ) ;
66
77
8+ async function prompt_yaml_editor ( options ) {
9+ const data = await inquirer . prompt ( {
10+ type : 'editor' ,
11+ name : 'input' ,
12+ message : options . message ,
13+ default : yaml . safeDump ( options . defaultValue ) ,
14+ validate : function ( text ) {
15+ let content ;
16+ try {
17+ content = yaml . safeLoad ( text ) ;
18+ } catch ( e ) {
19+ return `You can not change the data format (YAML). The data entered is not a valid YAML document.\n${ e . message } ` ;
20+ }
21+ return options . validator ? options . validator ( content ) : true ;
22+ } ,
23+ } ) ;
24+ return yaml . safeLoad ( data . input ) ;
25+ }
26+
827module . exports = resource => {
928 const options = {
1029 [ resource . name ] : {
@@ -22,44 +41,30 @@ module.exports = resource => {
2241 handler : async args => {
2342 const url = `${ resource . url ( args ) } /${ args [ resource . name ] } ` ;
2443 const result = await args . helpers . api . get ( url ) ;
25-
26- const questions = {
27- type : 'editor' ,
28- name : 'input' ,
29- message : 'Specify the name and access policy of the token.' ,
30- default : yaml . safeDump ( {
44+ const input = await prompt_yaml_editor ( {
45+ defaultValue : {
3146 name : result . name ,
3247 access : result . access . map ( access => ( {
3348 method : access . method ,
3449 path : access . path ,
3550 } ) ) ,
36- } ) ,
37- validate : function ( text ) {
38- try {
39- yaml . safeLoad ( text ) ;
40- } catch ( e ) {
41- return `You can not change the data format (YAML). The data entered is not a valid YAML document.\n${ e . message } ` ;
42- }
43- return true ;
4451 } ,
45- } ;
46-
47- const { input} = await inquirer . prompt ( questions ) ;
48- const data = yaml . safeLoad ( input ) ;
52+ message : 'Specify the name and access policy of the token.' ,
53+ } ) ;
4954 const requests = [ ] ;
50- if ( 'name' in data && data . name !== result . name ) {
55+ if ( 'name' in input && input . name !== result . name ) {
5156 requests . push ( args . helpers . api . patch ( `${ args . $node . parent . config . url ( args ) } /${ args [ resource . name ] } ` , {
52- name : data . name ,
57+ name : input . name ,
5358 } ) ) ;
5459 }
55- if ( 'access' in data ) {
60+ if ( 'access' in input ) {
5661 // Add new access
57- requests . push ( ...data . access
62+ requests . push ( ...input . access
5863 . filter ( access => ! result . access . find ( acc => acc . path === access . path && acc . method === access . method ) )
5964 . map ( access => args . helpers . api . post ( `${ url } /access` , access ) ) ) ;
6065 // Remove missing access
6166 requests . push ( ...result . access
62- . filter ( access => ! data . access . find ( acc => acc . path === access . path && acc . method === access . method ) )
67+ . filter ( access => ! input . access . find ( acc => acc . path === access . path && acc . method === access . method ) )
6368 . map ( access => args . helpers . api . delete ( `${ url } /access/${ access . _id } ` ) ) ) ;
6469 }
6570 for ( const req in requests ) {
0 commit comments