Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

javascript - How to download a static file during npm install

I have a link in my angular app which upon clicking should open a pdf file. But, this file should first be downloaded and to make it statically available.

Is there a way to download a file via http request while running npm install?

Basically, before hosting the app I need to download and copy a file to a static location which is then referred to from the href within the app

Something like

npm download http://download.com/file.pdf

or

instruct npm to download via paclage.json

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can define a postinstall script in your app's package.json :

package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "start": "node src/index.js",
    "postinstall": "node downloadAssets.js", // <--------------------------
    // ...
  },
  "dependencies": { /* ... */ }
  // ...
}

And then create a script to do it:

downloadAssets.js

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

const file = fs.createWriteStream("file.pdf");
http.get("http://download.com/file.pdf", function(response) {
  response.pipe(file);
});

It will be executed when you install your app (after everything is installed)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...