基于 Node 生成 vue 模板文件
代码如下:
const fs = require('fs');
const path = require('path');
// 获取到组件名
const args = process.argv.splice(2);
const componentName = args[0];
console.log('prepare to creat a ' + componentName + ' component');
let root = './src/components/';
// 读取模板文件,并修改内容
let template = fs.readFileSync(path.join(__dirname, './template.vue'), 'utf8');
let content = template.replace(/componentName/g, componentName); // target file content
// 目标文件夹和目标文件的路径
let targetDirPath = path.join(__dirname, root, componentName);
let targetFilePath = path.join(__dirname, root, componentName, componentName + '.vue');
// mkdirSync
if (!fs.existsSync(targetDirPath)) {
fs.mkdirSync(targetDirPath);
console.log('The ' + targetDirPath + ' folder has been created!');
}
// writeFile async
if (!fs.existsSync(targetFilePath)) {
fs.writeFile(targetFilePath, content, (err) => {
if (err) throw err;
console.log('The ' + targetFilePath + ' has been created!');
});
} else {
console.error('error!\n' + targetFilePath + ' has already been existed!');
}
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
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
template.vue
的内容如下:
<template>
<div class="componentName">
</div>
</template>
<script>
export default {
name: 'componentName',
data () {
return {
}
}
}
</script>
<style lang="scss" scoped>
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
上次更新: 2024/01/30, 00:35:17
- 02
- Node与GLIBC_2.27不兼容解决方案08-19
- 03
- Git清空本地文件跟踪缓存08-13