Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to download some resources upon 'npm install', presumably using a 'prepublish' script

Tags:

node.js

npm

Specifically, I want to download angular.min.js when running 'npm install'.

From what I read here the place to do such things is in a prepublish script. It also mentions I don't need to rely on having wget/curl installed on the system.

However it doesn't go into any detail on how to go about doing it.

Anybody knows how to do it?

like image 307
Sebastian Ferreyra Avatar asked Jan 13 '23 22:01

Sebastian Ferreyra


1 Answers

Ok, solved it myself.

I added a small javascript file (prepublish.js):

var http = require('http');
var fs = require('fs');

function download(filename, url) {
  var file = fs.createWriteStream(filename);
  var request = http.get(url, function(response) {
    response.pipe(file);
  });
}

console.log('Downloading angular');
download('public/scripts/angular.min.js', 'http://code.angularjs.org/1.0.7/angular.min.js');
download('public/scripts/angular.js', 'http://code.angularjs.org/1.0.7/angular.js');
console.log('Downloading angular-ui-router');
download('public/scripts/angular-ui-router.min.js', 'http://angular-ui.github.io/ui-router/release/angular-ui-router.min.js');
download('public/scripts/angular-ui-router.js', 'http://angular-ui.github.io/ui-router/release/angular-ui-router.js');

and I modified my package.json:

...
"scripts": {
  "start": "node app.js",
  "prepublish": "node prepublish.js"
},

Now when running 'npm install' it downloads my dependencies!

like image 176
Sebastian Ferreyra Avatar answered Feb 11 '23 06:02

Sebastian Ferreyra