@@ -78,63 +78,68 @@ export default async function({log = true, sandbox = false, community = {}} = {}
7878 logger ( "metrics/setup > load package.json > success" )
7979
8080 //Load community templates
81- if ( ( typeof conf . settings . community . templates === "string" ) && ( conf . settings . community . templates . length ) ) {
82- logger ( "metrics/setup > parsing community templates list" )
83- conf . settings . community . templates = [ ...new Set ( [ ...decodeURIComponent ( conf . settings . community . templates ) . split ( "," ) . map ( v => v . trim ( ) . toLocaleLowerCase ( ) ) . filter ( v => v ) ] ) ]
84- }
85- if ( ( Array . isArray ( conf . settings . community . templates ) ) && ( conf . settings . community . templates . length ) ) {
86- //Clean remote repository
87- logger ( `metrics/setup > ${ conf . settings . community . templates . length } community templates to install` )
88- await fs . promises . rm ( path . join ( __templates , ".community" ) , { recursive : true , force : true } )
89- //Download community templates
90- for ( const template of conf . settings . community . templates ) {
91- try {
92- //Parse community template
93- logger ( `metrics/setup > load community template ${ template } ` )
94- const { repo, branch, name, trust = false } = template . match ( / ^ (?< repo > [ \s \S ] + ?) @ (?< branch > [ \s \S ] + ?) : (?< name > [ \s \S ] + ?) (?< trust > [ + ] t r u s t ) ? $ / ) ?. groups ?? null
95- const command = `git clone --single-branch --branch ${ branch } https://github.com/${ repo } .git ${ path . join ( __templates , ".community" ) } `
96- logger ( `metrics/setup > run ${ command } ` )
97- //Clone remote repository
98- processes . execSync ( command , { stdio : "ignore" } )
99- //Extract template
100- logger ( `metrics/setup > extract ${ name } from ${ repo } @${ branch } ` )
101- await fs . promises . rm ( path . join ( __templates , `@${ name } ` ) , { recursive : true , force : true } )
102- await fs . promises . rename ( path . join ( __templates , ".community/source/templates" , name ) , path . join ( __templates , `@${ name } ` ) )
103- //JavaScript file
104- if ( trust )
105- logger ( `metrics/setup > keeping @${ name } /template.mjs (unsafe mode is enabled)` )
106- else if ( fs . existsSync ( path . join ( __templates , `@${ name } ` , "template.mjs" ) ) ) {
107- logger ( `metrics/setup > removing @${ name } /template.mjs` )
108- await fs . promises . unlink ( path . join ( __templates , `@${ name } ` , "template.mjs" ) )
109- const inherit = yaml . load ( `${ fs . promises . readFile ( path . join ( __templates , `@${ name } ` , "metadata.yml" ) ) } ` ) . extends ?? null
110- if ( inherit ) {
111- logger ( `metrics/setup > @${ name } extends from ${ inherit } ` )
112- if ( fs . existsSync ( path . join ( __templates , inherit , "template.mjs" ) ) ) {
113- logger ( `metrics/setup > @${ name } extended from ${ inherit } ` )
114- await fs . promises . copyFile ( path . join ( __templates , inherit , "template.mjs" ) , path . join ( __templates , `@${ name } ` , "template.mjs" ) )
115- }
116- else {
117- logger ( `metrics/setup > @${ name } could not extends ${ inherit } as it does not exist` )
81+ if ( ( conf . settings . extras ?. features ?. includes ( "metrics.setup.community.templates" ) ) || ( conf . settings . extras ?. features === true ) || ( conf . settings . extras ?. default ) ) {
82+ if ( ( typeof conf . settings . community . templates === "string" ) && ( conf . settings . community . templates . length ) ) {
83+ logger ( "metrics/setup > parsing community templates list" )
84+ conf . settings . community . templates = [ ...new Set ( [ ...decodeURIComponent ( conf . settings . community . templates ) . split ( "," ) . map ( v => v . trim ( ) . toLocaleLowerCase ( ) ) . filter ( v => v ) ] ) ]
85+ }
86+ if ( ( Array . isArray ( conf . settings . community . templates ) ) && ( conf . settings . community . templates . length ) ) {
87+ //Clean remote repository
88+ logger ( `metrics/setup > ${ conf . settings . community . templates . length } community templates to install` )
89+ await fs . promises . rm ( path . join ( __templates , ".community" ) , { recursive : true , force : true } )
90+ //Download community templates
91+ for ( const template of conf . settings . community . templates ) {
92+ try {
93+ //Parse community template
94+ logger ( `metrics/setup > load community template ${ template } ` )
95+ const { repo, branch, name, trust = false } = template . match ( / ^ (?< repo > [ \s \S ] + ?) @ (?< branch > [ \s \S ] + ?) : (?< name > [ \s \S ] + ?) (?< trust > [ + ] t r u s t ) ? $ / ) ?. groups ?? null
96+ const command = `git clone --single-branch --branch ${ branch } https://github.com/${ repo } .git ${ path . join ( __templates , ".community" ) } `
97+ logger ( `metrics/setup > run ${ command } ` )
98+ //Clone remote repository
99+ processes . execSync ( command , { stdio : "ignore" } )
100+ //Extract template
101+ logger ( `metrics/setup > extract ${ name } from ${ repo } @${ branch } ` )
102+ await fs . promises . rm ( path . join ( __templates , `@${ name } ` ) , { recursive : true , force : true } )
103+ await fs . promises . rename ( path . join ( __templates , ".community/source/templates" , name ) , path . join ( __templates , `@${ name } ` ) )
104+ //JavaScript file
105+ if ( trust )
106+ logger ( `metrics/setup > keeping @${ name } /template.mjs (unsafe mode is enabled)` )
107+ else if ( fs . existsSync ( path . join ( __templates , `@${ name } ` , "template.mjs" ) ) ) {
108+ logger ( `metrics/setup > removing @${ name } /template.mjs` )
109+ await fs . promises . unlink ( path . join ( __templates , `@${ name } ` , "template.mjs" ) )
110+ const inherit = yaml . load ( `${ fs . promises . readFile ( path . join ( __templates , `@${ name } ` , "metadata.yml" ) ) } ` ) . extends ?? null
111+ if ( inherit ) {
112+ logger ( `metrics/setup > @${ name } extends from ${ inherit } ` )
113+ if ( fs . existsSync ( path . join ( __templates , inherit , "template.mjs" ) ) ) {
114+ logger ( `metrics/setup > @${ name } extended from ${ inherit } ` )
115+ await fs . promises . copyFile ( path . join ( __templates , inherit , "template.mjs" ) , path . join ( __templates , `@${ name } ` , "template.mjs" ) )
116+ }
117+ else {
118+ logger ( `metrics/setup > @${ name } could not extends ${ inherit } as it does not exist` )
119+ }
118120 }
119121 }
122+ else {
123+ logger ( `metrics/setup > @${ name } /template.mjs does not exist` )
124+ }
125+
126+ //Clean remote repository
127+ logger ( `metrics/setup > clean ${ repo } @${ branch } ` )
128+ await fs . promises . rm ( path . join ( __templates , ".community" ) , { recursive : true , force : true } )
129+ logger ( `metrics/setup > loaded community template ${ name } ` )
120130 }
121- else {
122- logger ( `metrics/setup > @${ name } /template.mjs does not exist` )
131+ catch ( error ) {
132+ logger ( `metrics/setup > failed to load community template ${ template } ` )
133+ logger ( error )
123134 }
124-
125- //Clean remote repository
126- logger ( `metrics/setup > clean ${ repo } @${ branch } ` )
127- await fs . promises . rm ( path . join ( __templates , ".community" ) , { recursive : true , force : true } )
128- logger ( `metrics/setup > loaded community template ${ name } ` )
129- }
130- catch ( error ) {
131- logger ( `metrics/setup > failed to load community template ${ template } ` )
132- logger ( error )
133135 }
134136 }
137+ else {
138+ logger ( "metrics/setup > no community templates to install" )
139+ }
135140 }
136141 else {
137- logger ( "metrics/setup > no community templates to install " )
142+ logger ( "metrics/setup > community templates are disabled " )
138143 }
139144
140145 //Load templates
@@ -188,6 +193,18 @@ export default async function({log = true, sandbox = false, community = {}} = {}
188193 //Load metadata
189194 conf . metadata = await metadata ( { log} )
190195
196+ //Modes
197+ if ( ( ! conf . settings . modes ) || ( ! conf . settings . modes . length ) )
198+ conf . settings . modes = [ "embed" , "insights" ]
199+ logger ( `metrics/setup > setup > enabled modes ${ JSON . stringify ( conf . settings . modes ) } ` )
200+
201+ //Allowed outputs formats
202+ if ( ( ! conf . settings . outputs ) || ( ! conf . settings . outputs . length ) )
203+ conf . settings . outputs = metadata . inputs . config_output . values
204+ else
205+ conf . settings . outputs = conf . settings . outputs . filter ( format => metadata . inputs . config_output . values . includes ( format ) )
206+ logger ( `metrics/setup > setup > allowed outputs ${ JSON . stringify ( conf . settings . outputs ) } ` )
207+
191208 //Store authenticated user
192209 if ( conf . settings . token ) {
193210 try {
0 commit comments