how to write cd NAME_FOLDER and recursively search for it by name and then go to it if necessary (see below for more details).
the NAME_OF_FOLDER is unique so no worried about that.
but the challenging things are:
- that it needs to search from the PARENT to the CHILD,
- and if it didn't find it that way, search from CHILD to PARENT.
(or you can use any other logic if you think my logic is slow)
example folder
here is an example image of my folder:
possible scenarios
here are some scenarios:
- if I am inside
./package
-> don't runcd
./test
->cd ./package
./src
->cd ../ && cd ./package
./lib
->cd ../../ && cd ./package
and so on for every deep folder structure
docs:
../
means go from child to parent
./
means go from parent to child
why do I need it?
I am a javascript developer,
and I am using the sveltekit framework
to create a svelte package library.
and I need to publish that library to npm.
and this is ok.
but since I write a lot the same CLI codes.
I am changing the package.json
's scripts
object,
so I write only one time npm run build
to run 6 commands
{
...
"build": "
svelte-kit sync
&& svelte-package
&& npm version patch
&& cd ./package # only this I need to solve this (the others are solved)
&& npm publish
&& git commit
"
}
this is in one line, but for making you read it easily the code in multiple lines
here how it is in my code:
what does the build
command should do?
- the command generates a
./package
folder always on the root of the folder
(where we can findpackage.json
,.gitignore
,./src
, etc...) - increase the number of versions automatically when we use the build command,
then... TODO:
- do the script I need to access the
./package
folder from every folder I am in now. (likecd ./package
) npm publish
my os?
windows 11 (but using bash with vscode) or also powershell will be good but prefer bash
any other details, I will tell you. thanks
CodePudding user response:
For testing, I created this structure:
test/
package/
src/
lib/
routes/
Then I created that script:
#!/bin/bash
topdir="test"
while [[ $(basename "$(pwd)") != "$topdir" ]]
do
if [[ -d package ]]
then
cd package
pwd
else
cd ..
pwd
fi
done
if [[ -d package ]]
then
cd package
pwd
fi
This script "climbs" the directories until it finds a "package" directory. It the cd
into it.
To use the script, you have to source it. If you execute it, it will change directories while the script is running, but it will not affect your current terminal.
So, lets assume the script is ~/bin/cd_package.bash
You would call it like this: . ~/bin/cd_package.bash
Note the pwd
commands are just so you can follow what is going on and can be removed once you are convinced it works.