11const lodash = require ( 'lodash' ) ;
22const Templator = require ( '../lib/templator' ) ;
33const persist = require ( '../lib/persist' ) ;
4- const { tryJSONParse, output, commonYargsOptions } = require ( '../lib/utils' ) ;
4+ const {
5+ tryJSONParse,
6+ output,
7+ commonYargsOptions,
8+ parseTags,
9+ } = require ( '../lib/utils' ) ;
510
611exports . command = 'generate <mapping> [context-selector]' ;
712
@@ -28,13 +33,17 @@ exports.builder = (yargs) => {
2833 describe : 'Hide the header from the human readable output' ,
2934 type : 'boolean' ,
3035 default : false ,
31- implies : 'limit-to ' ,
36+ implies : 'filter-by ' ,
3237 } )
33- . options ( 'limit-to ' , {
38+ . options ( 'filter-by ' , {
3439 describe : 'Only show file with a give set of tags' ,
3540 type : 'string' ,
3641 implies : 'human-readable' ,
37- coerce : ( param ) => tryJSONParse ( param ) ,
42+ coerce : ( param ) => parseTags ( param ) ,
43+ } )
44+ . options ( 'group-by' , {
45+ describe : 'group templates by a set of given tags names' ,
46+ type : 'array' ,
3847 } )
3948 . options ( 'just-mapping' , {
4049 describe : 'Just render mapping, not templates, useful to debug issues' ,
@@ -49,13 +58,6 @@ exports.builder = (yargs) => {
4958
5059function _human_reabable ( render , args ) {
5160 for ( const item of render . locations ) {
52- if (
53- ! lodash . isUndefined ( args . limitTo ) &&
54- ! lodash . isMatch ( item . tags , args . limitTo )
55- ) {
56- continue ;
57- }
58-
5961 if ( ! args . hideHeaders ) {
6062 console . log ( '---' ) ;
6163 console . log (
@@ -83,15 +85,29 @@ exports.handler = async (args) => {
8385 return ;
8486 }
8587
86- const render = await templator . render ( args . mapping , args . contextSelector ) ;
88+ let render = await templator . render ( args . mapping , args . contextSelector ) ;
89+
90+ if ( ! lodash . isUndefined ( args . filterBy ) ) {
91+ render . locations = lodash . filter ( render . locations , ( render ) =>
92+ lodash . isMatch ( render . tags , args . filterBy )
93+ ) ;
94+ }
8795
8896 if ( args . h ) {
8997 _human_reabable ( render , args ) ;
9098 }
9199
92- output ( render , args . output ) ;
100+ if ( ! lodash . isUndefined ( args . groupBy ) ) {
101+ for ( const location of render . locations ) {
102+ location . group = Object . values (
103+ lodash . pick ( location . tags , args . groupBy )
104+ ) . join ( '_' ) ;
105+ }
106+ }
93107
94108 if ( args . persist ) {
95109 output ( await persist ( render , args ) , args . output || 'json' ) ;
110+ } else {
111+ output ( render , args . output ) ;
96112 }
97113} ;
0 commit comments