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.
Related
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
just like to add a short preface to this question - It may be a true issue of how Node functions, or it may be a fundamental misunderstanding on my part of how modules in Node work.
Recently, I've been working on a new project, specifically, a new Node module that requires some configuration via a .json file. I've had quite a few issues with this, being tested via linking the module to an Angular 2 (v5) project, and attempting to use the module in a service.
As I have the module set up right now, the configuration is loaded immediately as the module is initialized. It attempts to look for a config.json file, and read values from aforementioned file. I have this working, to an extent. I am able to require the file, read the file, and throw an error if the file is not found - but only in the same directory as the module's main file. The issue that I've come across is trying to determine how to find the path to the host project's root path - in this case, the Angular project. For example, say the absolute path to my module's root is /home/dev/angular2project/src/node_modules/module/. How can I determine the root path of the host, in this case /home/dev/angular2project/? What if the module has been installed globally?
I have attempted to use my weak understanding of the path module, paired with various searches of SO, to resolve this issue. I've tried using path.resolve(__dirname) to no avail path.resolve(process.cwd). This works well when debugging the module by itself. When attempted to be run from the Angular project, it simply returns / as the path, despite the actual root of the Angular project being home/dev/Desktop/angular2proj.
I want to find some built-in modules of node js directory's like URL,Fs or http!
Where is their root library?
I guess you won't be able to find it on your machine because they are most likely (for the sake of simplicity and performance) bundled inside of the Node executable.
However you can find the source code on the original Nodejs repo. All modules you are looking for are located here
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.
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