So our websites we have composer set,
And after it has got the packages we run some commands.
"scripts": {
"post-install-cmd": [
"php -r \"shell_exec('cp -rf sourcefiles/. source/.');\"",
"php -r \"shell_exec('rm -rf sourcefiles');\""
]
}
These run find with version 2016-02-24_11-44-07-45f6b37
But when I run the self updater to get the latest version it brakes the code and no longer works, even though nothing has changed.
This is a cause for concern as it means we can no longer update our composer. Any idea why is has broke are we doing something wrong?
Many Thanks.
Someone on github told the the problem,
So only run the script on "post-install-cmd" this works pre 1.0 now you need to run the scripts on "post-install-cmd" and "post-update-cmd" if a lock file exists it runs the update command.
So the code now looks like this:
"scripts": {
"post-install-cmd": [
"cp -rf sourcefiles/. source/.",
"rm -rf sourcefiles"
],
"post-update-cmd": [
"cp -rf sourcefiles/. source/.",
"rm -rf sourcefiles"
]
}
Thank you for helping remove the PHP dependency
The funny thing about your scripts is: The real shell command is cp -rf sourcefiles/. source/., and you are wrapping this into a call to the PHP function shell_exec(), and because this function cannot be called without the help of PHP, you pass this source code to PHP on the shell.
Alternative script suggestion:
"scripts": {
"post-install-cmd": [
"cp -rf sourcefiles/. source/.",
"rm -rf sourcefiles"
]
}
However, you have to provide more details on the error, any message you get etc.
Related
I'm trying to do make a cloud function reload and get invoked on any source file change.
Here is how I try to achieve this in package.json:
"scripts": {
"dev": "cd dist && functions-framework --target=helloGET & (sleep 2 && curl http://localhost:8080 && exit -0)",
"start": "rm -rf dist && tsc-watch --onSuccess 'yarn run dev'"
},
The problem is that functions-framework with & (sleep... the functions-framework keeps running in background instead of getting terminated. How do I fix this?
I am creating a bash script which runs through each of my projects and runs npm run test if the test script exists.
I know that if I get into a project and run npm run it will give me the list of available scripts as follows:
Lifecycle scripts included in www:
start
node server.js
test
mocha --require #babel/register --require dotenv/config --watch-extensions js **/*.test.js
available via `npm run-script`:
dev
node -r dotenv/config server.js
dev:watch
nodemon -r dotenv/config server.js
build
next build
However, I have no idea how to grab that information, see if test is available and then run it.
Here is my current code:
#!/bin/bash
ROOT_PATH="$(cd "$(dirname "$0")" && pwd)"
BASE_PATH="${ROOT_PATH}/../.."
while read MYAPP; do # reads from a list of projects
PROJECT="${MYAPP}"
FOLDER="${BASE_PATH}/${PROJECT}"
cd "$FOLDER"
if [ check here if the command exists ]; then
npm run test
echo ""
fi
done < "${ROOT_PATH}/../assets/apps-manifest"
EDIT:
As mentioned by Marie and James if you only want to run the command if it exists, npm has an option for that:
npm run test --if-present
This way you can have a generic script that work with multiple projects (that may or may not have an specific task) without having the risk of receiving an error.
Source: https://docs.npmjs.com/cli/run-script
EDIT
You could do a grep to check for the word test:
npm run | grep -q test
this return true if the result in npm run contains the word test
In your script it would look like this:
#!/bin/bash
ROOT_PATH="$(cd "$(dirname "$0")" && pwd)"
BASE_PATH="${ROOT_PATH}/../.."
while read MYAPP; do # reads from a list of projects
PROJECT="${MYAPP}"
FOLDER="${BASE_PATH}/${PROJECT}"
cd "$FOLDER"
if npm run | grep -q test; then
npm run test
echo ""
fi
done < "${ROOT_PATH}/../assets/apps-manifest"
It just would be a problem if the word test is in there with another meaning
Hope it helps
The right solution is using the if-present flag:
npm run test --if-present
--if-present doesn't allow you to "check if a npm script exists", but runs the script if it exists. If you have fallback logic this won't suffice. In my case, I want to run npm run test:ci if it exists and if not check for and run, npm run test. Using --if-present would run the test:ci AND test scripts if both exists. By checking if one exists first, we can decide which to run.
Because I have both "test" and "test:ci" scripts, the npm run | grep approach wasn't sufficient. As much as I'd like to do this with strictly npm, I have jq in my environments so I decided to go that route to have precision.
To check for a script named "test:ci":
if [[ $(jq '.scripts["test:ci"]' < package.json;) != null ]]; then
// script exists
fi
How do I pass a parameter? When I run "yarn generate" it will make both a "-p" and a "test" directory. But it works well when I run "mkdir -p test" in bash. I tried to [-p] as well but it only creates that directory.
"scripts": {
"generate": "mkdir -p test"
}
Although I could not reproduce the issue that you mentioned (my config: node v8.11.1 and yarn v1.2.1, latest MacOS), according to the yarn docs, you can pass the arguments to yarn script by appending them normally, like so:
yarn generate -p test
In this case your npm (yarn) scripts config (in the package.json, I assume) would look like
"scripts": {
"generate": "mkdir"
}
If you're using Windows, you indeed won't have the mkdir -p flag (read this). In order to make what you want (check if the folder does not exist and if so, create one) you'd need to use some cmd commands. So your package.json will contain smth like
"scripts": {
"generate": "IF NOT EXIST test mkdir test"
}
Ok I'm starting to lose my mind here. When I deploy my app to elastic beanstalk I get this error:
[2017-12-15 17:50:18] Tylercd100\LERN.CRITICAL: RuntimeException was thrown! The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.
To be clear I deploy my app source without dependencies installed and with APP_KEY not set, I'm leaving the dependency installation to elastic beanstalk which installs them during deployment.
In my aws .config file I have defined deployment commands as follows:
---
commands:
00init:
command: "sudo yum install gcc-c++"
01init:
command: "rm -f amazon-elasticache-cluster-client.so"
02init:
command: "wget https://s3.amazonaws.com/php-amazon-elasticache-cluster-client-7-1/amazon-elasticache-cluster-client.so"
03init:
command: "sudo mv amazon-elasticache-cluster-client.so /usr/lib64/php/7.1/modules/"
04init:
command: "echo \"extension=amazon-elasticache-cluster-client.so\" | sudo tee /etc/php-7.1.d/50-memcached.ini"
05init:
command: "sudo /etc/init.d/httpd restart"
container_commands:
00permissions:
command: "find * -type d -print0 | xargs -0 chmod 0755"
01permissions:
command: "find . -type f -print0 | xargs -0 chmod 0644"
02permissions:
command: "chmod -R 775 storage bootstrap/cache"
03cache:
command: "php artisan cache:clear"
04key:
command: "php artisan key:generate"
05cache:
command: "php artisan config:cache"
06cache:
command: "php artisan route:cache"
07optimize:
command: "php artisan optimize"
These commands are running during deployment to aws without any error.
When I go and check .env directly on the virtual machine the APP_KEY is set as it should be considering the commands above.
Yet I get the cipher error.
Assuming you set APP_KEY in elasticbeanstalk configuration page in dashboard, there are two things that I would like to point out.
1- When php artisan config:cache is run in container_commands, it caches file paths as /var/app/ondeck/... This causes runtime errors while laravel trying to access the cached files.
2- Cipher error occurs when laravel cannot access the APP_KEY value from your .env file. If a line like APP_KEY=${APP_KEY} exists in your .env file, that is the main cause of the error. You assume that APP_KEY value is going to be read from the environment configuration made in the dashboard. However, environment variables have not been set by the beanstalk yet somehow when your commands or container_commands are running. You can solve this issue my sourcing environment variables by yourself by including below command in your commands or files.
source /opt/elasticbeanstalk/support/envvars
e.g.
"/opt/elasticbeanstalk/hooks/appdeploy/post/91_config_cache.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
source /opt/elasticbeanstalk/support/envvars
echo "Running php artisan config:cache"
cd /var/app/current
php artisan config:cache
echo "Finished php artisan config:cache"
I wrote the script in package.json
"scripts": {
"build": ". ./envsetup.sh | ./build"
}
when in envsetup.sh script I set variables and I want to share them in build script.
If I run it by npm run build I see KeyError the variable does not exist.
But if I run this script in console by 2 commands:
. ./envsetup.sh
./build
the script is successful.
You can't use a pipe here. This should work though:
"scripts": {
"build": ". ./envsetup.sh && ./build"
}