@@ -335,39 +335,7 @@ const runInstruction = async function({
335335 console . log ( chalk . redBright . bold ( `> ${ index + 1 } . Running "${ hook } " ${ lifecycleName } from "${ name } " ${ source } ` ) )
336336 console . log ( )
337337
338- let apiClient
339- if ( netlifyToken ) {
340- apiClient = new API ( netlifyToken )
341- /* Redact API methods to scopes. Default scopes '*'... revisit */
342- if ( scopes ) {
343- const apiMethods = Object . getPrototypeOf ( apiClient )
344- const apiMethodArray = Object . keys ( apiMethods )
345- /* validate scopes */
346- scopes . forEach ( ( scopeName , i ) => {
347- if ( scopeName !== '*' && ! apiMethodArray . includes ( scopeName ) ) {
348- console . log ( chalk . redBright ( `Invalid scope "${ scopeName } " in "${ name } " plugin.` ) )
349- console . log ( chalk . white . bold ( `Please use a valid event name. One of:` ) )
350- console . log (
351- `${ [ '*' ]
352- . concat ( apiMethodArray )
353- . map ( n => `"${ n } "` )
354- . join ( ', ' ) } `
355- )
356- console . log ( )
357- throw new Error ( `Invalid scope "${ scopeName } " in "${ name } " plugin.` )
358- }
359- } )
360- /* If scopes not *, redact the methods not allowed */
361- if ( ! scopes . includes ( '*' ) ) {
362- apiMethodArray . forEach ( meth => {
363- if ( ! scopes . includes ( meth ) ) {
364- // TODO figure out if Object.setPrototypeOf will work
365- apiClient . __proto__ [ meth ] = disableApiMethod ( name , meth ) // eslint-disable-line
366- }
367- } )
368- }
369- }
370- }
338+ const apiClient = getApiClient ( { netlifyToken, name, scopes } )
371339
372340 // set log context
373341 netlifyLogs . setContext ( name )
@@ -425,6 +393,48 @@ const runInstruction = async function({
425393 }
426394}
427395
396+ const getApiClient = function ( { netlifyToken, name, scopes } ) {
397+ if ( ! netlifyToken ) {
398+ return
399+ }
400+
401+ const apiClient = new API ( netlifyToken )
402+
403+ /* Redact API methods to scopes. Default scopes '*'... revisit */
404+ if ( scopes ) {
405+ const apiMethods = Object . getPrototypeOf ( apiClient )
406+ const apiMethodArray = Object . keys ( apiMethods )
407+
408+ /* validate scopes */
409+ scopes . forEach ( ( scopeName , i ) => {
410+ if ( scopeName !== '*' && ! apiMethodArray . includes ( scopeName ) ) {
411+ console . log ( chalk . redBright ( `Invalid scope "${ scopeName } " in "${ name } " plugin.` ) )
412+ console . log ( chalk . white . bold ( `Please use a valid event name. One of:` ) )
413+ console . log (
414+ `${ [ '*' ]
415+ . concat ( apiMethodArray )
416+ . map ( n => `"${ n } "` )
417+ . join ( ', ' ) } `
418+ )
419+ console . log ( )
420+ throw new Error ( `Invalid scope "${ scopeName } " in "${ name } " plugin.` )
421+ }
422+ } )
423+
424+ /* If scopes not *, redact the methods not allowed */
425+ if ( ! scopes . includes ( '*' ) ) {
426+ apiMethodArray . forEach ( meth => {
427+ if ( ! scopes . includes ( meth ) ) {
428+ // TODO figure out if Object.setPrototypeOf will work
429+ apiClient . __proto__ [ meth ] = disableApiMethod ( name , meth ) // eslint-disable-line
430+ }
431+ } )
432+ }
433+ }
434+
435+ return apiClient
436+ }
437+
428438function disableApiMethod ( pluginName , method ) {
429439 return ( ) => {
430440 throw new Error (
0 commit comments