From a48325cee1e5b4c431b127709e14a772cc08ac37 Mon Sep 17 00:00:00 2001 From: Damian Bielecki Date: Wed, 16 Feb 2022 19:47:57 +0100 Subject: [PATCH] feat(serverless): improve generator --- packages/nx-serverless/README.md | 2 +- .../application/files/serverless.yml | 14 +++--- .../files/src/environments/config.dev.json | 3 -- .../files/src/environments/config.prod.json | 3 -- .../src/environments/environment.ts__tmpl__ | 4 +- .../generators/application/generator.spec.ts | 46 +++++++++++++++---- .../src/generators/application/generator.ts | 2 + .../application/lib/get-project-config.ts | 17 ++++++- .../src/generators/init/generator.ts | 30 +++++++++++- 9 files changed, 92 insertions(+), 29 deletions(-) delete mode 100644 packages/nx-serverless/src/generators/application/files/src/environments/config.dev.json delete mode 100644 packages/nx-serverless/src/generators/application/files/src/environments/config.prod.json diff --git a/packages/nx-serverless/README.md b/packages/nx-serverless/README.md index a02ff69a..a7dfeb78 100644 --- a/packages/nx-serverless/README.md +++ b/packages/nx-serverless/README.md @@ -36,7 +36,7 @@ nx generate @ns3/nx-serverless:app my-app-name --plugin @ns3/nx-serverless/plugi ## Available commands ``` -nx run my-app-name:build +nx run my-app-name:package nx run my-app-name:serve nx run my-app-name:deploy nx run my-app-name:remove diff --git a/packages/nx-serverless/src/generators/application/files/serverless.yml b/packages/nx-serverless/src/generators/application/files/serverless.yml index a94c6caa..ea2d677f 100644 --- a/packages/nx-serverless/src/generators/application/files/serverless.yml +++ b/packages/nx-serverless/src/generators/application/files/serverless.yml @@ -1,8 +1,5 @@ service: <%= name %> -configValidationMode: error # remove in serverless v3 -variablesResolutionMode: 20210326 # remove in serverless v3 - plugins: - '<%= plugin %>' - serverless-offline @@ -21,18 +18,21 @@ custom: linting: false tsConfig: 'tsconfig.app.json'<% } %> +params: + prod: + production: true + default: + production: false + provider: name: aws runtime: nodejs14.x stage: ${self:custom.stage} endpointType: regional timeout: 20 - lambdaHashingVersion: 20201221 # removes deprecation warning (remove in serverless v3) - apiGateway: - shouldStartNameWithService: true # removes deprecation warning (remove in serverless v3) environment: APP_ENV: ${self:custom.stage} - IS_PRODUCTION: '${file(./src/environments/config.${self:custom.stage}.json):is_production}' + IS_PRODUCTION: ${param:production} NODE_OPTIONS: --enable-source-maps functions: diff --git a/packages/nx-serverless/src/generators/application/files/src/environments/config.dev.json b/packages/nx-serverless/src/generators/application/files/src/environments/config.dev.json deleted file mode 100644 index bb6c1045..00000000 --- a/packages/nx-serverless/src/generators/application/files/src/environments/config.dev.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "is_production": false -} diff --git a/packages/nx-serverless/src/generators/application/files/src/environments/config.prod.json b/packages/nx-serverless/src/generators/application/files/src/environments/config.prod.json deleted file mode 100644 index 6952c98d..00000000 --- a/packages/nx-serverless/src/generators/application/files/src/environments/config.prod.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "is_production": true -} diff --git a/packages/nx-serverless/src/generators/application/files/src/environments/environment.ts__tmpl__ b/packages/nx-serverless/src/generators/application/files/src/environments/environment.ts__tmpl__ index 28461093..a311cd3e 100644 --- a/packages/nx-serverless/src/generators/application/files/src/environments/environment.ts__tmpl__ +++ b/packages/nx-serverless/src/generators/application/files/src/environments/environment.ts__tmpl__ @@ -1,5 +1,5 @@ -const IS_PRODUCTION = process.env.IS_PRODUCTION === 'true'; -const IS_OFFLINE = process.env.IS_OFFLINE === 'true'; +const IS_PRODUCTION = process.env['IS_PRODUCTION'] === 'true'; +const IS_OFFLINE = process.env['IS_OFFLINE'] === 'true'; export const environment = { production: IS_PRODUCTION, diff --git a/packages/nx-serverless/src/generators/application/generator.spec.ts b/packages/nx-serverless/src/generators/application/generator.spec.ts index be860039..a0ac418e 100644 --- a/packages/nx-serverless/src/generators/application/generator.spec.ts +++ b/packages/nx-serverless/src/generators/application/generator.spec.ts @@ -28,24 +28,37 @@ describe('serverless generator', () => { projectType: 'application', sourceRoot: 'apps/sample/src', targets: { - build: { + serve: { executor: '@ns3/nx-serverless:sls', - outputs: ['apps/sample/.serverless', 'dist/apps/sample'], options: { - command: 'package', + command: 'offline', }, }, - serve: { + package: { executor: '@ns3/nx-serverless:sls', + outputs: ['apps/sample/.serverless', 'dist/apps/sample'], + dependsOn: [ + { + target: 'build', + projects: 'dependencies', + }, + ], options: { - command: 'offline', + command: 'package', }, }, deploy: { executor: '@ns3/nx-serverless:sls', outputs: ['apps/sample/.serverless', 'dist/apps/sample'], + dependsOn: [ + { + target: 'package', + projects: 'self', + }, + ], options: { command: 'deploy', + package: '.serverless', }, }, remove: { @@ -97,7 +110,7 @@ describe('serverless generator', () => { projectType: 'application', sourceRoot: 'apps/sample/src', targets: { - 'build-base': { + build: { executor: '@nrwl/node:build', outputs: ['{options.outputPath}'], options: { @@ -119,23 +132,36 @@ describe('serverless generator', () => { executor: '@ns3/nx-serverless:sls', options: { command: 'offline', - buildTarget: 'sample:build-base', + buildTarget: 'sample:build', }, }, - build: { + package: { executor: '@ns3/nx-serverless:sls', outputs: ['apps/sample/.serverless', 'dist/apps/sample'], + dependsOn: [ + { + target: 'build', + projects: 'dependencies', + }, + ], options: { command: 'package', - buildTarget: 'sample:build-base:production', + buildTarget: 'sample:build:production', }, }, deploy: { executor: '@ns3/nx-serverless:sls', outputs: ['apps/sample/.serverless', 'dist/apps/sample'], + dependsOn: [ + { + target: 'package', + projects: 'self', + }, + ], options: { command: 'deploy', - buildTarget: 'sample:build-base:production', + package: '.serverless', + buildTarget: 'sample:build:production', }, }, remove: { diff --git a/packages/nx-serverless/src/generators/application/generator.ts b/packages/nx-serverless/src/generators/application/generator.ts index 4c64b0dc..54dbbdfc 100644 --- a/packages/nx-serverless/src/generators/application/generator.ts +++ b/packages/nx-serverless/src/generators/application/generator.ts @@ -62,6 +62,8 @@ function updateTsConfig(tree: Tree, options: ServerlessGeneratorNormalizedSchema ...json.compilerOptions, forceConsistentCasingInFileNames: true, strict: true, + noImplicitOverride: true, + noPropertyAccessFromIndexSignature: true, noImplicitReturns: true, noFallthroughCasesInSwitch: true, }; diff --git a/packages/nx-serverless/src/generators/application/lib/get-project-config.ts b/packages/nx-serverless/src/generators/application/lib/get-project-config.ts index 8cfca8ca..9ce92f99 100644 --- a/packages/nx-serverless/src/generators/application/lib/get-project-config.ts +++ b/packages/nx-serverless/src/generators/application/lib/get-project-config.ts @@ -8,7 +8,7 @@ export function getProjectConfig( options: ServerlessGeneratorNormalizedSchema, ): ProjectConfiguration { const outputPath = getOutputPath(options); - const buildTargetName = 'build-base'; + const buildTargetName = 'build'; const buildTargetDev = `${options.name}:${buildTargetName}`; const buildTargetProd = `${buildTargetDev}:production`; const buildBaseConfig = getBuildBaseConfig(options); @@ -30,9 +30,15 @@ export function getProjectConfig( : {}), }, }, - build: { + package: { executor: '@ns3/nx-serverless:sls', outputs: [outputPath, buildBaseConfig.options.outputPath], + dependsOn: [ + { + target: 'build', + projects: 'dependencies', + }, + ], options: { command: 'package', ...(options.plugin === '@ns3/nx-serverless/plugin' @@ -43,8 +49,15 @@ export function getProjectConfig( deploy: { executor: '@ns3/nx-serverless:sls', outputs: [outputPath, buildBaseConfig.options.outputPath], + dependsOn: [ + { + target: 'package', + projects: 'self', + }, + ], options: { command: 'deploy', + package: '.serverless', ...(options.plugin === '@ns3/nx-serverless/plugin' ? { buildTarget: buildTargetProd } : {}), diff --git a/packages/nx-serverless/src/generators/init/generator.ts b/packages/nx-serverless/src/generators/init/generator.ts index 6f51d51f..0256de93 100644 --- a/packages/nx-serverless/src/generators/init/generator.ts +++ b/packages/nx-serverless/src/generators/init/generator.ts @@ -1,4 +1,11 @@ -import { addDependenciesToPackageJson, formatFiles, GeneratorCallback, Tree } from '@nrwl/devkit'; +import { + addDependenciesToPackageJson, + formatFiles, + GeneratorCallback, + readWorkspaceConfiguration, + Tree, + updateWorkspaceConfiguration, +} from '@nrwl/devkit'; import { jestInitGenerator } from '@nrwl/jest'; import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; import { setDefaultCollection } from '@nrwl/workspace/src/utilities/set-default-collection'; @@ -10,6 +17,7 @@ export default async function serverlessInitGenerator(host: Tree, options: InitG setDefaultCollection(host, '@ns3/nx-serverless'); updateGitignore(host); + addCacheableOperation(host); if (!options.unitTestRunner || options.unitTestRunner === 'jest') { const jestTask = jestInitGenerator(host, {}); @@ -53,3 +61,23 @@ function updateGitignore(host: Tree) { host.write('.gitignore', ignore); } } + +function addCacheableOperation(tree: Tree) { + const workspace = readWorkspaceConfiguration(tree); + if ( + !workspace.tasksRunnerOptions || + !workspace.tasksRunnerOptions.default || + workspace.tasksRunnerOptions.default.runner !== '@nrwl/workspace/tasks-runners/default' + ) { + return; + } + + workspace.tasksRunnerOptions.default.options = workspace.tasksRunnerOptions.default.options || {}; + + workspace.tasksRunnerOptions.default.options.cacheableOperations = + workspace.tasksRunnerOptions.default.options.cacheableOperations || []; + if (!workspace.tasksRunnerOptions.default.options.cacheableOperations.includes('package')) { + workspace.tasksRunnerOptions.default.options.cacheableOperations.push('package'); + } + updateWorkspaceConfiguration(tree, workspace); +}