首页 > 解决方案 > 从 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" />

在这种情况下有什么可行的解决方案吗?

标签: asp.net-corenpmwebpack.net-coreasp.net-core-mvc

解决方案


你的 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)是您在属性组中定义的值,因此构建将知道您的角度应用程序的工作目录


推荐阅读