-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.js
154 lines (144 loc) · 4.19 KB
/
build.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
process.env.NODE_ENV = 'production'
var path = require('path')
var webpack = require('webpack')
var axios = require('axios')
var fs = require('fs')
var config = []
/* getting configs from server */
function getConfigs () {
return Promise.all([
axios.get('https://flespi.io/platform/api.json').then(resp => resp.data),
axios.get('https://flespi.io/gw/api.json').then(resp => resp.data),
axios.get('https://flespi.io/storage/api.json').then(resp => resp.data),
axios.get('https://flespi.io/mqtt/api.json').then(resp => resp.data),
axios.get('https://flespi.io/auth/api.json').then(resp => resp.data)
])
}
/* generate config by config from flespi */
function generate (config) {
/* recursive resolve references by config */
function refResolver (ref) {
const parts = ref.split('/').slice(1)
const result = parts.reduce((result, part) => { return result[part] }, config)
if (result.$ref) { refResolver(result.$ref) } else { return result }
}
/* getting modified parameters by array of parameters from config */
function getParams (parameters) {
return parameters
? parameters.reduce((result, param) => {
if (param.$ref) {
const resolved = refResolver(param.$ref)
result.push({ name: resolved.name, in: resolved.in })
} else {
result.push({ name: param.name, in: param.in })
}
return result
}, [])
: []
}
var result = { basePath: config.basePath, paths: {} }
Object.keys(config.paths).forEach((path) => {
var configByPath = config.paths[path],
methods = Object.keys(configByPath).filter((method) => method !== 'parameters')
result.paths[path] = {}
if (configByPath.parameters) {
result.paths[path].parameters = getParams(configByPath.parameters)
}
methods.forEach((method) => {
result.paths[path][method] = {}
if (config.paths[path][method].parameters) {
result.paths[path][method].parameters = getParams(config.paths[path][method].parameters)
}
})
})
return result
}
/* generate config method */
function generateConfig (name) {
var config = {
mode: 'production',
context: path.resolve(__dirname, 'src'),
entry: ['./index.js'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: name + '.js',
library: ['flespiIO'],
libraryTarget: 'umd'
},
optimization: {
minimize: true
},
externals: [
{
mqtt: 'mqtt',
axios: 'axios'
},
/^lodash/i
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
}
}
if (name === 'main') {
config.output.libraryExport = 'default'
}
/* for to make vue plugin rewrite entry */
if (name === 'vue-plugin' || name === 'rest' || name === 'mqtt') {
config.entry = [`./${name}.js`]
}
if (name === 'module' || name === 'vue-plugin' || name === 'node' || name === 'rest' || name === 'mqtt') {
config.externals = {
axios: 'axios',
'lodash/merge': 'lodash/merge',
'lodash/uniqueId': 'lodash/uniqueId',
'async-mqtt': 'async-mqtt',
mqtt: 'mqtt',
'form-data': 'form-data'
}
}
if (name === 'node') {
config.target = 'node'
config.output.libraryExport = 'default'
config.module.rules.unshift(
{
test: /\.js$/,
loader: 'shebang-loader'
}
)
}
return config
}
/* make build by name */
getConfigs()
.then((configs) => {
configs = configs.map(config => generate(config))
fs.writeFileSync(path.resolve(__dirname, 'src', 'configs.json'), JSON.stringify(configs), 'utf8')
})
.then(() => {
['main', 'module', 'vue-plugin', 'node', 'rest', 'mqtt'].forEach(function (name) {
config.push(generateConfig(name))
})
webpack(config, (err, stats) => {
if (err) {
console.error(err.stack || err)
if (err.details) {
console.error(err.details)
}
return
}
const info = stats.toJson()
if (stats.hasErrors()) {
console.error(info.errors)
}
if (stats.hasWarnings()) {
console.warn(info.warnings)
}
console.log(stats)
})
})