@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/create.js CHANGED
@@ -1,11 +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 } = require('../package.json');
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());
9
12
 
10
13
  /**
11
14
  * @typedef {Object} PluginTemplateOptions
@@ -17,6 +20,7 @@ const { version } = require('../package.json');
17
20
  * @property {string} license
18
21
  * @property {string} mapVersion
19
22
  * @property {boolean} addDevDep
23
+ * @property {Array<string>} peerDeps
20
24
  */
21
25
 
22
26
  /**
@@ -36,8 +40,8 @@ async function createPluginTemplate(options) {
36
40
  }
37
41
 
38
42
  await fs.promises.mkdir(pluginPath);
43
+ await fs.promises.mkdir(path.join(pluginPath, 'plugin-assets'));
39
44
  logger.debug('created plugin directory');
40
-
41
45
  const packageJson = {
42
46
  name: options.name,
43
47
  version: options.version,
@@ -50,7 +54,7 @@ async function createPluginTemplate(options) {
50
54
  vcm: options.mapVersion,
51
55
  },
52
56
  dependencies: {},
53
- devDependencies: options.addDevDep ? { 'vcmplugin-cli': `^${version}` } : {},
57
+ devDependencies: options.addDevDep ? { [name]: `^${version}` } : {}, // XXX should we add all our deps here? cesium, ol, vue etc.
54
58
  };
55
59
 
56
60
  const writePackagePromise = fs.promises.writeFile(
@@ -60,7 +64,6 @@ async function createPluginTemplate(options) {
60
64
 
61
65
  const configJson = {
62
66
  name: options.name,
63
- version: options.version,
64
67
  };
65
68
 
66
69
  const writeConfigPromise = fs.promises.writeFile(
@@ -81,23 +84,43 @@ async function createPluginTemplate(options) {
81
84
 
82
85
  const writeIndexPromise = fs.promises.writeFile(
83
86
  path.join(pluginPath, 'src', 'index.js'),
84
- ['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
+ '};',
85
103
  '',
86
- 'export default {',
87
- ' version,',
88
- ' // preInitialize',
89
- ' // postInitialize',
90
- ' // registerUiPlugin',
91
- ' // postUiInitialize',
92
- '};'].join('\n'),
104
+ ].join('\n'),
93
105
  );
94
106
 
95
- await Promise.all([writePackagePromise, writeConfigPromise, writeReadmePromise, writeIndexPromise]);
107
+ await Promise.all([
108
+ writePackagePromise,
109
+ writeConfigPromise,
110
+ writeReadmePromise,
111
+ writeIndexPromise,
112
+ writeLicense(options.author, options.license, pluginPath),
113
+ ]);
114
+
96
115
 
97
- logger.spin('installing dependencies...');
116
+ logger.spin('installing dependencies... (this may take a while)');
98
117
  const exec = util.promisify(childProcess.exec);
99
118
  try {
100
- 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 });
101
124
  logger.log(stdout);
102
125
  logger.error(stderr);
103
126
  logger.success('installed dependencies');
@@ -112,19 +135,39 @@ async function createPluginTemplate(options) {
112
135
  /**
113
136
  * @returns {Promise<void>}
114
137
  */
115
- async function create() {
138
+ export default async function create() {
116
139
  const scriptChoices = [
117
140
  { title: 'build', value: { build: 'vcmplugin build' } },
118
141
  { title: 'pack', value: { pack: 'vcmplugin pack' } },
142
+ { title: 'start', value: { start: 'vcmplugin serve' } },
143
+ { title: 'preview', value: { preview: 'vcmplugin preview' } },
119
144
  { title: 'test', value: { test: 'echo "Error: no test specified" && exit 1' } },
120
145
  ];
121
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
+
122
156
  const questions = [
123
157
  {
124
158
  type: 'text',
125
159
  name: 'name',
126
160
  message: 'Name',
127
- validate: value => !!value,
161
+ validate: (value) => {
162
+ if (!value) {
163
+ return false;
164
+ }
165
+
166
+ if (fs.existsSync(path.join(process.cwd(), value))) {
167
+ return `Directory ${value} already exists`;
168
+ }
169
+ return true;
170
+ },
128
171
  },
129
172
  {
130
173
  type: 'text',
@@ -153,16 +196,28 @@ async function create() {
153
196
  initial: 'author <email>',
154
197
  },
155
198
  {
156
- type: 'text',
199
+ type: 'select',
157
200
  name: 'license',
158
201
  message: 'License',
159
- initial: 'ISC',
202
+ initial: 0,
203
+ choices: Object.values(LicenseType)
204
+ .map(type => ({
205
+ title: type,
206
+ value: type,
207
+ })),
160
208
  },
161
209
  {
162
210
  type: 'text',
163
211
  name: 'mapVersion',
164
212
  message: 'Map version',
165
- 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',
166
221
  },
167
222
  {
168
223
  type: 'toggle',
@@ -178,5 +233,3 @@ async function create() {
178
233
 
179
234
  await createPluginTemplate(answers);
180
235
  }
181
-
182
- module.exports = create;
@@ -1,19 +1,37 @@
1
- const { Command } = require('commander');
1
+ import { Command } from 'commander';
2
+ import { logger } from '@vcsuite/cli-logger';
3
+ import { setContext } from './context.js';
2
4
 
3
5
  Command.prototype.defaultOptions = function defaultOptions() {
4
6
  this
5
- .option('-n, --plugin-name [name]', 'a name override to use. extracts the name from the package.json by default')
6
- .option('--no-condense-whitespace', 'do not condense white space')
7
- .option('-e, --entry <entry>', 'entrypoint override');
7
+ .option('--context [path]', 'an optional context, default is cwd', (value) => {
8
+ setContext(value);
9
+ return value;
10
+ });
8
11
 
9
12
  return this;
10
13
  };
11
14
 
12
- Command.prototype.defaultBuildOptions = function defaultBuildOptions() {
15
+ Command.prototype.defaultServeOptions = function defaultServeOptions() {
13
16
  this
14
- .defaultOptions()
15
- .option('-l, --library [name]', 'whether to create a library with [name] or not')
16
- .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');
17
21
 
18
22
  return this;
19
23
  };
24
+
25
+ Command.prototype.safeAction = function safeAction(action) {
26
+ this.action(async (options) => {
27
+ try {
28
+ await action(options);
29
+ } catch (e) {
30
+ if (e) {
31
+ logger.error(e);
32
+ }
33
+ logger.stopSpinner();
34
+ process.exit(1);
35
+ }
36
+ });
37
+ };
@@ -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
+
@@ -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
+ }