11//Imports
2- import ejs from "ejs"
32import fs from "fs/promises"
3+ import ejs from "ejs"
44import fss from "fs"
5+ import yaml from "js-yaml"
56import paths from "path"
6- import url from "url"
77import sgit from "simple-git"
8+ import url from "url"
89import metadata from "../../source/app/metrics/metadata.mjs"
9- import yaml from "js-yaml"
1010
1111//Mode
1212const [ mode = "dryrun" ] = process . argv . slice ( 2 )
@@ -25,34 +25,36 @@ const __test_secrets = paths.join(paths.join(__metrics, "tests/secrets.json"))
2525//Git setup
2626const git = sgit ( __metrics )
2727const staged = new Set ( )
28- const secrets = Object . assign ( JSON . parse ( `${ await fs . readFile ( __test_secrets ) } ` ) , { $regex :/ \$ \{ \{ \s * s e c r e t s \. (?< secret > \w + ) \s * \} \} / } )
29- const { plugins, templates} = await metadata ( { log :false , diff :true } )
28+ const secrets = Object . assign ( JSON . parse ( `${ await fs . readFile ( __test_secrets ) } ` ) , { $regex : / \$ \{ \{ \s * s e c r e t s \. (?< secret > \w + ) \s * \} \} / } )
29+ const { plugins, templates } = await metadata ( { log : false , diff : true } )
3030const workflow = [ ]
3131
3232//Config and general documentation auto-generation
3333for ( const step of [ "config" , "documentation" ] ) {
3434 switch ( step ) {
3535 case "config" :
36- await update ( { source :paths . join ( __action , "action.yml" ) , output :"action.yml" } )
37- await update ( { source :paths . join ( __web , "settings.example.json" ) , output :"settings.example.json" } )
36+ await update ( { source : paths . join ( __action , "action.yml" ) , output : "action.yml" } )
37+ await update ( { source : paths . join ( __web , "settings.example.json" ) , output : "settings.example.json" } )
3838 break
3939 case "documentation" :
40- await update ( { source :paths . join ( __readme , "README.md" ) , output :"README.md" , options :{ root :__readme } } )
41- await update ( { source :paths . join ( __readme , "partials/documentation/plugins.md" ) , output :"source/plugins/README.md" } )
42- await update ( { source :paths . join ( __readme , "partials/documentation/templates.md" ) , output :"source/templates/README.md" } )
40+ await update ( { source : paths . join ( __readme , "README.md" ) , output : "README.md" , options : { root : __readme } } )
41+ await update ( { source : paths . join ( __readme , "partials/documentation/plugins.md" ) , output : "source/plugins/README.md" } )
42+ await update ( { source : paths . join ( __readme , "partials/documentation/templates.md" ) , output : "source/templates/README.md" } )
4343 break
4444 }
4545}
4646
4747//Plugins
4848for ( const id of Object . keys ( plugins ) ) {
49- const { examples, options, readme, tests, header} = await plugin ( id )
49+ const { examples, options, readme, tests, header } = await plugin ( id )
5050
5151 //Readme
52- await fs . writeFile ( readme . path , readme . content
53- . replace ( / ( < ! - - h e a d e r - - > ) [ \s \S ] * ( < ! - - \/ h e a d e r - - > ) / g, `$1\n${ header } \n$2` )
54- . replace ( / ( < ! - - e x a m p l e s - - > ) [ \s \S ] * ( < ! - - \/ e x a m p l e s - - > ) / g, `$1\n${ examples . map ( ( { test, prod, ...step } ) => [ "```yaml" , yaml . dump ( step ) , "```" ] . join ( "\n" ) ) . join ( "\n" ) } \n$2` )
55- . replace ( / ( < ! - - o p t i o n s - - > ) [ \s \S ] * ( < ! - - \/ o p t i o n s - - > ) / g, `$1\n${ options } \n$2` )
52+ await fs . writeFile (
53+ readme . path ,
54+ readme . content
55+ . replace ( / ( < ! - - h e a d e r - - > ) [ \s \S ] * ( < ! - - \/ h e a d e r - - > ) / g, `$1\n${ header } \n$2` )
56+ . replace ( / ( < ! - - e x a m p l e s - - > ) [ \s \S ] * ( < ! - - \/ e x a m p l e s - - > ) / g, `$1\n${ examples . map ( ( { test, prod, ...step } ) => [ "```yaml" , yaml . dump ( step ) , "```" ] . join ( "\n" ) ) . join ( "\n" ) } \n$2` )
57+ . replace ( / ( < ! - - o p t i o n s - - > ) [ \s \S ] * ( < ! - - \/ o p t i o n s - - > ) / g, `$1\n${ options } \n$2` ) ,
5658 )
5759 console . log ( `Generating source/plugins/${ id } /README.md` )
5860
@@ -64,12 +66,14 @@ for (const id of Object.keys(plugins)) {
6466
6567//Templates
6668for ( const id of Object . keys ( templates ) ) {
67- const { examples, readme, tests, header} = await template ( id )
69+ const { examples, readme, tests, header } = await template ( id )
6870
6971 //Readme
70- await fs . writeFile ( readme . path , readme . content
71- . replace ( / ( < ! - - h e a d e r - - > ) [ \s \S ] * ( < ! - - \/ h e a d e r - - > ) / g, `$1\n${ header } \n$2` )
72- . replace ( / ( < ! - - e x a m p l e s - - > ) [ \s \S ] * ( < ! - - \/ e x a m p l e s - - > ) / g, `$1\n${ examples . map ( ( { test, prod, ...step } ) => [ "```yaml" , yaml . dump ( step ) , "```" ] . join ( "\n" ) ) . join ( "\n" ) } \n$2` )
72+ await fs . writeFile (
73+ readme . path ,
74+ readme . content
75+ . replace ( / ( < ! - - h e a d e r - - > ) [ \s \S ] * ( < ! - - \/ h e a d e r - - > ) / g, `$1\n${ header } \n$2` )
76+ . replace ( / ( < ! - - e x a m p l e s - - > ) [ \s \S ] * ( < ! - - \/ e x a m p l e s - - > ) / g, `$1\n${ examples . map ( ( { test, prod, ...step } ) => [ "```yaml" , yaml . dump ( step ) , "```" ] . join ( "\n" ) ) . join ( "\n" ) } \n$2` ) ,
7377 )
7478 console . log ( `Generating source/templates/${ id } /README.md` )
7579
@@ -80,7 +84,7 @@ for (const id of Object.keys(templates)) {
8084}
8185
8286//Example workflows
83- await update ( { source :paths . join ( __metrics , ".github/scripts/files/examples.yml" ) , output :".github/workflows/examples.yml" , context :{ steps :yaml . dump ( workflow ) } } )
87+ await update ( { source : paths . join ( __metrics , ".github/scripts/files/examples.yml" ) , output : ".github/workflows/examples.yml" , context : { steps : yaml . dump ( workflow ) } } )
8488
8589//Commit and push
8690if ( mode === "publish" ) {
@@ -98,10 +102,10 @@ console.log("Success!")
98102//==================================================================================
99103
100104//Update generated files
101- async function update ( { source, output, context = { } , options = { } } ) {
105+ async function update ( { source, output, context = { } , options = { } } ) {
102106 console . log ( `Generating ${ output } ` )
103- const { plugins, templates, packaged, descriptor} = await metadata ( { log :false } )
104- const content = await ejs . renderFile ( source , { plugins, templates, packaged, descriptor, ...context } , { async :true , ...options } )
107+ const { plugins, templates, packaged, descriptor } = await metadata ( { log : false } )
108+ const content = await ejs . renderFile ( source , { plugins, templates, packaged, descriptor, ...context } , { async : true , ...options } )
105109 const file = paths . join ( __metrics , output )
106110 await fs . writeFile ( file , content )
107111 staged . add ( file )
@@ -114,16 +118,16 @@ async function plugin(id) {
114118 const examples = paths . join ( path , "examples.yml" )
115119 const tests = paths . join ( __test_cases , `${ id } .plugin.yml` )
116120 return {
117- readme :{
118- path :readme ,
119- content :`${ await fs . readFile ( readme ) } `
121+ readme : {
122+ path : readme ,
123+ content : `${ await fs . readFile ( readme ) } ` ,
120124 } ,
121- tests :{
122- path :tests
125+ tests : {
126+ path : tests ,
123127 } ,
124- examples :fss . existsSync ( examples ) ? yaml . load ( await fs . readFile ( examples ) , "utf8" ) ?? [ ] : [ ] ,
125- options :plugins [ id ] . readme . table ,
126- header :plugins [ id ] . readme . header
128+ examples : fss . existsSync ( examples ) ? yaml . load ( await fs . readFile ( examples ) , "utf8" ) ?? [ ] : [ ] ,
129+ options : plugins [ id ] . readme . table ,
130+ header : plugins [ id ] . readme . header ,
127131 }
128132}
129133
@@ -134,45 +138,45 @@ async function template(id) {
134138 const examples = paths . join ( path , "examples.yml" )
135139 const tests = paths . join ( __test_cases , `${ id } .template.yml` )
136140 return {
137- readme :{
138- path :readme ,
139- content :`${ await fs . readFile ( readme ) } `
141+ readme : {
142+ path : readme ,
143+ content : `${ await fs . readFile ( readme ) } ` ,
140144 } ,
141- tests :{
142- path :tests
145+ tests : {
146+ path : tests ,
143147 } ,
144- examples :fss . existsSync ( examples ) ? yaml . load ( await fs . readFile ( examples ) , "utf8" ) ?? [ ] : [ ] ,
145- header :templates [ id ] . readme . header
148+ examples : fss . existsSync ( examples ) ? yaml . load ( await fs . readFile ( examples ) , "utf8" ) ?? [ ] : [ ] ,
149+ header : templates [ id ] . readme . header ,
146150 }
147151}
148152
149153//Testcase generator
150154function testcase ( name , env , args ) {
151- const { prod = { } , test = { } , ...step } = JSON . parse ( JSON . stringify ( args ) )
152- const context = { prod, test} [ env ] ?? { }
155+ const { prod = { } , test = { } , ...step } = JSON . parse ( JSON . stringify ( args ) )
156+ const context = { prod, test } [ env ] ?? { }
153157 if ( context . skip )
154158 return null
155159
156160 Object . assign ( step . with , context . with ?? { } )
157161 delete context . with
158- const result = { ...step , ...context , name :`${ name } - ${ step . name ?? "(unnamed)" } ` }
162+ const result = { ...step , ...context , name : `${ name } - ${ step . name ?? "(unnamed)" } ` }
159163 for ( const [ k , v ] of Object . entries ( result . with ) ) {
160- if ( ( env === "test" ) && ( secrets . $regex . test ( v ) ) )
164+ if ( ( env === "test" ) && ( secrets . $regex . test ( v ) ) )
161165 result . with [ k ] = v . replace ( secrets . $regex , secrets [ v . match ( secrets . $regex ) ?. groups ?. secret ] )
162166 }
163167
164168 if ( env === "prod" ) {
165169 result . if = "${{ success() || failure() }}"
166170 result . uses = "lowlighter/metrics@master"
167- Object . assign ( result . with , { plugins_errors_fatal :"yes" , output_action :"none" , delay :120 } )
171+ Object . assign ( result . with , { plugins_errors_fatal : "yes" , output_action : "none" , delay : 120 } )
168172 }
169173
170174 if ( env === "test" ) {
171175 if ( ! result . with . base )
172176 delete result . with . base
173177 delete result . with . filename
174- Object . assign ( result . with , { use_mocked_data :"yes" , verify :"yes" } )
178+ Object . assign ( result . with , { use_mocked_data : "yes" , verify : "yes" } )
175179 }
176180
177181 return result
178- }
182+ }
0 commit comments