@vcmap/plugin-cli 1.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +173 -36
- package/assets/helloWorld/.gitignore +2 -0
- package/assets/helloWorld/.gitlab-ci.yml +93 -0
- package/assets/helloWorld/src/helloWorld.vue +40 -0
- package/assets/helloWorld/src/index.js +35 -0
- package/assets/index.html +81 -0
- package/cli.js +26 -16
- package/index.js +6 -11
- package/package.json +45 -20
- package/src/build.js +88 -26
- package/src/context.js +9 -12
- package/src/create.js +122 -44
- package/src/defaultCommand.js +26 -8
- package/src/hostingHelpers.js +300 -0
- package/src/licenses.js +128 -0
- package/src/pack.js +28 -52
- package/src/packageJsonHelpers.js +10 -14
- package/src/preview.js +107 -0
- package/src/serve.js +98 -183
- package/src/setupMapUi.js +9 -0
- package/src/getWebpackConfig.js +0 -290
- package/src/webpack.config.js +0 -29
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { fileURLToPath, URL } from 'url';
|
|
2
|
+
import { promisify } from 'util';
|
|
3
|
+
import { exec } from 'child_process';
|
|
4
|
+
import https from 'https';
|
|
5
|
+
import http from 'http';
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import { logger } from '@vcsuite/cli-logger';
|
|
9
|
+
import { getContext, resolveContext } from './context.js';
|
|
10
|
+
import { getPluginEntry, getPluginName } from './packageJsonHelpers.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {Object} HostingOptions
|
|
14
|
+
* @property {string} [config] - an optional fileName to use for configuring the plugin
|
|
15
|
+
* @property {string} [auth] - potential auth string to download assets (index.html, config) with
|
|
16
|
+
* @property {number} [port]
|
|
17
|
+
* @property {boolean} [https]
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @type {(arg1: string, opt?: Object) => Promise<string>}
|
|
22
|
+
*/
|
|
23
|
+
const promiseExec = promisify(exec);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {...string} pathSegments
|
|
27
|
+
* @returns {string}
|
|
28
|
+
*/
|
|
29
|
+
export function resolveMapUi(...pathSegments) {
|
|
30
|
+
return path.join(getContext(), 'node_modules', '@vcmap', 'ui', ...pathSegments);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function checkReservedDirectories() {
|
|
34
|
+
['assets', 'plugins', 'config']
|
|
35
|
+
.forEach((dir) => {
|
|
36
|
+
if (fs.existsSync(path.join(getContext(), dir))) {
|
|
37
|
+
logger.warning(`found reserved directory ${dir}. serving my not work as exptected`);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @returns {string}
|
|
44
|
+
*/
|
|
45
|
+
export function getDirname() {
|
|
46
|
+
return path.dirname(fileURLToPath(import.meta.url));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {string} stringUrl
|
|
51
|
+
* @param {string} auth
|
|
52
|
+
* @param {function(http.IncomingMessage)} handler
|
|
53
|
+
*/
|
|
54
|
+
export function httpGet(stringUrl, auth, handler) {
|
|
55
|
+
const url = new URL(stringUrl);
|
|
56
|
+
const options = {
|
|
57
|
+
hostname: url.hostname,
|
|
58
|
+
port: url.port,
|
|
59
|
+
path: url.pathname,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
if (auth) {
|
|
63
|
+
options.headers = { Authorization: `Basic ${Buffer.from(auth).toString('base64')}` };
|
|
64
|
+
}
|
|
65
|
+
if (url.protocol === 'https:') {
|
|
66
|
+
https.get(options, handler);
|
|
67
|
+
} else {
|
|
68
|
+
http.get(options, handler);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {string} fileName
|
|
74
|
+
* @returns {Promise<Object>}
|
|
75
|
+
*/
|
|
76
|
+
export async function readConfigJson(fileName) {
|
|
77
|
+
const configFileName = fileName || resolveContext('config.json');
|
|
78
|
+
let config = {};
|
|
79
|
+
if (fs.existsSync(configFileName)) {
|
|
80
|
+
const content = await fs.promises.readFile(configFileName);
|
|
81
|
+
config = JSON.parse(content.toString());
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return config;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const configMap = new Map();
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @returns {Promise<string>}
|
|
91
|
+
*/
|
|
92
|
+
export async function printVcmapUiVersion() {
|
|
93
|
+
const packageJsonPath = resolveMapUi('package.json');
|
|
94
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
95
|
+
throw new Error(`Cannot find the @vcmap/ui package in ${getContext()}. Are you sure you installed it?`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const content = await fs.promises.readFile(packageJsonPath);
|
|
99
|
+
const { version } = JSON.parse(content.toString());
|
|
100
|
+
logger.info(`Using @vcmap/ui version: ${version} found in current project.`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param {Object} config
|
|
105
|
+
* @param {Object} pluginConfig
|
|
106
|
+
* @param {boolean} production
|
|
107
|
+
* @returns {Promise<void>}
|
|
108
|
+
*/
|
|
109
|
+
export async function reWriteConfig(config, pluginConfig, production) {
|
|
110
|
+
config.plugins = config.plugins ?? []; // XXX check if we have plugins in this repos dependencies?
|
|
111
|
+
pluginConfig.entry = production ? 'dist/index.js' : await getPluginEntry();
|
|
112
|
+
pluginConfig.name = await getPluginName();
|
|
113
|
+
const idx = config.plugins.findIndex(p => p.name === pluginConfig.name);
|
|
114
|
+
if (idx > -1) {
|
|
115
|
+
config.plugins.splice(idx, 1, pluginConfig);
|
|
116
|
+
} else {
|
|
117
|
+
config.plugins.push(pluginConfig);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {string} [mapConfig] - fs or https to config. defaults to @vcmap/ui/map.config.json
|
|
123
|
+
* @param {string} [auth]
|
|
124
|
+
* @param {boolean} [production]
|
|
125
|
+
* @param {string} [configFile]
|
|
126
|
+
* @returns {Promise<unknown>}
|
|
127
|
+
*/
|
|
128
|
+
export function getConfigJson(mapConfig, auth, production, configFile) {
|
|
129
|
+
const usedConfig = mapConfig || resolveMapUi('map.config.json');
|
|
130
|
+
if (configMap.has('map.config.json')) {
|
|
131
|
+
return Promise.resolve(configMap.get('map.config.json'));
|
|
132
|
+
}
|
|
133
|
+
const isWebVcm = /^https?:\/\//.test(usedConfig);
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
function handleStream(stream) {
|
|
136
|
+
let data = '';
|
|
137
|
+
stream.on('data', (chunk) => {
|
|
138
|
+
data += chunk.toString();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
stream.on('close', async () => {
|
|
142
|
+
try {
|
|
143
|
+
const configJson = JSON.parse(data);
|
|
144
|
+
configMap.set('map.config.json', configJson);
|
|
145
|
+
const pluginConfig = await readConfigJson(configFile);
|
|
146
|
+
await reWriteConfig(configJson, pluginConfig, production);
|
|
147
|
+
resolve(configJson);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
reject(e);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
if (isWebVcm) {
|
|
154
|
+
httpGet(usedConfig, auth, (res) => {
|
|
155
|
+
if (res.statusCode < 400) {
|
|
156
|
+
handleStream(res);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
} else {
|
|
160
|
+
handleStream(fs.createReadStream(path.join(usedConfig)));
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @param {string} stringUrl
|
|
167
|
+
* @param {string} auth
|
|
168
|
+
* @returns {Promise<string>}
|
|
169
|
+
*/
|
|
170
|
+
function getIndexHtml(stringUrl, auth) {
|
|
171
|
+
return new Promise((resolve, reject) => {
|
|
172
|
+
httpGet(stringUrl, auth, (res) => {
|
|
173
|
+
let index = '';
|
|
174
|
+
if (res.statusCode >= 400) {
|
|
175
|
+
logger.error('got status code: ', res.statusCode);
|
|
176
|
+
reject(new Error(`StatusCode ${res.statusCode}`));
|
|
177
|
+
}
|
|
178
|
+
res.on('data', (chunk) => {
|
|
179
|
+
index += chunk.toString();
|
|
180
|
+
});
|
|
181
|
+
res.on('end', () => {
|
|
182
|
+
resolve(index);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @returns {import("vite").Plugin}
|
|
190
|
+
*/
|
|
191
|
+
export function createConfigJsonReloadPlugin() {
|
|
192
|
+
return {
|
|
193
|
+
name: 'ConfigJsonReload',
|
|
194
|
+
handleHotUpdate({ file }) {
|
|
195
|
+
if (file === path.join(getContext(), 'config.json')) {
|
|
196
|
+
configMap.clear();
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @param {Express} app
|
|
204
|
+
* @param {string} mapConfig
|
|
205
|
+
* @param {string} [auth]
|
|
206
|
+
* @param {string} [configFile]
|
|
207
|
+
* @param {boolean} [production]
|
|
208
|
+
*/
|
|
209
|
+
export function addMapConfigRoute(app, mapConfig, auth, configFile, production) {
|
|
210
|
+
app.get('/map.config.json', (req, res) => {
|
|
211
|
+
getConfigJson(mapConfig, auth, production, configFile)
|
|
212
|
+
.then((config) => {
|
|
213
|
+
const stringConfig = JSON.stringify(config, null, 2);
|
|
214
|
+
res.setHeader('Content-Type', 'application/json');
|
|
215
|
+
res.write(stringConfig);
|
|
216
|
+
res.end();
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @param {Express} app
|
|
223
|
+
* @param {string} [auth]
|
|
224
|
+
* @param {string} [configFileName]
|
|
225
|
+
* @param {boolean} [production]
|
|
226
|
+
*/
|
|
227
|
+
export async function addConfigRoute(app, auth, configFileName, production) { // IDEA pass in available plugins and strip unavailable ones?
|
|
228
|
+
const mapUiDir = resolveMapUi();
|
|
229
|
+
const pluginConfig = await readConfigJson(configFileName);
|
|
230
|
+
|
|
231
|
+
app.get('/config*', async (req, res) => {
|
|
232
|
+
const { url } = req;
|
|
233
|
+
const fileName = path.join(mapUiDir, ...url.substring(1).split('/'));
|
|
234
|
+
let response;
|
|
235
|
+
if (configMap.has(url)) {
|
|
236
|
+
response = JSON.stringify(configMap.get(url));
|
|
237
|
+
} else if (fs.existsSync(fileName)) {
|
|
238
|
+
try {
|
|
239
|
+
const configContent = await fs.promises.readFile(fileName);
|
|
240
|
+
const config = JSON.parse(configContent);
|
|
241
|
+
configMap.set(url, config);
|
|
242
|
+
await reWriteConfig(config, pluginConfig, production);
|
|
243
|
+
response = JSON.stringify(config);
|
|
244
|
+
} catch (e) {
|
|
245
|
+
configMap.delete(url);
|
|
246
|
+
logger.warning(`Failed to parse config ${url}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (!response) {
|
|
251
|
+
res.statusCode = 404;
|
|
252
|
+
} else {
|
|
253
|
+
res.setHeader('Content-Type', 'application/json');
|
|
254
|
+
res.write(response);
|
|
255
|
+
}
|
|
256
|
+
res.end();
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* @param {boolean} [production]
|
|
262
|
+
* @returns {Promise<string>}
|
|
263
|
+
*/
|
|
264
|
+
export async function getMapUiIndexHtml(production) {
|
|
265
|
+
const indexHtmlFileName = production ?
|
|
266
|
+
resolveMapUi('dist', 'index.html') :
|
|
267
|
+
path.join(getDirname(), '..', 'assets', 'index.html');
|
|
268
|
+
const buffer = await fs.promises.readFile(indexHtmlFileName);
|
|
269
|
+
return buffer.toString();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* @param {Express} app
|
|
274
|
+
* @param {import("vite").ViteDevServer} server
|
|
275
|
+
* @param {boolean} [production]
|
|
276
|
+
* @param {string} [hostedVcm]
|
|
277
|
+
* @param {string} [auth]
|
|
278
|
+
*/
|
|
279
|
+
export function addIndexRoute(app, server, production, hostedVcm, auth) {
|
|
280
|
+
app.get('/', async (req, res) => {
|
|
281
|
+
let originalIndex = hostedVcm ?
|
|
282
|
+
await getIndexHtml(`${hostedVcm}/`, auth) :
|
|
283
|
+
await getMapUiIndexHtml(production); // TODO change hosted vcm index via option?
|
|
284
|
+
|
|
285
|
+
originalIndex = await server.transformIndexHtml('/index.html', originalIndex);
|
|
286
|
+
|
|
287
|
+
res.status(200)
|
|
288
|
+
.set({ 'Content-Type': 'text/html' })
|
|
289
|
+
.end(originalIndex);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* @param {string} command
|
|
295
|
+
* @returns {Promise<string>}
|
|
296
|
+
*/
|
|
297
|
+
export function executeUiNpm(command) {
|
|
298
|
+
const mapUiDir = resolveMapUi();
|
|
299
|
+
return promiseExec(`npm run ${command}`, { cwd: mapUiDir });
|
|
300
|
+
}
|
package/src/licenses.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} user
|
|
6
|
+
* @param {number} year
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
function mit(user, year) {
|
|
10
|
+
return `
|
|
11
|
+
Copyright ${year} ${user}
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
14
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
|
|
15
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
16
|
+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
21
|
+
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
|
22
|
+
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
23
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24
|
+
`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {string} user
|
|
29
|
+
* @param {number} year
|
|
30
|
+
* @returns {string}
|
|
31
|
+
*/
|
|
32
|
+
function apache(user, year) {
|
|
33
|
+
return `
|
|
34
|
+
Copyright ${year} ${user}
|
|
35
|
+
|
|
36
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
37
|
+
you may not use this file except in compliance with the License.
|
|
38
|
+
You may obtain a copy of the License at
|
|
39
|
+
|
|
40
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
41
|
+
|
|
42
|
+
Unless required by applicable law or agreed to in writing, software
|
|
43
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
44
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
45
|
+
See the License for the specific language governing permissions and
|
|
46
|
+
limitations under the License.
|
|
47
|
+
`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {string} user
|
|
52
|
+
* @param {number} year
|
|
53
|
+
* @returns {string}
|
|
54
|
+
*/
|
|
55
|
+
function gpl3(user, year) {
|
|
56
|
+
return `
|
|
57
|
+
Copyright (C) ${year} ${user}
|
|
58
|
+
|
|
59
|
+
This program is free software: you can redistribute it and/or modify
|
|
60
|
+
it under the terms of the GNU General Public License as published by
|
|
61
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
62
|
+
(at your option) any later version.
|
|
63
|
+
|
|
64
|
+
This program is distributed in the hope that it will be useful,
|
|
65
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
66
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
67
|
+
GNU General Public License for more details.
|
|
68
|
+
|
|
69
|
+
You should have received a copy of the GNU General Public License
|
|
70
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
71
|
+
`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {string} user
|
|
76
|
+
* @param {number} year
|
|
77
|
+
* @returns {string}
|
|
78
|
+
*/
|
|
79
|
+
function isc(user, year) {
|
|
80
|
+
return `
|
|
81
|
+
Copyright (c) ${year}, ${user}
|
|
82
|
+
|
|
83
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
84
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
85
|
+
copyright notice and this permission notice appear in all copies.
|
|
86
|
+
|
|
87
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
88
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
89
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
90
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
91
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
92
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
93
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
94
|
+
`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @enum {string}
|
|
99
|
+
*/
|
|
100
|
+
export const LicenseType = {
|
|
101
|
+
MIT: 'MIT',
|
|
102
|
+
ISC: 'ISC',
|
|
103
|
+
'APACHE-2.0': 'APACHE-2.0',
|
|
104
|
+
'GPL-3.0': 'GPL-3.0',
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @type {Object<LicenseType, function(string, number):string>}
|
|
109
|
+
*/
|
|
110
|
+
const LicenseTypeFunctions = {
|
|
111
|
+
[LicenseType.MIT]: mit,
|
|
112
|
+
[LicenseType['GPL-3.0']]: gpl3,
|
|
113
|
+
[LicenseType.ISC]: isc,
|
|
114
|
+
[LicenseType['APACHE-2.0']]: apache,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @param {string} user
|
|
119
|
+
* @param {LicenseType} type
|
|
120
|
+
* @param {string} pluginPath
|
|
121
|
+
* @returns {Promise<void>}
|
|
122
|
+
*/
|
|
123
|
+
export function writeLicense(user, type, pluginPath) {
|
|
124
|
+
const year = new Date().getFullYear();
|
|
125
|
+
const text = LicenseTypeFunctions[type](user, year);
|
|
126
|
+
|
|
127
|
+
return fs.promises.writeFile(path.join(pluginPath, 'LICENSE.md'), text);
|
|
128
|
+
}
|
package/src/pack.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const { resolveContext, getContext } = require('./context');
|
|
1
|
+
import { Transform } from 'stream';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import vinylFs from 'vinyl-fs';
|
|
4
|
+
import archiver from 'archiver';
|
|
5
|
+
import { logger } from '@vcsuite/cli-logger';
|
|
6
|
+
import { getPluginName } from './packageJsonHelpers.js';
|
|
7
|
+
import { resolveContext, getContext } from './context.js';
|
|
8
|
+
import build from './build.js';
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* @param {string} name
|
|
@@ -17,7 +16,7 @@ function replaceAssets(name) {
|
|
|
17
16
|
objectMode: true,
|
|
18
17
|
transform(data, encoding, callback) {
|
|
19
18
|
data.contents = Buffer.from(String(data.contents)
|
|
20
|
-
.replace(/\.?\/?(assets
|
|
19
|
+
.replace(/\.?\/?(plugin-assets)\//g, `plugins/${name}/$1/`));
|
|
21
20
|
|
|
22
21
|
callback(null, data);
|
|
23
22
|
},
|
|
@@ -69,7 +68,7 @@ async function ensureConfigJson() {
|
|
|
69
68
|
*/
|
|
70
69
|
function zip(name) {
|
|
71
70
|
return new Promise((resolve, reject) => {
|
|
72
|
-
const zipStream = fs.createWriteStream(resolveContext('dist', `${name}.zip`));
|
|
71
|
+
const zipStream = fs.createWriteStream(resolveContext('dist', `${name.replace(/\//, '-')}.zip`));
|
|
73
72
|
const archive = archiver('zip', { zlib: { level: 5 } });
|
|
74
73
|
|
|
75
74
|
zipStream.on('close', () => {
|
|
@@ -88,61 +87,38 @@ function zip(name) {
|
|
|
88
87
|
[
|
|
89
88
|
['package.json'],
|
|
90
89
|
['README.md'],
|
|
91
|
-
['
|
|
92
|
-
['dist',
|
|
90
|
+
['config.json'],
|
|
91
|
+
['dist', 'index.js'],
|
|
92
|
+
['dist', 'style.css'],
|
|
93
93
|
].forEach((fileArray) => {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
['assets', 'img'].forEach((dir) => {
|
|
98
|
-
if (fs.existsSync(resolveContext(dir))) {
|
|
99
|
-
archive.directory(resolveContext(dir), `${name}/${dir}`);
|
|
94
|
+
const file = resolveContext(...fileArray);
|
|
95
|
+
if (fs.existsSync(file)) {
|
|
96
|
+
archive.file(file, { name: `${name}/${fileArray.pop()}` });
|
|
100
97
|
}
|
|
101
98
|
});
|
|
102
99
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
100
|
+
if (fs.existsSync(resolveContext('plugin-assets'))) {
|
|
101
|
+
archive.directory(resolveContext('plugin-assets'), `${name}/plugin-assets`);
|
|
102
|
+
}
|
|
106
103
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
* @returns {Promise<void>}
|
|
110
|
-
*/
|
|
111
|
-
function compile(options) {
|
|
112
|
-
return getProdWebpackConfig(options)
|
|
113
|
-
.then((webpackConfig) => {
|
|
114
|
-
return new Promise((resolve, reject) => {
|
|
115
|
-
webpack(webpackConfig, (err, stats) => {
|
|
116
|
-
if (err) {
|
|
117
|
-
logger.error(err);
|
|
118
|
-
reject(err);
|
|
119
|
-
} else if (stats.hasErrors()) {
|
|
120
|
-
logger.log(stats.compilation.errors);
|
|
121
|
-
reject(stats.compilation.errors[0].Error);
|
|
122
|
-
} else {
|
|
123
|
-
logger.success(`build ${options.pluginName}`);
|
|
124
|
-
resolve();
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
});
|
|
104
|
+
archive.finalize().then(() => {
|
|
105
|
+
resolve();
|
|
128
106
|
});
|
|
107
|
+
});
|
|
129
108
|
}
|
|
130
109
|
|
|
131
110
|
/**
|
|
132
|
-
* @param {ProdOptions} options
|
|
133
111
|
* @returns {Promise<void>}
|
|
134
112
|
*/
|
|
135
|
-
async function pack(
|
|
136
|
-
|
|
137
|
-
logger.spin(`building plugin: ${
|
|
138
|
-
await
|
|
139
|
-
await replaceAssets(
|
|
113
|
+
export default async function pack() {
|
|
114
|
+
const pluginName = await getPluginName();
|
|
115
|
+
logger.spin(`building plugin: ${pluginName}`);
|
|
116
|
+
await build({});
|
|
117
|
+
await replaceAssets(pluginName);
|
|
140
118
|
logger.debug('fixed asset paths');
|
|
141
119
|
await ensureConfigJson();
|
|
142
120
|
logger.debug('ensuring config.json');
|
|
143
|
-
await zip(
|
|
121
|
+
await zip(pluginName);
|
|
144
122
|
logger.stopSpinner();
|
|
145
123
|
logger.success('build finished');
|
|
146
124
|
}
|
|
147
|
-
|
|
148
|
-
module.exports = pack;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const { resolveContext } = require('./context');
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { resolveContext } from './context.js';
|
|
4
3
|
|
|
5
4
|
/** @type {Object|null} */
|
|
6
5
|
let packageJson = null;
|
|
@@ -25,28 +24,25 @@ async function getPackageJson() {
|
|
|
25
24
|
/**
|
|
26
25
|
* @returns {Promise<string>}
|
|
27
26
|
*/
|
|
28
|
-
async function getPluginName() {
|
|
27
|
+
export async function getPluginName() {
|
|
29
28
|
const { name } = await getPackageJson();
|
|
30
29
|
if (!name) {
|
|
31
|
-
throw new Error('please
|
|
30
|
+
throw new Error('please specify the plugins name in the package.json');
|
|
32
31
|
}
|
|
33
32
|
return name;
|
|
34
33
|
}
|
|
35
34
|
|
|
36
35
|
/**
|
|
37
|
-
* @returns {Promise<string
|
|
36
|
+
* @returns {Promise<string>}
|
|
38
37
|
*/
|
|
39
|
-
async function getPluginEntry() {
|
|
38
|
+
export async function getPluginEntry() {
|
|
40
39
|
const { main, module, type } = await getPackageJson();
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
let entry = type === 'module' ? module : null;
|
|
42
|
+
entry = entry || main;
|
|
43
|
+
if (!entry) {
|
|
44
|
+
throw new Error('Could not determine entry point');
|
|
45
45
|
}
|
|
46
46
|
return entry;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
module.exports = {
|
|
50
|
-
getPluginName,
|
|
51
|
-
getPluginEntry,
|
|
52
|
-
};
|
package/src/preview.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { createServer } from 'vite';
|
|
4
|
+
import express from 'express';
|
|
5
|
+
import { logger } from '@vcsuite/cli-logger';
|
|
6
|
+
import {
|
|
7
|
+
addConfigRoute,
|
|
8
|
+
addIndexRoute,
|
|
9
|
+
addMapConfigRoute,
|
|
10
|
+
checkReservedDirectories,
|
|
11
|
+
createConfigJsonReloadPlugin,
|
|
12
|
+
printVcmapUiVersion, resolveMapUi,
|
|
13
|
+
} from './hostingHelpers.js';
|
|
14
|
+
import build, { getDefaultConfig, getLibraryPaths } from './build.js';
|
|
15
|
+
import { getContext } from './context.js';
|
|
16
|
+
import setupMapUi from './setupMapUi.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {HostingOptions} PreviewOptions
|
|
20
|
+
* @property {string} [vcm]
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {Object<string, string>} alias
|
|
25
|
+
* @param {Object<string, string>} libraryPaths
|
|
26
|
+
*/
|
|
27
|
+
function setAliases(alias, libraryPaths) {
|
|
28
|
+
Object.values(libraryPaths).forEach((entry) => {
|
|
29
|
+
alias[entry] = entry.replace(/(..\/)*assets/, '/assets');
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {string} [hostedVcm]
|
|
35
|
+
* @param {boolean} [https]
|
|
36
|
+
* @returns {Promise<import("vite").InlineConfig>}
|
|
37
|
+
*/
|
|
38
|
+
async function getServerOptions(hostedVcm, https) {
|
|
39
|
+
let proxy;
|
|
40
|
+
const normalLibraries = await getLibraryPaths('normal');
|
|
41
|
+
const scopedLibraries = await getLibraryPaths('@scoped/plugin');
|
|
42
|
+
const alias = {};
|
|
43
|
+
setAliases(alias, normalLibraries);
|
|
44
|
+
setAliases(alias, scopedLibraries);
|
|
45
|
+
|
|
46
|
+
if (hostedVcm) {
|
|
47
|
+
proxy = {
|
|
48
|
+
'^/style.css': hostedVcm,
|
|
49
|
+
'^/assets': hostedVcm,
|
|
50
|
+
'^/plugins': hostedVcm,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
publicDir: false,
|
|
56
|
+
plugins: [
|
|
57
|
+
createConfigJsonReloadPlugin(),
|
|
58
|
+
],
|
|
59
|
+
resolve: {
|
|
60
|
+
alias,
|
|
61
|
+
},
|
|
62
|
+
server: {
|
|
63
|
+
middlewareMode: 'html',
|
|
64
|
+
proxy,
|
|
65
|
+
https,
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @param {PreviewOptions} options
|
|
72
|
+
* @returns {Promise<void>}
|
|
73
|
+
*/
|
|
74
|
+
export default async function preview(options) {
|
|
75
|
+
if (!options.vcm) {
|
|
76
|
+
await printVcmapUiVersion();
|
|
77
|
+
}
|
|
78
|
+
checkReservedDirectories();
|
|
79
|
+
await build({ development: false, watch: true });
|
|
80
|
+
const app = express();
|
|
81
|
+
logger.info('Starting preview server...');
|
|
82
|
+
const server = await createServer(await getServerOptions(options.vcm, options.https));
|
|
83
|
+
|
|
84
|
+
addMapConfigRoute(app, options.vcm ? `${options.vcm}/map.config.json` : null, options.auth, options.config, true);
|
|
85
|
+
addIndexRoute(app, server, true, options.vcm, options.auth);
|
|
86
|
+
|
|
87
|
+
if (!options.vcm) {
|
|
88
|
+
logger.spin('compiling preview');
|
|
89
|
+
if (!fs.existsSync(resolveMapUi('plugins', 'node_modules'))) {
|
|
90
|
+
logger.info('Could not detect node_modules in map ui plugins. Assuming map UI not setup');
|
|
91
|
+
await setupMapUi();
|
|
92
|
+
}
|
|
93
|
+
const { buildPluginsForPreview } = await import('@vcmap/ui/build/buildHelpers.js');
|
|
94
|
+
await buildPluginsForPreview(getDefaultConfig(), true);
|
|
95
|
+
logger.stopSpinner();
|
|
96
|
+
logger.info('@vcmap/ui built for preview');
|
|
97
|
+
app.use('/assets', express.static(path.join(getContext(), 'node_modules', '@vcmap', 'ui', 'dist', 'assets')));
|
|
98
|
+
app.use('/plugins', express.static(path.join(getContext(), 'dist', 'plugins')));
|
|
99
|
+
await addConfigRoute(app, options.auth, options.config, true);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
app.use(server.middlewares);
|
|
103
|
+
|
|
104
|
+
const port = options.port || 5005;
|
|
105
|
+
await app.listen(port);
|
|
106
|
+
logger.info(`Server running on port ${port}`);
|
|
107
|
+
}
|