@vcmap/plugin-cli 1.1.1 → 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/create.js CHANGED
@@ -1,12 +1,14 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const prompts = require('prompts');
4
- const semver = require('semver');
5
- const util = require('util');
6
- const childProcess = require('child_process');
7
- const { logger } = require('@vcsuite/cli-logger');
8
- const { version, name } = require('../package.json');
9
- const { LicenseType, writeLicense } = require('./licenses');
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import prompts from 'prompts';
4
+ import semver from 'semver';
5
+ import util from 'util';
6
+ import childProcess from 'child_process';
7
+ import { logger } from '@vcsuite/cli-logger';
8
+ import { LicenseType, writeLicense } from './licenses.js';
9
+ import { getDirname } from './hostingHelpers.js';
10
+
11
+ export const { version, name } = JSON.parse(fs.readFileSync(path.join(getDirname(), '..', 'package.json')).toString());
10
12
 
11
13
  /**
12
14
  * @typedef {Object} PluginTemplateOptions
@@ -18,6 +20,7 @@ const { LicenseType, writeLicense } = require('./licenses');
18
20
  * @property {string} license
19
21
  * @property {string} mapVersion
20
22
  * @property {boolean} addDevDep
23
+ * @property {Array<string>} peerDeps
21
24
  */
22
25
 
23
26
  /**
@@ -37,8 +40,8 @@ async function createPluginTemplate(options) {
37
40
  }
38
41
 
39
42
  await fs.promises.mkdir(pluginPath);
43
+ await fs.promises.mkdir(path.join(pluginPath, 'plugin-assets'));
40
44
  logger.debug('created plugin directory');
41
-
42
45
  const packageJson = {
43
46
  name: options.name,
44
47
  version: options.version,
@@ -51,7 +54,7 @@ async function createPluginTemplate(options) {
51
54
  vcm: options.mapVersion,
52
55
  },
53
56
  dependencies: {},
54
- devDependencies: options.addDevDep ? { [name]: `^${version}` } : {},
57
+ devDependencies: options.addDevDep ? { [name]: `^${version}` } : {}, // XXX should we add all our deps here? cesium, ol, vue etc.
55
58
  };
56
59
 
57
60
  const writePackagePromise = fs.promises.writeFile(
@@ -61,7 +64,6 @@ async function createPluginTemplate(options) {
61
64
 
62
65
  const configJson = {
63
66
  name: options.name,
64
- version: options.version,
65
67
  };
66
68
 
67
69
  const writeConfigPromise = fs.promises.writeFile(
@@ -82,15 +84,24 @@ async function createPluginTemplate(options) {
82
84
 
83
85
  const writeIndexPromise = fs.promises.writeFile(
84
86
  path.join(pluginPath, 'src', 'index.js'),
85
- ['import { version } from \'../package.json\';',
87
+ [
88
+ 'import { version, name } from \'../package.json\';',
89
+ '',
90
+ '/**',
91
+ ' * @param {VcsApp} app - the app from which this plugin is loaded.',
92
+ ' * @param {Object} config - the configuration of this plugin instance, passed in from the app.',
93
+ ' */',
94
+ 'export default function(app, config) {',
95
+ ' return {',
96
+ ' get name() { return name; },',
97
+ ' get version() { return version; },',
98
+ ' initialize: async (vcsUiApp) => { console.log(\'Called before loading the rest of the current context. Passed in the containing Vcs UI App \'); },',
99
+ ' onVcsAppMounted: async (vcsUiApp) => { console.log(\'Called when the root UI component is mounted and managers are ready to accept components\'); },',
100
+ ' toJSON: async () => { console.log(\'Called when serializing this plugin instance\'); },',
101
+ ' };',
102
+ '};',
86
103
  '',
87
- 'export default {',
88
- ' version,',
89
- ' // preInitialize',
90
- ' // postInitialize',
91
- ' // registerUiPlugin',
92
- ' // postUiInitialize',
93
- '};'].join('\n'),
104
+ ].join('\n'),
94
105
  );
95
106
 
96
107
  await Promise.all([
@@ -101,10 +112,15 @@ async function createPluginTemplate(options) {
101
112
  writeLicense(options.author, options.license, pluginPath),
102
113
  ]);
103
114
 
104
- logger.spin('installing dependencies...');
115
+
116
+ logger.spin('installing dependencies... (this may take a while)');
105
117
  const exec = util.promisify(childProcess.exec);
106
118
  try {
107
- const { stdout, stderr } = await exec('npm i', { cwd: pluginPath });
119
+ let installCmd = 'npm i';
120
+ if (options.peerDeps.length > 0) {
121
+ installCmd = `${installCmd} --save-peer ${options.peerDeps.join(' ')}`;
122
+ }
123
+ const { stdout, stderr } = await exec(installCmd, { cwd: pluginPath });
108
124
  logger.log(stdout);
109
125
  logger.error(stderr);
110
126
  logger.success('installed dependencies');
@@ -119,13 +135,24 @@ async function createPluginTemplate(options) {
119
135
  /**
120
136
  * @returns {Promise<void>}
121
137
  */
122
- async function create() {
138
+ export default async function create() {
123
139
  const scriptChoices = [
124
140
  { title: 'build', value: { build: 'vcmplugin build' } },
125
141
  { title: 'pack', value: { pack: 'vcmplugin pack' } },
142
+ { title: 'start', value: { start: 'vcmplugin serve' } },
143
+ { title: 'preview', value: { preview: 'vcmplugin preview' } },
126
144
  { title: 'test', value: { test: 'echo "Error: no test specified" && exit 1' } },
127
145
  ];
128
146
 
147
+ const peerDependencyChoices = [
148
+ { title: '@vcmap/ui', value: '@vcmap/ui', selected: true },
149
+ { title: '@vcsuite/ui-components', value: '@vcsuite/ui-components', selected: true },
150
+ { title: '@vcmap/core', value: '@vcmap/core' },
151
+ { title: '@vcmap/cesium', value: '@vcmap/cesium' },
152
+ { title: 'ol', value: 'ol@~6.13.0' },
153
+ { title: '@vue/composition-api', value: '@vue/composition-api@~1.4.5' },
154
+ ];
155
+
129
156
  const questions = [
130
157
  {
131
158
  type: 'text',
@@ -183,7 +210,14 @@ async function create() {
183
210
  type: 'text',
184
211
  name: 'mapVersion',
185
212
  message: 'Map version',
186
- initial: '>=4.0',
213
+ initial: '>=5.0',
214
+ },
215
+ {
216
+ name: 'peerDeps',
217
+ type: 'multiselect',
218
+ message: 'Add the following peer dependencies to the package.json.',
219
+ choices: peerDependencyChoices,
220
+ hint: '- Space to select. Enter to submit',
187
221
  },
188
222
  {
189
223
  type: 'toggle',
@@ -199,5 +233,3 @@ async function create() {
199
233
 
200
234
  await createPluginTemplate(answers);
201
235
  }
202
-
203
- module.exports = create;
@@ -1,25 +1,23 @@
1
- const { Command } = require('commander');
2
- const { logger } = require('@vcsuite/cli-logger');
3
- const { setContext } = require('./context');
1
+ import { Command } from 'commander';
2
+ import { logger } from '@vcsuite/cli-logger';
3
+ import { setContext } from './context.js';
4
4
 
5
5
  Command.prototype.defaultOptions = function defaultOptions() {
6
6
  this
7
- .option('-n, --plugin-name [name]', 'a name override to use. extracts the name from the package.json by default')
8
7
  .option('--context [path]', 'an optional context, default is cwd', (value) => {
9
8
  setContext(value);
10
9
  return value;
11
- })
12
- .option('--no-condense-whitespace', 'do not condense white space')
13
- .option('-e, --entry <entry>', 'entrypoint override');
10
+ });
14
11
 
15
12
  return this;
16
13
  };
17
14
 
18
- Command.prototype.defaultBuildOptions = function defaultBuildOptions() {
15
+ Command.prototype.defaultServeOptions = function defaultServeOptions() {
19
16
  this
20
- .defaultOptions()
21
- .option('-l, --library [name]', 'whether to create a library with [name] or not')
22
- .option('--library-target [target]', 'library target', 'commonjs2');
17
+ .option('-p, --port [port]', 'the port to listen on', /\d+/)
18
+ .option('--auth <user:password>', 'an optional auth to append to proxy requests')
19
+ .option('-c, --config <config>', 'a config override to not use the default plugin config')
20
+ .option('--https', 'use https for serving');
23
21
 
24
22
  return this;
25
23
  };
@@ -0,0 +1,226 @@
1
+ import { fileURLToPath, URL } from 'url';
2
+ import https from 'https';
3
+ import http from 'http';
4
+ import fs from 'fs';
5
+ import path from 'path';
6
+ import { logger } from '@vcsuite/cli-logger';
7
+ import { getContext, resolveContext } from './context.js';
8
+ import { getPluginEntry, getPluginName } from './packageJsonHelpers.js';
9
+
10
+ /**
11
+ * @typedef {Object} HostingOptions
12
+ * @property {string} [config] - an optional fileName to use for configuring the plugin
13
+ * @property {string} [auth] - potential auth string to download assets (index.html, config) with
14
+ * @property {number} [port]
15
+ * @property {boolean} [https]
16
+ */
17
+
18
+ export function checkReservedDirectories() {
19
+ ['assets', 'plugins']
20
+ .forEach((dir) => {
21
+ if (fs.existsSync(path.join(getContext(), dir))) {
22
+ logger.warning(`found reserved directory ${dir}. serving my not work as exptected`);
23
+ }
24
+ });
25
+ }
26
+
27
+ /**
28
+ * @returns {string}
29
+ */
30
+ export function getDirname() {
31
+ return path.dirname(fileURLToPath(import.meta.url));
32
+ }
33
+
34
+ /**
35
+ * @param {string} stringUrl
36
+ * @param {string} auth
37
+ * @param {function(http.IncomingMessage)} handler
38
+ */
39
+ export function httpGet(stringUrl, auth, handler) {
40
+ const url = new URL(stringUrl);
41
+ const options = {
42
+ hostname: url.hostname,
43
+ port: url.port,
44
+ path: url.pathname,
45
+ };
46
+
47
+ if (auth) {
48
+ options.headers = { Authorization: `Basic ${Buffer.from(auth).toString('base64')}` };
49
+ }
50
+ if (url.protocol === 'https:') {
51
+ https.get(options, handler);
52
+ } else {
53
+ http.get(options, handler);
54
+ }
55
+ }
56
+
57
+ /**
58
+ * @param {string} fileName
59
+ * @returns {Promise<Object>}
60
+ */
61
+ export async function readConfigJson(fileName) {
62
+ const configFileName = fileName || resolveContext('config.json');
63
+ let config = {};
64
+ if (fs.existsSync(configFileName)) {
65
+ const content = await fs.promises.readFile(configFileName);
66
+ config = JSON.parse(content.toString());
67
+ }
68
+
69
+ return config;
70
+ }
71
+
72
+ let configJson = null;
73
+
74
+ /**
75
+ * @returns {Promise<string>}
76
+ */
77
+ export async function printVcmapUiVersion() {
78
+ const packageJsonPath = path.join(getContext(), 'node_modules', '@vcmap', 'ui', 'package.json');
79
+ if (!fs.existsSync(packageJsonPath)) {
80
+ throw new Error(`Cannot find the @vcmap/ui package in ${getContext()}. Are you sure you installed it?`);
81
+ }
82
+
83
+ const content = await fs.promises.readFile(packageJsonPath);
84
+ const { version } = JSON.parse(content.toString());
85
+ logger.info(`Using @vcmap/ui version: ${version} found in current project.`);
86
+ }
87
+
88
+ /**
89
+ * @param {string} [mapConfig] - fs or https to config. defaults to @vcmap/ui/map.config.json
90
+ * @param {string} [auth]
91
+ * @param {boolean} [production]
92
+ * @param {string} [configFile]
93
+ * @returns {Promise<unknown>}
94
+ */
95
+ export function getConfigJson(mapConfig, auth, production, configFile) {
96
+ const usedConfig = mapConfig || path.join(getContext(), 'node_modules', '@vcmap', 'ui', 'dist', 'config', 'base.config.json');
97
+ if (configJson) {
98
+ return Promise.resolve(configJson);
99
+ }
100
+ const isWebVcm = /^https?:\/\//.test(usedConfig);
101
+ return new Promise((resolve, reject) => {
102
+ function handleStream(stream) {
103
+ let data = '';
104
+ stream.on('data', (chunk) => {
105
+ data += chunk.toString();
106
+ });
107
+
108
+ stream.on('close', async () => {
109
+ try {
110
+ configJson = JSON.parse(data);
111
+ configJson.plugins = production && configJson.plugins ? configJson.plugins : [];
112
+ const pluginConfig = await readConfigJson(configFile);
113
+ pluginConfig.entry = production ? 'dist/index.js' : await getPluginEntry();
114
+ pluginConfig.name = await getPluginName();
115
+ const idx = configJson.plugins.findIndex(p => p.name === pluginConfig.name);
116
+ if (idx > -1) {
117
+ configJson.plugins.splice(idx, 1, pluginConfig);
118
+ } else {
119
+ configJson.plugins.push(pluginConfig);
120
+ }
121
+ resolve(configJson);
122
+ } catch (e) {
123
+ reject(e);
124
+ }
125
+ });
126
+ }
127
+ if (isWebVcm) {
128
+ httpGet(usedConfig, auth, (res) => {
129
+ if (res.statusCode < 400) {
130
+ handleStream(res);
131
+ }
132
+ });
133
+ } else {
134
+ handleStream(fs.createReadStream(path.join(usedConfig)));
135
+ }
136
+ });
137
+ }
138
+
139
+ /**
140
+ * @param {string} stringUrl
141
+ * @param {string} auth
142
+ * @returns {Promise<string>}
143
+ */
144
+ function getIndexHtml(stringUrl, auth) {
145
+ return new Promise((resolve, reject) => {
146
+ httpGet(stringUrl, auth, (res) => {
147
+ let index = '';
148
+ if (res.statusCode >= 400) {
149
+ logger.error('got status code: ', res.statusCode);
150
+ reject(new Error(`StatusCode ${res.statusCode}`));
151
+ }
152
+ res.on('data', (chunk) => {
153
+ index += chunk.toString();
154
+ });
155
+ res.on('end', () => {
156
+ resolve(index);
157
+ });
158
+ });
159
+ });
160
+ }
161
+
162
+ /**
163
+ * @returns {import("vite").Plugin}
164
+ */
165
+ export function createConfigJsonReloadPlugin() {
166
+ return {
167
+ name: 'ConfigJsonReload',
168
+ handleHotUpdate({ file }) {
169
+ if (file === path.join(getContext(), 'config.json')) {
170
+ configJson = null;
171
+ }
172
+ },
173
+ };
174
+ }
175
+
176
+ /**
177
+ * @param {Express} app
178
+ * @param {string} mapConfig
179
+ * @param {string} [auth]
180
+ * @param {string} [configFile]
181
+ * @param {boolean} [production]
182
+ */
183
+ export function addMapConfigRoute(app, mapConfig, auth, configFile, production) {
184
+ app.get('/map.config.json', (req, res) => {
185
+ getConfigJson(mapConfig, auth, production, configFile)
186
+ .then((config) => {
187
+ const stringConfig = JSON.stringify(config, null, 2);
188
+ res.setHeader('Content-Type', 'application/json');
189
+ res.write(stringConfig);
190
+ res.end();
191
+ });
192
+ });
193
+ }
194
+
195
+ /**
196
+ * @param {boolean} [production]
197
+ * @returns {Promise<string>}
198
+ */
199
+ export async function getMapUiIndexHtml(production) {
200
+ const indexHtmlFileName = production ?
201
+ path.join(getContext(), 'node_modules', '@vcmap', 'ui', 'dist', 'index.html') :
202
+ path.join(getDirname(), '..', 'assets', 'index.html');
203
+ const buffer = await fs.promises.readFile(indexHtmlFileName);
204
+ return buffer.toString();
205
+ }
206
+
207
+ /**
208
+ * @param {Express} app
209
+ * @param {import("vite").ViteDevServer} server
210
+ * @param {boolean} [production]
211
+ * @param {string} [hostedVcm]
212
+ * @param {string} [auth]
213
+ */
214
+ export function addIndexRoute(app, server, production, hostedVcm, auth) {
215
+ app.get('/', async (req, res) => {
216
+ let originalIndex = hostedVcm ?
217
+ await getIndexHtml(`${hostedVcm}/`, auth) :
218
+ await getMapUiIndexHtml(production); // TODO change hosted vcm index via option?
219
+
220
+ originalIndex = await server.transformIndexHtml('index.html', originalIndex);
221
+ res.status(200)
222
+ .set({ 'Content-Type': 'text/html' })
223
+ .end(originalIndex);
224
+ });
225
+ }
226
+
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
- };