0

not sure what the "generate" line does inside package.json (I am referring to "generate": "node ./server/generate.js > ./server/database.json",) of an angular project or where to find out more about this, any references or tips please? I am ready to delete the question if you guys think it's lacking in details.

 "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",r
        "lint": "ng lint",
        "e2e": "ng e2e",
        "generate": "node ./server/generate.js > ./server/database.json",
        "server": "json-server --watch ./server/database.json"
      },

So basically generate.js builds an object named database, initialised as: var database = { products: []}; (this is just before adding fake data to the "products" subtype); whereas, database.json just contains this:

{
    "products": []
}

What does this node ./ (a path) / file > (path to json) do? What do you need > for?

What if I add the code to populate database with fake data inside of a .ts class instead of a js file?

And can I directly type a command like this "node ./server/generate.js > ./server/database.json" from the terminal instead of "npm run generate"?

Sami
  • 393
  • 8
  • 22
  • It's a command-line redirect. `node ./server/generate.js` is run at a terminal (command window), and it's output is redirected to `./server/database.json`. Using a single `>` overwrites any existing file of the same name. If it was `>>`, it would append instead of overwrite. – Ken White Feb 19 '20 at 00:19
  • Does this answer your question? [In the shell, what does " 2>&1 " mean?](https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean) – Heretic Monkey Feb 19 '20 at 00:32

1 Answers1

0

Let's dissect the script inside the quotes:

node ./server/generate.js > ./server/database.json

node
The command to run (javascript interpreter).

./server/generate.js
The script to give the interpreter as parameter, i.e. the program to be executed.

>
Redirect the standard output from the command to a file.

./server/database.json
The file to write the output to.

If you want to do the same with typescript, replace node with ts-node and generate.js with your script file. Basically you can run any command inside the quotes, that's the whole idea of the scripts section.

VeeKoo
  • 36
  • 3