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/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/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) + }) +}) 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\"", 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" } }