Skip to content

Commit

Permalink
feat(serverless): improve generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Bielik20 committed Feb 16, 2022
1 parent b202ed6 commit a48325c
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/nx-serverless/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
service: <%= name %>

configValidationMode: error # remove in serverless v3
variablesResolutionMode: 20210326 # remove in serverless v3

plugins:
- '<%= plugin %>'
- serverless-offline
Expand All @@ -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:
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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,
Expand Down
46 changes: 36 additions & 10 deletions packages/nx-serverless/src/generators/application/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ function updateTsConfig(tree: Tree, options: ServerlessGeneratorNormalizedSchema
...json.compilerOptions,
forceConsistentCasingInFileNames: true,
strict: true,
noImplicitOverride: true,
noPropertyAccessFromIndexSignature: true,
noImplicitReturns: true,
noFallthroughCasesInSwitch: true,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'
Expand All @@ -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 }
: {}),
Expand Down
30 changes: 29 additions & 1 deletion packages/nx-serverless/src/generators/init/generator.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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, {});
Expand Down Expand Up @@ -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);
}

0 comments on commit a48325c

Please sign in to comment.