netlify-cli 9.2.0 → 9.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ const process = require('process')
5
5
 
6
6
  const { GraphQL, GraphQLHelpers, InternalConsole, NetlifyGraph } = require('netlify-onegraph-internal')
7
7
 
8
- const { detectServerSettings, error, execa, getFunctionsDir, log, warn } = require('../../utils')
8
+ const { chalk, detectServerSettings, error, execa, getFunctionsDir, log, warn } = require('../../utils')
9
9
 
10
10
  const { printSchema } = GraphQL
11
11
 
@@ -167,6 +167,8 @@ const getNetlifyGraphConfig = async ({ command, options, settings }) => {
167
167
  }
168
168
  }
169
169
 
170
+ const defaulFunctionsPath = ['netlify', 'functions']
171
+
170
172
  const siteRoot = [path.sep, ...filterRelativePathItems(site.root.split(path.sep))]
171
173
 
172
174
  const tsConfig = 'tsconfig.json'
@@ -178,7 +180,7 @@ const getNetlifyGraphConfig = async ({ command, options, settings }) => {
178
180
  const detectedFunctionsPathString = getFunctionsDir({ config, options })
179
181
  const detectedFunctionsPath = detectedFunctionsPathString
180
182
  ? [path.sep, ...detectedFunctionsPathString.split(path.sep)]
181
- : null
183
+ : defaulFunctionsPath
182
184
  const baseConfig = { ...NetlifyGraph.defaultNetlifyGraphConfig, ...userSpecifiedConfig }
183
185
  const defaultFrameworkConfig = makeDefaultFrameworkConfig({ baseConfig, detectedFunctionsPath, siteRoot })
184
186
 
@@ -278,10 +280,11 @@ const runPrettier = async (filePath) => {
278
280
  })
279
281
 
280
282
  await commandProcess
283
+ // eslint-disable-next-line unicorn/prefer-optional-catch-binding
281
284
  } catch (prettierError) {
285
+ // It would be nice to log this error to help debugging, but it's potentially a bit scary for the dev to see it
282
286
  if (!disablePrettierDueToPreviousError) {
283
287
  disablePrettierDueToPreviousError = true
284
- warn(prettierError)
285
288
  warn("Error while running prettier, make sure you have installed it globally with 'npm i -g prettier'")
286
289
  }
287
290
  }
@@ -295,9 +298,10 @@ const runPrettier = async (filePath) => {
295
298
  * @param {string} context.operationsDoc The GraphQL operations doc to use when generating the functions
296
299
  * @param {Record<string, NetlifyGraph.ExtractedFunction>} context.functions The parsed queries with metadata to use when generating library functions
297
300
  * @param {Record<string, NetlifyGraph.ExtractedFragment>} context.fragments The parsed queries with metadata to use when generating library functions
301
+ * @param {(message: string) => void=} context.logger A function that if provided will be used to log messages
298
302
  * @returns {void} Void, effectfully writes the generated library to the filesystem
299
303
  */
300
- const generateFunctionsFile = ({ fragments, functions, netlifyGraphConfig, operationsDoc, schema }) => {
304
+ const generateFunctionsFile = ({ fragments, functions, logger, netlifyGraphConfig, operationsDoc, schema }) => {
301
305
  const { clientSource, typeDefinitionsSource } = NetlifyGraph.generateFunctionsSource(
302
306
  netlifyGraphConfig,
303
307
  schema,
@@ -307,12 +311,15 @@ const generateFunctionsFile = ({ fragments, functions, netlifyGraphConfig, opera
307
311
  )
308
312
 
309
313
  ensureNetlifyGraphPath(netlifyGraphConfig)
310
- fs.writeFileSync(path.resolve(...netlifyGraphConfig.netlifyGraphImplementationFilename), clientSource, 'utf8')
311
- fs.writeFileSync(
312
- path.resolve(...netlifyGraphConfig.netlifyGraphTypeDefinitionsFilename),
313
- typeDefinitionsSource,
314
- 'utf8',
315
- )
314
+ const implementationResolvedPath = path.resolve(...netlifyGraphConfig.netlifyGraphImplementationFilename)
315
+ fs.writeFileSync(implementationResolvedPath, clientSource, 'utf8')
316
+ const implementationRelativePath = path.relative(process.cwd(), implementationResolvedPath)
317
+ logger && logger(`Wrote ${chalk.cyan(implementationRelativePath)}`)
318
+
319
+ const typeDefinitionsResolvedPath = path.resolve(...netlifyGraphConfig.netlifyGraphTypeDefinitionsFilename)
320
+ fs.writeFileSync(typeDefinitionsResolvedPath, typeDefinitionsSource, 'utf8')
321
+ const typeDefinitionsRelativePath = path.relative(process.cwd(), typeDefinitionsResolvedPath)
322
+ logger && logger(`Wrote ${chalk.cyan(typeDefinitionsRelativePath)}`)
316
323
  runPrettier(path.resolve(...netlifyGraphConfig.netlifyGraphImplementationFilename))
317
324
  runPrettier(path.resolve(...netlifyGraphConfig.netlifyGraphTypeDefinitionsFilename))
318
325
  }
@@ -338,26 +345,36 @@ const readGraphQLOperationsSourceFile = (netlifyGraphConfig) => {
338
345
 
339
346
  /**
340
347
  * Write an operations doc to the filesystem using the given NetlifyGraphConfig
341
- * @param {NetlifyGraph.NetlifyGraphConfig} netlifyGraphConfig
342
- * @param {string} operationsDocString The GraphQL operations doc to write
348
+ * @param {object} input
349
+ * @param {(message: string) => void=} input.logger A function that if provided will be used to log messages
350
+ * @param {NetlifyGraph.NetlifyGraphConfig} input.netlifyGraphConfig
351
+ * @param {string} input.operationsDocString The GraphQL operations doc to write
343
352
  */
344
- const writeGraphQLOperationsSourceFile = (netlifyGraphConfig, operationsDocString) => {
353
+ const writeGraphQLOperationsSourceFile = ({ logger, netlifyGraphConfig, operationsDocString }) => {
345
354
  const graphqlSource = operationsDocString
346
355
 
347
356
  ensureNetlifyGraphPath(netlifyGraphConfig)
348
- fs.writeFileSync(path.resolve(...netlifyGraphConfig.graphQLOperationsSourceFilename), graphqlSource, 'utf8')
357
+ const resolvedPath = path.resolve(...netlifyGraphConfig.graphQLOperationsSourceFilename)
358
+ fs.writeFileSync(resolvedPath, graphqlSource, 'utf8')
359
+ const relativePath = path.relative(process.cwd(), resolvedPath)
360
+ logger && logger(`Wrote ${chalk.cyan(relativePath)}`)
349
361
  }
350
362
 
351
363
  /**
352
364
  * Write a GraphQL Schema printed in SDL format to the filesystem using the given NetlifyGraphConfig
353
- * @param {NetlifyGraph.NetlifyGraphConfig} netlifyGraphConfig
354
- * @param {GraphQL.GraphQLSchema} schema The GraphQL schema to print and write to the filesystem
365
+ * @param {object} input
366
+ * @param {(message: string) => void=} input.logger A function that if provided will be used to log messages
367
+ * @param {NetlifyGraph.NetlifyGraphConfig} input.netlifyGraphConfig
368
+ * @param {GraphQL.GraphQLSchema} input.schema The GraphQL schema to print and write to the filesystem
355
369
  */
356
- const writeGraphQLSchemaFile = (netlifyGraphConfig, schema) => {
370
+ const writeGraphQLSchemaFile = ({ logger, netlifyGraphConfig, schema }) => {
357
371
  const graphqlSource = printSchema(schema)
358
372
 
359
373
  ensureNetlifyGraphPath(netlifyGraphConfig)
360
- fs.writeFileSync(path.resolve(...netlifyGraphConfig.graphQLSchemaFilename), graphqlSource, 'utf8')
374
+ const resolvedPath = path.resolve(...netlifyGraphConfig.graphQLSchemaFilename)
375
+ fs.writeFileSync(resolvedPath, graphqlSource, 'utf8')
376
+ const relativePath = path.relative(process.cwd(), resolvedPath)
377
+ logger && logger(`Wrote ${chalk.cyan(relativePath)}`)
361
378
  }
362
379
 
363
380
  /**
@@ -372,19 +389,21 @@ const readGraphQLSchemaFile = (netlifyGraphConfig) => {
372
389
 
373
390
  /**
374
391
  * Given a NetlifyGraphConfig, read the appropriate files and write a handler for the given operationId to the filesystem
375
- * @param {NetlifyGraph.NetlifyGraphConfig} netlifyGraphConfig
376
- * @param {GraphQL.GraphQLSchema} schema The GraphQL schema to use when generating the handler
377
- * @param {string} operationId The operationId to use when generating the handler
378
- * @param {object} handlerOptions The options to use when generating the handler
392
+ * @param {object} input
393
+ * @param {NetlifyGraph.NetlifyGraphConfig} input.netlifyGraphConfig
394
+ * @param {GraphQL.GraphQLSchema} input.schema The GraphQL schema to use when generating the handler
395
+ * @param {string} input.operationId The operationId to use when generating the handler
396
+ * @param {object} input.handlerOptions The options to use when generating the handler
397
+ * @param {(message: string) => void=} input.logger A function that if provided will be used to log messages
379
398
  * @returns
380
399
  */
381
- const generateHandlerByOperationId = (netlifyGraphConfig, schema, operationId, handlerOptions) => {
400
+ const generateHandlerByOperationId = ({ handlerOptions, logger, netlifyGraphConfig, operationId, schema }) => {
382
401
  let currentOperationsDoc = readGraphQLOperationsSourceFile(netlifyGraphConfig)
383
402
  if (currentOperationsDoc.trim().length === 0) {
384
403
  currentOperationsDoc = NetlifyGraph.defaultExampleOperationsDoc
385
404
  }
386
405
 
387
- const payload = {
406
+ const generateHandlerPayload = {
388
407
  handlerOptions,
389
408
  schema,
390
409
  netlifyGraphConfig,
@@ -392,7 +411,7 @@ const generateHandlerByOperationId = (netlifyGraphConfig, schema, operationId, h
392
411
  operationsDoc: currentOperationsDoc,
393
412
  }
394
413
 
395
- const result = NetlifyGraph.generateHandlerSource(payload)
414
+ const result = NetlifyGraph.generateHandlerSource(generateHandlerPayload)
396
415
 
397
416
  if (!result) {
398
417
  warn(`No handler was generated for operationId ${operationId}`)
@@ -432,19 +451,23 @@ const generateHandlerByOperationId = (netlifyGraphConfig, schema, operationId, h
432
451
  const absoluteFilename = path.resolve(...filenameArr)
433
452
 
434
453
  fs.writeFileSync(absoluteFilename, content)
454
+ const relativePath = path.relative(process.cwd(), absoluteFilename)
455
+ logger && logger(`Wrote ${chalk.cyan(relativePath)}`)
435
456
  runPrettier(absoluteFilename)
436
457
  })
437
458
  }
438
459
 
439
460
  /**
440
461
  * Given a NetlifyGraphConfig, read the appropriate files and write a handler for the given operationId to the filesystem
441
- * @param {NetlifyGraph.NetlifyGraphConfig} netlifyGraphConfig
442
- * @param {GraphQL.GraphQLSchema} schema The GraphQL schema to use when generating the handler
443
- * @param {string} operationName The name of the operation to use when generating the handler
444
- * @param {object} handlerOptions The options to use when generating the handler
462
+ * @param {object} input
463
+ * @param {NetlifyGraph.NetlifyGraphConfig} input.netlifyGraphConfig
464
+ * @param {GraphQL.GraphQLSchema} input.schema The GraphQL schema to use when generating the handler
465
+ * @param {string} input.operationName The name of the operation to use when generating the handler
466
+ * @param {object} input.handlerOptions The options to use when generating the handler
467
+ * @param {(message: string) => void=} input.logger A function that if provided will be used to log messages
445
468
  * @returns
446
469
  */
447
- const generateHandlerByOperationName = (netlifyGraphConfig, schema, operationName, handlerOptions) => {
470
+ const generateHandlerByOperationName = ({ handlerOptions, logger, netlifyGraphConfig, operationName, schema }) => {
448
471
  let currentOperationsDoc = readGraphQLOperationsSourceFile(netlifyGraphConfig)
449
472
  if (currentOperationsDoc.trim().length === 0) {
450
473
  currentOperationsDoc = NetlifyGraph.defaultExampleOperationsDoc
@@ -462,7 +485,7 @@ const generateHandlerByOperationName = (netlifyGraphConfig, schema, operationNam
462
485
  return
463
486
  }
464
487
 
465
- generateHandlerByOperationId(netlifyGraphConfig, schema, operation.id, handlerOptions)
488
+ generateHandlerByOperationId({ logger, netlifyGraphConfig, schema, operationId: operation.id, handlerOptions })
466
489
  }
467
490
 
468
491
  // Export the minimal set of functions that are required for Netlify Graph