-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
96 lines (77 loc) · 2.5 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
const path = require('path')
const fse = require('fs-extra')
let defaultOptions = {
root: process.cwd(),
replace: false,
debug: false,
env: 'wechat',
dirname: 'index',
filename: '',
files: []
}
function AppFileCreate(options){
options = Object.assign({}, defaultOptions, options || {});
options.filename = options.filename || options.dirname;
let pageRoot = path.join(options.root, options.dirname, '/');
let env = options.env;
let files = [];
let outputFiles = [];
// handler files
options.files.forEach((item, idx) => {
if(typeof item === 'string'){
item = {
ext: item
};
}
let fileOptions = {
filename: item.filename || options.filename,
ext: item.ext
};
let template = '';
if(item.hasOwnProperty('template')){
template = item.template;
}else{
switch(fileOptions.ext){
case 'js':
template = require('./templates/page.js');
break;
case 'json':
template = require(`./templates/${ env === 'alipay' ? 'alipay' : 'wechat' }-json.js`);
break;
case 'wxml':
case 'axml':
case 'xml':
template = require('./templates/xml.js');
break;
}
if(env !== 'alipay' && env !== 'wechat'){
template = '';
}
}
if(typeof template === 'function'){
fileOptions.template = template(item.args || {});
}else if(typeof template === 'string'){
fileOptions.template = template;
}
fileOptions.filePath = pageRoot;
fileOptions.file = path.join(pageRoot, fileOptions.filename + '.' + fileOptions.ext);
files.push(fileOptions);
// create files
let exists = fse.pathExistsSync(fileOptions.file);
if(!exists || options.replace === true){
fse.outputFileSync(fileOptions.file, fileOptions.template);
outputFiles.push(fileOptions);
if(options.debug === true){
console.log(`${ exists ? 'replaced' : 'created'}: ${fileOptions.file}`);
}
}
});
return {
files: files,
outputFiles: outputFiles
}
}
AppFileCreate.config = function(options){
Object.assign(defaultOptions, options);
}
module.exports = AppFileCreate