Skip to content

Commit 801e69c

Browse files
fix: Fix package.py commands after :zip not being executed (#606)
Co-authored-by: Anton Babenko <anton@antonbabenko.com>
1 parent a84d8af commit 801e69c

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

examples/build-package/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Note that this example may create resources which cost money. Run `terraform des
3737
| <a name="module_lambda_layer"></a> [lambda\_layer](#module\_lambda\_layer) | ../../ | n/a |
3838
| <a name="module_lambda_layer_pip_requirements"></a> [lambda\_layer\_pip\_requirements](#module\_lambda\_layer\_pip\_requirements) | ../.. | n/a |
3939
| <a name="module_lambda_layer_poetry"></a> [lambda\_layer\_poetry](#module\_lambda\_layer\_poetry) | ../../ | n/a |
40+
| <a name="module_npm_package_with_commands_and_patterns"></a> [npm\_package\_with\_commands\_and\_patterns](#module\_npm\_package\_with\_commands\_and\_patterns) | ../../ | n/a |
4041
| <a name="module_package_dir"></a> [package\_dir](#module\_package\_dir) | ../../ | n/a |
4142
| <a name="module_package_dir_pip_dir"></a> [package\_dir\_pip\_dir](#module\_package\_dir\_pip\_dir) | ../../ | n/a |
4243
| <a name="module_package_dir_poetry"></a> [package\_dir\_poetry](#module\_package\_dir\_poetry) | ../../ | n/a |

examples/build-package/main.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,31 @@ module "package_with_commands_and_patterns" {
244244
]
245245
}
246246

247+
# Some use cases might require the production packages are deployed while maintaining local node_modules folder
248+
# This example saves the node_modules folder by moving it to an ignored directory
249+
# After the zip file is created with production node_modules, the dev node_modules folder is restored
250+
module "npm_package_with_commands_and_patterns" {
251+
source = "../../"
252+
253+
create_function = false
254+
255+
runtime = "nodejs18.x"
256+
source_path = [
257+
{
258+
path = "${path.module}/../fixtures/node-app"
259+
commands = [
260+
"[ ! -d node_modules ] || mv node_modules node_modules_temp",
261+
"npm install --production",
262+
":zip",
263+
"rm -rf node_modules",
264+
"[ ! -d node_modules_temp ] || mv node_modules_temp node_modules",
265+
]
266+
patterns = [
267+
"!node_modules_temp/.*"
268+
]
269+
}
270+
]
271+
}
247272
# Create zip-archive with various sources and patterns.
248273
# Note, that it is possible to write comments in patterns.
249274
module "package_with_patterns" {

package.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,9 @@ def commands_step(path, commands):
779779
)
780780
else:
781781
batch.append(c)
782+
if batch:
783+
step("sh", path, "\n".join(batch))
784+
batch.clear()
782785

783786
for claim in claims:
784787
if isinstance(claim, str):

tests/fixtures/node-app/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// test

tests/fixtures/node-app/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "app",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
},
13+
"devDependencies": {
14+
"axios": "^1.7.3"
15+
}
16+
}

0 commit comments

Comments
 (0)