-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateVue3Component.js
81 lines (75 loc) · 3.09 KB
/
createVue3Component.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateVueComponent = void 0;
const vscode = require("vscode");
const path = require("path");
const fs = require("fs");
//@ts-ignore
const prettier_1 = require("./prettier")
class CreateVueComponent {
constructor() {
this.createCommands();
}
createCommands() {
vscode.commands.registerCommand('extension.createVue3Component',(uri)=>{
this.CreateVue3Component(uri)
} );
}
CreateVue3Component(uri){
vscode.window.showInputBox({placeHolder:'please input component name'}).then(res=>{
if(res){
if(res.match(/^.*[A-Z]+.*$/) === null){
const filePath = path.join(uri.fsPath, res);
//创建文件夹
fs.mkdirSync(filePath)
fs.writeFileSync(path.join(filePath, 'index.less'), '')
fs.writeFileSync(path.join(filePath, 'script.tsx'), `
import { defineComponent } from 'vue'
export default defineComponent({
name: '${res.split('-').map(i=>i.replace(i[0],i[0].toUpperCase())).join('')}',
setup() {
return {}
},
})
`);
this.prettierFiles(path.join(filePath, 'script.tsx'))
fs.writeFileSync(path.join(filePath, 'index.vue'), `
<template>
<div></div>
</template>
<script lang="tsx" src="./script.tsx"></script>
<style lang="less" src="./index.less" scoped></style>
`);
this.prettierFiles(path.join(filePath, 'index.vue'))
vscode.window.showInformationMessage('组件创建成功!')
}else{
vscode.window.showErrorMessage('命名格式不正确,请使用小写横杆间隔命名!')
}
}
})
}
/** 格式化文件 */
prettierFiles(file) {
let rootpath = vscode.workspace.rootPath;
const prettierConfigPath = require.resolve(rootpath + '/.prettierrc.yml');
const options = prettier_1.resolveConfig.sync(file, {
config: prettierConfigPath,
});
const fileInfo = prettier_1.getFileInfo.sync(file);
if (fileInfo.ignored) {
return;
}
try {
const input = fs.readFileSync(file, 'utf8');
const withParserOptions = Object.assign(Object.assign({}, options), { parser: fileInfo.inferredParser });
const output = prettier_1.format(input, withParserOptions);
if (output !== input) {
fs.writeFileSync(file, output, 'utf8');
}
}
catch (e) {
console.log('格式化出错了');
}
}
}
exports.CreateVueComponent = CreateVueComponent;