15

I'm writing an automated build script that needs to run ng build in a specific directory. My project has the following structure:

project
├ build-script
└ project-client
  └ package.json

ng build is looking for the package.json file, but it can't find it in its current location. Is there a way I can direct ng build to the subdirectory without doing a cd (since bash is a bit wonky about changing directories)? Essentially, I just need a way to tell ng build to operate in a directory that I specify.

I have tried doing ng build ./project-client/ but it is giving the error that "You have to be inside an angular-cli project in order to use the build command"

Thanks.

JGrindal
  • 793
  • 2
  • 8
  • 23
  • Possible duplicate of [How can Bash execute a command in a different directory context?](https://stackoverflow.com/questions/10566532/how-can-bash-execute-a-command-in-a-different-directory-context) – 4castle Apr 26 '18 at 15:28
  • @4castle Specifying the directory in that way did not work. I'm hoping there's something similar to how `--prefix` works for `npm` – JGrindal Apr 26 '18 at 15:34
  • The documentation for [`ng build`](https://github.com/angular/angular-cli/wiki/build) makes no mention of such a flag, so I don't think it exists. Could you explain in more detail how it didn't work? A [mcve] would be appreciated. – 4castle Apr 26 '18 at 15:40
  • @4castle As noted in the question, it's giving the error "You have to be inside an angular-cli project in order to use the build command" – JGrindal Apr 26 '18 at 15:42
  • I meant the error that you get when you use `cd`. – 4castle Apr 26 '18 at 15:43
  • `cd` within a bash script doesn't actually change the directory for the rest of the script, only the subshell that the command runs in. See https://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-shell-script . The workaround would be adding an alias to .bashrc, which I would like to avoid since the relative path to the project is subject to change. – JGrindal Apr 26 '18 at 15:44
  • 1
    Did you try creating a subshell like in [this answer](https://stackoverflow.com/a/10566575/5743988) with `(cd project-client; ng build)`? – 4castle Apr 26 '18 at 15:47
  • That worked. Thanks! – JGrindal Apr 26 '18 at 15:51

3 Answers3

28

When I need to go to certain directory just to run a script I usually use pushd and popd. Example:

pushd /path/to/dir
ng build
popd

After this snippet is run your working directory (pwd) will remain unchanged.

dmadic
  • 477
  • 4
  • 7
12

Note: This answer is specific to Angular commands

You can leverage the npm run command to run an Angular command while using the parameter --prefix to indicate the sub-directory.


Example 1: Original question (ng build)

Since Angular already has a shortcut for this command, you can simply run

npm run build --prefix path\to\Angular\project

Example 2: Custom command (Eg: ng build --prod)

First, create a shortcut for this command in your package.json file

{
    "scripts": {
        // "CMD-SHORTCUT": "custom-command"
        "prod-build": "ng build --prod"
    },
}

Then, run the new shortcut in the same way as example 1:

npm run prod-build --prefix path\to\Angular\project
Burhan
  • 668
  • 5
  • 27
7

This is what I did.

pushd angular-src && ng build && popd
Hiran Walawage
  • 2,048
  • 1
  • 22
  • 20