How to write inside a file at specific position using nodejs? - node.js

I am building an interactive CLI project generator and I want to have a similar feature like angular CLI have.
Although I am working on to generate folders and files but I am not sure that how can I write inside a file at a specific position.
Suppose, when we generate a component using angular cli, it registers the component inside our modules file. So how can I write inside a file at specific location? Say if I want to include a require tag in node inside a file how would I do it? How would I know at what position the imports are ending and at what position do I include it?

You could create simple CLI using REPL module in Node.js
https://nodejs.org/dist/latest-v10.x/docs/api/repl.html
For creating folders/files you could use fs module
https://nodejs.org/dist/latest-v10.x/docs/api/fs.html
For running external commands (like git init), you could use child_process module (exec method)
https://nodejs.org/dist/latest-v10.x/docs/api/child_process.html#child_process_child_process_exec_command_options_callback

Related

Is it possible to use Node.js in NetSuite/SuiteScript?

I am wondering if it is possible to reference node modules in SuiteScript. For example, I am aware that I can create a third party library in the file cabinet and reference the moment.js file. However, is it possible to do an npm install moment on your suitecloud project and then have access moment in that script by simply importing it as you would in Node?
Actually you cannot use Node.js inside suite script. But you can trigger scripts from Node.js. Example if you want to create a record from a json file, you can trigger node.js to pass the json to a Map/Reduce or RestLet script.
is it possible to do an npm install moment on your suitecloud project and then have access moment in that script by simply importing it as you would in Node?
Yes and no. What you could do in this case is to copy the required .js files along with the SuiteScript files when doing the deployment, having in mind that the SS files would be referencing the library using a relative path.
All of this could be programmed as a task in node (or maybe webpack), which would all execute in your machine, and then upload the results to NetSuite.

Running multiple node applications using same configuration file that is outside of each project

I am using pm2 to run several node applications. Problem is that I am using config files for every node application and so what I want to do is easily have say a json file outside of all the node application folders in which they can ALL point all for common database connections etc...
Prefer to not use linux environment variables unless there is an easy and great way of setting it up
pm2 does have the ecosystem, but it doesn't seem to be very well documented to me
what other solutions?
pm2 ecosystem // this generates .config.js not a .json
Create json or yml file. Put it in your root projects folder. And write "configProvider" which will read the file and populate configuration. It works really well for us. Especially this file can also been shared between different languages, not only javascript.

Use NodeJS Helper Script in Meteor

I have written a small helper module in regular NodeJS to be used with NodeJS batch scripts. I've placed this and all the batch scripts in the "private" folder inside my Meteor project.
I'd like to also use the helper module on the server-side of Meteor as well, but I don't know the best way to handle that.
This is my current project structure:
client
... client files ...
private
scripts
helpers.js
batch_script1.js
server
... server files ...
So for Meteor to include the "helpers.js" file into the server, it either has to be located in the "server" folder, or imported via a package. Creating a symlink won't work, as multiple developers will be working on this and may have the repository checked out to a different directory location (seeing as how you need an absolute path to create a symlink).
I also don't want to have to duplicate the file and maintain two copies, so what are my options for sharing a helper script between a Meteor app and a NodeJS script?
Thanks
I was able to find help on the Meteor forums: https://forums.meteor.com/t/use-nodejs-helper-script-in-meteor/11056/3

Ember-cli addon read configurations

I am trying to create an ember-cli addon and I want this addon to include custom command. This part I have already done by creating a lib/commands file structure and including the script that will run the command then used includedCommands in the addon index.js.
The part I am struggling with is I want the command to be able to read a configuration file within the host applications directory. How can I do this? If I could find out the host apps absolute path I suppose I could parse a file using built in node tools but how do I find this path?
If there are better ways to accomplish this task I am all ears.
In your commands run function, you can access this.project which provides details about the project.
What you want to use is this.project.root.. it's the root directory of the project.
I have managed to solve the problem. Don't know if it is the best way. So I am able to obtain the absolute process path in node just by using process.cwd() Then I can simply append a config file name to the end of the string to get my config file absolute path.
If there are any ember-cli specific ways that I should be doing this then please let me know.

How to write a node-webkit application with custom modules?

I am currently writing a node-webkit application. The application supports certain modules which lie in the module folder inside the application.
I would like to dynamically install and remove those modules from inside the application. For that i would need to dynamically write inside the application.nw file. Is there a best practice solution to my problem?
That's not really a good idea. Just create a folder for app files and put downloaded packages there.

Resources