-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
207 lines (178 loc) · 5.94 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const fs = require('fs-extra');
const compiler = require('vue-template-compiler');
const {extname, join, basename, parse: pathparse} = require('path');
const {parse} = require('@vue/component-compiler-utils');
const {log, exists, isFile, isDirectory} = require('./lib/utils');
const {compileStyleFile, scss} = require('./lib/parseVueStyles');
const parseTemplate = require('./lib/parseTemplate');
const traverseScriptAst = require('./lib/traverseScriptAst');
const createWxJson = require('./lib/createWxJson');
const argv = require('yargs-parser')(process.argv, {
alias: {
output: 'o',
input: 'i'
}
});
const {output, input} = argv;
if(!input) return log.error(`'input' parameter expected!`, 'exit');
const COMPONENT_ENTRY_DIR = input//'src/packages/cell';
const COMPONENT_OUTPUT_DIR = output || 'mp-nutui'//'src/mp-nutui';
const MPPREFIX = 'mp-';
const VUEFILE = '.vue';
const STYLEFILE = '.scss';
const MPFILES_MAP = new Map([
['WXML', '.wxml'],
['WXSS', '.wxss'],
['JSON', '.json'],
['JS', '.js']
]);
function parseVueFile(source, filename) {
return parse({
compiler,
source,
filename
});
}
function createMPComponent() {
const hasDir = exists(COMPONENT_ENTRY_DIR) && isDirectory(COMPONENT_ENTRY_DIR);
if(!hasDir) {
log.error('the entry component directory is not exists !', 'exit');
}else{
const files = fs.readdirSync(COMPONENT_ENTRY_DIR);
const component = (COMPONENT_ENTRY_DIR.match(/\/([^\/]+)\/?$/) || [])[1] || '';
convertVues(files, COMPONENT_ENTRY_DIR, component);
}
}
function getVueFiles(files, dir) {
return files.filter(_file => {
const file = join(dir, _file);
return extname(file) === VUEFILE && isFile(file);
});
}
function getSubDirs(files, dir) {
return files.filter(_file => {
const file = join(dir, _file);
return isDirectory(file);
});
}
function convertVues(files = [], dir, compDir, parentDir) {
if(files.length) {
const mainFile = compDir + VUEFILE; // 组件入口文件统一为 [component].vue
const vueFiles = getVueFiles(files, dir);
const subDirs = getSubDirs(files, dir);
if(vueFiles.length) {
if(!~vueFiles.indexOf(basename(mainFile))) {
// 验证组件是否存在主文件 [component].vue
log.error('component main file is not exists !', 'exit');
}
for(const file of vueFiles) {
const currDir = dir;
const filename = pathparse(file).name;
const filepath = join(dir, file);
if(mainFile === file) {
console.log('\n组件主文件:', file);
const stylepath = join(dir, compDir + STYLEFILE);
createMP(null, filename, filepath, parentDir, stylepath);
}else{
console.log('\n组件依赖文件:', file);
// 生成依赖组件的文件夹
const subDir = pathparse(mainFile).name;
const stylepath = join(dir, filename + STYLEFILE);
fs.ensureDirSync(join(COMPONENT_OUTPUT_DIR, subDir));
createMP(subDir, filename, filepath, parentDir, stylepath);
}
}
}
if(subDirs.length) {
subDirs.forEach(subdir => {
const currDir = join(dir, subdir);
const subFiles = fs.readdirSync(currDir);
convertVues(subFiles, currDir, subdir, compDir)
});
}
}
}
/**
* 创建小程序组件 */
async function createMP(basedir, fileName, filepath, parentDir, stylepath) {
for(const ext of MPFILES_MAP.values()) {
const fullfile = join(COMPONENT_OUTPUT_DIR, basedir || '', parentDir || '', fileName, fileName + ext);
fs.ensureFileSync(fullfile);
if(filepath) {
const source = fs.readFileSync(filepath);
const {template, script, styles} = parseVueFile(source.toString(), filepath + VUEFILE);
//console.log("script:",script)
if(ext === MPFILES_MAP.get('WXML') && template && template.content) {
fs.writeFileSync(fullfile, parseTemplate(template.content));
console.log('已生成小程序文件.wxml:%s', fullfile);
}
let styleStr = '';
if(styles && styles.length) {
try{
const styleContent = styles.reduce((content, curr) => {
return content += curr.content;
}, '');
styleStr += await scss(styleContent);
}catch(_) {}
}
if(stylepath && exists(stylepath)) {
const stylesFromFile = await compileStyleFile(stylepath, STYLEFILE);
styleStr += stylesFromFile;
}
if(ext === MPFILES_MAP.get('WXSS')) {
fs.writeFileSync(fullfile, styleStr);
console.log('已生成小程序文件.wxss:%s', fullfile);
}
if(ext === MPFILES_MAP.get('JSON')) {
const json = createWxJson(fileName);
fs.writeFileSync(fullfile, JSON.stringify(json));
console.log('已生成小程序文件.json:%s', fullfile);
}
if(ext === MPFILES_MAP.get('JS') && script && script.content) {
const testData = `
export default {
name: "nut-cell",
props: {
title: {
type: String,
default: ""
},
subTitle: {
type: String,
default: ""
},
desc: {
type: String,
default: ""
},
isLink: {
type: Boolean,
default: false
},
linkUrl: {
type: String,
default: null
},
showIcon: {
type: Boolean,
default: false
},
bgColor: {
type: String,
default: "#FFFFFF"
}
},
data() {
return {};
},
methods: {}
};
`
console.log("fullfile",fullfile,"script",script.content)
fs.writeFileSync(fullfile, traverseScriptAst(script.content));
console.log('已生成小程序文件:%s', fullfile);
}
}
}
}
createMPComponent();