从Netcore部署脚本在子目录中执行npm命令

时间:2019-05-17 11:30:59

标签: asp.net-core npm csproj

我有一个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" />

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

1 个答案:

答案 0 :(得分:1)

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