@@ -20,7 +20,7 @@ const { getLogMessage } = require('../lib/log')
2020
2121const { detectFunctionsBuilder } = require ( './detect-functions-builder' )
2222const { getFunctions } = require ( './get-functions' )
23- const { NETLIFYDEVLOG , NETLIFYDEVWARN , NETLIFYDEVERR } = require ( './logo' )
23+ const { NETLIFYDEVLOG , NETLIFYDEVERR } = require ( './logo' )
2424
2525const formatLambdaLocalError = ( err ) => `${ err . errorType } : ${ err . errorMessage } \n ${ err . stackTrace . join ( '\n ' ) } `
2626
@@ -153,12 +153,18 @@ const buildClientContext = function (headers) {
153153 }
154154}
155155
156- const clearCache = ( action ) => ( path ) => {
157- console . log ( `${ NETLIFYDEVLOG } ${ path } ${ action } , reloading...` )
156+ const clearCache = ( { action, omitLog } ) => ( path ) => {
157+ if ( ! omitLog ) {
158+ console . log ( `${ NETLIFYDEVLOG } ${ path } ${ action } , reloading...` )
159+ }
160+
158161 Object . keys ( require . cache ) . forEach ( ( key ) => {
159162 delete require . cache [ key ]
160163 } )
161- console . log ( `${ NETLIFYDEVLOG } ${ path } ${ action } , successfully reloaded!` )
164+
165+ if ( ! omitLog ) {
166+ console . log ( `${ NETLIFYDEVLOG } ${ path } ${ action } , successfully reloaded!` )
167+ }
162168}
163169
164170const shouldBase64Encode = function ( contentType ) {
@@ -173,11 +179,13 @@ const validateFunctions = function ({ functions, capabilities, warn }) {
173179 }
174180}
175181
176- const createHandler = async function ( { dir, capabilities, warn } ) {
182+ const createHandler = async function ( { dir, capabilities, omitFileChangesLog , warn } ) {
177183 const functions = await getFunctions ( dir )
178184 validateFunctions ( { functions, capabilities, warn } )
179185 const watcher = chokidar . watch ( dir , { ignored : / n o d e _ m o d u l e s / } )
180- watcher . on ( 'change' , clearCache ( 'modified' ) ) . on ( 'unlink' , clearCache ( 'deleted' ) )
186+ watcher
187+ . on ( 'change' , clearCache ( { action : 'modified' , omitLog : omitFileChangesLog } ) )
188+ . on ( 'unlink' , clearCache ( { action : 'deleted' , omitLog : omitFileChangesLog } ) )
181189
182190 const logger = winston . createLogger ( {
183191 levels : winston . config . npm . levels ,
@@ -363,7 +371,7 @@ const createFormSubmissionHandler = function ({ siteUrl, warn }) {
363371 }
364372}
365373
366- const getFunctionsServer = async function ( { dir, siteUrl, capabilities, warn } ) {
374+ const getFunctionsServer = async function ( { dir, omitFileChangesLog , siteUrl, capabilities, warn } ) {
367375 const app = express ( )
368376 app . set ( 'query parser' , 'simple' )
369377
@@ -385,21 +393,21 @@ const getFunctionsServer = async function ({ dir, siteUrl, capabilities, warn })
385393 res . status ( 204 ) . end ( )
386394 } )
387395
388- app . all ( '*' , await createHandler ( { dir, capabilities, warn } ) )
396+ app . all ( '*' , await createHandler ( { dir, capabilities, omitFileChangesLog , warn } ) )
389397
390398 return app
391399}
392400
393401const getBuildFunction = ( { functionBuilder, log } ) =>
394- async function build ( ) {
402+ async function build ( updatedPath ) {
395403 log (
396404 `${ NETLIFYDEVLOG } Function builder ${ chalk . yellow ( functionBuilder . builderName ) } ${ chalk . magenta (
397405 'building' ,
398406 ) } functions from directory ${ chalk . yellow ( functionBuilder . src ) } `,
399407 )
400408
401409 try {
402- await functionBuilder . build ( )
410+ await functionBuilder . build ( updatedPath )
403411 log (
404412 `${ NETLIFYDEVLOG } Function builder ${ chalk . yellow ( functionBuilder . builderName ) } ${ chalk . green (
405413 'finished' ,
@@ -417,32 +425,40 @@ const getBuildFunction = ({ functionBuilder, log }) =>
417425 }
418426 }
419427
420- const setupFunctionsBuilder = async ( { site, log, warn } ) => {
421- const functionBuilder = await detectFunctionsBuilder ( site . root )
422- if ( functionBuilder ) {
423- log (
424- `${ NETLIFYDEVLOG } Function builder ${ chalk . yellow (
425- functionBuilder . builderName ,
426- ) } detected: Running npm script ${ chalk . yellow ( functionBuilder . npmScript ) } `,
427- )
428- warn (
429- `${ NETLIFYDEVWARN } This is a beta feature, please give us feedback on how to improve at https://github.com/netlify/cli/` ,
430- )
428+ const setupFunctionsBuilder = async ( { config, errorExit, functionsDirectory, log, site } ) => {
429+ const functionBuilder = await detectFunctionsBuilder ( {
430+ config,
431+ errorExit,
432+ functionsDirectory,
433+ log,
434+ projectRoot : site . root ,
435+ } )
431436
432- const debouncedBuild = debounce ( getBuildFunction ( { functionBuilder, log } ) , 300 , {
433- leading : true ,
434- trailing : true ,
435- } )
437+ if ( ! functionBuilder ) {
438+ return { }
439+ }
436440
437- await debouncedBuild ( )
441+ const npmScriptString = functionBuilder . npmScript
442+ ? `: Running npm script ${ chalk . yellow ( functionBuilder . npmScript ) } `
443+ : ''
438444
439- const functionWatcher = chokidar . watch ( functionBuilder . src )
440- functionWatcher . on ( 'ready' , ( ) => {
441- functionWatcher . on ( 'add' , debouncedBuild )
442- functionWatcher . on ( 'change' , debouncedBuild )
443- functionWatcher . on ( 'unlink' , debouncedBuild )
444- } )
445- }
445+ log ( `${ NETLIFYDEVLOG } Function builder ${ chalk . yellow ( functionBuilder . builderName ) } detected${ npmScriptString } .` )
446+
447+ const debouncedBuild = debounce ( getBuildFunction ( { functionBuilder, log } ) , 300 , {
448+ leading : true ,
449+ trailing : true ,
450+ } )
451+
452+ await debouncedBuild ( )
453+
454+ const functionWatcher = chokidar . watch ( functionBuilder . src )
455+ functionWatcher . on ( 'ready' , ( ) => {
456+ functionWatcher . on ( 'add' , debouncedBuild )
457+ functionWatcher . on ( 'change' , debouncedBuild )
458+ functionWatcher . on ( 'unlink' , debouncedBuild )
459+ } )
460+
461+ return functionBuilder
446462}
447463
448464const startServer = async ( { server, settings, log, errorExit } ) => {
@@ -458,12 +474,25 @@ const startServer = async ({ server, settings, log, errorExit }) => {
458474 } )
459475}
460476
461- const startFunctionsServer = async ( { settings, site, log, warn, errorExit, siteUrl, capabilities } ) => {
477+ const startFunctionsServer = async ( { config , settings, site, log, warn, errorExit, siteUrl, capabilities } ) => {
462478 // serve functions from zip-it-and-ship-it
463479 // env variables relies on `url`, careful moving this code
464480 if ( settings . functions ) {
465- await setupFunctionsBuilder ( { site, log, warn } )
466- const server = await getFunctionsServer ( { dir : settings . functions , siteUrl, capabilities, warn } )
481+ const { omitFileChangesLog, target : functionsDirectory } = await setupFunctionsBuilder ( {
482+ config,
483+ errorExit,
484+ functionsDirectory : settings . functions ,
485+ log,
486+ site,
487+ } )
488+ const server = await getFunctionsServer ( {
489+ dir : functionsDirectory || settings . functions ,
490+ omitFileChangesLog,
491+ siteUrl,
492+ capabilities,
493+ warn,
494+ } )
495+
467496 await startServer ( { server, settings, log, errorExit } )
468497 }
469498}
0 commit comments