Is there a tool for changing the folder context of specific commands?

This isn’t an elm-specific question, but it’s one that comes up while working with elm a lot for me.

Are there any tools that let you create a config file or something in a folder that makes it so that certain commands, if run from that folder, are automatically run in a different folder context?

To illustrate: I have a Phoenix app that uses Elm, and when I run, for example, npm install, elm-json, elm commands, etc, etc, I have to be careful to remember to cd into the /assets folder where my elm.json, package.json, etc, stuff is, and then remember to cd back out again to do anything else with the app. This results in a lot of frustrating “Oh I forgot where I was and now I have to go delete the random files this command just generated in the wrong place” moments. Using npm --prefix assets is helpful for npm commands, but what I’d really like is a tool where I could write a configuration file that looks something like, say, this

npm: ./assets
elm: ./assets
elm-json: ./assets

and then put it in my project root, and then when I run any of those commands from the root, it automatically runs them as though they’re being run from the specified folder context (e.g. the assets folder). Seems like this must be a common frustration, but it’s a hard type of tool to google for, so I figured I’d ask if anyone knows of something like this, or similar things, or ways around this that I haven’t thought of?

I use Make or Just (GitHub - casey/just: 🤖 Just a command runner)

Then have:

build-assets:
   cd assets && npm run something
3 Likes

It’s not perfect, but I often find myself running elm make src/Main --output /dev/null && (cd generator && elm make src/Main.elm --output /dev/null). It feels a little more declarative because I don’t have to remember to go back to where I started before I started the subshell (i.e. the parens). And there’s a visual indicator that it’s running in different context.

It is awkward figuring out where to set the boundaries between scripts for different sub-projects in a monorepo. Maybe there’s a nice tool to help with that, I’m not sure if lerna or other monorepo tools help at all.

1 Like

I moved the package.json and elm.json in the root folder. In the root of the project I have the src folder for the elm code and the lib folder for elixir code. I still keep the JS and SCSS files in the assets folder. All the commands I use run in the root of the project.

1 Like

You can do that easily with direnv. E.g. put .envrc in the root folder and setup aliases.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.