forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexAPI.js
More file actions
313 lines (279 loc) · 7.71 KB
/
IndexAPI.js
File metadata and controls
313 lines (279 loc) · 7.71 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
const semver = require('semver')
const { merge } = require('webpack-merge')
const { fatal } = require('../helpers/logger')
const getPackageJson = require('../helpers/get-package-json')
const getCallerPath = require('../helpers/get-caller-path')
const extensionJson = require('./extension-json')
const BaseAPI = require('./BaseAPI')
// for backward compatibility
function getPackageName (packageName) {
return packageName === '@quasar/app'
? '@quasar/app-webpack'
: packageName
}
/**
* API for extension's /index.js script
*/
module.exports = class IndexAPI extends BaseAPI {
ctx
__hooks = {
extendQuasarConf: [],
extendWebpack: [],
chainWebpackMainElectronProcess: [],
extendWebpackMainElectronProcess: [],
chainWebpackWebserver: [],
extendWebpackWebserver: [],
chainWebpack: [],
beforeDev: [],
afterDev: [],
beforeBuild: [],
afterBuild: [],
onPublish: [],
commands: {},
describeApi: {}
}
constructor ({ ctx, ...opts }) {
super(opts)
this.ctx = ctx
}
/**
* Get the internal persistent config of this extension.
* Returns empty object if it has none.
*
* @return {object} cfg
*/
getPersistentConf () {
return extensionJson.getInternal(this.extId)
}
/**
* Set the internal persistent config of this extension.
* If it already exists, it is overwritten.
*
* @param {object} cfg
*/
setPersistentConf (cfg) {
extensionJson.setInternal(this.extId, cfg || {})
}
/**
* Deep merge into the internal persistent config of this extension.
* If extension does not have any config already set, this is
* essentially equivalent to setting it for the first time.
*
* @param {object} cfg
*/
mergePersistentConf (cfg = {}) {
const currentCfg = this.getPersistentConf()
this.setPersistentConf(merge({}, currentCfg, cfg))
}
/**
* Ensure the App Extension is compatible with
* host app package through a
* semver condition.
*
* If the semver condition is not met, then
* @quasar/app-webpack errors out and halts execution
*
* Example of semver condition:
* '1.x || >=2.5.0 || 5.0.0 - 7.2.3'
*
* @param {string} packageName
* @param {string} semverCondition
*/
compatibleWith (packageName, semverCondition) {
const name = getPackageName(packageName)
const json = getPackageJson(name)
if (json === void 0) {
fatal(`Extension(${this.extId}): Dependency not found - ${name}. Please install it.`)
}
if (!semver.satisfies(json.version, semverCondition)) {
fatal(`Extension(${this.extId}): is not compatible with ${name} v${json.version}. Required version: ${semverCondition}`)
}
}
/**
* Check if a host app package is installed. Can also
* check its version against specific semver condition.
*
* Example of semver condition:
* '1.x || >=2.5.0 || 5.0.0 - 7.2.3'
*
* @param {string} packageName
* @param {string} (optional) semverCondition
* @return {boolean} package is installed and meets optional semver condition
*/
hasPackage (packageName, semverCondition) {
const name = getPackageName(packageName)
const json = getPackageJson(name)
if (json === void 0) {
return false
}
return semverCondition !== void 0
? semver.satisfies(json.version, semverCondition)
: true
}
/**
* Check if another app extension is installed
* (app extension npm package is installed and it was invoked)
*
* @param {string} extId
* @return {boolean} has the extension installed & invoked
*/
hasExtension (extId) {
return extensionJson.has(extId)
}
/**
* Get the version of a host app package.
*
* @param {string} packageName
* @return {string|undefined} version of app's package
*/
getPackageVersion (packageName) {
const name = getPackageName(packageName)
const json = getPackageJson(name)
return json !== void 0
? json.version
: void 0
}
/**
* Extend quasar.config.js
*
* @param {function} fn
* (cfg: Object, ctx: Object) => undefined
*/
extendQuasarConf (fn) {
this.__addHook('extendQuasarConf', fn)
}
/**
* Chain webpack config
*
* @param {function} fn
* (cfg: ChainObject, invoke: Object {isClient, isServer}) => undefined
*/
chainWebpack (fn) {
this.__addHook('chainWebpack', fn)
}
/**
* Extend webpack config
*
* @param {function} fn
* (cfg: Object, invoke: Object {isClient, isServer}) => undefined
*/
extendWebpack (fn) {
this.__addHook('extendWebpack', fn)
}
/**
* Chain webpack config of main electron process
*
* @param {function} fn
* (cfg: ChainObject) => undefined
*/
chainWebpackMainElectronProcess (fn) {
this.__addHook('chainWebpackMainElectronProcess', fn)
}
/**
* Extend webpack config of main electron process
*
* @param {function} fn
* (cfg: Object) => undefined
*/
extendWebpackMainElectronProcess (fn) {
this.__addHook('extendWebpackMainElectronProcess', fn)
}
/**
* Chain webpack config of SSR webserver
*
* @param {function} fn
* (cfg: ChainObject) => undefined
*/
chainWebpackWebserver (fn) {
this.__addHook('chainWebpackWebserver', fn)
}
/**
* Extend webpack config of SSR webserver
*
* @param {function} fn
* (cfg: Object) => undefined
*/
extendWebpackWebserver (fn) {
this.__addHook('extendWebpackWebserver', fn)
}
/**
* Register a command that will become available as
* `quasar run <ext-id> <cmd> [args]` and `quasar <ext-id> <cmd> [args]`
*
* @param {string} commandName
* @param {function} fn
* ({ args: [ string, ... ], params: {object} }) => ?Promise
*/
registerCommand (commandName, fn) {
this.__hooks.commands[commandName] = fn
}
/**
* Register an API file for "quasar describe" command
*
* @param {string} name
* @param {string} relativePath (or node_modules reference if it starts with "~")
* (relative path to Api file)
*/
registerDescribeApi (name, relativePath) {
const callerPath = getCallerPath()
this.__hooks.describeApi[name] = {
callerPath,
relativePath
}
}
/**
* Prepare external services before dev command runs.
*
* @param {function} fn
* (api, { quasarConf }) => ?Promise
*/
beforeDev (fn) {
this.__addHook('beforeDev', fn)
}
/**
* Run hook after Quasar dev server is started ($ quasar dev).
* At this point, the dev server has been started and is available
* should you wish to do something with it.
*
* @param {function} fn
* (api, { quasarConf }) => ?Promise
*/
afterDev(fn) {
this.__addHook('afterDev', fn)
}
/**
* Run hook before Quasar builds app for production ($ quasar build).
* At this point, the distributables folder hasn't been created yet.
*
* @param {function} fn
* (api, { quasarConf }) => ?Promise
*/
beforeBuild (fn) {
this.__addHook('beforeBuild', fn)
}
/**
* Run hook after Quasar built app for production ($ quasar build).
* At this point, the distributables folder has been created and is available
* should you wish to do something with it.
*
* @param {function} fn
* (api, { quasarConf }) => ?Promise
*/
afterBuild (fn) {
this.__addHook('afterBuild', fn)
}
/**
* Run hook if publishing was requested ("$ quasar build -P"),
* after Quasar built app for production and the afterBuild
* hook (if specified) was executed.
*
* @param {function} fn
* ({ arg, ...}) => ?Promise
* * arg - argument supplied to "--publish"/"-P" parameter
* * quasarConf - quasar.config.js config object
* * distDir - folder where distributables were built
*/
onPublish (fn) {
this.__addHook('onPublish', fn)
}
}