The only requirement is that you have some basic knowledge of programming fundamentals and javascript, es6 might help, but not necessary.
A starter point into the CLI world for node, without some complex frameworks or local/private installations/distributions.
Just some scripting, a couple of .mjs
files, and even some type safety into it.
This is meant to show the possibilities and provide some pieces of the bigger picture, it is up to you how you put them together
I'm using pnpm
, npm
or yarn
will also do the job.
You don't have to, but typescript is installed, and for the complete run, you'll have to install it, you can do so by:
pnpm install # or npm or yarn
Scripts and entrypoints are executed via pnpm
, you can replace them, and most can be executed like:
pnpm run [script] [...args] # or npm or yarn
My recommended script order:
- JS Hello world
- With imports
- Hello name
- CLI Args parser
- EZ Write to file
- Write to file
- Faster TSC
- Type safe
- Hello name with TypeScript
Creating almost similar structures by copying and pasting folders is prone to error, mistakes, or simply, oversights. Automating that process can be crucial in bigger apps with more complex structure, even more so if the architecture is complex and you want to assure quality.
You can do so by defining templates, and how you'd like them to interact with, what should be done before creating them, and after.
Different frameworks use different files, extensions, structures, and projects and teams define the overarching architecture of it's elements, thus, it's up to you how the structure should actually be implemented.
You can even add different set of (initially failing) tests, services, queries and many more...
pnpm run make:from --template=components --name=always-use-kebab-case
It would be highly recommended to make the default case be kebab-case
, serpent_case
could be the only other candidate that could also assure consistency.
It's easy to fall under the pit of writing scripts in pure javascript, or commonjs, and variants. It's not a bad thing per se but it could be improved, and it should on bigger CLIs.
Typesafety could be achieved by simplify transpiling the content. I'd recommend using .mts
that would transpile to .mjs
, as to maximize the JS ecosystem in newer versions and be able to import/export utilities.
In case the utilities you're using are in Javascript, and migrating them might be too much, you could declare it's content with a module declaration file (.js
-> .d.ts
, .cjs
-> .d.cts
, .mjs
-> .d.mts
).
At times we write code, that we don't test, and we should. CLIs are no exception and actually are a great use case and example.
Not everything in a CLI should be tested, i.e. saving a file, you'd be testing the actual system, but you could check the contents of that file looking for some specific change, even though it would be "slower", it could be necessary.
Manually saving the output to a log, checking some details about the system (referent to the project), a custom update script
It's not really faster, literally, but it will seem like it, instead of visually logging the output, just save it into a file, it's almost always much faster. Plus, the advantage of having a log file you can always check as a point of reference
pnpm run faster-tsc
It will generate a log with the typescript errors, if any
We can also automate the validity of our structure, it can actually make PRs/MRs easier to check, since it's one less thing to worry about.
pnpm run check-structure
This will throw an exception if something's not valid.
If you don't want to wait for every file to be checked and just want to see if at least one is wrong, earlyStopping
comes to the rescue!
pnpm run check-structure --earlyStopping
You may also want to check for a flat structure validity, or that's the standard in your app, you can do so by utilizing the flat
flag.
pnpm run check-structure --flat
That you can stack...
pnpm run check-structure --earlyStopping --flat
Tech stack:
- Node
- Typescript
- Vitest (testing) and coverage packs
Requirements:
- Node v18.5.0
- NPM 8.12.1 (not used directly)
- PNPM 7.18.1 (Node Agent, should also work with NPM and YARN)