asp.net-core - 从 netcore 部署脚本在子目录中执行 npm 命令
问题描述
我有 netcore webAPI 项目,其中带有 package.json 和 angular CLI 的前端不位于根文件夹中。在部署之前,我需要在文件夹中执行npm
命令。./ClientApp
项目结构:
├── ClientApp
│ ├── package.json
│ ├── angular.json
│ ├── node_modules
│ └── ...
├── Controllers
├── Views
├── *.csproj
└── startup.cs
如果 packpage.json 位于根文件夹中,则可以在这样的*.csproj
文件中完成:
<Exec Command="npm run prod" />
但是,在执行以下命令之前,我需要更改目录。像这样的东西:
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec Command="cd .\ClientApp; npm run prod" />
在这种情况下有什么可行的解决方案吗?
解决方案
你的 csproj 文件应该是这样的
<PropertyGroup>
<SpaRoot>ClientApp\</SpaRoot> // you define the folder to run npm script
</PropertyGroup>
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
这$(SpaRoot)
是您在属性组中定义的值,因此构建将知道您的角度应用程序的工作目录
推荐阅读
- scala - 从 spark 数据框中访问特定行
- javascript - 传递一个函数
- c - fscanf 不读取/识别浮点数?
- ruby - 仅接受整数作为输入 Ruby 的条件语句
- python-3.x - 如何修复第二个 QMainWindow 小部件中缺少的任务栏图标
- java - 如何为基于 json 的插件“安装 docker 插件”
- java - 测试服务器上的 java.lang.ClassCastException 错误
- ruby-on-rails - 我可以使用 Ruby 轻松计算日期吗?
- python - python中if语句中的值错误。太多值需要解压
- c# - 如何在选择列表中进行不显眼的 Ajax 调用?