Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow arguments to be passed to npm scripts #356

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

oltionchampari
Copy link
Contributor

@oltionchampari oltionchampari commented Apr 16, 2020

Closes #354

Developer Checklist (Definition of Done)

  • Descriptive title for this pull request provided (will be used for release notes later)
  • All acceptance criteria from the issue are met
  • Branch is up-to-date with the branch to be merged with, i.e., develop
  • Code is cleaned up and formatted
  • Code documentation written
  • Unit tests written
  • Build is successful
  • Summary of changes written
  • Wiki documentation written
  • Assign at least one reviewer
  • Assign at least one assignee
  • Add type label (e.g., bug, enhancement) to this pull request
  • Add next version label (e.g., release: minor) to this PR following semver

Summary of changes

To test

  • Add a script to a plugin i.e., "myscript":"git"
  • yo phovea:update in workspace
  • npm run myscript:<plugin> -- -b my_branch

@oltionchampari oltionchampari added type: feature New feature or request release: minor PR merge results in a new minor version labels Apr 16, 2020
@oltionchampari oltionchampari requested a review from a user April 16, 2020 13:37
@oltionchampari oltionchampari assigned ghost Apr 16, 2020
@dvvanessastoiber dvvanessastoiber mentioned this pull request Apr 20, 2020
21 tasks
Copy link
Contributor

@thinkh thinkh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test the withinEnv case again and post the results here. Thanks.

@@ -265,7 +265,7 @@ class Generator extends Base {
// no pre post test tasks
cmds.filter((s) => toPatch.test(s)).forEach((s) => {
// generate scoped tasks
let cmd = `.${path.sep}withinEnv "exec 'cd ${p} && npm run ${s}'"`;
let cmd = `.${path.sep}withinEnv "exec 'cd ${p} && npm run ${s}'" --`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let cmd = `.${path.sep}withinEnv "exec 'cd ${p} && npm run ${s}'" --`;
let cmd = `.${path.sep}withinEnv "exec 'cd ${p} && npm run ${s} --'"`;

@oltionchampari Did you test this case? @anita-steiner and I assume that the -- must be placed directly after the run command (as in the other change above). It could even be that this does not work at all, as you want to pass arguments through multiple layers (e.g., withinEnv and exec).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running npm run check:phovea_server or any other withinEnv command from the workspace results in an error.

withinEnv_test

I do not know if there is a problem with my docker setup or the withinEnv script is outdated.
@thinkh Could you please test if the commands work for you. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. This whole command does not work...

Investigation report

  • (I had to switch the line ending for withinEnv from CRLF to LF, which might be caused by my Windows)
  • I can confirm your results; I get the same output
  • Executing the command from withinEnv manually (docker-compose run --service-ports api) starts the container
  • docker-compose run --service-ports api exec results in Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"exec\": executable file not found in $PATH": unknown
  • docker-compose run --service-ports api /bin/sh results in a terminal inside the container
    • However, npm -v fails with /bin/sh: 2: npm: not found, as npm is not part of the python:3.7 base image!
  • docker-compose run --service-ports api /bin/sh -c "cd phovea_server && npm run test" should be the correct command, but it also fails with /bin/sh: 1: npm: not found (see previous point)
  • Replacing docker-compose run --service-ports api with withinEnv (command: ./withinEnv /bin/sh -c "cd phovea_server && echo test") prints docker-compose run --service-ports api /bin/sh -c cd phovea_server && echo test, i.e., without additional quotes and results in an empty terminal output
  • Escaping the command ./withinEnv "/bin/sh -c \"cd phovea_server && echo test\"" results in docker-compose run --service-ports api /bin/sh -c "cd phovea_server && echo test" and prints phovea_server: 1: phovea_server: Syntax error: Unterminated quoted string
    • Since the print statement itself works, but the execution itself fails it must be a problem with the $@
    • Reading the documentation about $@ it seems that it is not possible to pass strings from incoming parameter to the subsequent command
    • An alternative syntax to -c is the <<< syntax (see https://stackoverflow.com/a/40487106)
  • ./withinEnv /bin/bash <<< "cd phovea_server && echo test" results in the expected output test
    • Note that the whole bin/sh command is not wrapped in single quotes so that $@ can expand it as different paramters and pass it down to docker-compose
  • Copying the new command to package.json ("test:phovea_server": "./withinEnv /bin/bash <<< \"cd phovea_server && npm run test\"") and executing npm run test:phovea_server results in the following error: sh: 1: Syntax error: redirection unexpected
    • I found no obvious way how to fix this syntax in the npm script
    • The only solution I found is to move the redirection into the withinEnv: docker-compose run --service-ports api /bin/bash <<< $@
    • Change the npm script to ./withinEnv \"cd phovea_server && npm run test\"
  • Running npm run test:phovea_server results in the expected /bin/sh: 1: npm: not found
  • Now only the npm script npm run start:phovea_server does not work anymore and needs to be changed to "start:phovea_server": "./withinEnv \"cd phovea_server && npm run start\"",
    • Alternatively we can use "start:phovea_server": "./withinEnv \"python phovea_server\"", to start the phovea server

oltionchampari added a commit that referenced this pull request May 6, 2020
Copy link
Contributor

@thinkh thinkh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See investigation report for the full list of commands I tried.

TL;DR

Long story short... this command will never work, since npm is not part of the python:3.7 base image.

Beside that fact the exec call needs to be removed and the quotes needs to be changed from the npm scripts:

    "start:phovea_server": "./withinEnv \"cd phovea_server && npm run start\"",
    "check:phovea_server": "./withinEnv \"cd phovea_server && npm run check\"",
    "test:phovea_server": "./withinEnv \"cd phovea_server && npm run test\"",
    "dist:phovea_server": "./withinEnv \"cd phovea_server && npm run dist\""

Last the withinEnv needs to be adapted by the /bin/sh <<< call:

#!/usr/bin/env bash
set -e

echo "docker-compose run --service-ports api /bin/bash <<< $@"
docker-compose run --service-ports api /bin/bash <<< $@

Finally, I'm not really sure, if we need the -- at all since all parameters are passed $@ to the bash. I couldn't test it with the npm script either since npm is not installed.

@@ -265,7 +265,7 @@ class Generator extends Base {
// no pre post test tasks
cmds.filter((s) => toPatch.test(s)).forEach((s) => {
// generate scoped tasks
let cmd = `.${path.sep}withinEnv "exec 'cd ${p} && npm run ${s}'"`;
let cmd = `.${path.sep}withinEnv "exec 'cd ${p} && npm run ${s}'" --`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. This whole command does not work...

Investigation report

  • (I had to switch the line ending for withinEnv from CRLF to LF, which might be caused by my Windows)
  • I can confirm your results; I get the same output
  • Executing the command from withinEnv manually (docker-compose run --service-ports api) starts the container
  • docker-compose run --service-ports api exec results in Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"exec\": executable file not found in $PATH": unknown
  • docker-compose run --service-ports api /bin/sh results in a terminal inside the container
    • However, npm -v fails with /bin/sh: 2: npm: not found, as npm is not part of the python:3.7 base image!
  • docker-compose run --service-ports api /bin/sh -c "cd phovea_server && npm run test" should be the correct command, but it also fails with /bin/sh: 1: npm: not found (see previous point)
  • Replacing docker-compose run --service-ports api with withinEnv (command: ./withinEnv /bin/sh -c "cd phovea_server && echo test") prints docker-compose run --service-ports api /bin/sh -c cd phovea_server && echo test, i.e., without additional quotes and results in an empty terminal output
  • Escaping the command ./withinEnv "/bin/sh -c \"cd phovea_server && echo test\"" results in docker-compose run --service-ports api /bin/sh -c "cd phovea_server && echo test" and prints phovea_server: 1: phovea_server: Syntax error: Unterminated quoted string
    • Since the print statement itself works, but the execution itself fails it must be a problem with the $@
    • Reading the documentation about $@ it seems that it is not possible to pass strings from incoming parameter to the subsequent command
    • An alternative syntax to -c is the <<< syntax (see https://stackoverflow.com/a/40487106)
  • ./withinEnv /bin/bash <<< "cd phovea_server && echo test" results in the expected output test
    • Note that the whole bin/sh command is not wrapped in single quotes so that $@ can expand it as different paramters and pass it down to docker-compose
  • Copying the new command to package.json ("test:phovea_server": "./withinEnv /bin/bash <<< \"cd phovea_server && npm run test\"") and executing npm run test:phovea_server results in the following error: sh: 1: Syntax error: redirection unexpected
    • I found no obvious way how to fix this syntax in the npm script
    • The only solution I found is to move the redirection into the withinEnv: docker-compose run --service-ports api /bin/bash <<< $@
    • Change the npm script to ./withinEnv \"cd phovea_server && npm run test\"
  • Running npm run test:phovea_server results in the expected /bin/sh: 1: npm: not found
  • Now only the npm script npm run start:phovea_server does not work anymore and needs to be changed to "start:phovea_server": "./withinEnv \"cd phovea_server && npm run start\"",
    • Alternatively we can use "start:phovea_server": "./withinEnv \"python phovea_server\"", to start the phovea server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: minor PR merge results in a new minor version type: feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants