-
Notifications
You must be signed in to change notification settings - Fork 121
/
vite.js
103 lines (93 loc) · 2.63 KB
/
vite.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
const path = require('path')
const fs = require('fs')
const DocTypeInterfaceGenerator = require('./scripts/generateInterface')
module.exports = function proxyOptions({
port = 8080,
source = '^/(app|login|api|assets|files|private)',
} = {}) {
const commonSiteConfig = getCommonSiteConfig()
const webserver_port = commonSiteConfig
? commonSiteConfig.webserver_port
: 8000
if (!commonSiteConfig) {
console.log('No common_site_config.json found, using default port 8000')
}
let proxy = {}
proxy[source] = {
target: `http://127.0.0.1:${webserver_port}`,
ws: true,
router: function (req) {
const site_name = req.headers.host.split(':')[0]
return `http://${site_name}:${webserver_port}`
},
}
return {
name: 'frappeui-vite-plugin',
config: async () => {
await generateDocTypeInterfaces()
return {
server: {
port: port,
proxy: proxy,
},
}
},
}
}
async function generateDocTypeInterfaces() {
const config = getConfig()
if (!(config && config.typeGeneration && config.typeGeneration.input)) return
const frontendFolder = process.cwd()
let outputPath = config.typeGeneration.output || 'src/types/doctypes.ts'
if (!path.isAbsolute(outputPath)) {
outputPath = path.join(frontendFolder, outputPath)
}
const appsFolder = findAppsFolder()
if (!appsFolder) {
console.error('Could not find frappe-bench/apps folder')
return
}
const generator = new DocTypeInterfaceGenerator(
appsFolder,
config.typeGeneration.input,
outputPath,
)
await generator.generate()
}
function getCommonSiteConfig() {
let currentDir = path.resolve('.')
// traverse up till we find frappe-bench with sites directory
while (currentDir !== '/') {
if (
fs.existsSync(path.join(currentDir, 'sites')) &&
fs.existsSync(path.join(currentDir, 'apps'))
) {
let configPath = path.join(currentDir, 'sites', 'common_site_config.json')
if (fs.existsSync(configPath)) {
return JSON.parse(fs.readFileSync(configPath))
}
return null
}
currentDir = path.resolve(currentDir, '..')
}
return null
}
function findAppsFolder() {
let currentDir = process.cwd()
while (currentDir !== '/') {
if (
fs.existsSync(path.join(currentDir, 'apps')) &&
fs.existsSync(path.join(currentDir, 'sites'))
) {
return path.join(currentDir, 'apps')
}
currentDir = path.resolve(currentDir, '..')
}
return null
}
function getConfig() {
let configPath = path.join(process.cwd(), 'frappeui.json')
if (fs.existsSync(configPath)) {
return JSON.parse(fs.readFileSync(configPath))
}
}