工欲善其事必先利其器。vscode做为优秀的开发工具,给个人平常开发工做提供了极大的便利。其拓展机制更是如此。node
可是,最近在作年度专业线任务时,有须要用到漂亮的行尾注释对齐,搜索后发现vscode官方插件市场没有我想要的。git
因而便想着本身来开发这么个东西,一方面方便后边本身使用,一方面也能学习下vscode的插件开发、发布方法,另外一方面要是发布后对其余人有所帮助就更好了。github
这篇文章是在我完成插件开发、发布后写的,记录下方法。chrome
主要包含两个方面的内容:typescript
vscode、nodejs、git、微软帐号,这个的准备无需多说。npm
vscode插件生产工具:官方推荐使用Yeoman 和 VS Code Extension Generator。用以下命令安装:json
npm install -g yo generator-code
至此开发所需的准备已作好。api
执行以下指令工具
yo code
结果以下:post
$ yo code _-----_ ╭──────────────────────────╮ | | │ Welcome to the Visual │ |--(o)--| │ Studio Code Extension │ `---------´ │ generator! │ ( _´U`_ ) ╰──────────────────────────╯ /___A___\ / | ~ | __'.___.'__ ´ ` |° ´ Y ` ? What type of extension do you want to create? (Use arrow keys) > New Extension (TypeScript) New Extension (JavaScript) New Color Theme New Language Support New Code Snippets New Keymap New Extension Pack (Move up and down to reveal more choices)
在os系统上经过上下键来选择要建立的类型,在win上输入一、二、3后按回车来选择。
其他步骤根据提示便可。获得以下结构目录结构:
├── .vscode // 资源配置文件 ├── CHANGELOG.md // 更改记录文件,会展现到vscode插件市场 ├── extension.js // 拓展源代码文件 ├── jsconfig.json ├── package.json // 资源配置文件 ├── README.md // 插件介绍文件,会展现到vscode插件市场 ├── test // 测试文件 └── vsc-extension-quickstart.md
ps:其他项目类型的文档目录可能会有所差异,以生成的文件目录为准。在js拓展项目下,最重要的就是extension.js
和package.json
。
灵感来源于生活、工做,这个想到了就能够去作。好比我这个行尾注释对齐(上面的目录注释就是用的我刚开发好的插件)。
关于comment-aligner的具体思路就不写在这里了,感兴趣的能够去下载源码看看,里边包含了完整的注释。逻辑十分简单。
这里不得不说一下官方文档不太好看,至少不是那么友好。传送门https://code.visualstudio.com/api/references/vscode-api。英文实在吃力的可使用chrome的一键翻译,翻译的还算准确的。
对于基本的应用主要查看window
相关的就足够了,结合yo code
生成的基本代码能够实现简单的功能。
npm install -g vsce
在插件市场官网建立发布人
package.json
package.json
中有vscode的自定义配置,在执行打包命令时vscode会自检,若是配置错误或者遗漏会有提示信息。
较完整的信息以下(下方是我发布的comment aligner的package.json文件):
{ "name": "comment-aligner", "displayName": "comment aligner", "description": "A tool for aligning the inline trailing comment", "version": "1.0.1", "publisher": "huangbaoshan", // 发布人,在插件市场官网建立的发布人id "icon": "icon/icon.png", // 插件图标,用于在插件市场展现的icon;能够放到同级目录内,打包会带入 "repository": { "type": "git", "url":"https://github.com/gitshan/vscode-extension-comment-aligner.git" }, "engines": { "vscode": "^1.30.0" // vscode版本号 }, "categories": [ "Other" // vscode插件类别,会在插件市场的对应类别中展现 ], "activationEvents": [ "onCommand:extension.commentaligner" ], "main": "./extension.js", "contributes": { "commands": [{ "command": "extension.commentaligner", // 插件注册的类名 "title": "Comment Aligner" // 插件在命令面包的展现名称 }] }, "scripts": { "postinstall": "node ./node_modules/vscode/bin/install", "test": "node ./node_modules/vscode/bin/test" }, "devDependencies": { "typescript": "^3.1.4", "vscode": "^1.1.25", "eslint": "^4.11.0", "@types/node": "^8.10.25", "@types/mocha": "^2.2.42" } }
执行以下命令:
vsce package
在根目录获得:comment-aligner-1.0.1.vsix
文件
vsce publish
工具来发布,可是须要在官网配置Personal Access Token
较为繁琐。可参考官方教程 上传后点击肯定便可发布成功。
以上均表示已发布成功。
在发现bug和新功能开发完成后,须要更新插件,只须要将新生产的.vsix包上传到官网便可。
但愿有用,谢谢你们。