From f07fa4ecbf2d4d97d39fd45ecd49f3ddf993d8fa Mon Sep 17 00:00:00 2001 From: koichimurakami Date: Tue, 3 Dec 2024 13:11:41 +0000 Subject: [PATCH 1/3] v0.1.41-beta.0 --- lerna.json | 2 +- package-lock.json | 16 ++++++++-------- packages/cli/package.json | 2 +- packages/core/package.json | 2 +- packages/sequence/package.json | 4 ++-- packages/task/package.json | 4 ++-- packages/ui-setting/package.json | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lerna.json b/lerna.json index 388f0d5..517422a 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "packages": ["packages/*"] } diff --git a/package-lock.json b/package-lock.json index 0c9851c..ae37a02 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28309,7 +28309,7 @@ }, "packages/cli": { "name": "@mbc-cqrs-serverless/cli", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "license": "MIT", "dependencies": { "commander": "^11.1.0", @@ -28333,7 +28333,7 @@ }, "packages/core": { "name": "@mbc-cqrs-serverless/core", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "license": "MIT", "dependencies": { "@aws-crypto/sha256-js": "^5.2.0", @@ -28381,26 +28381,26 @@ }, "packages/sequence": { "name": "@mbc-cqrs-serverless/sequence", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "license": "MIT", "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.40-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.41-beta.0" } }, "packages/task": { "name": "@mbc-cqrs-serverless/task", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "license": "MIT", "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.40-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.41-beta.0" } }, "packages/ui-setting": { "name": "@mbc-cqrs-serverless/ui-setting", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "license": "MIT", "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.40-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.41-beta.0" } } } diff --git a/packages/cli/package.json b/packages/cli/package.json index fadc954..a6d0d7a 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/cli", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "description": "a CLI to get started with MBC CQRS serverless framework", "keywords": [ "mbc", diff --git a/packages/core/package.json b/packages/core/package.json index 6bda788..3c63100 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/core", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "description": "CQRS and event base core", "keywords": [ "mbc", diff --git a/packages/sequence/package.json b/packages/sequence/package.json index 286df3b..7785157 100644 --- a/packages/sequence/package.json +++ b/packages/sequence/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/sequence", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "description": "Generate increment sequence with time-rotation", "keywords": [ "mbc", @@ -41,6 +41,6 @@ "access": "public" }, "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.40-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.41-beta.0" } } diff --git a/packages/task/package.json b/packages/task/package.json index d77163d..fa7d82c 100644 --- a/packages/task/package.json +++ b/packages/task/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/task", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "description": "long-running task", "keywords": [ "mbc", @@ -41,6 +41,6 @@ "access": "public" }, "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.40-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.41-beta.0" } } diff --git a/packages/ui-setting/package.json b/packages/ui-setting/package.json index 7d23e77..8102fab 100644 --- a/packages/ui-setting/package.json +++ b/packages/ui-setting/package.json @@ -1,6 +1,6 @@ { "name": "@mbc-cqrs-serverless/ui-setting", - "version": "0.1.40-beta.0", + "version": "0.1.41-beta.0", "description": "Setting master data", "keywords": [ "mbc", @@ -41,6 +41,6 @@ "access": "public" }, "dependencies": { - "@mbc-cqrs-serverless/core": "^0.1.40-beta.0" + "@mbc-cqrs-serverless/core": "^0.1.41-beta.0" } } From c1085184b9de765ddf8c0120cc5a19e2e77854a0 Mon Sep 17 00:00:00 2001 From: Quan Nguyen Ba Date: Fri, 6 Dec 2024 11:11:50 +0700 Subject: [PATCH 2/3] isolate docker environment for each project --- packages/cli/src/actions/new.action.ts | 20 ++++++++++++++++++++ packages/cli/templates/.env.local | 4 +++- packages/cli/templates/package.json | 8 ++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/actions/new.action.ts b/packages/cli/src/actions/new.action.ts index c405e3f..736a0c5 100644 --- a/packages/cli/src/actions/new.action.ts +++ b/packages/cli/src/actions/new.action.ts @@ -63,6 +63,12 @@ export default async function newAction( const infraGitignore = path.join(destDir, 'infra/gitignore') copyFileSync(infraGitignore, path.join(destDir, 'infra/.gitignore')) unlinkSync(infraGitignore) + // replace project_name in .env.local + updateEnvLocal( + path.join(destDir, '.env.local'), + '%%projectName%%', + projectName, + ) // cp .env.local .env copyFileSync(path.join(destDir, '.env.local'), path.join(destDir, '.env')) @@ -139,10 +145,24 @@ function getPackageVersion(packageName: string, isLatest = false): string[] { return versions } +function updateEnvLocal( + envPath: string, + searchValue: string, + replaceValue: string, +): void { + const envLocalContent = readFileSync(envPath, 'utf8') + const newEnvLocalContent = envLocalContent.replaceAll( + searchValue, + replaceValue, + ) + writeFileSync(envPath, newEnvLocalContent) +} + export let exportsForTesting = { usePackageVersion, getPackageVersion, isLatestCli, + updateEnvLocal, } if (process.env.NODE_ENV !== 'test') { exportsForTesting = undefined diff --git a/packages/cli/templates/.env.local b/packages/cli/templates/.env.local index e0e6101..6ad904a 100644 --- a/packages/cli/templates/.env.local +++ b/packages/cli/templates/.env.local @@ -5,7 +5,9 @@ AWS_DEFAULT_REGION=ap-northeast-1 # running environment NODE_ENV=local # local, dev, stg, prod # name of application -APP_NAME=demo +APP_NAME=%%projectName%% +# name of docker compose +COMPOSE_PROJECT_NAME=%%projectName%% # set log levels LOG_LEVEL=verbose # debug, verbose, info, warn, error, fatal # disable event route for API GW integration diff --git a/packages/cli/templates/package.json b/packages/cli/templates/package.json index a5184c3..9473e5a 100644 --- a/packages/cli/templates/package.json +++ b/packages/cli/templates/package.json @@ -16,11 +16,11 @@ "start:repl": "nest start --watch --entryFile repl", "start:prod": "node dist/main", "offline:docker:build": "run-script-os", - "offline:docker:build:default": "cd infra-local && docker compose up --build --remove-orphans", - "offline:docker:build:win32": "powershell -Command \"Set-Location infra-local; docker compose up --build --remove-orphans\"", + "offline:docker:build:default": "ln -f .env $PWD/infra-local/.env && cd infra-local && docker compose up --build --remove-orphans", + "offline:docker:build:win32": "powershell -Command \"Copy-Item '.env' -Destination './infra-local/.env'; Set-Location infra-local; docker compose up --build --remove-orphans\"", "offline:docker": "run-script-os", - "offline:docker:default": "cd infra-local && mkdir -p docker-data/.cognito && cp -r cognito-local/db docker-data/.cognito && docker compose up --remove-orphans", - "offline:docker:win32": "powershell -Command \"Set-Location infra-local; New-Item -ItemType Directory -Force -Path 'docker-data/.cognito'; Copy-Item -Recurse -Force 'cognito-local/db' 'docker-data/.cognito'; docker compose up --remove-orphans\"", + "offline:docker:default": "ln -f .env $PWD/infra-local/.env && cd infra-local && mkdir -p docker-data/.cognito && cp -r cognito-local/db docker-data/.cognito && docker compose up --remove-orphans", + "offline:docker:win32": "powershell -Command \"Copy-Item '.env' -Destination './infra-local/.env'; Set-Location infra-local; New-Item -ItemType Directory -Force -Path 'docker-data/.cognito'; Copy-Item -Recurse -Force 'cognito-local/db' 'docker-data/.cognito'; docker compose up --remove-orphans\"", "offline:sls": "run-script-os", "offline:sls:default": "/bin/bash ./infra-local/scripts/resources.sh && /bin/bash ./infra-local/scripts/trigger_ddb_stream.sh & ln -f .env $PWD/infra-local/.env && cd infra-local && NODE_ENV=development AWS_ACCESS_KEY_ID=DUMMYIDEXAMPLE AWS_SECRET_ACCESS_KEY=DUMMYEXAMPLEKEY SLS_DEBUG=* serverless offline start", "offline:sls:win32": "npm run resources:win32 && concurrently \"npm run trigger-ddb:win32\" \"npm run sls:win32\"", From d18f37d728a5b213722bad361509b3671c877142 Mon Sep 17 00:00:00 2001 From: Quan Nguyen Ba Date: Fri, 6 Dec 2024 11:13:14 +0700 Subject: [PATCH 3/3] unit test for update env local function --- .../new.action.update-env-local.spec.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 packages/cli/src/actions/new.action.update-env-local.spec.ts diff --git a/packages/cli/src/actions/new.action.update-env-local.spec.ts b/packages/cli/src/actions/new.action.update-env-local.spec.ts new file mode 100644 index 0000000..9a8bab7 --- /dev/null +++ b/packages/cli/src/actions/new.action.update-env-local.spec.ts @@ -0,0 +1,49 @@ +import { readFileSync, writeFileSync } from 'fs' + +import { exportsForTesting } from './new.action' + +const { updateEnvLocal } = exportsForTesting + +jest.mock('fs') + +describe('updateEnvLocal', () => { + const mockEnvContent = ` + # name of application + APP_NAME=%%projectName%% + # name of docker compose + COMPOSE_PROJECT_NAME=%%projectName%% + ` + + const envPath = './.env.local' + + beforeEach(() => { + jest.clearAllMocks() + }) + + it('should update the specified value in the .env.local file', () => { + const searchValue = '%%projectName%%' + const replaceValue = 'new-project-name' + + ;(readFileSync as jest.Mock).mockReturnValue(mockEnvContent) + + updateEnvLocal(envPath, searchValue, replaceValue) + + expect(readFileSync).toHaveBeenCalledWith(envPath, 'utf8') + + const expectedContent = mockEnvContent.replaceAll(searchValue, replaceValue) + expect(writeFileSync).toHaveBeenCalledWith(envPath, expectedContent) + }) + + it('should not change the file content if searchValue is not found', () => { + const searchValue = 'non-existent-value' + const replaceValue = 'new-project-name' + + ;(readFileSync as jest.Mock).mockReturnValue(mockEnvContent) + + updateEnvLocal(envPath, searchValue, replaceValue) + + expect(readFileSync).toHaveBeenCalledWith(envPath, 'utf8') + + expect(writeFileSync).toHaveBeenCalledWith(envPath, mockEnvContent) + }) +})