bash 在特定目录中运行 `ng build`

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50046729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:54:27  来源:igfitidea点击:

Running `ng build` in a specific directory

bashangular-cli

提问by JGrindal

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

我正在编写一个需要ng build在特定目录中运行的自动构建脚本。我的项目具有以下结构:

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

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

ng build正在寻找 package.json 文件,但在当前位置找不到它。有没有一种方法可以直接ng build到子目录而不做cd(因为 bash 改变目录有点不稳定)?本质上,我只需要一种方法来告诉ng build在我指定的目录中进行操作。

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"

我试过这样做,ng build ./project-client/但它给出了“你必须在一个 angular-cli 项目中才能使用构建命令”的错误

Thanks.

谢谢。

回答by dmadic

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

当我需要去某个目录只是为了运行一个脚本时,我通常使用pushdpopd. 例子:

pushd /path/to/dir
ng build
popd

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

运行此代码段后,您的工作目录 ( pwd) 将保持不变。

回答by Hiran D.A Walawage

This is what I did.

这就是我所做的。

pushd angular-src && ng build && popd

回答by Burhan

Note: This answer is specific to Angular commands

注意:此答案特定于 Angular 命令

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

您可以利用该npm run命令运行 Angular 命令,同时使用参数--prefix指示子目录。



Example 1: Original question (ng build)

示例 1:原始问题 ( ng build)

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

由于 Angular 已经有这个命令的快捷方式,你可以简单地运行

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


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

实施例2:自定义命令(例如:ng build --prod

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

首先,在您的package.json文件中为此命令创建快捷方式

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

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

然后,以与示例 1 相同的方式运行新的快捷方式:

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