As a front-end development engineer, THE CLI is necessary to set up some basic Settings.

As for how to make cli, a lot of articles, I believe we can search online.

However, I have checked how to debug the CLI tool for two days, but failed to find it.

The development tool I use is vscode. I knew the answer would be in VSCode debugging and NodeJS debugging 101, but I was dumb and didn’t know how to mix.

But god forbid, I finally found a solution.

Not much BB, directly on the code and screenshots.

Methods a

1. Add launch TAB for vscode

Copy is done, pay attention to the port number, is based on the subsequent generation of port number to fill in, you can fill in a first.

{
      "type": "node"."request": "attach"."name": Manual link to test."port": 9229}.Copy the code

2. Add code to project entry

#! /usr/bin/env node --inspect-brk
Copy the code

#! /usr/bin/env is a /usr/bin/env node.

Add –inspect-brk to stop it on startup.

3. To the debugger

At this time we execute their own CLI command (I have link, how link is not much to say, Baidu can find)

szyx-cli init aaa
Copy the code

So let’s say I’m doing init here, and as you can see, it’s stalled, and it’s given a port number, and I’m going to set that port number to the port in the launch option in step 1.

Use vscode for debugging

As you can see, we’re in! In this way, you can view errors generated during cli project execution. Feel free to write your own CLI tools.

Way 2

Write the launch

The configuration is as follows:

 {
      "type": "node"."request": "launch"."name": "Debug operation"."program": "${workspaceFolder}/src/index.js"."outFiles": []."protocol": "inspector"."console": "integratedTerminal"."args": ["init"."aaa"]}Copy the code

Program to execute its own entry file, args to fill in the subsequent command parameters. The disadvantage is that when there are many CLI commands, debugging needs to be constantly changed.

Of course, there are solutions, but I didn’t dig into them.

digression

Don’t use vscode friends how to debug. You can check NodeJS Debugging101 with Chrome ://inspect for debugging. But I didn’t look into it either.

conclusion

In the course of this inquiry, I met the help of many warm-hearted friends. Thank you very much.

Some friends write tool chain just by reading the log, indeed 🐂🍺.

But I still think that sharpening the knife is not wrong to cut wood workers. The e first step in writing a program must be knowing how to debug, otherwise I always feel like I can’t develop at all.

The resources

Nodejs Debugs globally installed CLI scripts

How to Debug Node command-line development