11import Command from '../../core/command'
22import { flags } from '@contentstack/cli-command'
33import cli from 'cli-ux'
4- import buildOutput from '../../core/content-type/list '
4+ import { createDiagram , CreateDiagramOptions } from '../../core/content-type/diagram '
55
66export default class DiagramCommand extends Command {
77 static description = 'create a visual diagram of a Stack\'s Content Types' ;
88
99 static examples = [
10- '$ csdx content-type:diagram -s "xxxxxxxxxxxxxxxxxxx"' ,
11- '$ csdx content-type:diagram -a "management token"' ,
10+ '$ csdx content-type:diagram -s "xxxxxxxxxxxxxxxxxxx" -o "./content-model.svg"' ,
11+ '$ csdx content-type:diagram -a "management token" -o "./content-model.svg"' ,
12+ '$ csdx content-type:diagram -a "management token" -o "./content-model.svg" -d "landscape"' ,
13+ '$ csdx content-type:diagram -a "management token" -o "./content-model.dot" -t "dot"' ,
1214 ] ;
1315
1416 static flags = {
@@ -24,28 +26,75 @@ export default class DiagramCommand extends Command {
2426 char : 'a' ,
2527 description : 'management token alias' ,
2628 required : false ,
27- } )
29+ } ) ,
30+
31+ output : flags . string ( {
32+ char : 'o' ,
33+ description : 'full path to output' ,
34+ hidden : false ,
35+ multiple : false ,
36+ required : true ,
37+ } ) ,
38+
39+ direction : flags . string ( {
40+ char : 'd' ,
41+ description : 'graph orientation' ,
42+ default : 'portrait' ,
43+ options : [ 'portrait' , 'landscape' ] ,
44+ hidden : false ,
45+ multiple : false ,
46+ required : true ,
47+ } ) ,
48+
49+ type : flags . string ( {
50+ char : 't' ,
51+ description : 'graph output file type' ,
52+ default : 'svg' ,
53+ options : [ 'svg' , 'dot' ] ,
54+ hidden : false ,
55+ multiple : false ,
56+ required : true ,
57+ } ) ,
2858 }
2959
3060 async run ( ) {
3161 try {
3262 const { flags} = this . parse ( DiagramCommand )
3363 this . setup ( flags )
3464
65+ const outputPath = flags . output
66+
67+ if ( ! outputPath || ! outputPath . trim ( ) ) {
68+ this . error ( 'Please provide an output path.' , { exit : 2 } )
69+ }
70+
3571 cli . action . start ( Command . RequestDataMessage )
3672
3773 const [ stack , contentTypes , globalFields ] = await Promise . all ( [
3874 this . client . getStack ( this . apiKey ) ,
3975 this . client . getContentTypes ( this . apiKey , false ) ,
40- this . client . getGlobalFields ( this . apiKey )
76+ this . client . getGlobalFields ( this . apiKey ) ,
4177 ] )
4278
4379 cli . action . stop ( )
4480
45- console . log ( globalFields ) ;
81+
82+ const diagramOptions : CreateDiagramOptions = {
83+ stackName : stack . name ,
84+ contentTypes : contentTypes . content_types ,
85+ globalFields : globalFields . global_fields ,
86+ outputFileName : outputPath ,
87+ outputFileType : flags . type ,
88+ style : {
89+ orientation : flags . direction === 'portrait' ? 'LR' : 'TD'
90+ }
91+ }
92+
93+ const output = await createDiagram ( diagramOptions )
94+ this . log ( `Created Graph: ${ output . outputPath } ` )
4695
4796 } catch ( error ) {
4897 this . error ( error , { exit : 1 , suggestions : error . suggestions } )
4998 }
5099 }
51- }
100+ }
0 commit comments