-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtaro-designer-server.js
112 lines (91 loc) · 2.71 KB
/
taro-designer-server.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
/*
* @Author: fang.yang
* @Date: 2020-12-15 17:25:20
* @Description [node server]
*/
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const JSZip = require('jszip');
const prettier = require('prettier');
const app = express();
const { host, port } = require('./server-config');
app.use(bodyParser.json());
const jsxPath = path.join(__dirname, './output/taro.jsx');
const cssPath = path.join(__dirname, './output/index.less');
const formatOptions = {
printWidth: 160,
tabWidth: 4,
arrowParens: 'avoid',
bracketSpacing: true,
htmlWhitespaceSensitivity: 'ignore',
jsxBracketSameLine: true,
singleQuote: true
};
app.post('/taro-designer-api/format', (req, res) => {
let jsxRes = '';
let cssRes = '';
const { jsx, css } = req.body;
function format() {
return new Promise((resolve, reject) => {
jsxRes = prettier.format(jsx, Object.assign(formatOptions, { parser: 'babel' }));
cssRes = prettier.format(css, Object.assign(formatOptions, { parser: 'less' }));
resolve();
});
}
format()
.then(() => {
res.status(200).json({ jsxRes, cssRes });
})
.catch(() => {
res.status(500).json({ msg: '抱歉出错了,请稍后重试~' });
});
});
app.post('/taro-designer-api/download', (req, res) => {
res.setHeader('Content-Type', 'application/zip');
fs.truncateSync(jsxPath);
fs.truncateSync(cssPath);
const zip = new JSZip();
const folder = zip.folder('code');
const { contents, css } = req.body;
function wirteFile() {
return new Promise((resolve, reject) => {
const jsxContent = prettier.format(contents, Object.assign(formatOptions, { parser: 'babel' }));
fs.writeFileSync(jsxPath, jsxContent, { encoding: 'utf8' }, error => {
if (error) {
console.log(error);
throw error;
}
});
const cssContent = prettier.format(css, Object.assign(formatOptions, { parser: 'less' }));
fs.writeFileSync(cssPath, cssContent, { encoding: 'utf8' }, error => {
if (error) {
console.log(error);
throw error;
}
});
resolve();
});
}
wirteFile()
.then(() => {
folder.file('taro.jsx', fs.readFileSync(jsxPath, { encoding: 'utf8' }));
folder.file('index.less', fs.readFileSync(cssPath, { encoding: 'utf8' }));
zip.generateAsync({ type: 'base64' }).then(content => {
res.status(200).send(content);
});
})
.catch(() => {
res.status(500).json({ msg: '下载出错了,请稍后重试~' });
});
});
app.listen(port, host, err => {
if (err) {
console.log(err);
return;
}
console.log(' ============================== ');
console.log(`GOD BLESS ME ~ Mock server listening at http://${host}:${port}`);
console.log(' ============================== ');
});