0

I am trying to use NPM as my ASP.NET 5 (Vnext) build tool as well as use to build my js/cs files but i do not want to duplicate configuration values in my npm and asp.net 5 config files.

in my project i have the following files

package.json

"scripts": {
  "build:js": "browserify assets/scripts/main.js > $npm_package_config_aspnet5_webroot/main.js"
}

project.json

{
    "version": "1.0.0-*",
    "webroot": "wwwroot",
}

And I want to be able to extract the webroot value from the ASP.NET 5 json file and use it in npm scripts commands like the following

Madu Alikor
  • 2,544
  • 4
  • 21
  • 36

1 Answers1

0

The json npm package is a really cool CLI tool that I've been using for things like this. You can use it to extract individual values of a json file. Combining this with bash scripts means you could fairly trivially extract the asp.net webroot from your project.json.

Assuming your project.json looks like this:

{
    "webroot": "www/"
}

You can use the json tool, like so:

$ json -f project.json webroot
www/

Now, its simply a case of combining this with your build:js command:

"scripts": {
    "build:js": "browserify assets/scripts/main.js > $(json -f project.json webroot)/main.js"
}

You could also make an npm task out of extracting the value, which would be useful if you need to use it multiple times:

"scripts": {
    "getwebroot": "json -f project.json webroot",
    "build:js": "browserify assets/scripts/main.js > $(npm run getwebroot -s)/main.js",
    "build:css": "cat assets/styles/* > $(npm run getwebroot -s)/style.css"
}
Keithamus
  • 1,819
  • 17
  • 21
  • Is it possible to load a whole json object into environment variables so that you can use in many different script commands? – Madu Alikor Jul 08 '15 at 20:40
  • what does the argument -s do for (npm run getwebroot -s) is this documented? unable to find it – Madu Alikor Jul 08 '15 at 20:43
  • you need to edit the above like following $(npm run getwebroot -s) – Madu Alikor Jul 08 '15 at 21:11
  • You could write a quick JavaScript file to turn a json file into a set of environment variables - [heres a gist which does just that](https://gist.github.com/keithamus/acfb4e7a135381ea8eda), feel free to use it for whatever you want, e.g. you could turn it into a node module! – Keithamus Jul 08 '15 at 21:22
  • `-s` silences the output of npm, a shortcut for `--loglevel silent` - if you omit this then npm will echo out the script it wants to run. Try it out on the command line to see the difference! – Keithamus Jul 08 '15 at 21:23