-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
87 lines (77 loc) · 2.4 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const vscode = require("vscode");
const path = require("path");
const fs = require("fs");
const { Console } = require("console");
const VueTemplate = require("./template/compTemplate").fullVueComp;
const apiTempate = require("./template/apiTemplate").apiTemplate;
const mockTemplate = require("./template/mockTemplate").mockTemplate;
const routeTemplate = require("./template/routeTemplate").routeTemplate;
const vuexTemplate = require("./template/vuexTemplate").vuexTemplate;
/**
* 模板Map
*/
const templateMap = {
1: VueTemplate,
2: mockTemplate,
3: apiTempate,
4: routeTemplate,
5: vuexTemplate,
};
/**
* 生成文件
* @param {String} url 文件路径
* @param {String} data 文件内数据
*/
function generateFile(url, data) {
// 去除自动生成路径前面的"/"
const filePath = url.slice(1, url.length);
console.log("filePath===", filePath);
return new Promise((resolve, reject) => {
fs.writeFile(filePath, data, "utf8", (err) => {
if (err) {
reject(err);
} else {
resolve(true);
}
});
});
}
/**
* 生成模板文件
* @param {string} url 生成地址
* @param {number} type 类型 1-vue文档|2-mock服务|3-api文档|4-路由文档|5-vuex文档|
*
*/
function generateTemplate(url, type) {
const fileName = path.parse(url).name;
console.log("filename===", fileName);
generateFile(url, templateMap[type](fileName));
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let createComp = vscode.commands.registerCommand("quick-vue-template.createComp", function (url) {
console.log("url.path====", url.path);
generateTemplate(url.path, 1);
});
let createMock = vscode.commands.registerCommand("quick-vue-template.createMock", function (url) {
generateTemplate(url.path, 2);
});
let createApi = vscode.commands.registerCommand("quick-vue-template.createApi", function (url) {
generateTemplate(url.path, 3);
});
let createRouter = vscode.commands.registerCommand("quick-vue-template.createRouter", function (url) {
generateTemplate(url.path, 4);
});
let createVuex = vscode.commands.registerCommand("quick-vue-template.createVuex", function (url) {
generateTemplate(url.path, 5);
});
context.subscriptions.push(createComp, createMock, createApi, createRouter, createVuex);
}
exports.activate = activate;
function deactivate() {}
module.exports = {
activate,
deactivate,
};