How do I self-invoke cloud function while developing locally? - bash

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?

Related

How to replace text in file with npm run scripts in package.json Windows OS

I developed chrome extension using Nextjs and My develop environment is Windows.
But I have issue when build and export the app and load it on extension because cannot load extension with file or directory name _next.
So need to move the _next file to 'assets' file and replace '_next' to 'assets' in index.html.
I installed sed-4.2.1 and added these lines in "scripts" of package.json
"scripts": {
"build": "next build && next export && npm run prepare",
"prepare": "move out/_next out/assets && sed -i 's/\\/_next/\\/assets/g' out/index.html"}
But 'sed' not worked with this error: sed: -e expression #1, char 1: unknown command: `''
So, I tried to run 'perl -p -i -e's/_next/assets/g' out/index.html' on cmd and it worked well.
But I added line in scripts, it has not worked.
"scripts": {
"build": "next build && next export && npm run prepare",
"prepare": "move out/_next out/assets && perl -p -i -e's/_next/assets/g' out/index.html"}
What's the reason?
And what is the best way to replace text???

Iterating over directories containing a string shell script

I have a set of directories, 10 at the moment that are named client-1, client-2,..., client-10 and 1 directory that is named nestjs-wrapper
I want to iterate over the client directories, enter each of them and fire npm install and node index.js in every one.
I could do it by hand, but the number of clients may increment in the future so I would like to automate this process.
So the flow would be something like this:
in the parent directory I would like to fire nvm use to make sure I have the desired node version
then cd into each directory, fire npm install & node index.js
cd back to parent directory
repeat this until packages are installed in every client directory
run docker-compose up in a detached terminal
cd from parent directory into a nestjs-wrapper and start it in watch mode with npm run start:dev
This is the start of the attempt, it installs the packages in the client directories, now I would somehow need to do the rest of the flow:
pattern="/home/dario/my-folder/client"
for _dir in "${pattern}-"[[:digit:]]*; do
[ -d "$_dir" ] || continue;
pushd "$_dir" && npm install;
done
I would like to start docker-compose from the parent directory in a detached terminal.
To do this, I just created a new script named start-docker.sh in which I only have docker-compose up.
And after that open a separate dir in the parent directory (one that is not named client-) and run npm run start:dev in it.
So it would go something like:
pattern="client"
for _dir in "${pattern}-"[[:digit:]]*; do
[ -d "$_dir" ] || continue;
pushd "$_dir" && npm install && node index.js;
popd;
done
gnome-terminal -- ./start-docker.sh;
pushd nestjs_wrapper && npm run start:dev;
This does the trick, I switched back to relative pathnames. First I iterate over all the client directories and install the packages, then after that I bring up docker-compose and start the wrapper in watch mode.
Following the input from the comments, here is the working solution:
pattern="client"
for _dir in "${pattern}-"[[:digit:]]*; do
[ -d "$_dir" ] || continue;
pushd "$_dir" && npm install && node index.js;
popd;
done
gnome-terminal -- ./start-docker.sh;
pushd nestjs_wrapper && npm run start:dev;

yarn run script with parameters

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"
}

How can I force cancel dockerbuild using bash a command?

The reason why I'm asking is because we're using AWS codebuild & I need to do DB migrations. If a DB migration breaks, I want to cancel the codebuild & rollback the migration which was just made. I've got this part working, all I need to do now is cancel the dockerbuild mid way. How can I do this?
This is my .sh file with the knex migration commands:
#!/bin/bash
echo "running"
function mytest {
"$#"
local status=$?
if [ $status -ne 0 ]; then
knex migrate:rollback
echo "Rolling back knex migrate $1" >&2
exit
fi
return $status
}
mytest knex migrate:latest
Running exit will not cancel/break the docker build.
My Dockerfile (just incase):
FROM node:6.2.0
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY . /usr/src/app
RUN chmod +x /usr/src/app/migrate.sh
RUN /usr/src/app/migrate.sh
EXPOSE 8080
CMD npm run build && npm start
Running exit will not cancel/break the docker build.
running exit 1 should
Docker should respond to the error codes returned by the RUN shell scripts in said Dockerfile.

Source and npm sripts

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"
}

Resources