Skip to content

Commit

Permalink
Improvements (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs authored Nov 29, 2023
2 parents 78c82a9 + 7ad4954 commit ae08ffc
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 21 deletions.
20 changes: 20 additions & 0 deletions packages/gcp-functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>
```

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).
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion packages/gcp-functions/src/generators/init/init.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand Down
16 changes: 8 additions & 8 deletions packages/gcp-secrets/src/executors/deploy/deploy.impl.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function deployExecutor(
getCommandOptions(options)
]),
{
silent: true,
silent: !USE_VERBOSE_LOGGING,
asJSON: true
}
).map((secret) => ({
Expand All @@ -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
Expand Down Expand Up @@ -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 }
Expand All @@ -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 = (
Expand Down
17 changes: 7 additions & 10 deletions packages/gcp-secrets/src/utils/add-or-update-secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/strapi/src/executors/serve/serve.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function serveExecutor(

return execPackageManagerCommand(buildCommand([
'strapi develop',
'--ignore-prompts',
!build && '--no-build',
watchAdmin && '--watch-admin',
browser && `--browser=${browser}`
Expand Down

0 comments on commit ae08ffc

Please sign in to comment.