Skip to content

Commit

Permalink
It works
Browse files Browse the repository at this point in the history
Even with rimrafall
  • Loading branch information
DavidBruant committed Feb 28, 2018
1 parent d9b6bc2 commit 13de7da
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
5 changes: 0 additions & 5 deletions bin/contained-run-script-sh

This file was deleted.

39 changes: 39 additions & 0 deletions bin/contained-run-script-sh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

const { spawnSync } = require('child_process');
const { relative, join } = require('path');

const cwd = process.cwd();

const hostProjectRoot = process.env.INIT_CWD; // TODO maybe throw an `npm prefix` in addition
const guestProjectRoot = '/home/node-app'

const GUEST_CWD = join(guestProjectRoot, relative(hostProjectRoot, cwd))

console.log(cwd)
console.log(hostProjectRoot)
console.log(guestProjectRoot)
console.log(relative(hostProjectRoot, cwd))
console.log(GUEST_CWD)

// TODO : if process.argv.slice(2)[1].trim().startsWith('npm '), then execute the command without docker-compose

spawnSync(
'docker-compose',
[
'-f', '/home/david/projects/containednpm/contained-services.yml',
'run', 'contained_npm_script',
'sh'
].concat(process.argv.slice(2)),
{
stdio: 'inherit',
env: Object.assign(
{},
process.env, // way too many things here. TODO : clean that up
{
GUEST_CWD,
HOST_CWD: cwd
}
)
}
)
18 changes: 4 additions & 14 deletions contained-services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@ version: '3'
services:
contained_npm_script:
build: .
working_dir: /home/node-app
working_dir: "${GUEST_CWD}"
# Passing the current user id as user id in the container
user: "${USER_ID}"
volumes:
# Current directory as working_dir
- ~/projects/containednpm/project-alpha:/home/node-app
- ~/projects/containednpm/project-alpha/node_modules:/home/node-app/node_modules
- "${INIT_CWD}:/home/node-app:ro"
- "${HOST_CWD}:${GUEST_CWD}" # allowing the package to writing in its own directory

# Passing node binary
- /usr/bin/node:/usr/bin/node:ro
# Apparently, node needs this file to start ($HOME=/ in the container)

###
# npm is run with $PWD = ./project-alpha in host
# contained-run-script-sh is run with ./project-alpha:/home/node-app
# and $PWD = ./project-alpha/node_modules/worm
# node infect.js is run inside the container with $PWD = /home/node-app
#
# What would be needed is a way to forward the part of contained-run-script-sh::$PWD that is after the thing that matches /home/node-app, that is running node infect.js with $PWD = /home/node-app/node_modules/worm (= contained-run-script-sh::$PWD - what /home/node-app is mapped to (~/projects/containednpm/project-alpha))
# Maybe `npm prefix can help`
- /usr/bin/node:/usr/bin/node:ro
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cd containednpm
# (optional but recommanded) builds the image a first time and make sure it runs properly
docker-compose -f contained-services.yml run contained_npm_script echo 'success'

npm config set script-shell "$PWD"/bin/contained-run-script-sh
npm config set script-shell "$PWD"/bin/contained-run-script-sh.js
````


Expand All @@ -48,7 +48,7 @@ cat package.json
cd ..
git checkout project-alpha

npm config set script-shell "$PWD"/bin/contained-run-script-sh
npm config set script-shell "$PWD"/bin/contained-run-script-sh.js

cd project-alpha
ls -l node_modules
Expand Down

0 comments on commit 13de7da

Please sign in to comment.