@@ -6,8 +6,9 @@ import { readFile, writeFile } from 'fs/promises'
66import path from 'path'
77import { slug } from 'github-slugger'
88import yaml from 'js-yaml'
9+ import walk from 'walk-sync'
910
10- import { getContents } from '#src/workflows/git-utils.js'
11+ import { getContents , getDirectoryContents } from '#src/workflows/git-utils.js'
1112import permissionSchema from './permission-list-schema.js'
1213import enabledSchema from './enabled-list-schema.js'
1314import { validateJson } from '#src/tests/lib/validate-json-schema.js'
@@ -156,22 +157,26 @@ export async function getProgAccessData(progAccessSource, isRest = false) {
156157 let progAccessDataRaw
157158 let progActorResources
158159 const progAccessFilepath = 'config/access_control/programmatic_access.yaml'
159- const progActorFilepath = 'config/locales/programmatic_actor_fine_grained_resources.en.yml'
160+ const progActorDirectory =
161+ 'config/access_control/fine_grained_permissions/programmatic_actor_fine_grained_resources'
160162
161163 if ( ! useRemoteGitHubFiles ) {
162164 progAccessDataRaw = yaml . load (
163165 await readFile ( path . join ( progAccessSource , progAccessFilepath ) , 'utf8' ) ,
164166 )
165- progActorResources = yaml . load (
166- await readFile ( path . join ( progAccessSource , progActorFilepath ) , 'utf8' ) ,
167- ) . en . programmatic_actor_fine_grained_resources
167+ progActorResources = await getProgActorResourceContent ( {
168+ localDirectory : path . join ( progAccessSource , progActorDirectory ) ,
169+ } )
168170 } else {
169171 progAccessDataRaw = yaml . load (
170172 await getContents ( 'github' , 'github' , 'master' , progAccessFilepath ) ,
171173 )
172- progActorResources = yaml . load (
173- await getContents ( 'github' , 'github' , 'master' , progActorFilepath ) ,
174- ) . en . programmatic_actor_fine_grained_resources
174+ progActorResources = await getProgActorResourceContent ( {
175+ owner : 'github' ,
176+ repo : 'github' ,
177+ branch : 'master' ,
178+ path : progActorDirectory ,
179+ } )
175180 }
176181
177182 const progAccessData = { }
@@ -291,3 +296,49 @@ async function validateAppData(data, pageType) {
291296 }
292297 }
293298}
299+
300+ // When getting files from the GitHub repo locally (or in a Codespace)
301+ // you can pass the full or relative path to the `github` repository
302+ // directory on disk.
303+ // When the source directory is `rest-api-description` (which is more common)
304+ // you can pass the `owner`, `repo`, `branch`, and `path` (repository path)
305+ async function getProgActorResourceContent ( {
306+ owner,
307+ repo,
308+ branch,
309+ path,
310+ gitHubSourceDirectory = null ,
311+ } ) {
312+ // Get files either locally from disk or from the GitHub remote repo
313+ let files
314+ if ( gitHubSourceDirectory ) {
315+ files = await getProgActorContentFromDisk ( gitHubSourceDirectory )
316+ } else {
317+ files = await getDirectoryContents ( owner , repo , branch , path )
318+ }
319+
320+ // We need to format the file content into a single object. Each file
321+ // contains a single key and a single value that needs to be added
322+ // to the object.
323+ const progActorResources = { }
324+ for ( const file of files ) {
325+ const fileContent = yaml . load ( file )
326+ // Each file should only contain a single key and value.
327+ if ( Object . keys ( fileContent ) . length !== 1 ) {
328+ throw new Error ( `Error: The file ${ JSON . stringify ( fileContent ) } must only have one key.` )
329+ }
330+ Object . entries ( fileContent ) . forEach ( ( [ key , value ] ) => {
331+ progActorResources [ key ] = value
332+ } )
333+ }
334+ return progActorResources
335+ }
336+
337+ async function getProgActorContentFromDisk ( directory ) {
338+ const files = walk ( directory , {
339+ includeBasePath : true ,
340+ directories : false ,
341+ } )
342+ const promises = files . map ( async ( file ) => await readFile ( file , 'utf8' ) )
343+ return await Promise . all ( promises )
344+ }
0 commit comments