@vcmap/plugin-cli 1.1.1 → 2.0.3

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.
@@ -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 CHANGED
@@ -1,5 +1,5 @@
1
- const fs = require('fs');
2
- const path = require('path');
1
+ import fs from 'fs';
2
+ import path from 'path';
3
3
 
4
4
  /**
5
5
  * @param {string} user
@@ -97,7 +97,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
97
97
  /**
98
98
  * @enum {string}
99
99
  */
100
- const LicenseType = {
100
+ export const LicenseType = {
101
101
  MIT: 'MIT',
102
102
  ISC: 'ISC',
103
103
  'APACHE-2.0': 'APACHE-2.0',
@@ -120,14 +120,9 @@ const LicenseTypeFunctions = {
120
120
  * @param {string} pluginPath
121
121
  * @returns {Promise<void>}
122
122
  */
123
- function writeLicense(user, type, pluginPath) {
123
+ export function writeLicense(user, type, pluginPath) {
124
124
  const year = new Date().getFullYear();
125
125
  const text = LicenseTypeFunctions[type](user, year);
126
126
 
127
127
  return fs.promises.writeFile(path.join(pluginPath, 'LICENSE.md'), text);
128
128
  }
129
-
130
- module.exports = {
131
- writeLicense,
132
- LicenseType,
133
- };
package/src/pack.js CHANGED
@@ -1,12 +1,11 @@
1
- const { Transform } = require('stream');
2
- const fs = require('fs');
3
- const webpack = require('webpack');
4
- const vinylFs = require('vinyl-fs');
5
- const archiver = require('archiver');
6
- const { logger } = require('@vcsuite/cli-logger');
7
- const { getProdWebpackConfig } = require('./getWebpackConfig');
8
- const { getPluginName } = require('./packageJsonHelpers');
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|img|fonts|media)\//g, `plugins/${name}/$1/`));
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
- ['dist', 'config.json'],
92
- ['dist', `${name}.js`],
90
+ ['config.json'],
91
+ ['dist', 'index.js'],
92
+ ['dist', 'style.css'],
93
93
  ].forEach((fileArray) => {
94
- archive.file(resolveContext(...fileArray), { name: `${name}/${fileArray.pop()}` });
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
- archive.finalize();
104
- });
105
- }
100
+ if (fs.existsSync(resolveContext('plugin-assets'))) {
101
+ archive.directory(resolveContext('plugin-assets'), `${name}/plugin-assets`);
102
+ }
106
103
 
107
- /**
108
- * @param {ProdOptions} options
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(options) {
136
- options.pluginName = options.pluginName || await getPluginName();
137
- logger.spin(`building plugin: ${options.pluginName}`);
138
- await compile(options);
139
- await replaceAssets(options.pluginName);
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(options.pluginName);
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
- const fs = require('fs');
2
- const path = require('path');
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 speciy the plugins name in the package.json');
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|undefined>}
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
- const entry = type === 'module' ? module : main;
43
- if (entry && !path.isAbsolute(entry) && !/^\./.test(entry)) { // webpack requires a dot for relative paths
44
- return `./${entry}`;
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
+ }