@mbc-cqrs-serverless/cli 0.1.13-beta.0 → 0.1.15-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/templates/README.md +0 -3
- package/templates/infra-local/scripts/resources.ps1 +25 -0
- package/templates/infra-local/scripts/resources.sh +19 -0
- package/templates/infra-local/scripts/trigger_ddb_stream.ps1 +133 -0
- package/templates/infra-local/scripts/trigger_ddb_stream.sh +23 -0
- package/templates/infra-local/serverless.yml +1 -0
- package/templates/package.json +14 -3
- package/templates/infra-local/resources.sh +0 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mbc-cqrs-serverless/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15-beta.0",
|
|
4
4
|
"description": "a CLI to get started with MBC CQRS serverless framework",
|
|
5
5
|
"bin": {
|
|
6
6
|
"mbc": "./dist/index.js"
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@faker-js/faker": "^8.3.1",
|
|
36
|
-
"@mbc-cqrs-serverless/core": "^0.1.
|
|
36
|
+
"@mbc-cqrs-serverless/core": "^0.1.15-beta.0"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "7748d7c3559857b8e70a24e147e806bbc2aa64de"
|
|
39
39
|
}
|
package/templates/README.md
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Load environment variables from .env file (assuming you have a utility to load it)
|
|
2
|
+
Get-Content .env | ForEach-Object {
|
|
3
|
+
if ($_ -match "^\s*#") {
|
|
4
|
+
return
|
|
5
|
+
}
|
|
6
|
+
if ($_ -match "^\s*(\w+)\s*=\s*(.*)\s*$") {
|
|
7
|
+
$name = $matches[1]
|
|
8
|
+
$value = $matches[2]
|
|
9
|
+
$value = $value -replace '\s*#.*', ''
|
|
10
|
+
[System.Environment]::SetEnvironmentVariable($name, $value, "Process")
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
Write-Host "======= check if S3 bucket exists ======="
|
|
15
|
+
$bucketExists = aws --endpoint-url=http://localhost:4566 s3 ls | Select-String $env:S3_BUCKET_NAME
|
|
16
|
+
|
|
17
|
+
if (-not $bucketExists) {
|
|
18
|
+
Write-Host "Bucket $env:S3_BUCKET_NAME does not exist. Creating it..."
|
|
19
|
+
aws --endpoint-url=http://localhost:4566 s3 mb "s3://$env:S3_BUCKET_NAME"
|
|
20
|
+
} else {
|
|
21
|
+
Write-Host "Bucket $env:S3_BUCKET_NAME already exists."
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
Write-Host "======= list S3 buckets ======="
|
|
25
|
+
aws --endpoint-url=http://localhost:4566 s3 ls
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
|
|
3
|
+
# Load environment variables from .env file
|
|
4
|
+
if [ -f .env ]; then
|
|
5
|
+
export $(echo $(cat .env | sed 's/#.*//g' | xargs) | envsubst)
|
|
6
|
+
fi
|
|
7
|
+
|
|
8
|
+
echo "======= check if S3 bucket exists ======="
|
|
9
|
+
bucket_exists=$(aws --endpoint-url=http://localhost:4566 s3 ls | grep "$S3_BUCKET_NAME" | wc -l)
|
|
10
|
+
|
|
11
|
+
if [ "$bucket_exists" -eq 0 ]; then
|
|
12
|
+
echo "Bucket $S3_BUCKET_NAME does not exist. Creating it..."
|
|
13
|
+
aws --endpoint-url=http://localhost:4566 s3 mb s3://$S3_BUCKET_NAME
|
|
14
|
+
else
|
|
15
|
+
echo "Bucket $S3_BUCKET_NAME already exists."
|
|
16
|
+
fi
|
|
17
|
+
|
|
18
|
+
echo "======= list S3 buckets ======="
|
|
19
|
+
aws --endpoint-url=http://localhost:4566 s3 ls
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Set AWS environment variables
|
|
2
|
+
$env:AWS_DEFAULT_REGION = "ap-northeast-1"
|
|
3
|
+
$env:AWS_ACCOUNT_ID = "101010101010"
|
|
4
|
+
$env:AWS_ACCESS_KEY_ID = "local"
|
|
5
|
+
$env:AWS_SECRET_ACCESS_KEY = "local"
|
|
6
|
+
|
|
7
|
+
$endpoint = "http://localhost:8000"
|
|
8
|
+
|
|
9
|
+
# Load environment variables from .env file (assuming you have a utility to load it)
|
|
10
|
+
Get-Content .env | ForEach-Object {
|
|
11
|
+
if ($_ -match "^\s*#") {
|
|
12
|
+
return
|
|
13
|
+
}
|
|
14
|
+
if ($_ -match "^\s*(\w+)\s*=\s*(.*)\s*$") {
|
|
15
|
+
$name = $matches[1]
|
|
16
|
+
$value = $matches[2]
|
|
17
|
+
$value = $value -replace '\s*#.*', ''
|
|
18
|
+
[System.Environment]::SetEnvironmentVariable($name, $value, "Process")
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
Write-Host "Read table name"
|
|
23
|
+
|
|
24
|
+
# Read table names from JSON file
|
|
25
|
+
$tables = (Get-Content .\prisma\dynamodbs\cqrs.json | ConvertFrom-Json)
|
|
26
|
+
|
|
27
|
+
# Check table health
|
|
28
|
+
$start = Get-Date
|
|
29
|
+
foreach ($table in $tables) {
|
|
30
|
+
while ($true) {
|
|
31
|
+
$elapsed = (New-TimeSpan -Start $start).TotalSeconds
|
|
32
|
+
if ($elapsed -gt 10) {
|
|
33
|
+
Write-Host "Timeout"
|
|
34
|
+
exit 1
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
Write-Host "Check health table local-$env:APP_NAME-$table-command"
|
|
38
|
+
Write-Host "local-$env:APP_NAME-$table-command"
|
|
39
|
+
$status = aws --endpoint $endpoint dynamodb describe-table --table-name "local-$env:APP_NAME-$table-command" --query "Table.TableStatus"
|
|
40
|
+
|
|
41
|
+
Write-Host "Table status: $status"
|
|
42
|
+
if ($status -eq '"ACTIVE"') {
|
|
43
|
+
Write-Host "Table $table is ACTIVE"
|
|
44
|
+
break
|
|
45
|
+
} else {
|
|
46
|
+
Write-Host "Table $table is not ACTIVE"
|
|
47
|
+
Start-Sleep -Seconds 1
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
# Check the health of 'tasks' table
|
|
53
|
+
$start = Get-Date
|
|
54
|
+
while ($true) {
|
|
55
|
+
$elapsed = (New-TimeSpan -Start $start).TotalSeconds
|
|
56
|
+
if ($elapsed -gt 10) {
|
|
57
|
+
Write-Host "Timeout"
|
|
58
|
+
exit 1
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
Write-Host "Check health table tasks"
|
|
62
|
+
$status = aws --endpoint $endpoint dynamodb describe-table --table-name "local-$env:APP_NAME-tasks" --query "Table.TableStatus"
|
|
63
|
+
|
|
64
|
+
Write-Host "Table status: $status"
|
|
65
|
+
if ($status -eq '"ACTIVE"') {
|
|
66
|
+
Write-Host "Table tasks is ACTIVE"
|
|
67
|
+
break
|
|
68
|
+
} else {
|
|
69
|
+
Write-Host "Table tasks is not ACTIVE"
|
|
70
|
+
Start-Sleep -Seconds 1
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
# Wait for serverless to start
|
|
75
|
+
$start = Get-Date
|
|
76
|
+
while ($true) {
|
|
77
|
+
$elapsed = (New-TimeSpan -Start $start).TotalSeconds
|
|
78
|
+
if ($elapsed -gt 100) {
|
|
79
|
+
Write-Host "Timeout"
|
|
80
|
+
exit 1
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
Write-Host "Check health serverless"
|
|
84
|
+
try {
|
|
85
|
+
$response = Invoke-WebRequest -Uri "http://localhost:3000" -UseBasicParsing -ErrorAction Stop
|
|
86
|
+
$status = $response.StatusCode
|
|
87
|
+
} catch {
|
|
88
|
+
if ($_.Exception.Response -ne $null) {
|
|
89
|
+
$status = $_.Exception.Response.StatusCode.Value__
|
|
90
|
+
} else {
|
|
91
|
+
$status = 0 # Assign 0 or another value if there's no HTTP response (e.g., connection failure)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
Write-Host "Serverless status: $status"
|
|
96
|
+
|
|
97
|
+
if ($status -eq 200) {
|
|
98
|
+
Write-Host "Serverless is ACTIVE"
|
|
99
|
+
break
|
|
100
|
+
} else {
|
|
101
|
+
Write-Host "Serverless is not ACTIVE"
|
|
102
|
+
Start-Sleep -Seconds 1
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
# Trigger command stream
|
|
108
|
+
$timestamp = [math]::Round((Get-Date).Subtract((Get-Date "01/01/1970")).TotalSeconds)
|
|
109
|
+
foreach ($table in $tables) {
|
|
110
|
+
Write-Host "Send a command to trigger command stream $table"
|
|
111
|
+
$item = @{
|
|
112
|
+
pk = @{ S = "test" }
|
|
113
|
+
sk = @{ S = "$timestamp" }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
# Convert the item to a JSON string with double quotes
|
|
117
|
+
$jsonItem = $item | ConvertTo-Json -Compress
|
|
118
|
+
|
|
119
|
+
$jsonItemString = [string]$jsonItem
|
|
120
|
+
|
|
121
|
+
$escapedJsonItemString = $jsonItemString -replace '"', '\"'
|
|
122
|
+
|
|
123
|
+
Write-Host "Send a item to trigger command $table"
|
|
124
|
+
|
|
125
|
+
aws dynamodb put-item --endpoint http://localhost:8000 --table-name "local-$env:APP_NAME-$table-command" --item $escapedJsonItemString
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
# Trigger asks stream
|
|
129
|
+
Write-Host "Send a command to trigger command stream tasks"
|
|
130
|
+
$command = @"
|
|
131
|
+
aws dynamodb put-item --endpoint http://localhost:8000 --table-name "local-$env:APP_NAME-tasks" --item '{\"input\":{\"M\":{}},\"sk\":{\"S\":\"$timestamp\"},\"pk\":{\"S\":\"test\"}}'
|
|
132
|
+
"@
|
|
133
|
+
Invoke-Expression $command
|
|
@@ -39,6 +39,26 @@ for table in "${tables[@]}"; do
|
|
|
39
39
|
done
|
|
40
40
|
done
|
|
41
41
|
|
|
42
|
+
start=$(date +%s)
|
|
43
|
+
while true; do
|
|
44
|
+
elapsed=$(($(date +%s) - ${start}))
|
|
45
|
+
if [[ ${elapsed} -gt 10 ]]; then
|
|
46
|
+
echo "Timeout"
|
|
47
|
+
exit 1
|
|
48
|
+
fi
|
|
49
|
+
|
|
50
|
+
echo "Check health table tasks"
|
|
51
|
+
status=$(aws --endpoint=${endpoint} dynamodb describe-table --table-name local-${APP_NAME}-tasks --query 'Table.TableStatus')
|
|
52
|
+
echo "Table status: ${status}"
|
|
53
|
+
if [[ "${status}" == "\"ACTIVE\"" ]]; then
|
|
54
|
+
echo "Table tasks is ACTIVE"
|
|
55
|
+
break
|
|
56
|
+
else
|
|
57
|
+
echo "Table tasks is not ACTIVE"
|
|
58
|
+
sleep 1
|
|
59
|
+
fi
|
|
60
|
+
done
|
|
61
|
+
|
|
42
62
|
# Wait serverless start
|
|
43
63
|
start=$(date +%s)
|
|
44
64
|
while true; do
|
|
@@ -67,3 +87,6 @@ for table in "${tables[@]}"; do
|
|
|
67
87
|
echo "Send a command to trigger command stream ${table}"
|
|
68
88
|
aws --endpoint=${endpoint} dynamodb put-item --table-name local-${APP_NAME}-${table}-command --item "{\"pk\": {\"S\": \"test\" }, \"sk\": { \"S\": \"${timestamp}\" }}"
|
|
69
89
|
done
|
|
90
|
+
|
|
91
|
+
echo "Send a command to trigger command stream tasks"
|
|
92
|
+
aws --endpoint=http://localhost:8000 dynamodb put-item --table-name local-demo-tasks --item "{\"input\":{\"M\":{}},\"sk\":{\"S\":\"${timestamp}\"},\"pk\":{\"S\":\"test\"}}"
|
package/templates/package.json
CHANGED
|
@@ -12,9 +12,18 @@
|
|
|
12
12
|
"build:prod": "nest build",
|
|
13
13
|
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
|
14
14
|
"start:repl": "nest start --watch --entryFile repl",
|
|
15
|
-
"offline:docker:build": "
|
|
16
|
-
"offline:docker": "cd infra-local &&
|
|
17
|
-
"offline:
|
|
15
|
+
"offline:docker:build": "run-script-os",
|
|
16
|
+
"offline:docker:build:default": "cd infra-local && docker compose up --build --remove-orphans",
|
|
17
|
+
"offline:docker:build:win32": "powershell -Command \"Set-Location infra-local; docker compose up --build --remove-orphans\"",
|
|
18
|
+
"offline:docker": "run-script-os",
|
|
19
|
+
"offline:docker:default": "cd infra-local && mkdir -p docker-data/.cognito && cp -r cognito-local/db docker-data/.cognito && docker compose up --remove-orphans",
|
|
20
|
+
"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\"",
|
|
21
|
+
"offline:sls": "run-script-os",
|
|
22
|
+
"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",
|
|
23
|
+
"offline:sls:win32": "npm run resources:win32 && concurrently \"npm run trigger-ddb:win32\" \"npm run sls:win32\"",
|
|
24
|
+
"trigger-ddb:win32": "powershell -ExecutionPolicy Bypass -File ./infra-local/scripts/trigger_ddb_stream.ps1",
|
|
25
|
+
"resources:win32": "powershell -ExecutionPolicy Bypass -File ./infra-local/scripts/resources.ps1",
|
|
26
|
+
"sls:win32": "powershell -Command \" Copy-Item \".env\" -Destination \".\\infra-local\\.env\" \"; Set-Location infra-local; $env:NODE_ENV='development'; $env:AWS_ACCESS_KEY_ID='DUMMYIDEXAMPLE'; $env:AWS_SECRET_ACCESS_KEY='DUMMYEXAMPLEKEY'; $env:SLS_DEBUG='*'; serverless offline start\"",
|
|
18
27
|
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
|
19
28
|
"test": "jest",
|
|
20
29
|
"test:watch": "jest --watch",
|
|
@@ -60,6 +69,7 @@
|
|
|
60
69
|
"@typescript-eslint/parser": "^6.15.0",
|
|
61
70
|
"class-transformer": "^0.5.1",
|
|
62
71
|
"class-validator": "^0.14.0",
|
|
72
|
+
"concurrently": "^9.0.1",
|
|
63
73
|
"eslint": "^8.56.0",
|
|
64
74
|
"eslint-config-prettier": "^9.1.0",
|
|
65
75
|
"eslint-plugin-import": "^2.29.1",
|
|
@@ -69,6 +79,7 @@
|
|
|
69
79
|
"nestjs-spelunker": "^1.3.0",
|
|
70
80
|
"prettier": "^3.1.1",
|
|
71
81
|
"prisma": "^5.7.1",
|
|
82
|
+
"run-script-os": "^1.1.6",
|
|
72
83
|
"serverless": "^3.38.0",
|
|
73
84
|
"serverless-dynamodb": "^0.2.47",
|
|
74
85
|
"serverless-localstack": "^1.1.2",
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
#!/bin/sh
|
|
2
|
-
|
|
3
|
-
if [ -f .env ]; then
|
|
4
|
-
export $(echo $(cat .env | sed 's/#.*//g'| xargs) | envsubst)
|
|
5
|
-
fi
|
|
6
|
-
|
|
7
|
-
echo "======= create S3 buckets ======="
|
|
8
|
-
aws --endpoint-url=http://localhost:4566 s3 mb s3://$S3_BUCKET_NAME
|
|
9
|
-
|
|
10
|
-
echo "======= list S3 buckets ======="
|
|
11
|
-
aws --endpoint-url=http://localhost:4566 s3 ls
|