Pointers for creating a CLI-based device with node

Pointers for creating a CLI-based device with node

[ad_1]

Howdy @amandeepmittal!
Thank you for attaining out to me! I am satisfied you loved
my route 😀

I have made a couple of CLIs. Most definitely my most simple fresh one is
split-guide. You can see the CLI
code lives
right here.
And I inform npm to make use of the transpiled model of that report because the bin within the
bundle.json
right here
the place the key in that object is the identify of the binary (so what you’ll kind in
the terminal to make use of the CLI) and the worth is the trail within the bundle the place
the binary is situated (in my case it is within the dist listing which I transpile
to with babel
right here
(I am the use of p-s for my scripts).

With that configuration, when npm (or yarn) installs
my bundle, it’ll create a
symlink to that report within the
node_modules/.bin listing (or, if it is globally put in, it is going to put it
anyplace your world programs are loaded into your $PATH). For in the community
put in programs, you’ll be able to use the ones binaries along with your npm scripts (this
conduct is kinda defined
right here within the legitimate medical doctors).

So, for instance, as a result of I’ve setup split-guide like that, I will be able to use it in
my react-jest-workshop
right here.

Let’s glance once more at
the real
bin
report
itself now. There are some things to notice:

  1. The primary line
    has #!/usr/bin/env node. This is known as a
    shebang which
    successfully tells the device to run the script with node.
  2. Just about the remainder of the report is configuring
    yargs to just accept the arguments I
    need it to. There are in fact numerous npm programs to lend a hand parse
    procedure.argv
    into one thing that is usable (flags and so on.) I have used
    commander,
    meow and a couple of others I will be able to’t
    recall, however I have been maximum pleased with
    yargs. It is beautiful darn tough.
  3. You can additionally understand that I am not doing a large number of common sense in right here. That is as a result of
    unit trying out this report is just a little of a ache. You can in finding my assessments
    right here
    and spot that this can be a bit complicated, however it is in fact a lovely forged
    integration check. Maximum of it’s in fact simply the use of
    Jest snapshots
    which is in fact tremendous enabling and makes me beautiful sure of the affect
    my adjustments will make at the device. I could not be happier with Jest snapshot
    trying out and for sure suggest it for trying out your CLIs!
  4. The primary common sense for the bundle lives in the remainder of the
    src
    listing. Generally I’d unit check this, however this bundle specifically
    is person who I do not be expecting a large number of folks to make use of so I did not take some time.
    In truth, I do not even document protection in this mission which is kinda uncommon
    for me 😅

I am hoping this was once useful! Just right success!

See you on Twitter!

See you on Twitter



[ad_2]

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back To Top
0
Would love your thoughts, please comment.x
()
x