6

So as the title says, i am wondering if its possible to do the equivalent of "npm run " with the dotnet cli?? e.g. this snippet is from my project.json:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.1.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Configuration": "1.1.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Configuration.CommandLine": "1.1.0",
    "Microsoft.AspNetCore.Mvc":"1.1.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    //"MySql.Data.Core": "7.0.4-ir-191",
    //"MySql.Data.EntityFrameworkCore": "7.0.4-ir-191",
    "SapientGuardian.EntityFrameworkCore.MySql": "7.1.14",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "moq":  "4.6.38-alpha",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.Relational": "1.1.0"

  },
  "scripts": {
        "migrate-identity": "dotnet ef database update --context 'ApplicationDbContext'"
},

The migrate-identity is one i added my self, is it possible to execute this script??

Otherwise i guess i will just have a scripts folder in my project, no big deal, but still :)

Tseng
  • 61,549
  • 15
  • 193
  • 205
DenLilleMand
  • 3,732
  • 5
  • 25
  • 34
  • The scripts in the scripts section are for certain build/publish events, i.e. `precompile`, `prepublish`, `postpublish` etc. – Tseng Nov 19 '16 at 17:13
  • Yeah, i read about that. But ... i thouth it would be smart if those scripts could be executed manually aswell. – DenLilleMand Nov 19 '16 at 17:19

2 Answers2

4

Just add the scripts you want in your scripts section in your project.json

"scripts": {
    "prepublish": [ "gulp mifiny-js", "gulp minify-css", "gulp remove-non-minified-files" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}

I'm using gulp but this is the same with npm

Here is the full project.json schema

As you can see, the scripts section can receive a bunch of different properties corresponding to different events.

"scripts": {
        "type": "object",
        "description": "Scripts to execute during the various stages.",
        "properties": {
            "precompile": { "$ref": "#/definitions/script" },
            "postcompile": { "$ref": "#/definitions/script" },
            "prepack": { "$ref": "#/definitions/script" },
            "postpack": { "$ref": "#/definitions/script" },
            "prepublish": { "$ref": "#/definitions/script" },
            "postpublish": { "$ref": "#/definitions/script" },
            "prerestore": { "$ref": "#/definitions/script" },
            "postrestore": { "$ref": "#/definitions/script" },
            "prepare": { "$ref": "#/definitions/script" }
        }
    },

Also I see in your comment that you wish to execute them manually. It's possible with Visual Studio at least (never tried VS Code). Just use the Task Runner Explorer. You need to build your project in order to see your npm or gulp tasks.

I'm not able to open it using the shortcut (Ctrl + Alt + Backspace) but the window is also accessible through View -> Other Windows -> Task Runner Explorer

Community
  • 1
  • 1
Jérôme MEVEL
  • 7,031
  • 6
  • 46
  • 78
1

project.json files are no longer supported. However, it is possible to handle it by using XML-based csproj files.

Example:

<Project Sdk="Microsoft.NET.Sdk.Web">
...
  <Target Name="MyTarget" AfterTargets="Build">
    <Exec Command="echo 'I believe you will see this message right after a successfull building'" />
  </Target>
...
</Project>

Result:

enter image description here

For further details:

MS Docs: Mapping between project.json and csproj

denizkanmaz
  • 566
  • 5
  • 9