diff --git a/bin/drun.sh b/bin/drun.sh new file mode 100644 index 0000000..d08357d --- /dev/null +++ b/bin/drun.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +DOCKER_CONTAINERS=`docker ps -a | grep portfolio_dev | wc -l` + +if [ "$DOCKER_CONTAINERS" -ge "1" ] +then + echo "Running in docker... $@" + docker-compose run --rm dev $@ +else + eval "$@" +fi diff --git a/bin/pre-commit.sh b/bin/pre-commit.sh index 38b785d..ce77e0b 100644 --- a/bin/pre-commit.sh +++ b/bin/pre-commit.sh @@ -2,8 +2,8 @@ docker-compose up --detach test-web -nodejs node_modules/lerna/bin/lerna.js run pre-commit \ - && npm run check_code_format +sh ./bin/drun.sh lerna run pre-commit \ + && npm run check_code_format EXIT=$? docker-compose stop test-web diff --git a/bin/runTests.js b/bin/runTests.js index cb1f3f5..d450bd8 100755 --- a/bin/runTests.js +++ b/bin/runTests.js @@ -32,7 +32,7 @@ const testPath = path.join(packageRoot, TEST_FILENAME); const webRoot = path.relative(PORTFOLIO_DIR, testPath); const items = fs.readdirSync(packageRoot); -let chrome, protocol; +let chrome, protocol, chromeIsLocal; writeSpecRunner(); runTestsInChrome(); @@ -85,12 +85,24 @@ function writeSpecRunner() { } async function runTestsInChrome() { - chrome = await launchChrome(); - protocol = await CDP({ - port: chrome.port - }); + console.log(`Starting Chrome`); + try { + chrome = await launchChrome(); + protocol = await CDP({ port }); + chromeIsLocal = true; + } catch(e) { + console.log(`Failed to start Chrome, accessing local on port 9222`); + // We were not able to launch Chrome. Assume we're in a docker container. + try { + protocol = await CDP({ host: 'host.docker.internal', port: 9222 }); + chromeIsLocal = false; + } catch (f) { + console.log(`Error: ${f}`); + return; + } + } - const { DOM, Network, Page, Emulation, Runtime, Console, Debugger } = protocol; + const { DOM, Network, Page, Runtime, Console, Debugger } = protocol; await Promise.all([ Network.enable(), @@ -100,6 +112,7 @@ async function runTestsInChrome() { Console.enable(), DEBUG ? Debugger.enable() : Promise.resolve() ]); + console.log(`Running tests at http://${HOST}:${PORT}/${webRoot}`); await Page.navigate({ url: `http://${HOST}:${PORT}/${webRoot}` }); let indentation = 0; @@ -195,8 +208,10 @@ function exit(code) { if (DEBUG) { return; } - fs.unlink(testPath); + fs.unlinkSync(testPath); protocol.close(); - chrome.kill(); + if (chromeIsLocal) { + chrome.kill(); + } process.exit(code); } diff --git a/docker-compose.yml b/docker-compose.yml index 48a49de..7d5a8d1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,14 @@ services: - 8090:80 volumes: - ./:/usr/share/nginx/html:ro + dev: + build: + context: ./docker + dockerfile: dev.dockerfile + volumes: + - ./:/workspace/ + working_dir: /workspace + entrypoint: /docker-entrypoint.sh cloud9: build: context: ./docker diff --git a/docker/dev.dockerfile b/docker/dev.dockerfile new file mode 100644 index 0000000..6e3ca96 --- /dev/null +++ b/docker/dev.dockerfile @@ -0,0 +1,18 @@ +FROM node:slim + +COPY docker-entrypoint.sh / +RUN chmod 755 /docker-entrypoint.sh + +RUN buildDeps='g++ make python' && softDeps="git iproute2 iputils-ping" \ + && apt update && apt upgrade -y \ + && apt install -y $buildDeps $softDeps --no-install-recommends \ + && npm install -g lerna \ + && apt-get purge -y --auto-remove $buildDeps \ + && apt-get autoremove -y && apt-get autoclean -y && apt-get clean -y \ + && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \ + && npm cache clean --force + +VOLUME /workspace + +ENTRYPOINT /docker-entrypoint.sh +CMD /bin/bash diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh new file mode 100755 index 0000000..15fe8b2 --- /dev/null +++ b/docker/docker-entrypoint.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +# This entrypoint script is designed to populate "host.docker.internal" in /etc/hosts on startup until https://github.com/docker/for-linux/issues/264 is fixed and released. + +HOST_DOMAIN="host.docker.internal" +grep $HOST_DOMAIN /etc/hosts && ping -q -c1 $HOST_DOMAIN +if [ $? -ne 0 ]; then + HOST_IP=$(ip route | awk 'NR==1 {print $3}') + echo "$HOST_IP\t$HOST_DOMAIN" >> /etc/hosts + echo "Added $HOST_IP $HOST_DOMAIN to /etc/hosts" +fi + +echo Running command "$@" +eval "$@" diff --git a/package.json b/package.json index e0b07a4..947dd34 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,16 @@ { "devDependencies": { "chrome-launcher": "^0.10.2", - "chrome-remote-interface": "^0.25.5", + "chrome-remote-interface": "0.26.1", "husky": "1.0.0-rc.13", "lerna": "3.1.4", "prettier": "1.14.2" }, "scripts": { - "check_code_format": "nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --list-different \"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\"", - "format_code": "nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --write \"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\"", - "test": "sh ./bin/pre-commit.sh" + "check_code_format": "sh ./bin/drun.sh \"nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --list-different \\\"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\\\"\"", + "format_code": "sh ./bin/drun.sh \"nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --write \\\"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\\\"\"", + "test": "sh ./bin/pre-commit.sh", + "bootstrap": "sh ./bin/drun.sh lerna bootstrap" }, "husky": { "hooks": {