Possible way to create a command line tool in node.js - node.js

I am building a nodejs based small framework for my application. Say, the name of the framework is coffee-cup, situated in the folder of the same name. The folder has a structure inside. To simplify the work of developers, I want to add few command line methods. For example, consider following folder structure.
coffee-cup
|--config
|--cups
|--cup1
|--index.js
|--report
|--cup-config
|--cup2
|--index.js
|--report
|--cup-config
We see that inside cup1 and cup2, there is same structure. If a developer wants to add a new cup called cup3, they will need to create a folder called cup3 and add the underlying structure into that folder.
To automate this task, I am thinking of a way to build a command line utility like
$ coffee-cup new cup cup3
that can work on linux, windows or mac terminals.
Expected result of Entering this command in terminal: would create a new folder called cup3 in cups folder and will create the file index.js and folders reporting and cup-config inside the folder cup3.
I can create folder using fs.mkdirSync()and file using fs.writeFile().
I am however, unable to find a way to make the terminal identify the coffee-cup command. If terminal identifies this command, it should collect the following arguments and pass on to the nodejs file and I can continue from there. What I know is, that there exist npm packages like inquire which are useful in creating command line applications. But it does not help in building command line utilities like I mentioned above.
So I am looking for guidance on making terminal identify the coffee-cup command, collect the following arguments and pass on everything to a nodejs file. It is just like we install npm and then terminal starts identifying the npm keyword and subsequent commands like $ npm install. Any help by experts will be highly appreciated.

Related

Adding a command line script to the user environment whilst installing an application using electron-builder

I'm currently working on a project with Electron 9.0.4 and Electron-Builder 22.8.0 and am faced with a problem that doesn't seem too difficult but there isn't a valid solution online! (At least I couldn't find it)
I have my main program that does all of the UI tasks, and a command line script that does some backend. The reason I have this command line script is so that I can run certain parts of the application without opening the window itself. Everything works fine on my computer. After running npm link, my CL script is added to my environment variables and I can just run it from the console. However, when I try to build with electron-builder, the problem occurs.
If I use my Setup.exe on another computer, the command line script just won't be added to the environment variables and I couldn't find instructions on how to do this in the electron, nodejs, or electron-builder documentation. What I found was a suggestion on another question to add npm -g install as a post-install script, but that had no effect either.
Someone else suggested adding npm link as a post-installation script, but firstly if I am not mistaken this function is not intended for production and secondly it created an infinite loop as npm link triggered the post-installation script over and over again.
Thats how the script is added to the project
"bin": {
"command-name": "/cl.js"
}
Any help is appreciated!
Since I couldn't find a direct solution to my problem and didn't want to look any further for a solution while being able to take a different approach.
I decided to take a step back and look for another method to solve my problem I came to the conclusion that I didn't really need to add a script to the command line. My solution was to look for a certain argument when starting the regular application.
if (process.argv.includes("cli")) { /* Do commandline stuff */ }
When the custom argument is found, I simply run the script that should've been run from the command line. Using this approach, you can create a shortcut to my executable that contains the custom argument and then instead of the application it runs the command line script.

I got a problem when I input any node cmd,

I got a problem when I input any npm cmd, it says
internal/modules/cjs/loader.js:584
Operating System: Windows 8 pro 64bit
What shall I do to solve this issue?
Your error message of:
Error: Cannot find module 'C:\Users\NUMAN ALI\myfirst.js'
indicates that Node can't find your myfirst.js file to run within your current directory of: C:\Users\NUMAN ALI. Your myfirst.js file isn't located within this directory so you need to move into the correct directory containing this file, or alternatively create this file in the current directory. Then you need to add your desired JavaScript within this file to run it.
With your comment of: "The file you have just created must be initiated by Node.js before any action can take place" I am going to assume you don't have this file in your current directory. Maybe you could try creating the file in a text editor, such as notepad at the very least on a Windows machine, and adding the JavaScript contents into this file. Then try re-running the file with a node myfirst.js. Check out this accepted answer here as I think this will help you out a lot. Also, for command line help for creating files, check out this link.
Hopefully that helps!

how to run mbpipe... a program I just installed

I'm trying to use this tool: https://github.com/mapbox/node-mbtiles/wiki/Post-processing-MBTiles-with-MBPipe
I've installed mbtiles with npm install -g mbtiles. I've also installed it locally (in the dir I'm working in) with just plain npm install mbtiles.
That part worked (I was able to download the files), but now according to the readme I can just start entering in commands like
mbpipe 'pngquant 64' myMbTilesFile.mbtiles and it's supposed to work?
Umm... don't I have to run a specific script file (like "node scriptfile.js")? This is acting like I can call functions within a script and pass it variables? I can tell you, as the user mentioned, that 'mbpipe' is within the utils.js file I have.... but how am I supposed to use it?
when I enter in the above command, I of course get "mbpipe: command not found"
So... what are they talking about?

How to work with directories in Gulp?

I was following this tutorial. The writer mentions that I need to install node.js which I did. It is installed in
C:\users\me\AppData\Roaming\npm
After that the writer says I need to create a folder called project which I did. The folder is here
C:\users\me\project
Next the writer says that I need to
Run the npm init command from inside that directory.
which I don't understand. When I start command prompt as administrator I get
C:\Windows\System32>
when I run it without administrator, I get
C:\users\me>
How do I go inside my project directory then?
I have never used Gulp before and only once or twice used command prompt. I have been searching for a few hours now but I could not find anything. Maybe I am using the wrong search terms. Any help would be appreciated.
Why was it downvoted? If I could find the answer I swear I would not have posted this question and I clearly mentioned that I am a beginner.
First off your problem has nothing to do with gulp specifically. You're not "working with directories in Gulp" as your title puts it. You're just working with directories in the Windows commmand prompt (cmd).
You should probably read a tutorial on how to use cmd first, before using a command line tool like gulp. Here's one for example.
To answer your question: open cmd as a non-admin user. You're now in the \users\me directory. Type:
cd project
You're now in the \users\me\project directory and can issue the npm init command.

Run a node.js server from Geany

A simple question: Is it possible to configure the Geany IDE so that Node.js servers can be run directly from Geany using the "Run" button?
When inside a JS file, go to Build > Set Build Commands, there should be a section title Execute commands. To use node to execute your files, put: node "%f" in the "Execute" command textbox.
When you change this, any .js files you are editing will run node in the virtual terminal when you hit F5.
If you want to set up an entire project to run the server whenever you're working somewhere within a given directory structure, you'll have to mess with project-level configuration. (something I don't usually bother with) My solution here just gives you a quick way to execute a single JS file without using an external terminal.
UPDATE: node "%f" seems to be legacy, but nodejs "%f" works

Resources