『手撕Vue-CLI』添加自定义指令

前言

经上篇『手撕Vue-CLI』添加帮助和版本号的介绍之后,已经可以在控制台中输入 nue --help 来查看帮助信息了,但是在帮助信息中只有 --version--help 这两个指令,而 vue-cli 中还有很多指令,例如 createservebuild 等等,所以本章将继续添加自定义指令,例如 create 指令。

添加 create 指令

在 vue-cli 中,create 指令是用来创建一个新的项目的,我实现的 nue --help 的帮助信息中只有 --version--help 这两个指令,所以当用户使用我的 nue-cli 时,并不知道有 create 这个指令,所以接下来要完成的就是添加 create 指令到 nue-cli --help 的帮助信息中。

添加 create 指令到 --help 的帮助信息中

是否大家还记得在上一篇『手撕Vue-CLI』添加帮助和版本号中,我引入了 commander 这个库,这个库是用来处理命令行参数的,所以我们可以使用这个库来添加 create 指令到 nue-cli --help 的帮助信息中。

首先我们需要在 /bin/index.js 中引入 commander 这个库,这一步上一篇已经完成了,所以这里就不再赘述。

然后需要在 /bin/index.js 中添加 create 指令,这里我们可以使用 commandercommand 方法来添加指令,如下:

command 方法接收一个参数,第一个参数是指令的名称,调用方式是通过 commander 实例调用 command 方法,如下:

+ program
+   .command('create');

这样我们就添加了 create 指令:

这里只是单单的添加了 create 指令,但是并没有添加 create 指令的描述信息,告诉一下用户这个指令是干嘛干嘛用的之类的话术,所以我们需要添加 create 指令的描述信息,如下:

那么如何添加 create 指令的描述信息呢?紧接着上面的代码,在 command 方法后面添加 description 方法链式调用, description 方法的作用就是添加指令的描述信息,接收一个参数,就是指令的描述信息,如下:

 program
   .command('create')
+  .description('create a new project powered by nue-cli-service');

好了指令的描述设置好了,还可以设置下 alias 别名,就是可以通过简写的方式进行使用指令,继续链式调用 alias 方法,alias 方法的作用就是设置指令的别名,接收一个参数,就是指令的别名,如下:

 program
   .command('create')
+  .alias('c')
   .description('create a new project powered by nue-cli-service');

还可以设置 action 方法,继续链式调用 action 方法,action 方法的作用就是设置指令的回调函数,当用户输入了这个指令的时候,就会执行这个回调函数,如下:

 program
   .command('create')
   .alias('c')
   .description('create a new project powered by nue-cli-service')
+  .action(() => {
+    console.log('创建一个 Nue 项目');
+  });

这样我们就添加了 create 指令,并且添加了 create 指令的描述信息,别名,回调函数,现在如果用户使用 nue --help 就可以看到 create 指令了:

总结

其实就几点,介绍了一下 commandercommanddescriptionaliasaction 方法,这几个方法是用来添加指令的,设置指令的描述信息,别名,回调函数的,这样就可以添加自定义指令了。

有个注意点大家需要注意一下,就是 program.version(version).parse(process.argv); 这行代码要放在所有指令的后面,不然指令就不会生效了。

指令添加完成了,但是呢有一个问题,因为我本人是比较熟悉 vue-cli 所以知道有 create 并且知道怎么用,那么如果是一个新手呢?如果他知道了有 create 但是不知道怎么用呢?所以还需要添加 create 指令的使用示例。

添加 create 指令的使用示例

这个我相信对于新手又或者说有经验的人来说使用示例是啥就不用多说了,就是告诉用户怎么用这个指令。

那么如何添加 create 指令的使用示例呢?紧接着上面的代码,其实在 commander 中也有对应的解决方案,就是通过 commander.on 进行监听,监听 --help 事件,然后在监听事件中添加 create 指令的使用示例,如下:

+ program.on('--help', () => {
+   console.log('');
+   console.log('Examples:');
+   console.log('  nue create <app-name>  ');
+ });

封装公共解决方案

为啥我还要起一个标题来说这个呢?我现在只有一个 create 自定义指令,那么在后面还会有很多自定义指令,那么每次都要写一遍 commanddescriptionaliasactionon 这些方法,那么这样就会显得很冗余,所以可以封装一个公共解决方案,这样就可以减少代码量,提高代码的可维护性。

首先定义一个 commandMap 对象,用来存放指令的信息,可以将后续需要使用的指令都放里面进行存放起来,如下:

+ const commandMap = {
+   create: {
+     alias: 'c',
+     description: 'create a new project powered by vue-cli-service',
+     example: 'nue-cli create <app-name>',
+   },
+   add: {
+     alias: 'a',
+     description: 'install a plugin and invoke its generator in an already created project',
+     example: 'nue-cli add [options] <plugin> [pluginOptions]',
+   },
+   '*': {
+     alias: '',
+     description: 'command not found',
+     example: '',
+   },
+ };

我这里定义了 createadd* 三个指令,* 是用来处理用户输入的指令不存在的情况,这里只是定义了三个指令,后续还可以继续添加。

commandMap 对象的取值就是指令的名称,然后值是一个对象,这个对象包含了指令的别名,描述信息,使用示例,字段,后续的改进就是遍历 commandMap 对象,循环的添加指令,如下:

- program
-   .command('create')
-   .alias('c')
-   .description('create a new project powered by nue-cli-service')
-   .action(() => {
-     console.log('创建一个 Nue 项目');
-   });
+ Reflect.ownKeys(commandMap).forEach((key) => {
+   const value = commandMap[key];
+   program
+     .command(key)
+     .alias(value.alias)
+     .description(value.description)
+     .action(() => {
+       if (key === '*') {
+         console.log(value.description);
+       } else {
+         console.log(value.description);
+       }
+     });
+ });

通过 Reflect.ownKeys 方法遍历 commandMap 对象,然后通过 program.command 方法添加指令,Reflect.ownKeys 这个是 ES6 提供的一个方法,用来获取对象自身的属性键,返回一个数组,这个方法是用来替代 Object.keys 方法的,Object.keys 方法只能获取对象自身的可枚举属性键,而 Reflect.ownKeys 方法可以获取对象自身的所有属性键,包括不可枚举属性键。

什么意思呢?就是说 Reflect.ownKeys 方法可以获取对象自身的所有属性键,包括不可枚举属性键,而 Object.keys 方法只能获取对象自身的可枚举属性键,所以 Reflect.ownKeys 方法更加强大。

不可枚举又是什么意思呢?通俗易通的说就是 private 与 public 的区别,private 是不可枚举的,public 是可枚举的。

通过这么一改造之后,之前通过 command 方法添加指令的代码写法已经优化完毕了,还有一个就是添加指令所对应的使用示例,代码也需要进行优化,如下:

- program.on('--help', () => {
-   console.log('');
-   console.log('Examples:');
-   console.log('  nue create <app-name>  ');
- });
+ program.on('--help', () => {
+     console.log('Examples:');
+     Reflect.ownKeys(commandMap).forEach((key) => {
+         const value = commandMap[key];
+         console.log(`  ${value.example}`);
+     });
+ });

改写方式就是通过 Reflect.ownKeys 方法遍历 commandMap 对象,然后通过 console.log 方法输出指令的使用示例。

最后在控制台在输入 nue --help 就可以看到所自定义的指令了:

posted @   BNTang  阅读(232)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
  1. 1 总会有人离开 王巨星
  2. 2 月亮 孟凡明
  3. 3 迟里乌布
  4. 4 我只能离开 颜人中
  5. 5 达尔文 蔡健雅
  6. 6 夜色滚烫 叶明净
  7. 7 你的星环 路飞文
  8. 8 不再说话 三块木头
  9. 9 黄昏 粥粥和小伙/粥粥
  10. 10 爱不单行 刘大拿
  11. 11 心动贩卖机 PIggy
  12. 12 别来无恙 苏星婕
  13. 13 我们的歌 刘大拿
  14. 14 一直很安静 王贰浪
  15. 15 去有风的地方 清音
  16. 16 雪 Distance Capper/罗言
  17. 17 坏女孩 徐良/小凌
  18. 18 乐园 沧桑Cang333/虎皮蛋/曲甲
  19. 19 Ayo(Explicit) Chris Brown/Tyga
  20. 20 我的美丽feat.海洋Bo 海洋Bo/高睿
  21. 21 世事可爱 粥粥和小伙/粥粥
  22. 22 我记得 赵雷
  23. 23 我想牵着你的手 许嵩
  24. 24 人们都不懂 刘诺然
  25. 25 寻一个你(电视剧《苍兰诀》温情主题曲) TTTTTeehom
  26. 26 子莫格尼 杉和
  27. 27 Cat Cafe Shoffy
  28. 28 风停了雨停了我们还拥抱着 Superluckyqi
  29. 29 寂寞沙洲冷 于潼
  30. 30 三国恋 王巨星
  31. 31 达尔文 林俊杰
  32. 32 有些 颜人中
  33. 33 小模样 张小只ya
  34. 34 是否 程响
  35. 35 楼顶上的小斑鸠 队长
  36. 36 笑场 薛之谦
  37. 37 还是分开 张叶蕾
  38. 38 修炼爱情 林俊杰
  39. 39 二零三 毛不易
  40. 40 雅俗共赏 许嵩
  41. 41 Serendipity 古瑞斯Graps/Zakiya晴子
  42. 42 就让这大雨全都落下·2023 刘大拿
  43. 43 老男孩 筷子兄弟
  44. 44 有何不可 许嵩
  45. 45 缓缓 杜宣达
  46. 46 好久不见 陈奕迅
  47. 47 爱的魔法(Cover 金莎) 封茗囧菌
  48. 48 在你的身边 盛哲
  49. 49 带我去找夜生活 告五人
  50. 50 假面舞会 很美味
  51. 51 STAY The Kid LAROI/Justin Bieber
  52. 52 我好想睡觉的 无敌西红柿
  53. 53 日不落(温柔版)
  54. 54 恋爱画板 锦零
  55. 55 7710 好乐无荒/尹露浠
  56. 56 给你呀(又名:for ya) 蒋小呢
  57. 57 Love Story Taylor Swift
  58. 58 Plain Jane(Remix 13z) 鱼幼微
  59. 59 晚风 7opy/BT07
  60. 60 拜托 孙晨
  61. 61 乌梅子酱 李荣浩
  62. 62 南半球与北海道 范倪Liu
  63. 63 星河万里 Rom邢锐
Ayo(Explicit) - Chris Brown/Tyga
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Chris Brown/Michael Ray Nguyen-Stevenson/Mark Kragen/Nicholas Balding

作曲 : Tyga

I need you, I need you (我需要你 需要你)

I need you, I need you (我需要你 需要你)

I need you, I need you (Lemme see) (我需要你 需要你)

I need you, I need you (我需要你 需要你)

We poppin' like ayo (哥们燥起来)

All my b*****s got real hair chillin' with the top down (在场的所有辣妹都甩起秀发尽情摇摆)

Screamin' like ayo (大声尖叫)

I'ma take her ass down if she bring her friend around (如果她带她朋友过来 我就当着众人面和她摇摆)

poping booty like ayo (尽情摇摆)

I'm a bougie ass nigga , left the roof at home (哥们主打一个风流倜傥 好似上房揭瓦)

We poppin' like ayo (哥们燥起来)

Ayo, ayo

But don't be actin' like I need you (但别表现得好像我离开你就活不了似的 哥们多你一个不多少你一个不少)

Ha!

Ayy, babe, this my new shit (shit) (宝贝 来听听我的新歌)

I'm the black Richie Rich with the roof missin' (Missin') (哥们就纯高富帅 开着敞篷跑车)

If it don't make dollars, don't make sense (Sense) (如果我们赚不到钱 那就不合理)

Z (Z), wake up like I gotta get it (Get it) (哥们就像Z世代 志在必得)

And I got an engine full of trunk space (后置引擎的跑车我轻松拿下)

I get money three ways, ****ing b*****s three ways (我有三种方式赚钱 三种方式找女人)

Seven different foreigns, plus she's no hablé (七辆不同的外国跑车 把她惊得目瞪口呆)

But I make that b***h walk for some cheesecake (Cheesecake) (可以让女人无偿做任何我想做的事)

Yeah, I'm the coldest nigga , icy (哥冷酷无情)

Lookin' in the mirror like I wish I can be me (看着镜子里的我 我希望我能成为真实的自己)

She too into me, I'm more into money (她太喜欢我了 但我更喜欢钱)

My hobby's her body, that b***h's my lobby (我的爱好是她 她就是我的大厅)

I'ma eat it (Eat it), I'ma eat it (Eat it) (我要开始品尝)

I don't lie, hold my shit, too conceited (我不说谎 我实在太自负)

Uh, told her she's my wife for the weekend (For the weekend) (告诉她 我们只能做周末夫妻)

But don't be actin' like I need you (别表现得好像我离开你就活不了似的 哥们多你一个不多少你一个不少)

'Cause we poppin' like ayo (Ha! Yeah) (因为哥们都嗨的翻天)

All my b*****s got real hair chillin' with the top down (在场的所有辣妹都甩起秀发尽情摇摆)

Screamin' like ayo (大声尖叫)

I'ma take her ass down if she bring her friend around (如果她带她朋友过来 我就当着众人面和她摇摆)

**** 'em both like ayo (尽情摇摆)

I'm a bougie ass nigga , left the roof at home (哥们主打一个风流倜傥 好似上房揭瓦)

We poppin' like ayo (哥们燥起来)

Ayo, we poppin' like ayo

But don't be actin' like I need you (但别表现得好像我离开你就活不了似的 哥们多你一个不多少你一个不少)

I'm in the Rolls, you don't roll right (Roll right) (哥们开大劳 你连方向盘都不会转)

My chain shine brighter than a strobe light (Yeah) (我的链子比闪光灯还亮)

I'm tryna **** Coco, this don't concern Ice (哥们看上Coco Austin了 这跟冰哥没关系(Coco Austin是老派说唱歌手 Ice-T 的妻子))

If I motorboat, she gon' motorbike (Ha!) (如果我开摩托艇 她就会开摩托车)

A nigga ain't worried about nothin' (我什么都不担心)

Rehabilitation just had me worried about ****ing (唯一担心的是我看破红尘)

Money, decision-makin', only worried 'bout stuntin' (赚钱 做出任何决定 只担心会被吓傻)

She worried 'bout me, her nigga worried 'bout cuffin' (她担心我 她的男伴担心我被束缚住)

I wanna see her body (Body) (我想领略她的曼妙)

Then she said, "Get inside of me (她求之不得)

I wanna feel you, baby" (Yeah) (然后她说:宝贝快来 我想感受你)

Just bring the animal right out of me (我身体里的野性早已按捺不住)

We lovin', she love it (我们喜欢 她也爱)

Especially when I go down on her (尤其是渐入佳境时)

Now we ****ing, she thuggin' (我们合体 她开始疯狂)

Gettin' loud (音量渐渐变大)

'Cause we poppin' like ayo (因为哥们嗨得要把房顶揭开)

All my b*****s got real hair chillin' with the top down (在场的所有辣妹都甩起秀发尽情摇摆)

Screamin' like ayo (大声尖叫)

I'ma take her ass down if she bring her friend around (如果她带她朋友过来 我就当着众人面和她摇摆)

**** 'em both like ayo (尽情摇摆)

I'm a bougie ass nigga , left the roof at home (哥们主打一个风流倜傥 好似上房揭瓦)

We poppin' like ayo (哥们燥起来)

Ayo (Huh, look), we poppin' like ayo

But don't be actin' like I need you (Alright) (但别表现得好像我离开你就活不了似的 哥们多你一个不多少你一个不少)

Now, now I can spot your b***h from a mile away ('way) (现在我在一英里外就能认出你的对象)

Valentine in that shit, it's a holiday (我和她一块过的情人节)

Uh, you losin' money, I win mills, Dr. J (J) (哥们赚数百万就像朱利叶斯·欧文扣篮那般容易)

She gon' to follow my lead, Simon Says (她会听我的 就像在玩儿童游戏)

Paper, paper, I'm ridin' 'scrapers in California ('fornia) (赚着票子 开着老爷车巡游加州)

Car smell like ammonia, we got that stank on us (车里一股氨水味 我们身上都有)

Never been a Outkast, that stank on ya (从没想过哥几个能达到OutKast那般高度)

From the ghetto but my b***h like Apollonia (出身贫民区 但我的女人像Apollonia Vitelli般高贵)

We in the hood, tatted like a Mexican (来自街区 行事就像墨西哥狠人)

Car too fast, give a **** about pedestrians (车开得太快 路人被我们抛在脑后)

Uh, and my section less nigga s, more lesbians (我这块儿兄弟少 蕾丝边多)

Got your b***h, I'm that nigga (你女人都是我的 哥直接开无敌模式)

Yeah, we poppin' like ayo (Ha! Yeah) (哥们嗨炸了)

All my b*****s got real hair chillin' with the top down (Yeah) (在场的所有辣妹都甩起秀发尽情摇摆)

Screamin' like ayo (大声尖叫)

I'ma take her ass down if she bring her friend around (如果她带她朋友过来 我就当着众人面和她摇摆)

**** 'em both like ayo (尽情摇摆)

I'm a bougie ass nigga , left the roof at home (哥们主打一个风流倜傥 好似上房揭瓦)

We poppin' like ayo (哥们燥起来)

Ayo, ayo

But don't be actin' like I need you (但别表现得好像我离开你就活不了似的 哥们多你一个不多少你一个不少)

Yeah, this that fly shit (I need you)

King shit only

Drop top, no roof, ha!

(I need you) ((我需要你))

点击右上角即可分享
微信分享提示