Skip to content

Commit

Permalink
Merge pull request #91 from mbc-net/feat/isolate-docker
Browse files Browse the repository at this point in the history
[CLI]: isolate docker environment for each project
  • Loading branch information
koichimurakami authored Dec 6, 2024
2 parents a7bce5e + d18f37d commit a00c406
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -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/*"]
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/src/actions/new.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

Expand Down Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions packages/cli/src/actions/new.action.update-env-local.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
4 changes: 3 additions & 1 deletion packages/cli/templates/.env.local
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/sequence/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions packages/task/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions packages/ui-setting/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}

0 comments on commit a00c406

Please sign in to comment.