Node Intellij Jetbrains Module, Easily generate Intellij Project in node

Posted on Friday, May 15, 2015



Want to easily create Intellij Project files for your Node project?  Use the npm jetbrains module and set up a simple script in package.json.

Before you do this it's a good idea to install the Node Plugin for Intellij and set up Code completion for Node.  I wrote up a how to do this at http://www.whiteboardcoder.com/2015/04/intellij-and-node.html if you have not done it yet.





.gitignore


Make sure you .gititnore file will ignore Intellij Files


  > vi .gitignore


Here is a my .gitignore file.  The highlighted section is the Intellij portion.


#Intellij
*.iml
.idea/

# Linux
.directory
*~

# OS X
.DS_Store*
Icon?
._*

# vim swap and backups
*.swp
*~

#Heroku
Procfile

# npm
node_modules
.grunt
npm-debug.log




Install Jetbrains module


From your Node application folder run the following commands.


  > npm install --save-dev jetbrains




You could run the following command to create the .idea folders.


  > node_modules/jetbrains/cli.js init


But there is a better way for your project.  Make it a callable script in the package.json file."


Edit the package.json file


  > vi package.json


And add a script.  (I am only showing the script section of my package.json file)


  "scripts": {
    "test": "mocha --recursive test",
    "gen-idea": "node node_modules/jetbrains/cli.js init"
  }, 




From the command line you can run


  > npm run gen-idea





An .idea folder with it's files should have been created.  And if you check git status it should be ignored J








The script call can be better


You can make the script call clearer, and shorter, in package.json

Change it to.



  > vi package.json



  "scripts": {
    "test": "mocha --recursive test",
    "gen-idea": "jetbrains init"
  },


Run the script again (You may need to remove the .idea folder first)


  > sudo rm -r .idea
  > npm run gen-idea







Why does this work? 



Poking around I found https://docs.npmjs.com/cli/run-script [1]
Looks like when you add a module a link is created in the node_modules/.bin folder.


  > ls -alh node_modules/.bin/




jetbrains links to -> ../jetbrains/cli.js

npm add node_modules/.bin to the PATH provided to the scripts.
So that works…

How does it know what to link to jetbrains links to cli.js?



Run this command


  > cat node_modules/jetbrains/package.json | grep -A 5 bin




You will see that the "bin" section defines the links to create.




If I run


  > cat node_modules/mocha/package.json | grep -A 5 bin






I can see the mocha package defines two variables to link in the bin section.

Cool that is a good thing to know J









References

[1]        Run arbitrary package scripts
                Accessed 5/2015


No comments:

Post a Comment