6 Star 44 Fork 10

Eno/vue-snippets

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
generate.js 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
wscats 提交于 2020-11-30 13:11 . 增加vue组件生成器
const vscode = require("vscode");
const fs = require("fs");
const path = require("path");
const { moduleFile } = require("./template/module");
const { componentFile } = require("./template/component");
const { serviceFile } = require("./template/service");
const findDir = (filePath) => {
if (fs.statSync(filePath).isFile()) {
return path.dirname(filePath);
}
return filePath;
};
const makeDirSync = (dir) => {
if (fs.existsSync(dir)) {
return;
}
if (!fs.existsSync(path.dirname(dir))) {
makeDirSync(path.dirname(dir));
}
fs.mkdirSync(dir);
};
const makeFileSync = (filename, content) => {
if (!fs.existsSync(filename)) {
makeDirSync(path.dirname(filename));
fs.createWriteStream(filename).write(content);
}
};
exports.generateComponent = (file) => {
vscode.window
.showInputBox({
value: "",
prompt: "Component name",
ignoreFocusOut: true,
valueSelection: [-1, -1],
})
.then((name) => {
if (!name) {
return;
}
const componentName = name.charAt(0).toUpperCase() + name.slice(1);
const dir = findDir(file.fsPath);
makeFileSync(
`${dir}/${componentName}.vue`,
componentFile.replace(/{componentName}/g, componentName)
);
});
};
exports.generateService = (file) => {
vscode.window
.showInputBox({
value: "",
prompt: "service name",
ignoreFocusOut: true,
valueSelection: [-1, -1],
})
.then((name) => {
if (!name) {
return;
}
const dir = findDir(file.fsPath);
const targetPath = path.join(dir, name);
makeFileSync(
`${targetPath}/${name}.js`,
serviceFile.replace(/{serviceName}/g, name)
);
});
};
exports.generateModule = (file) => {
vscode.window
.showInputBox({
value: "",
prompt: "module name",
ignoreFocusOut: true,
valueSelection: [-1, -1],
})
.then((name) => {
if (!name) {
return;
}
const dir = findDir(file.fsPath);
makeFileSync(`${dir}/${name}.js`, moduleFile);
});
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/wscats/vue-snippets.git
git@gitee.com:wscats/vue-snippets.git
wscats
vue-snippets
vue-snippets
master

搜索帮助