authup 1.0.0-beta.3 → 1.0.0-beta.30

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.
Files changed (73) hide show
  1. package/LICENSE +201 -21
  2. package/README.md +1 -1
  3. package/dist/index.mjs +331 -301
  4. package/dist/index.mjs.map +1 -1
  5. package/dist/module.d.ts +23 -0
  6. package/dist/module.d.ts.map +1 -0
  7. package/dist/packages/client-web/config/build.d.ts +3 -0
  8. package/dist/packages/client-web/config/build.d.ts.map +1 -0
  9. package/dist/packages/client-web/config/index.d.ts +3 -0
  10. package/dist/packages/client-web/config/index.d.ts.map +1 -0
  11. package/dist/packages/client-web/config/parse.d.ts +3 -0
  12. package/dist/packages/client-web/config/parse.d.ts.map +1 -0
  13. package/dist/packages/client-web/config/read/env.d.ts +3 -0
  14. package/dist/packages/client-web/config/read/env.d.ts.map +1 -0
  15. package/dist/packages/client-web/config/read/fs.d.ts +7 -0
  16. package/dist/packages/client-web/config/read/fs.d.ts.map +1 -0
  17. package/dist/packages/client-web/config/read/index.d.ts +2 -0
  18. package/dist/packages/client-web/config/read/index.d.ts.map +1 -0
  19. package/dist/packages/client-web/config/read/module.d.ts +8 -0
  20. package/dist/packages/client-web/config/read/module.d.ts.map +1 -0
  21. package/dist/packages/client-web/config/type.d.ts +9 -0
  22. package/dist/packages/client-web/config/type.d.ts.map +1 -0
  23. package/dist/packages/client-web/index.d.ts +3 -0
  24. package/dist/packages/client-web/index.d.ts.map +1 -0
  25. package/dist/packages/client-web/module.d.ts +9 -0
  26. package/dist/packages/client-web/module.d.ts.map +1 -0
  27. package/dist/packages/constants.d.ts +9 -0
  28. package/dist/packages/constants.d.ts.map +1 -0
  29. package/dist/packages/execute.d.ts +4 -0
  30. package/dist/packages/execute.d.ts.map +1 -0
  31. package/dist/packages/index.d.ts +6 -0
  32. package/dist/packages/index.d.ts.map +1 -0
  33. package/dist/packages/normalize.d.ts +3 -0
  34. package/dist/packages/normalize.d.ts.map +1 -0
  35. package/dist/packages/server-core/index.d.ts +2 -0
  36. package/dist/packages/server-core/index.d.ts.map +1 -0
  37. package/dist/packages/server-core/module.d.ts +7 -0
  38. package/dist/packages/server-core/module.d.ts.map +1 -0
  39. package/dist/packages/types.d.ts +9 -0
  40. package/dist/packages/types.d.ts.map +1 -0
  41. package/dist/utils/shell.d.ts +4 -4
  42. package/dist/utils/shell.d.ts.map +1 -1
  43. package/package.json +22 -19
  44. package/dist/commands/cleanup.d.ts +0 -3
  45. package/dist/commands/cleanup.d.ts.map +0 -1
  46. package/dist/commands/index.d.ts +0 -4
  47. package/dist/commands/index.d.ts.map +0 -1
  48. package/dist/commands/info.d.ts +0 -3
  49. package/dist/commands/info.d.ts.map +0 -1
  50. package/dist/commands/start.d.ts +0 -3
  51. package/dist/commands/start.d.ts.map +0 -1
  52. package/dist/index.cjs +0 -418
  53. package/dist/index.cjs.map +0 -1
  54. package/dist/services/check.d.ts +0 -3
  55. package/dist/services/check.d.ts.map +0 -1
  56. package/dist/services/client-web/config.d.ts +0 -4
  57. package/dist/services/client-web/config.d.ts.map +0 -1
  58. package/dist/services/client-web/execute.d.ts +0 -8
  59. package/dist/services/client-web/execute.d.ts.map +0 -1
  60. package/dist/services/client-web/index.d.ts +0 -3
  61. package/dist/services/client-web/index.d.ts.map +0 -1
  62. package/dist/services/constants.d.ts +0 -9
  63. package/dist/services/constants.d.ts.map +0 -1
  64. package/dist/services/execute.d.ts +0 -9
  65. package/dist/services/execute.d.ts.map +0 -1
  66. package/dist/services/index.d.ts +0 -5
  67. package/dist/services/index.d.ts.map +0 -1
  68. package/dist/services/server-core/config.d.ts +0 -4
  69. package/dist/services/server-core/config.d.ts.map +0 -1
  70. package/dist/services/server-core/execute.d.ts +0 -3
  71. package/dist/services/server-core/execute.d.ts.map +0 -1
  72. package/dist/services/server-core/index.d.ts +0 -3
  73. package/dist/services/server-core/index.d.ts.map +0 -1
package/dist/index.mjs CHANGED
@@ -1,69 +1,140 @@
1
1
  #!/usr/bin/env node
2
- import cac from 'cac';
3
- import { deserializeKey, Container, EnvKey } from '@authup/config';
4
- import consola from 'consola';
5
- import { buildConfig } from '@authup/client-web-config';
6
- import { makeURLPublicAccessible } from '@authup/core';
2
+ import { defineCommand, runMain } from 'citty';
3
+ import fs from 'node:fs';
7
4
  import path from 'node:path';
8
- import { exec } from 'node:child_process';
9
5
  import process from 'node:process';
10
- import { isObject, hasOwnProperty } from 'smob';
6
+ import { makeURLPublicAccessible, extendObject, isObject, hasOwnProperty } from '@authup/kit';
7
+ import { dycraft, defineGetter } from 'dycraft';
8
+ import zod from 'zod';
9
+ import { merge } from 'smob';
10
+ import { oneOf, readInt, read } from 'envix';
11
+ import { Container } from 'confinity';
12
+ import consola from 'consola';
13
+ import { exec } from 'node:child_process';
11
14
  import findUpPackagePath from 'resolve-package-path';
12
- import { parseConfig, buildConfig as buildConfig$1 } from '@authup/server-core';
13
- import readline from 'node:readline';
14
- import chalk from 'chalk';
15
15
 
16
- function isServiceValid(input) {
17
- let key;
18
- if (typeof input === 'string') {
19
- key = deserializeKey(input);
20
- } else {
21
- key = input;
16
+
17
+ // -- Shims --
18
+ import cjsUrl from 'node:url';
19
+ import cjsPath from 'node:path';
20
+ import cjsModule from 'node:module';
21
+ const __filename = cjsUrl.fileURLToPath(import.meta.url);
22
+ const __dirname = cjsPath.dirname(__filename);
23
+ const require = cjsModule.createRequire(import.meta.url);
24
+ function parseClientWebConfig(input = {}) {
25
+ const schema = zod.object({
26
+ port: zod.number().nonnegative().optional(),
27
+ host: zod.string().optional(),
28
+ apiUrl: zod.string().url().optional(),
29
+ publicUrl: zod.string().url().optional()
30
+ });
31
+ return schema.parse(input);
32
+ }
33
+
34
+ function buildClientWebConfig(raw) {
35
+ const config = dycraft({
36
+ defaults: {
37
+ port: 3000,
38
+ host: '0.0.0.0',
39
+ apiUrl: 'http://127.0.0.1:3001/'
40
+ },
41
+ getters: {
42
+ publicUrl: defineGetter((context)=>`http://${makeURLPublicAccessible(context.get('host'))}:${context.get('port')}/`)
43
+ }
44
+ });
45
+ return extendObject(config, parseClientWebConfig(raw));
46
+ }
47
+
48
+ function readClientWebConfigFromEnv() {
49
+ const config = {};
50
+ const port = oneOf([
51
+ readInt('UI_PORT'),
52
+ readInt('NITRO_UI_PORT'),
53
+ readInt('NUXT_UI_PORT'),
54
+ readInt('NUXT_PUBLIC_UI_PORT'),
55
+ readInt('PORT'),
56
+ readInt('NITRO_PORT'),
57
+ readInt('NUXT_PORT'),
58
+ readInt('NUXT_PUBLIC_PORT')
59
+ ]);
60
+ if (typeof port !== 'undefined') {
61
+ config.port = port;
22
62
  }
23
- if (key.group !== 'client' && key.group !== 'server') {
24
- return false;
63
+ const host = oneOf([
64
+ read('HOST'),
65
+ read('NITRO_HOST'),
66
+ read('NUXT_HOST')
67
+ ]);
68
+ if (host) {
69
+ config.host = host;
25
70
  }
26
- if (key.group === 'client') {
27
- return key.name === 'web';
71
+ const apiUrl = oneOf([
72
+ read('API_URL'),
73
+ read('NUXT_API_URL'),
74
+ read('NUXT_PUBLIC_API_URL')
75
+ ]);
76
+ if (apiUrl) {
77
+ config.apiUrl = apiUrl;
28
78
  }
29
- return key.name === 'core';
79
+ const publicURL = oneOf([
80
+ read('PUBLIC_URL'),
81
+ read('NUXT_PUBLIC_URL'),
82
+ read('NUXT_PUBLIC_PUBLIC_URL')
83
+ ]);
84
+ if (publicURL) {
85
+ config.publicUrl = publicURL;
86
+ }
87
+ return config;
30
88
  }
31
89
 
32
- async function buildClientWebConfig(container) {
33
- const client = container.getData('client/web');
34
- const server = container.getData('server/core');
35
- if (server) {
36
- if (!client.apiUrl && typeof server.publicUrl === 'string') {
37
- client.apiUrl = makeURLPublicAccessible(server.publicUrl);
90
+ async function readClientWebConfigFromFS(options = {}) {
91
+ const container = new Container({
92
+ prefix: 'authup',
93
+ cwd: options.cwd
94
+ });
95
+ if (options.file) {
96
+ await container.loadFile(options.file);
97
+ } else {
98
+ await container.load();
99
+ }
100
+ const clientRaw = container.get('client.web') || {};
101
+ const serverRaw = container.get('server.core') || {};
102
+ if (serverRaw) {
103
+ if (!clientRaw.apiUrl && typeof serverRaw.publicUrl === 'string') {
104
+ clientRaw.apiUrl = makeURLPublicAccessible(serverRaw.publicUrl);
38
105
  }
39
- if (!client.publicUrl && typeof server.authorizeRedirectUrl === 'string') {
40
- client.apiUrl = makeURLPublicAccessible(server.authorizeRedirectUrl);
106
+ if (!clientRaw.publicUrl && typeof serverRaw.authorizeRedirectUrl === 'string') {
107
+ clientRaw.apiUrl = makeURLPublicAccessible(serverRaw.authorizeRedirectUrl);
41
108
  }
42
109
  }
43
- return buildConfig({
44
- data: client,
45
- env: true
46
- });
110
+ return clientRaw;
47
111
  }
48
112
 
49
- /*
50
- * Copyright (c) 2023.
51
- * Author Peter Placzek (tada5hi)
52
- * For the full copyright and license information,
53
- * view the LICENSE file that was distributed with this source code.
54
- */ var ServicePackageName;
55
- (function(ServicePackageName) {
56
- ServicePackageName["CLIENT_WEB"] = "@authup/client-web";
57
- ServicePackageName["SERVER_CORE"] = "@authup/server-core";
58
- })(ServicePackageName || (ServicePackageName = {}));
59
- var ServiceCommand;
60
- (function(ServiceCommand) {
61
- ServiceCommand["START"] = "start";
62
- ServiceCommand["CLEANUP"] = "cleanup";
63
- })(ServiceCommand || (ServiceCommand = {}));
113
+ async function readClientWebConfigRaw(options = {}) {
114
+ if (options.fs && options.env) {
115
+ const fsOptions = boolableToObject(options.fs);
116
+ const fs = await readClientWebConfigFromFS(fsOptions);
117
+ const env = readClientWebConfigFromEnv();
118
+ return merge(env, fs);
119
+ }
120
+ if (options.fs) {
121
+ const fsOptions = boolableToObject(options.fs);
122
+ return readClientWebConfigFromFS(fsOptions);
123
+ }
124
+ if (options.env) {
125
+ return readClientWebConfigFromEnv();
126
+ }
127
+ return {};
128
+ }
129
+ function boolableToObject(input) {
130
+ if (typeof input === 'boolean') {
131
+ return {};
132
+ }
133
+ return input;
134
+ }
64
135
 
65
136
  /*
66
- * Copyright (c) 2022.
137
+ * Copyright (c) 2022-2024.
67
138
  * Author Peter Placzek (tada5hi)
68
139
  * For the full copyright and license information,
69
140
  * view the LICENSE file that was distributed with this source code.
@@ -97,7 +168,7 @@ function parseProcessOutputData(input) {
97
168
  }
98
169
 
99
170
  /*
100
- * Copyright (c) 2022.
171
+ * Copyright (c) 2022-2024.
101
172
  * Author Peter Placzek (tada5hi)
102
173
  * For the full copyright and license information,
103
174
  * view the LICENSE file that was distributed with this source code.
@@ -110,15 +181,13 @@ function parseProcessOutputData(input) {
110
181
  return parts.join(' ');
111
182
  }
112
183
 
113
- async function execShellCommand(ctx) {
114
- ctx.env = ctx.env || {};
115
- ctx.args = ctx.args || {};
184
+ async function execShellCommand(command, ctx = {}) {
116
185
  return new Promise((resolve, reject)=>{
117
- const childProcess = exec(`${ctx.command} ${stringifyObjectArgs(ctx.args)}`, {
186
+ const childProcess = exec(`${command} ${stringifyObjectArgs(ctx.args || {})}`, {
118
187
  env: {
119
188
  PATH: process.env.PATH,
120
189
  ...ctx.envFromProcess ? process.env : {},
121
- ...ctx.env
190
+ ...ctx.env ? ctx.env : {}
122
191
  }
123
192
  });
124
193
  childProcess.on('error', (data)=>{
@@ -127,27 +196,31 @@ async function execShellCommand(ctx) {
127
196
  childProcess.on('spawn', ()=>{
128
197
  resolve(childProcess);
129
198
  });
130
- childProcess.stderr.setEncoding('utf-8');
131
- childProcess.stderr.on('data', (data)=>{
132
- if (typeof data !== 'string' || data.length === 0) {
133
- return;
134
- }
135
- if (ctx.logErrorStream) {
136
- ctx.logErrorStream(data);
137
- }
138
- });
139
- childProcess.stdout.on('data', (data)=>{
140
- if (typeof data !== 'string' || data.length === 0) {
141
- return;
142
- }
143
- if (!ctx.logDataStream) {
144
- return;
145
- }
146
- const lines = parseProcessOutputData(data);
147
- for(let i = 0; i < lines.length; i++){
148
- ctx.logDataStream(lines[i]);
149
- }
150
- });
199
+ if (childProcess.stderr) {
200
+ childProcess.stderr.setEncoding('utf-8');
201
+ childProcess.stderr.on('data', (data)=>{
202
+ if (typeof data !== 'string' || data.length === 0) {
203
+ return;
204
+ }
205
+ if (ctx.logErrorStream) {
206
+ ctx.logErrorStream(data);
207
+ }
208
+ });
209
+ }
210
+ if (childProcess.stdout) {
211
+ childProcess.stdout.on('data', (data)=>{
212
+ if (typeof data !== 'string' || data.length === 0) {
213
+ return;
214
+ }
215
+ if (!ctx.logDataStream) {
216
+ return;
217
+ }
218
+ const lines = parseProcessOutputData(data);
219
+ for(let i = 0; i < lines.length; i++){
220
+ ctx.logDataStream(lines[i]);
221
+ }
222
+ });
223
+ }
151
224
  });
152
225
  }
153
226
 
@@ -164,253 +237,210 @@ function findModulePath(module) {
164
237
  return modulePath;
165
238
  }
166
239
 
167
- function extendEnv(input) {
168
- const env = {};
169
- const keys = Object.keys(input);
170
- for(let i = 0; i < keys.length; i++){
171
- env[keys[i]] = input[keys[i]];
172
- if (!keys[i].match(/^(?:NUXT|NITRO)_.*$/)) {
173
- env[`NUXT_PUBLIC_${keys[i]}`] = input[keys[i]];
174
- }
175
- }
176
- return env;
177
- }
178
- function buildWebAppExecutionContext(ctx) {
179
- if (ctx.command !== ServiceCommand.START) {
180
- throw new Error(`The command ${ctx.command} is not supported`);
181
- }
182
- const env = ctx.env || {};
183
- const data = ctx.container.getData('client/web');
184
- const config = buildConfig({
185
- data,
186
- env: true
187
- });
188
- if (config.host) {
189
- env.HOST = config.host;
190
- }
191
- if (config.port) {
192
- env.PORT = `${config.port}`;
193
- }
194
- if (config.apiUrl) {
195
- env.API_URL = config.apiUrl;
196
- }
197
- if (config.publicUrl) {
198
- env.PUBLIC_URL = config.publicUrl;
199
- }
200
- let command;
201
- const modulePath = findModulePath(ServicePackageName.CLIENT_WEB);
202
- if (typeof modulePath === 'string') {
203
- const directory = path.dirname(modulePath);
204
- const outputPath = path.join(directory, '.output', 'server', 'index.mjs');
205
- command = `node ${outputPath}`;
206
- } else {
207
- command = `npx ${ServicePackageName.CLIENT_WEB}`;
208
- }
209
- return {
210
- ...ctx,
211
- command,
212
- env: extendEnv(env)
213
- };
214
- }
215
-
216
- async function buildServerCoreConfig(container) {
217
- const data = parseConfig(container.getData('server/core'));
218
- return buildConfig$1({
219
- data,
220
- env: true
221
- });
222
- }
240
+ /*
241
+ * Copyright (c) 2023-2024.
242
+ * Author Peter Placzek (tada5hi)
243
+ * For the full copyright and license information,
244
+ * view the LICENSE file that was distributed with this source code.
245
+ */ var PackageName = /*#__PURE__*/ function(PackageName) {
246
+ PackageName["CLIENT_WEB"] = "@authup/client-web";
247
+ PackageName["SERVER_CORE"] = "@authup/server-core";
248
+ return PackageName;
249
+ }({});
250
+ var PackageID = /*#__PURE__*/ function(PackageID) {
251
+ PackageID["CLIENT_WEB"] = "client.web";
252
+ PackageID["SERVER_CORE"] = "server.core";
253
+ return PackageID;
254
+ }({});
223
255
 
224
- function buildServerCoreExecutionContext(ctx) {
225
- let command;
226
- const modulePath = findModulePath(ServicePackageName.SERVER_CORE);
227
- if (typeof modulePath === 'string') {
228
- const directory = path.dirname(modulePath);
229
- const outputPath = path.join(directory, 'dist', 'cli', 'index.js');
230
- command = `node ${outputPath}`;
231
- } else {
232
- command = `npx ${ServicePackageName.SERVER_CORE}`;
233
- }
234
- switch(ctx.command){
235
- case ServiceCommand.START:
236
- {
237
- command += ' start';
238
- break;
239
- }
240
- case ServiceCommand.CLEANUP:
241
- {
242
- command += ' reset';
243
- break;
244
- }
245
- default:
246
- {
247
- throw new Error(`The command ${ctx.command} is not supported.`);
256
+ class ClientWebPackage {
257
+ async execute(command, options = {}) {
258
+ const shellCommand = await this.buildShellCommand();
259
+ const env = await this.buildEnv({
260
+ configDirectory: options.configDirectory,
261
+ configFile: options.configFile
262
+ });
263
+ return execShellCommand(shellCommand, {
264
+ env,
265
+ logDataStream (line) {
266
+ consola.info(`${PackageID.CLIENT_WEB}: ${line}`);
267
+ },
268
+ logErrorStream (line) {
269
+ consola.warn(`${PackageID.CLIENT_WEB}: ${line}`);
248
270
  }
249
- }
250
- return {
251
- ...ctx,
252
- command
253
- };
254
- }
255
-
256
- function buildInfoCommand(cac) {
257
- cac.command('info', 'Get information about the configuration.').option('-c, --config [config]', 'Specify a configuration file').action(async (ctx)=>{
258
- const container = new Container({
259
- prefix: 'authup',
260
- keys: [
261
- 'client/web',
262
- 'server/core'
263
- ]
264
271
  });
265
- if (ctx.config) {
266
- await container.loadFromFilePath(ctx.config);
272
+ }
273
+ async buildShellCommand() {
274
+ let shellCommand;
275
+ const modulePath = findModulePath(PackageName.CLIENT_WEB);
276
+ if (typeof modulePath === 'string') {
277
+ const directory = path.dirname(modulePath);
278
+ const outputPath = path.join(directory, '.output', 'server', 'index.mjs');
279
+ shellCommand = `node ${outputPath}`;
267
280
  } else {
268
- await container.load();
281
+ shellCommand = `npx ${PackageName.CLIENT_WEB}`;
269
282
  }
270
- const clientWeb = await buildClientWebConfig(container);
271
- consola.info(`Host: ${clientWeb.host}`);
272
- consola.info(`Port: ${clientWeb.port}`);
273
- consola.info('------------');
274
- const serverCore = await buildServerCoreConfig(container);
275
- consola.info(`Host: ${serverCore.host}`);
276
- consola.info(`Port: ${serverCore.port}`);
277
- consola.info(`Environment: ${serverCore.env}`);
278
- consola.info(`RootPath: ${serverCore.rootPath}`);
279
- consola.info(`WritableDirectoryPath: ${serverCore.writableDirectoryPath}`);
280
- consola.info('------------');
281
- consola.info('Report an issue: https://github.com/authup/authup/issues/new');
282
- consola.info('Suggest an improvement: https://github.com/authup/authup/discussions/new');
283
- consola.info('Read documentation: https://authup.org');
284
- });
285
- }
286
-
287
- async function executeServiceCommand(context) {
288
- let service;
289
- if (typeof context.service === 'string') {
290
- service = deserializeKey(context.service);
291
- } else {
292
- service = context.service;
283
+ return shellCommand;
293
284
  }
294
- const env = {};
295
- const config = context.container.get(`${service.group}/${service.name}`);
296
- if (config) {
297
- const paths = config.paths.join(',');
298
- if (paths.length > 0) {
299
- env[EnvKey.CONFIG_FILE] = paths;
285
+ async buildEnv(ctx) {
286
+ const env = {};
287
+ const configRaw = await readClientWebConfigRaw({
288
+ fs: {
289
+ file: ctx.configFile,
290
+ cwd: ctx.configDirectory
291
+ }
292
+ });
293
+ const config = buildClientWebConfig(configRaw);
294
+ if (config.host) {
295
+ env.HOST = config.host;
300
296
  }
301
- }
302
- let shellExecContext;
303
- try {
304
- if (service.group === 'server' && service.name === 'core') {
305
- shellExecContext = buildServerCoreExecutionContext({
306
- command: context.command,
307
- env
308
- });
297
+ if (config.port) {
298
+ env.PORT = `${config.port}`;
309
299
  }
310
- if (service.group === 'client' && service.name === 'web') {
311
- shellExecContext = buildWebAppExecutionContext({
312
- container: context.container,
313
- command: context.command,
314
- env
315
- });
300
+ if (config.apiUrl) {
301
+ env.API_URL = config.apiUrl;
316
302
  }
317
- } catch (e) {
318
- consola.error(`${service.group}/${service.name}: The service command ${context.command} not supported.`);
319
- process.exit(1);
320
- }
321
- if (!shellExecContext) {
322
- consola.error(`${service.group}/${service.name}: The service is not supported.`);
323
- process.exit(1);
303
+ if (config.publicUrl) {
304
+ env.PUBLIC_URL = config.publicUrl;
305
+ }
306
+ return this.extendEnvKeys(env);
324
307
  }
325
- return execShellCommand({
326
- ...shellExecContext,
327
- logDataStream (line) {
328
- consola.info(`${service.group}/${service.name}: ${line}`);
329
- },
330
- logErrorStream (line) {
331
- consola.warn(`${service.group}/${service.name}: ${line}`);
308
+ extendEnvKeys(input) {
309
+ const env = {};
310
+ const keys = Object.keys(input);
311
+ for(let i = 0; i < keys.length; i++){
312
+ env[keys[i]] = input[keys[i]];
313
+ if (!keys[i].match(/^(?:NUXT|NITRO)_.*$/)) {
314
+ env[`NUXT_PUBLIC_${keys[i]}`] = input[keys[i]];
315
+ }
332
316
  }
333
- });
317
+ return env;
318
+ }
334
319
  }
335
- async function executeServicesCommand(context) {
336
- const container = new Container({
337
- prefix: 'authup',
338
- keys: context.servicesAllowed || context.services
339
- });
340
- if (context.config) {
341
- await container.loadFromFilePath(context.config);
342
- } else {
343
- await container.load();
320
+
321
+ class ServerCorePackage {
322
+ async execute(command, options = {}) {
323
+ const shellCommand = await this.buildShellCommand(command, {
324
+ configDirectory: options.configDirectory,
325
+ configFile: options.configFile
326
+ });
327
+ return execShellCommand(shellCommand, {
328
+ logDataStream (line) {
329
+ consola.info(`${PackageID.SERVER_CORE}: ${line}`);
330
+ },
331
+ logErrorStream (line) {
332
+ consola.warn(`${PackageID.SERVER_CORE}: ${line}`);
333
+ }
334
+ });
344
335
  }
345
- const promises = [];
346
- for(let i = 0; i < context.services.length; i++){
347
- const service = deserializeKey(context.services[i]);
348
- if (!isServiceValid(service)) {
349
- consola.error(`${chalk.red(`${service.group}/${service.name}`)}: The service does not exist.`);
350
- process.exit(1);
336
+ async buildShellCommand(command, options) {
337
+ const parts = [];
338
+ const modulePath = findModulePath(PackageName.SERVER_CORE);
339
+ if (typeof modulePath === 'string') {
340
+ const directory = path.dirname(modulePath);
341
+ const outputPath = path.join(directory, 'dist', 'cli', 'index.js');
342
+ parts.push(`node ${outputPath}`);
343
+ } else {
344
+ parts.push(`npx ${PackageName.SERVER_CORE}`);
351
345
  }
352
- if (context.servicesAllowed && context.servicesAllowed.indexOf(`${service.group}/${service.name}`) === -1) {
353
- consola.error(`${chalk.red(`${service.group}/${service.name}`)}: The service does not support the ${context.command} command.`);
354
- process.exit(1);
346
+ parts.push(command);
347
+ if (options.configFile) {
348
+ parts.push(`--configFile=${options.configFile}`);
355
349
  }
356
- promises.push(executeServiceCommand({
357
- command: context.command,
358
- service,
359
- container
360
- }));
361
- }
362
- const childProcesses = await Promise.all(promises);
363
- const readLine = readline.createInterface({
364
- input: process.stdin,
365
- output: process.stdout
366
- });
367
- readLine.on('SIGINT', ()=>{
368
- for(let i = 0; i < childProcesses.length; i++){
369
- childProcesses[i].kill();
350
+ if (options.configDirectory) {
351
+ parts.push(`--configDirectory=${options.configDirectory}`);
370
352
  }
371
- process.exit();
372
- });
353
+ return parts.join(' ');
354
+ }
373
355
  }
374
356
 
375
- function buildCleanupCommand(cac) {
376
- cac.command('cleanup [...services]', 'Cleanup a service').option('-c, --config [config]', 'Specify a configuration file').action(async (keysInput, ctx)=>{
377
- const servicesAllowed = [
378
- 'server/core'
379
- ];
380
- if (!keysInput || keysInput.length === 0) {
381
- keysInput = servicesAllowed;
382
- }
383
- await executeServicesCommand({
384
- config: ctx.config,
385
- command: ServiceCommand.CLEANUP,
386
- services: keysInput,
387
- servicesAllowed
388
- });
389
- });
357
+ async function executePackageCommand(pkg, command, options = {}) {
358
+ switch(pkg){
359
+ case PackageID.CLIENT_WEB:
360
+ {
361
+ const serverCore = new ClientWebPackage();
362
+ return serverCore.execute(command, options);
363
+ }
364
+ case PackageID.SERVER_CORE:
365
+ {
366
+ const serverCore = new ServerCorePackage();
367
+ return serverCore.execute(command, options);
368
+ }
369
+ }
370
+ throw new Error(`The package ${pkg} is not supported.`);
390
371
  }
391
372
 
392
- function buildStartCommand(cac) {
393
- cac.command('start [...services]', 'Start a service').option('-c, --config [config]', 'Specify a configuration file').action(async (keysInput, ctx)=>{
394
- const servicesAllowed = [
395
- 'client/web',
396
- 'server/core'
397
- ];
398
- if (!keysInput || keysInput.length === 0) {
399
- keysInput = servicesAllowed;
373
+ function normalizePackageID(input) {
374
+ const value = input.trim().toLowerCase();
375
+ switch(value){
376
+ case 'client.web':
377
+ case 'client/web':
378
+ case 'client-web':
379
+ {
380
+ return PackageID.CLIENT_WEB;
381
+ }
382
+ case 'server.core':
383
+ case 'server/core':
384
+ case 'server-core':
385
+ {
386
+ return PackageID.SERVER_CORE;
387
+ }
388
+ }
389
+ return null;
390
+ }
391
+
392
+ async function createCLIEntryPointCommand() {
393
+ const pkgRaw = await fs.promises.readFile(path.join(process.cwd(), 'package.json'), {
394
+ encoding: 'utf8'
395
+ });
396
+ const pkg = JSON.parse(pkgRaw);
397
+ return defineCommand({
398
+ meta: {
399
+ name: pkg.name,
400
+ version: pkg.version,
401
+ description: pkg.description
402
+ },
403
+ args: {
404
+ command: {
405
+ type: 'positional',
406
+ description: 'The command which should be forwarded to the package.',
407
+ required: true
408
+ },
409
+ package: {
410
+ type: 'positional',
411
+ description: 'The package, which should be targeted.',
412
+ required: false
413
+ },
414
+ configDirectory: {
415
+ type: 'string',
416
+ description: 'Config directory path',
417
+ alias: 'cD'
418
+ },
419
+ configFile: {
420
+ type: 'string',
421
+ description: 'Name of one or more configuration files.',
422
+ alias: 'cF'
423
+ }
424
+ },
425
+ async run (ctx) {
426
+ let packages = ctx.args.package ? ctx.args.package.split(',') : [];
427
+ if (packages.length > 0) {
428
+ packages = packages.map((pkg)=>normalizePackageID(pkg)).filter((pkg)=>Boolean(pkg)).map((pkg)=>`${pkg}`);
429
+ }
430
+ if (packages.length === 0) {
431
+ packages = Object.values(PackageID);
432
+ }
433
+ const promises = [];
434
+ for(let i = 0; i < packages.length; i++){
435
+ promises.push(executePackageCommand(packages[i], ctx.args.command, {
436
+ configFile: ctx.args.configFile,
437
+ configDirectory: ctx.args.configDirectory
438
+ }));
439
+ }
440
+ await Promise.all(promises);
400
441
  }
401
- await executeServicesCommand({
402
- config: ctx.config,
403
- command: ServiceCommand.START,
404
- services: keysInput,
405
- servicesAllowed
406
- });
407
442
  });
408
443
  }
409
444
 
410
- const cli = cac();
411
- buildInfoCommand(cli);
412
- buildStartCommand(cli);
413
- buildCleanupCommand(cli);
414
- cli.help();
415
- cli.parse();
445
+ Promise.resolve().then(()=>createCLIEntryPointCommand()).then((command)=>runMain(command));
416
446
  //# sourceMappingURL=index.mjs.map