do
The essence is to use consloe.log to output links about my personal social accounts on the console. Such as Github, Zhihu, Weibo, Twitter… Get the desired output from the command line or open it in a browser.
Encapsulate it as a tool that can be used in other NPM projects. Output information about yourself via Postinstall (not recommended, regardless of your own project or learning purpose)
What’s the use of
I personally think it’s just for learning purposes, maybe in communication with front end friends. I can tell him to use the command NPX wxh16144 to get to know me quickly. Its main purpose is still learning, such as:
- Quickly analyze command line parameters using MRI.
- Use Chalk to beautify the console output.
- Use the Open implementation to open my contact links using the browser.
- I used VSCode’s github copilot to help me write the code.
These are usually into the favorites to eat gray package tools, through writing a small tool for an initial experience. This project only relies on the above three NPM packages. Here are the details:
The directory structure
├── ├─ ├─ ├─ ├─ download.jsonCopy the code
- Wxh16144 plain Text file, store a logo character pattern, using Text to ASCII Art Generator (TAAG).
- The main implementation of the index.js output method.
- Bin /index.js is used by command lines.
Implement terminal output
Only output implementations of the –ersion and –help messages are shown here. The code is also relatively simple, detailed implementation of GitHub# l34-l89
if (args.version) {
console.log(`${chalk.bold(package.name)}: ${chalk.blue('v' + package.version)}`);
return;
}
if (args.help || Object.keys(links).length === 0) {
console.log(`
${chalk.green(`${package.name} [options]`)}
-p, --pick [The ${Object.keys(contactList).join('|')}]
-o, --open: use default browser to open the link.
-h, --hidelogo: hide auther logo.
-s, --speed: set the speed of animation.
-H, --help: show help.
-v, --version: show version. ${chalk.blue('v' + package.version)}
----------------------------------------
${chalk.bold('e.g.')} ${chalk.green(`${package.name} -hop The ${Object.keys(contactList)[0]}`)}
`)
return;
}
Copy the code
For logo output is more simple, directly use the FS module to read the file, and then output.
Implements formatting command line arguments
Using mri tool can quickly analyze command line parameters, detailed implementation of GitHub# l28-l32
const mri = require('mri');
const argv = mri(process.argv.slice(2), {
alias: { H: 'help'.v: 'version'.o: 'open'.p: 'pick'.h: 'hidelogo'.s: 'speed' },
boolean: [...Object.keys(contactList)],
default: { pick: Object.keys(contactList), speed: Math.floor(1000 / 60)}});Copy the code
Realize the command line function
Just add the bin field to the package.json file
package.json
{
"name": "wxh16144"."version": "0.0.0"."bin": "./bin/index.js",}Copy the code
bin/index.js
#! /usr/bin/env node
require('.. /index') ()Copy the code
At this point, you have implemented a command line tool about yourself. Issues# 1:
Expand the use
The previously mentioned feature on how to output information about yourself through NPM install in other NPM packages is also simple.
Methods a
{
"script": {"postinstall":"wxh16144 -hp github"}}Copy the code
Way 2
Because the methods in index.js were module exported previously, you can also call them directly into wXh16144. I have provided the annotation bin/index.js# L4-l6 here
script/install.js
require('wxh16144') ({hidelogo: true.pick: ['github']});// do something
Copy the code
{
"script": {"postinstall":"node script/install.js"}}Copy the code
Write in the last
The above code is just an idea, you can check out the Github Repo for the complete code. I’m sure the code is pretty easy to read.
In addition, you can consider using Docker to create a private NPM server for package testing, and then push to the official NPMJS market after satisfaction. This reduces the number of bad NPM packages. (I have too much OCD)