Skip to content

Commit 7e6ae0e

Browse files
authored
Merge pull request #106 from talyssonoc/tooling-and-code-cleanup
Tooling and code cleanup
2 parents 2b8eef1 + 1809a50 commit 7e6ae0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1288
-1195
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ If you want to just run the application in development mode, use the following c
1616

1717
```sh
1818
$ yarn dev
19+
1920
```
2021

2122
To run the application in debug mode in a way that the execution will stop when a debugger statement is called, use:
@@ -50,6 +51,68 @@ The boilerplate is prepared to run tests using Jest. We usually group the tests
5051
$ yarn test
5152
```
5253

54+
## Docker wrapper
55+
56+
In case you want to use Docker to run it, this project includes a [docker wrapper](https://github.com/brunohcastro/node-base) for development. Any command can be executed by calling the scripts under the `dbin/` folder.
57+
58+
```sh
59+
$ dbin/yarn dev
60+
61+
$ dbin/yarn debug
62+
63+
$ dbin/yarn cli
64+
65+
$ dbin/yarn remote [server address] [REPL port]
66+
67+
$ dbin/yarn test
68+
```
69+
70+
The container runs using host networking, so there's no need to map ports. Keep in mind that environment variables should be added to the docker-compose.yml.
71+
72+
### Wrapper commands
73+
74+
```sh
75+
# Runs the command inside an ephemeral container using the app service described in the docker-compose file as a base (use the --root flag if the command should be run as root)
76+
$ dbin/run [--root] <command>
77+
78+
# Rebuild the image after any changes to the dockerfile
79+
$ dbin/build
80+
81+
# Remove all containers and their volumes (WARNING any cache or files not stored in the filesystem will be deleted)
82+
$ dbin/dispose
83+
84+
# Appends a RUN command to the base image, useful to install new packages
85+
$ dbin/chimg <command>
86+
```
87+
88+
### Wrapper Aliases
89+
90+
```sh
91+
# Creates a new <name> file in dbin to alias the <command> inside the docker container (use the --root flag if the command should be run as root)
92+
$ dbin/mkalias [--root] <name> <command>
93+
94+
# Opens a new terminal in the project folder (use the --root flag if the shell should assume the root user)
95+
$ dbin/shell [--root]
96+
97+
# Runs npm in the project folder
98+
$ dbin/npm
99+
100+
# Runs npx in the project folder
101+
$ dbin/npx
102+
103+
# Runs yarn in the project folder
104+
$ dbin/yarn
105+
```
106+
107+
### Wrapper Helpers
108+
109+
```bash
110+
# Adds dbin folder to the PATH only for the current terminal session.
111+
$ source dbin/local-env
112+
113+
# After using this command you can use the any script inside the dbin folder without the dbin/ prefix
114+
```
115+
53116
## Dependency injection
54117

55118
We use [Awilix](https://www.npmjs.com/package/awilix) to implement dependency injection and decouple the parts of your application. The boilerplate was developed in a way that each [module](#modules) is able to consume and augment the registered dependencies separately. Click here to know more about [inversion of control and dependency injection](https://www.martinfowler.com/articles/injection.html). The creator of Awilix also has a very good series of posts about the design decisions of the library that you can read [clicking here](https://medium.com/@Jeffijoe/dependency-injection-in-node-js-2016-edition-f2a88efdd427).

dbin/build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
USER_ID=$(id -u) GROUP_ID=$(id -g) docker-compose build dev

dbin/chimg

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
PARENT_DIR="$(cd "$(dirname "$0")" && cd .. && pwd)"
4+
5+
cp "$PARENT_DIR/docker/Dockerfile.dev" "$PARENT_DIR/docker/Dockerfile.dev.bkp"
6+
7+
echo -e "\nRUN $*" >> "$PARENT_DIR/docker/Dockerfile.dev"
8+
9+
if USER_ID=$(id -u) GROUP_ID=$(id -g) docker-compose build dev; then
10+
rm -f "$PARENT_DIR/docker/Dockerfile.dev.bkp"
11+
else
12+
mv "$PARENT_DIR/docker/Dockerfile.dev.bkp" "$PARENT_DIR/docker/Dockerfile.dev"
13+
fi

dbin/dispose

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
docker-compose down -v

dbin/local-env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
DIR="$(cd "$(dirname "$0")" && pwd)"
4+
5+
export PATH="$PATH:$DIR"

dbin/mkalias

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
DIR="$(cd "$(dirname "$0")" && pwd)"
4+
5+
if [[ $1 == --root ]] ; then
6+
runner="\$DIR/run --root"
7+
name=$2
8+
shift 2
9+
else
10+
runner="\$DIR/run"
11+
name=$1
12+
shift 1
13+
fi
14+
15+
cat >"$DIR/$name" <<EOL
16+
#!/bin/bash
17+
18+
DIR="\$(cd "\$(dirname "\$0")" && pwd)"
19+
20+
. "$runner" $@ "\$@"
21+
EOL
22+
23+
chmod +x "$DIR/$name"

dbin/mvroot

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
PARENT_DIR="$(cd "$(dirname "$0")" && cd .. && pwd)"
4+
5+
TARGET_DIR="$1"
6+
7+
mv "$PARENT_DIR/README.md" "$PARENT_DIR/INSTRUCTIONS.md" 2>/dev/null
8+
9+
(shopt -s dotglob; mv "$PARENT_DIR/$TARGET_DIR"/* "$PARENT_DIR")
10+
11+
rm -r "${PARENT_DIR:?}/$TARGET_DIR"

dbin/npm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
DIR="$(cd "$(dirname "$0")" && pwd)"
4+
5+
. "$DIR/run" npm "$@"

dbin/npx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
DIR="$(cd "$(dirname "$0")" && pwd)"
4+
5+
. "$DIR/run" npx "$@"

dbin/run

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
user=""
4+
5+
if [[ $1 == --root ]] ; then
6+
user="--user root"
7+
shift 1
8+
fi
9+
10+
if docker-compose ps -a | grep -E -i -q 'app(\s*)running'; then
11+
USER_ID=$(id -u) GROUP_ID=$(id -g) docker-compose --profile dev exec $user dev "$@"
12+
else
13+
USER_ID=$(id -u) GROUP_ID=$(id -g) docker-compose --profile dev run --rm $user --service-ports dev "$@"
14+
fi

0 commit comments

Comments
 (0)