-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
171 lines (149 loc) · 4.26 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
var vuetojs = require('vue-to-js');
var path = require('path');
var fs = require('fs');
var compName = process.argv[2];
var compPath = path.resolve(__dirname, 'components', compName, 'index.vue');
var blocks = vuetojs.getBlocks(compPath);
var config;
try {
config = JSON.parse(blocks.config[0].code);
}
catch (e) {
config = {};
}
var configData = config.data ? jsObjToPhpArr(config.data) : 'array()';
var configComponents = config.components ? jsObjToPhpArr(config.components) : 'array()';
function jsObjToPhpArr(obj) {
var ret = 'array(';
var keys = Object.keys(obj);
keys.forEach(function (e, i) {
var val = obj[e];
ret += '"' + e + '" => ';
if (typeof val === 'string') {
val = val.replace(/(["'])/g, '\\$1');
ret += '"' + val + '"';
}
else if (typeof val === 'object') {
ret += jsObjToPhpArr(val);
}
else {
ret += val;
}
if (i + 1 < keys.length) {
ret += ',';
}
});
ret += ')';
return ret;
}
function jsObjToStr(obj, fn) {
fn = fn || function (str) {return "'" + str + "'";};
var ret = '';
if (Array.isArray(obj)) {
ret = '[';
obj.forEach(function (e, i) {
if (typeof e === 'object') {
ret += jsObjToStr(e);
}
else {
ret += fn(e);
}
if (i + 1 < obj.length) {
ret += ',';
}
});
ret += ']';
}
else {
ret = '{';
var keys = Object.keys(obj);
keys.forEach(function (e, i) {
var val = obj[e];
ret += '"' + e + '": ';
if (typeof val === 'object') {
ret += jsObjToStr(val);
}
else {
ret += fn(val);
}
if (i + 1 < keys.length) {
ret += ',';
}
});
ret += '}';
}
return ret;
}
var template = blocks.template.code;
var phpCompiler = require('./vue-template-php-compiler/build');
var phpInfo = phpCompiler.compile(template);
var styleCode = blocks.styles.map(function (e, i) {
return e.code;
}).join('').replace(/\n/g, '');
var phpRender = phpInfo.render;
var phpStaticRender = phpInfo.staticRenderFns;
var className = compName.replace(/-/g, '_');
className = className.split('_').map(function (e, i) {
var chars = e.split('');
chars[0] = chars[0].toUpperCase();
return chars.join('');
}).join('_');
var phpCode = `<?php
include_once('Vue_Base.php');
class ${className} extends Vue_Base {
public $_d = ${configData};
public $options = array(
"components" => ${configComponents}
);
public $style = "${styleCode}";
${phpStaticRender.map(function (fn, i) {
return `
public function _m${i}($ctx) {
${fn}
}
`;
}).join('\n')}
public function _render($ctx) {
${phpRender}
}
}
`;
var phpPath = path.resolve(__dirname, 'components', compName, 'index.php');
fs.writeFileSync(phpPath, phpCode);
var transform = require('babel-core').transform;
function getScript(code) {
var result = transform(code, {
plugins: ['transform-es2015-modules-commonjs'],
presets: ['env']
});
return result.code;
}
var jsData = config.data ? jsObjToStr(config.data, function (str) {
if (typeof str === 'string') {
return "'" + str + "'";
}
else {
return str;
}
}) : '{}';
var jsComponents = config.components ? jsObjToStr(config.components, function (str) {
var filepath = str;
return 'require(\'' + filepath + '\')';
}) : '{}';
var jsCode = `
var _module1 = {
exports: {}
};
(function (module, exports) {
${getScript(blocks.script.code)}
})(_module1, _module1.exports);
var obj = _module1.exports.default || _module1.exports;
obj.data = function () {
return ${jsData};
};
obj.components = ${jsComponents};
obj.template = \`${template}\`;
module.exports = obj;
`;
var jsPath = path.resolve(__dirname, 'components', compName, 'index.js');
fs.writeFileSync(jsPath, jsCode);