-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·66 lines (51 loc) · 2.07 KB
/
index.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
#! /usr/bin/env node
const path = require('path');
const fs = require('fs')
const shell = require('shelljs');
const { program } = require('commander');
const package = require('./package.json');
const genVue = require('./genVue')
program
.version(package.version, '-v, --version')
.option('-p, --path [name]', '输入小程序目录 如: /pages/index/index')
program.on('--help', () => {
console.log('');
console.log('Example call:');
console.log(' $ custom-help --help');
});
program.parse(process.argv);
if (program.path) {
// console.log(shell.pwd().stdout, program.path)
const pageBasePath = path.join(shell.pwd().stdout, 'dist/dev/mp-weixin', program.path)
const pageSkeletonWXMLPath = pageBasePath + '.skeleton.wxml'
const pageSkeletonWXSSPath = pageBasePath + '.skeleton.wxss'
// console.log(pageBasePath,pageSkeletonWXMLPath,pageSkeletonWXSSPath)
if (!fs.existsSync(pageSkeletonWXMLPath) || !fs.existsSync(pageSkeletonWXSSPath)) {
console.log('页面骨架图不存在,请检查路径是否错误或是否用微信开发者工具生成过骨架图')
return
}
// 合并文件
let wxmlString = fs.readFileSync(pageSkeletonWXMLPath, {
encoding: 'utf8'
})
// 删除可能会导致编译报错的属性
wxmlString = wxmlString.replace(/is=\"(.*?)\"/ig ,'')
wxmlString = wxmlString.replace(/data-event-opts=\"(.*?)\"/ig ,'')
wxmlString = wxmlString.replace(/name=\"(.*?)\"/ig ,'')
// console.log(wxmlString)
const wxssString = fs.readFileSync(pageSkeletonWXSSPath, {
encoding: 'utf8'
})
const vueString = genVue(wxmlString, wxssString)
// console.log(vueString)
const pathArr = program.path.split('/')
const pageName = pathArr.pop()
const outputPath = path.join(shell.pwd().stdout, 'src', pathArr.join('/'))
// console.log(outputPath)
fs.writeFileSync(path.join(outputPath, `${pageName}.skeleton.vue`), vueString, {
encoding:'utf8'
})
console.log('生成成功!')
} else {
console.log('请使用-p指定小程序页面路径')
}