npm install -g yo generator-code
yo code
// 启动命令为h5-helper
"activationEvents": [
"onCommand:h5-helper.h5-helper"
],
"main": "./extension.js",
"contributes": {
"commands": [
// 配置的启动命令
{
"command": "h5-helper.h5-helper",
"title": "h5-helper"
}
],
"menus": {
// 配置文件内右键快捷键
"editor/context": [
{
"when": "editorFocus",
"command": "h5-helper.h5-helper",
"group": "navigation"
}
]
}
},
(这里只获取了wifi下的,如果是网线链接,请自己修改方法)
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const os = require("os");
const fs = require('fs')
const QRCode = require('qrcode')
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function getIpAddress() {
/**os.networkInterfaces() 返回一个对象,该对象包含已分配了网络地址的网络接口 */
var interfaces = os.networkInterfaces();
console.log(interfaces)
const {
WLAN
} = interfaces
const ipv4 = WLAN.find(item => item.family === 'IPv4')
return ipv4.address
}
function getParams(form) {
let url = '';
if(!form){
return url
}
const keys = Object.keys(form);
for (let i = 0; i < keys.length; i++) {
if (!form[keys[i]]) {
continue;
}
if (url) {
url += `&${keys[i]}=${form[keys[i]]}`;
} else {
url += `?${keys[i]}=${form[keys[i]]}`;
}
}
return url;
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log('Congratulations, your extension "h5-helper" is now active!');
let disposable = vscode.commands.registerCommand('h5-helper.h5-helper', function (uri) {
// 文件路径
const filePath = uri.path.substring(1);
fs.stat(filePath, (err, stats) => {
if (err) {
vscode.window.showErrorMessage(`获取文件时遇到错误了${err}!!!`)
}
if (stats.isDirectory()) {
vscode.window.showWarningMessage(`检测的是文件夹,不是文件,请重新选择!!!`);
}
const splitFilePath = filePath.split('/')
const fileName = splitFilePath[splitFilePath.length - 1]
// 判断是否是需要的文件
if (fileName !== 'h5-helper.json') {
vscode.window.showWarningMessage(`请选中h5-helper.json文件,请重新选择!!!`);
return
}
// 读取文件的内容
const fileData = JSON.parse(fs.readFileSync(filePath).toString())
const {
port,
projectName,
params,
appLinkConfig
} = fileData
let isAddAppLink = appLinkConfig.includes(projectName) ? 'applink/':''
// 拼接成url路径
const qrCodepath = `http://${getIpAddress()}:${port}/${isAddAppLink}${projectName}/${projectName}.html${getParams(params)}`
// 开启一个新的页面,在页面中生成二维码
const panel = vscode.window.createWebviewPanel(
'Test', // 标识webview的类型
'webview1', // 展示给用户的面板的标题
vscode.ViewColumn.One, // 显示webview面板以编辑器新列的方式.
{} // webview其他的选项
)
function getWebviewContent(imgPath, url) {
return `
Cat Coding
${imgPath}" width="300" />
${url}
`;
}
// 创建一个二维码
QRCode.toDataURL(qrCodepath, function (err, url) {
panel.webview.html = getWebviewContent(url, qrCodepath);
})
});
});
context.subscriptions.push(disposable);
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}
vsce版本过高会导致不能打包
yarn add vsce@1.6.0 -g
vsce package
可能会出现缺少作者提示,package.json中添加 “publisher”: “xiaoshunshi” 即可
文件名称必须是h5-helper.json,不然会报错
{
"port":8090, //端口号
"projectName":"coupon", // 项目名称
"appLinkConfig":["coupon"],// 是否需要添加前缀applink
"params":{ // 链接参数
"name":"xiaoshunshi"
}
}