diff --git a/packages/gcp-functions/README.md b/packages/gcp-functions/README.md index 68c17b98..c2f7b5db 100644 --- a/packages/gcp-functions/README.md +++ b/packages/gcp-functions/README.md @@ -24,3 +24,23 @@ nx g @nx-extend/gcp-functions:init | name | type | default | description | |------|------|---------|-------------| +## Runner +This projects includes a runner to run all your functions, to generate the runner: +```sh +nx g @nx-extend/gcp-functions:init-runner +``` + +This will generate a `main.ts` file like this: +```ts +import { bootstrapRunner } from '@nx-extend/gcp-functions/runner' + +/* eslint-disable @nx/enforce-module-boundaries */ +bootstrapRunner(new Map([ + ['nx function project name', import('path to main of of project')] + ]) +) +``` + +You can now add all your functions to the map, **note:** make sure that the `nx function project name` is the same name as +known by Nx (`name` prop of that functions `project.json`). The runner uses the `deploy` target of that `project.json` to determine +how to serve the function (http/pub sub/bucket event). diff --git a/packages/gcp-functions/src/generators/init-runner/init.impl.ts b/packages/gcp-functions/src/generators/init-runner/init.impl.ts index 5ad7cf12..34dbbfff 100644 --- a/packages/gcp-functions/src/generators/init-runner/init.impl.ts +++ b/packages/gcp-functions/src/generators/init-runner/init.impl.ts @@ -72,11 +72,14 @@ export default async function ( options: { outputPath: `dist/${normalizedOptions.projectRoot}`, main: `${normalizedOptions.projectRoot}/src/main.ts`, - tsConfig: `${normalizedOptions.projectRoot}/tsconfig.app.json` + tsConfig: `${normalizedOptions.projectRoot}/tsconfig.app.json`, + compiler: 'tsc', + target: 'node', + namedChunks: true } }, serve: { - executor: '@nx/node:execute', + executor: '@nx/js:node', options: { buildTarget: `${normalizedOptions.projectName}:_build` } diff --git a/packages/gcp-functions/src/generators/init/files-http/src/main.ts.template b/packages/gcp-functions/src/generators/init/files-http/src/main.ts.template index 7dc0ae8b..ceefba2b 100644 --- a/packages/gcp-functions/src/generators/init/files-http/src/main.ts.template +++ b/packages/gcp-functions/src/generators/init/files-http/src/main.ts.template @@ -2,6 +2,8 @@ import type { HttpFunction } from '@google-cloud/functions-framework' const { foo = 'testBar'} = process.env +// Note: When changing "<%= className %>" to something else +// make sure to also update the "entryPoint" inside the "project.json" export const <%= className %>: HttpFunction = async (req, res) => { res.status(200) .send(foo) diff --git a/packages/gcp-functions/src/generators/init/init.impl.ts b/packages/gcp-functions/src/generators/init/init.impl.ts index 9291093a..85ec27f8 100644 --- a/packages/gcp-functions/src/generators/init/init.impl.ts +++ b/packages/gcp-functions/src/generators/init/init.impl.ts @@ -101,7 +101,8 @@ export default async function ( executor: '@nx-extend/gcp-functions:deploy', options: { functionName: normalizedOptions.projectName, - envVarsFile: `${normalizedOptions.projectRoot}/src/environments/production.yaml` + envVarsFile: `${normalizedOptions.projectRoot}/src/environments/production.yaml`, + entryPoint: names(normalizedOptions.name).className } } }, diff --git a/packages/gcp-secrets/src/executors/deploy/deploy.impl.ts b/packages/gcp-secrets/src/executors/deploy/deploy.impl.ts index b5699b5e..65feb23c 100644 --- a/packages/gcp-secrets/src/executors/deploy/deploy.impl.ts +++ b/packages/gcp-secrets/src/executors/deploy/deploy.impl.ts @@ -1,5 +1,5 @@ import { ExecutorContext, logger } from '@nx/devkit' -import { buildCommand, execCommand } from '@nx-extend/core' +import { buildCommand, execCommand, USE_VERBOSE_LOGGING } from '@nx-extend/core' import { existsSync, mkdirSync, unlinkSync, writeFileSync } from 'fs' import 'dotenv/config' @@ -48,7 +48,7 @@ export async function deployExecutor( getCommandOptions(options) ]), { - silent: true, + silent: !USE_VERBOSE_LOGGING, asJSON: true } ).map((secret) => ({ @@ -73,8 +73,7 @@ export async function deployExecutor( // Get the content of the file const fileContent = getFileContent(file) - const isFileEncrypted = - fileContent.__gcp_metadata.status === 'encrypted' + const isFileEncrypted = fileContent.__gcp_metadata.status === 'encrypted' const decryptedFileContent = decryptFile(fileContent, true) // Decrypt the file if it's encrypted @@ -142,9 +141,7 @@ export async function deployExecutor( success: secretsCreated.filter(Boolean).length === files.length } } catch (err) { - logger.error( - `Error happened trying to decrypt files: ${err.message || err}` - ) + logger.error(`Error happened trying to deploy files: ${err.message || err}`) console.error(err.trace) return { success: false } @@ -155,7 +152,10 @@ export async function deployExecutor( } export const getCommandOptions = (options: DeploySchema): string => { - return buildCommand([options.project && `--project=${options.project}`]) + return buildCommand([ + options.project && `--project=${options.project}`, + '--quiet' + ]) } export const addLabelsIfNeeded = ( diff --git a/packages/gcp-secrets/src/utils/add-or-update-secret.ts b/packages/gcp-secrets/src/utils/add-or-update-secret.ts index 629fa713..277138b7 100644 --- a/packages/gcp-secrets/src/utils/add-or-update-secret.ts +++ b/packages/gcp-secrets/src/utils/add-or-update-secret.ts @@ -42,16 +42,13 @@ export const addOrUpdateSecret = ( if (JSON.stringify(existingLabels) !== JSON.stringify(metadata.labels)) { logger.info(`Updating "${secretName}" it's labels`) - execCommand( - buildCommand([ - `gcloud secrets update ${secretName}`, - addLabelsIfNeeded(metadata.labels, false), - getCommandOptions(options) - ]), - { - silent: true - } - ) + execCommand(buildCommand([ + `gcloud secrets update ${secretName}`, + addLabelsIfNeeded(metadata.labels, false), + getCommandOptions(options) + ]), { + silent: true + }) } // Get the new version of the secret diff --git a/packages/strapi/src/executors/serve/serve.impl.ts b/packages/strapi/src/executors/serve/serve.impl.ts index 5eb47020..a8f9506b 100644 --- a/packages/strapi/src/executors/serve/serve.impl.ts +++ b/packages/strapi/src/executors/serve/serve.impl.ts @@ -29,6 +29,7 @@ export async function serveExecutor( return execPackageManagerCommand(buildCommand([ 'strapi develop', + '--ignore-prompts', !build && '--no-build', watchAdmin && '--watch-admin', browser && `--browser=${browser}`