@vcmap/plugin-cli 1.0.0 → 2.0.1

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/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,94 @@
1
+ import path from 'path';
2
+ import { createServer } from 'vite';
3
+ import express from 'express';
4
+ import { logger } from '@vcsuite/cli-logger';
5
+ import {
6
+ addIndexRoute,
7
+ addMapConfigRoute,
8
+ checkReservedDirectories,
9
+ createConfigJsonReloadPlugin,
10
+ printVcmapUiVersion,
11
+ } from './hostingHelpers.js';
12
+ import build, { getLibraryPaths } from './build.js';
13
+ import { getContext } from './context.js';
14
+
15
+ /**
16
+ * @typedef {HostingOptions} PreviewOptions
17
+ * @property {string} [vcm]
18
+ */
19
+
20
+ /**
21
+ * @param {Object<string, string>} alias
22
+ * @param {Object<string, string>} libraryPaths
23
+ */
24
+ function setAliases(alias, libraryPaths) {
25
+ Object.values(libraryPaths).forEach((entry) => {
26
+ alias[entry] = entry.replace(/(..\/)*assets/, '/assets');
27
+ });
28
+ }
29
+
30
+ /**
31
+ * @param {string} [hostedVcm]
32
+ * @param {boolean} [https]
33
+ * @returns {Promise<import("vite").InlineConfig>}
34
+ */
35
+ async function getServerOptions(hostedVcm, https) {
36
+ let proxy;
37
+ const normalLibraries = getLibraryPaths('normal');
38
+ const scopedLibraries = getLibraryPaths('@scoped/plugin');
39
+ const alias = {};
40
+ setAliases(alias, normalLibraries);
41
+ setAliases(alias, scopedLibraries);
42
+
43
+ if (hostedVcm) {
44
+ proxy = {
45
+ '^/style.css': hostedVcm,
46
+ '^/assets': hostedVcm,
47
+ '^/plugins': hostedVcm,
48
+ };
49
+ }
50
+
51
+ return {
52
+ publicDir: false,
53
+ plugins: [
54
+ createConfigJsonReloadPlugin(),
55
+ ],
56
+ resolve: {
57
+ alias,
58
+ },
59
+ server: {
60
+ middlewareMode: 'html',
61
+ proxy,
62
+ https,
63
+ },
64
+ };
65
+ }
66
+
67
+ /**
68
+ * @param {PreviewOptions} options
69
+ * @returns {Promise<void>}
70
+ */
71
+ export default async function preview(options) {
72
+ if (!options.vcm) {
73
+ await printVcmapUiVersion();
74
+ }
75
+ checkReservedDirectories();
76
+ build({ development: false, watch: true }, true);
77
+ const app = express();
78
+ logger.info('Starting preview server...');
79
+ const server = await createServer(await getServerOptions(options.vcm, options.https));
80
+
81
+ addMapConfigRoute(app, options.vcm ? `${options.vcm}/map.config.json` : null, options.auth, options.config, true);
82
+ addIndexRoute(app, server, true, options.vcm, options.auth);
83
+
84
+ if (!options.vcm) {
85
+ app.use('/assets', express.static(path.join(getContext(), 'node_modules', '@vcmap', 'ui', 'dist', 'assets')));
86
+ app.use('/plugins', express.static(path.join(getContext(), 'node_modules', '@vcmap', 'ui', 'dist', 'plugins')));
87
+ }
88
+
89
+ app.use(server.middlewares);
90
+
91
+ const port = options.port || 5005;
92
+ await app.listen(port);
93
+ logger.info(`Server running on port ${port}`);
94
+ }
package/src/serve.js CHANGED
@@ -1,202 +1,80 @@
1
- const path = require('path');
2
- const https = require('https');
3
- const http = require('http');
4
- const { URL } = require('url');
5
- const fs = require('fs');
6
- const WebpackDevServer = require('webpack-dev-server');
7
- const webpack = require('webpack');
8
- const { logger } = require('@vcsuite/cli-logger');
9
- const { getContext, resolveContext } = require('./context');
10
- const { getDevWebpackConfig } = require('./getWebpackConfig');
11
- const { getPluginName } = require('./packageJsonHelpers');
12
-
13
- function httpGet(stringUrl, auth, handler) {
14
- const url = new URL(stringUrl);
15
- const options = {
16
- hostname: url.hostname,
17
- port: url.port,
18
- path: url.pathname,
19
- };
20
-
21
- if (auth) {
22
- options.headers = { Authorization: `Basic ${Buffer.from(auth).toString('base64')}` };
23
- }
24
- if (url.protocol === 'https:') {
25
- https.get(options, handler);
26
- } else {
27
- http.get(options, handler);
28
- }
29
- }
30
-
31
- function getIndexHtml(stringUrl, fileName, auth) {
32
- return new Promise((resolve, reject) => {
33
- httpGet(stringUrl, auth, (res) => {
34
- if (res.statusCode >= 400) {
35
- logger.error('got status code: ', res.statusCode);
36
- reject(new Error(`StatusCode ${res.statusCode}`));
37
- }
38
- const write = fs.createWriteStream(resolveContext(fileName));
39
- write.on('finish', resolve);
40
- write.on('error', (err) => {
41
- reject(err);
42
- });
43
- res.pipe(write);
44
- });
45
- });
46
- }
47
-
48
- let configJson = null;
1
+ import fs from 'fs';
2
+ import { createServer } from 'vite';
3
+ import { createVuePlugin } from 'vite-plugin-vue2';
4
+ import express from 'express';
5
+ import { logger } from '@vcsuite/cli-logger';
6
+ import path from 'path';
7
+ import { VuetifyResolver } from 'unplugin-vue-components/dist/resolvers.js';
8
+ import Components from 'unplugin-vue-components/dist/vite.js';
9
+ import { getContext } from './context.js';
10
+ import {
11
+ addIndexRoute,
12
+ addMapConfigRoute,
13
+ checkReservedDirectories,
14
+ createConfigJsonReloadPlugin,
15
+ printVcmapUiVersion,
16
+ } from './hostingHelpers.js';
49
17
 
50
18
  /**
51
- * @param {string} fileName
52
- * @returns {Promise<Object>}
19
+ * @typedef {HostingOptions} ServeOptions
20
+ * @property {string} [mapConfig] - an filename or URL to a map config
53
21
  */
54
- async function readConfigJson(fileName) {
55
- const configFileName = fileName || resolveContext('config.json');
56
- let config = {};
57
- if (fs.existsSync(configFileName)) {
58
- const content = await fs.promises.readFile(configFileName);
59
- config = JSON.parse(content.toString());
60
- }
61
- // eslint-disable-next-line no-underscore-dangle
62
- delete config._esmodule;
63
- return config;
64
- }
65
22
 
66
- function getConfigJson(vcm, name, { auth, config: configFile }) {
67
- if (configJson) {
68
- return Promise.resolve(configJson);
23
+ /**
24
+ * @param {ServeOptions} options
25
+ * @returns {Promise<void>}
26
+ */
27
+ export default async function serve(options) {
28
+ if (!fs.existsSync(path.join(getContext(), 'node_modules', '@vcmap', 'ui'))) {
29
+ logger.error('Can only serve in dev mode, if the map ui is a dependency of the current context');
30
+ return;
69
31
  }
70
- const isWebVcm = /^https?:\/\//.test(vcm);
71
- return new Promise((resolve, reject) => {
72
- function handleStream(stream) {
73
- let data = '';
74
- stream.on('data', (chunk) => {
75
- data += chunk.toString();
76
- });
32
+ await printVcmapUiVersion();
33
+ checkReservedDirectories();
34
+ const app = express();
35
+ logger.info('Starting development server...');
77
36
 
78
- stream.on('close', async () => {
79
- try {
80
- configJson = JSON.parse(data);
81
- configJson.plugins = configJson.plugins || [];
82
- const pluginConfig = await readConfigJson(configFile);
83
- // eslint-disable-next-line no-underscore-dangle
84
- pluginConfig.entry = '/_dist/plugin.js';
85
- pluginConfig.name = name;
86
- const idx = configJson.plugins.findIndex(p => p.name === name);
87
- if (idx > -1) {
88
- configJson.plugins.splice(idx, 1, pluginConfig);
89
- } else {
90
- configJson.plugins.push(pluginConfig);
91
- }
92
- resolve(configJson);
93
- } catch (e) {
94
- reject(e);
95
- }
96
- });
97
- }
98
- if (isWebVcm) {
99
- httpGet(`${vcm}config.json`, auth, (res) => {
100
- if (res.statusCode < 400) {
101
- handleStream(res);
102
- }
103
- });
104
- } else {
105
- handleStream(fs.createReadStream(path.join(vcm, 'config.json')));
106
- }
107
- });
108
- }
109
-
110
- async function serve(options) {
111
- logger.spin('Starting development server...');
112
- let { vcm, index } = options;
113
- const pluginName = options.pluginName || await getPluginName();
114
- const isWebVcm = /^https?:\/\//.test(vcm);
115
-
116
- const proxy = {};
117
- const indexFilename = 'index.html'; // XXX maybe use some random filename when web to not clobber anything
118
- if (isWebVcm) {
119
- vcm = `${vcm.replace(/\/$/, '')}/`;
120
- await getIndexHtml(`${vcm}/${index}`, indexFilename, options.auth);
121
- ['/lib', '/css', '/fonts', '/images', '/img', '/templates', '/datasource-data', '/plugins']
122
- .concat(options.proxyRoute) // TODO allow for more complex proxy options, e.g add a target such as --proxyRoute myProxy=myTarget
123
- .forEach((p) => {
124
- proxy[p] = {
125
- target: vcm,
126
- changeOrigin: true,
127
- auth: options.auth,
128
- };
129
- });
130
- }
131
- const webpackConfig = await getDevWebpackConfig(options);
132
- const server = new WebpackDevServer(webpack(webpackConfig), {
133
- hot: true,
134
- hotOnly: true,
135
- open: false,
136
- injectClient: false,
137
- publicPath: '/_dist',
138
- logLevel: 'warn',
139
- clientLogLevel: 'silent',
140
- useLocalIp: true,
141
- historyApiFallback: {
142
- disableDotRule: true,
143
- rewrites: [
144
- { from: /./, to: '/index.html' },
37
+ const server = await createServer({
38
+ root: getContext(),
39
+ publicDir: false,
40
+ optimizeDeps: {
41
+ exclude: [
42
+ '@vcmap/ui',
43
+ '@vcmap/core',
44
+ 'ol',
45
+ '@vcsuite/ui-components',
46
+ 'proj4',
47
+ ],
48
+ include: [
49
+ 'fast-deep-equal',
50
+ 'rbush-knn',
51
+ 'pbf',
52
+ '@vcmap/cesium',
145
53
  ],
146
54
  },
147
- staticOptions: {
148
- fallthrough: false,
149
- },
150
- before(app) {
151
- app.use('/config.json', (req, res) => {
152
- getConfigJson(vcm, pluginName, options)
153
- .then((config) => {
154
- const stringConfig = JSON.stringify(config, null, 2);
155
- res.setHeader('Content-Type', 'application/json');
156
- res.write(stringConfig);
157
- res.end();
158
- });
159
- });
160
- },
161
- after(app) {
162
- app.use('/', (err, req, res, next) => {
163
- if (err.statusCode === 404 && isWebVcm) {
164
- httpGet(`${vcm}${req.url.replace(/^\//, '')}`, options.auth, (innerRes) => {
165
- if (innerRes.statusCode < 400) {
166
- innerRes.pipe(res);
167
- }
168
- });
169
- } else {
170
- next();
171
- }
172
- });
55
+ plugins: [
56
+ createVuePlugin(),
57
+ createConfigJsonReloadPlugin(),
58
+ Components({
59
+ resolvers: [
60
+ VuetifyResolver(),
61
+ ],
62
+ include: [],
63
+ exclude: [],
64
+ }),
65
+ ],
66
+ server: {
67
+ middlewareMode: 'html',
68
+ https: options.https,
173
69
  },
174
- proxy,
175
- contentBase: isWebVcm ? getContext() : [options.vcm, getContext()],
176
- contentBasePublicPath: '/',
177
- index: 'index.html',
178
- });
179
-
180
- logger.stopSpinner();
181
- server.listen(options.port, '0.0.0.0', (err) => {
182
- if (err) {
183
- logger.error(err);
184
- } else {
185
- logger.success('Your application is running');
186
- }
187
70
  });
188
71
 
189
- ['SIGINT', 'SIGTERM'].forEach((signal) => {
190
- process.on(signal, () => {
191
- server.close(() => {
192
- if (isWebVcm) {
193
- fs.unlinkSync(resolveContext(index));
194
- }
195
- process.exit(0);
196
- });
197
- });
198
- });
199
- }
72
+ addMapConfigRoute(app, options.mapConfig, options.auth, options.config);
73
+ addIndexRoute(app, server);
200
74
 
75
+ app.use(server.middlewares);
201
76
 
202
- module.exports = serve;
77
+ const port = options.port || 8008;
78
+ await app.listen(port);
79
+ logger.info(`Server running on port ${port}`);
80
+ }