@akala/pm 3.5.19 → 3.7.0

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.
@@ -3,12 +3,11 @@ import { homedir } from 'os';
3
3
  import fs from 'fs/promises';
4
4
  import { join } from 'path';
5
5
  import pmContainer from '../container';
6
- import { Container, Metadata, ignoredCommands, configure, ServeOptions, SelfDefinedCommand } from '@akala/commands';
7
- import { eachAsync } from '@akala/core';
8
- import { PassThrough, Readable } from 'stream';
6
+ import { Container, Metadata, ignoredCommands, configure, SelfDefinedCommand } from '@akala/commands';
7
+ import { PassThrough } from 'stream';
9
8
  import { EventEmitter } from 'events';
10
9
  import { CliContext } from '@akala/cli';
11
- import Configuration, { ProxyConfiguration } from '@akala/config';
10
+ import Configuration from '@akala/config';
12
11
 
13
12
  export async function metadata(container: Container<unknown>, deep?: boolean): Promise<Metadata.Container>
14
13
  {
@@ -45,7 +44,7 @@ export async function metadata(container: Container<unknown>, deep?: boolean): P
45
44
  return metacontainer;
46
45
  }
47
46
 
48
- export function isRunningContainer(c: Container<any>): c is RunningContainer
47
+ export function isRunningContainer(c: Container<unknown>): c is RunningContainer
49
48
  {
50
49
  return 'running' in c;
51
50
  }
@@ -60,16 +59,18 @@ export default async function (this: State, container: RunningContainer & pmCont
60
59
  process.stderr.write = function (...args)
61
60
  {
62
61
  stderr.call(process.stderr, ...args);
62
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
63
63
  return stderrPT.write(...args as [any, BufferEncoding]);
64
- } as any;
64
+ } as typeof process.stderr.write;
65
65
 
66
66
  const stdout = process.stdout.write;
67
67
  const stdoutPT = new PassThrough();
68
68
  process.stdout.write = function (...args)
69
69
  {
70
70
  stdout.call(process.stdout, ...args);
71
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
71
72
  return stdoutPT.write(...args as [any, BufferEncoding]);
72
- } as any;
73
+ } as typeof process.stdout.write;
73
74
  container.process = Object.assign(new EventEmitter(), {
74
75
  stdout: stdoutPT, stderr: stderrPT, stdio: null, stdin: process.stdin, pid: process.pid, connected: false
75
76
  , exitCode: undefined
@@ -1,4 +1,3 @@
1
- import { Container } from "@akala/commands";
2
1
  import { SocketAdapter } from "@akala/json-rpc-ws";
3
2
  import State from "../state";
4
3
 
@@ -1,7 +1,6 @@
1
1
  import * as cli from "@akala/cli";
2
2
  import { CliContext } from "@akala/cli";
3
3
  import State from '../state';
4
- import Configuration from "@akala/config";
5
4
 
6
5
  export default async function config(this: State, name: string | undefined | void, options: CliContext['options'] | undefined | void): Promise<string[] | State['config'] | State['config']['containers']['']>
7
6
  {
@@ -11,7 +11,7 @@ export default async function (this: State, name: string, socket: SocketAdapter)
11
11
  const connectionId = randomUUID();
12
12
  this.bridges[connectionId] = { left: socket };
13
13
  cp.dispatch('$bridge', connectionId);
14
- return new Promise<void>((resolve, reject) =>
14
+ return new Promise<void>((resolve) =>
15
15
  {
16
16
  const x = setInterval(() =>
17
17
  {
@@ -0,0 +1,27 @@
1
+ {
2
+ "$schema": "https://raw.githubusercontent.com/npenin/akala/master/packages/commands/command-schema.json",
3
+ "": {
4
+ "inject": [
5
+ "$container",
6
+ "param.0",
7
+ "param.1"
8
+ ]
9
+ },
10
+ "cli": {
11
+ "options": {
12
+ "wait": {
13
+ "aliases": [
14
+ "w"
15
+ ]
16
+ },
17
+ "inspect": {},
18
+ "new": {},
19
+ "name": {}
20
+ },
21
+ "inject": [
22
+ "$container",
23
+ "param.0",
24
+ "context"
25
+ ]
26
+ }
27
+ }
@@ -0,0 +1,9 @@
1
+ import { StdioOptions } from "child_process";
2
+ import pmContainer from '../container';
3
+ import { CliContext } from "@akala/cli";
4
+
5
+ export default async function restart(pm: pmContainer.container, name: string, context?: CliContext<{ new?: boolean, name: string, inspect?: boolean, verbose?: boolean, wait?: boolean }>): Promise<void | { execPath: string, args: string[], cwd: string, stdio: StdioOptions, shell: boolean, windowsHide: boolean }>
6
+ {
7
+ await pm.dispatch('stop', name);
8
+ await pm.dispatch('start', name, context);
9
+ }
@@ -3,7 +3,7 @@ import State, { RunningContainer, SidecarMetadata } from '../state';
3
3
  import { spawn, ChildProcess, StdioOptions } from "child_process";
4
4
  import pmContainer from '../container';
5
5
  import * as jsonrpc from '@akala/json-rpc-ws'
6
- import { eachAsync, lazy, logger } from "@akala/core";
6
+ import { eachAsync } from "@akala/core";
7
7
  import { NewLinePrefixer } from "../new-line-prefixer.js";
8
8
  import { CliContext, ErrorWithStatus } from "@akala/cli";
9
9
  import getRandomName from "./name";
@@ -66,7 +66,6 @@ export default async function start(this: State, pm: pmContainer.container & Con
66
66
  if (context.options && context.options.verbose)
67
67
  args.push('-v')
68
68
 
69
- const log = logger('akala:pm:' + context.options.name);
70
69
  let cp: ChildProcess;
71
70
  if (!this.isDaemon)
72
71
  {
package/src/container.ts CHANGED
@@ -19,6 +19,7 @@ namespace commands
19
19
  dispatch (cmd:'proxy', ...args: [Argument0<typeof import('./commands/proxy').default>, Argument1<typeof import('./commands/proxy').default>]): ReturnType<typeof import('./commands/proxy').default>
20
20
  dispatch (cmd:'ready', ...args: []): ReturnType<typeof import('./commands/ready').default>
21
21
  dispatch (cmd:'reload-metadata', ...args: [Argument0<typeof import('./commands/reload-metadata').default>]): ReturnType<typeof import('./commands/reload-metadata').default>
22
+ dispatch (cmd:'restart', ...args: [Argument1<typeof import('./commands/restart').default>, Argument2<typeof import('./commands/restart').default>]): ReturnType<typeof import('./commands/restart').default>
22
23
  dispatch (cmd:'start', ...args: [Argument1<typeof import('./commands/start').default>, Argument2<typeof import('./commands/start').default>]): ReturnType<typeof import('./commands/start').default>
23
24
  dispatch (cmd:'status', ...args: [Argument0<typeof import('./commands/status').default>]): ReturnType<typeof import('./commands/status').default>
24
25
  dispatch (cmd:'stop', ...args: [Argument0<typeof import('./commands/stop').default>]): ReturnType<typeof import('./commands/stop').default>
@@ -41,6 +42,7 @@ namespace commands
41
42
  'proxy'(...args: [Argument0<typeof import('./commands/proxy').default>, Argument1<typeof import('./commands/proxy').default>]): ReturnType<typeof import('./commands/proxy').default>
42
43
  'ready'(...args: []): ReturnType<typeof import('./commands/ready').default>
43
44
  'reload-metadata'(...args: [Argument0<typeof import('./commands/reload-metadata').default>]): ReturnType<typeof import('./commands/reload-metadata').default>
45
+ 'restart'(...args: [Argument1<typeof import('./commands/restart').default>, Argument2<typeof import('./commands/restart').default>]): ReturnType<typeof import('./commands/restart').default>
44
46
  'start'(...args: [Argument1<typeof import('./commands/start').default>, Argument2<typeof import('./commands/start').default>]): ReturnType<typeof import('./commands/start').default>
45
47
  'status'(...args: [Argument0<typeof import('./commands/status').default>]): ReturnType<typeof import('./commands/status').default>
46
48
  'stop'(...args: [Argument0<typeof import('./commands/stop').default>]): ReturnType<typeof import('./commands/stop').default>
package/src/fork.ts CHANGED
@@ -5,15 +5,14 @@ import * as path from 'path'
5
5
  import * as ac from '@akala/commands';
6
6
  import { lstat } from 'fs/promises';
7
7
  import { IpcAdapter } from "./ipc-adapter";
8
- import { Socket } from 'net';
9
- import { logger, Logger, module as coreModule } from '@akala/core';
8
+ import { logger, Logger, MiddlewareComposite, module as coreModule } from '@akala/core';
10
9
  import program, { buildCliContextFromProcess, NamespaceMiddleware } from '@akala/cli';
11
10
  import { Stats } from 'fs';
12
11
  import { registerCommands, SelfDefinedCommand, parseMetadata, StructuredParameters } from '@akala/commands';
13
12
 
14
13
  var isPm = false;
15
14
 
16
- program.useMiddleware({
15
+ program.useMiddleware(null, {
17
16
  handle: async c =>
18
17
  {
19
18
  isPm = c.options.program == 'pm';
@@ -39,120 +38,115 @@ logMiddleware.preAction(async c =>
39
38
  });
40
39
  const initMiddleware = new NamespaceMiddleware<{ program: string, name: string, tls: boolean }>(null);
41
40
  program.option<string, 'program'>('program', { needsValue: true }).option<string, 'name'>('name', { needsValue: true }).option<boolean, 'tls'>('tls', { needsValue: false }).
42
- use(async c => //If pure js file
41
+ preAction(async c => //If pure js file
43
42
  {
44
43
  folderOrFile = await lstat(c.options.program);
45
- if (!folderOrFile.isFile() || path.extname(c.options.program) !== '.js')
46
- throw undefined;
44
+ if (folderOrFile.isFile() && path.extname(c.options.program) === '.js')
45
+ return require(c.options.program);
47
46
 
48
- return require(c.options.program);
49
- }).
50
- useMiddleware({
51
- handle: async c => //if commandable
52
- {
53
- log = logger(c.options.name);
47
+ log = logger(c.options.name);
54
48
 
55
- cliContainer = new ac.Container('cli', {});
49
+ cliContainer = new ac.Container('cli', {});
56
50
 
57
- if (folderOrFile.isFile())
58
- processor = new ac.Processors.FileSystem(path.dirname(c.options.program));
59
- else
60
- processor = new ac.Processors.FileSystem(c.options.program);
61
- }
51
+ if (folderOrFile.isFile())
52
+ processor = new ac.Processors.FileSystem(path.dirname(c.options.program));
53
+ else
54
+ processor = new ac.Processors.FileSystem(c.options.program);
62
55
  }).
63
- useMiddleware(logMiddleware).
64
- useMiddleware({
65
- handle: async c =>
56
+ useMiddleware(null, MiddlewareComposite.new(logMiddleware,
66
57
  {
67
- cliContainer.name = c.options.name;
68
- const init = cliContainer.resolve('$init');
69
- if (init && init.config && init.config.cli && init.config.cli.options)
70
- ac.Triggers.addCliOptions(init, initMiddleware);
71
-
72
- initMiddleware.option<string, 'pmSocket'>('pmSocket', { aliases: ['pm-socket', 'pm-sock'] }).action(async c =>
58
+ handle: async c =>
73
59
  {
60
+ cliContainer.name = c.options.name;
61
+ const init = cliContainer.resolve('$init');
62
+ if (init && init.config && init.config.cli && init.config.cli.options)
63
+ ac.Triggers.addCliOptions(init, initMiddleware);
74
64
 
75
- let pm: ac.Container<unknown>;
76
- let pmConnectInfo: ac.ServeMetadata;
77
-
78
- if (!isPm)
65
+ initMiddleware.option<string, 'pmSocket'>('pmSocket', { aliases: ['pm-socket', 'pm-sock'] }).action(async c =>
79
66
  {
80
- const pmMeta = require('../commands.json');
81
- if (process.connected)
67
+
68
+ let pm: ac.Container<unknown>;
69
+ let pmConnectInfo: ac.ServeMetadata;
70
+
71
+ if (!isPm)
82
72
  {
83
- pm = new ac.Container('pm', null, new ac.Processors.JsonRpc(ac.Processors.JsonRpc.getConnection(new IpcAdapter(process), cliContainer), true));
84
- registerCommands(pmMeta.commands, null, pm);
73
+ //eslint-disable-next-line @typescript-eslint/no-var-requires
74
+ const pmMeta = require('../commands.json');
75
+ if (process.connected)
76
+ {
77
+ pm = new ac.Container('pm', null, new ac.Processors.JsonRpc(ac.Processors.JsonRpc.getConnection(new IpcAdapter(process), cliContainer), true));
78
+ registerCommands(pmMeta.commands, null, pm);
79
+ }
80
+ else
81
+ {
82
+ if (c.options.pmSocket)
83
+ pmConnectInfo = parseMetadata(c.options.pmSocket, c.options.tls);
84
+ else
85
+ pmConnectInfo = ac.serveMetadata('pm', { args: ['local'], options: {} })
86
+ const x = await ac.connectByPreference(pmConnectInfo, { metadata: pmMeta, container: cliContainer });
87
+ pm = x.container;
88
+ pm.processor.useMiddleware(20, x.processor);
89
+ const connect = pm.resolve('connect');
90
+ pm.unregister('connect');
91
+ pm.register(new SelfDefinedCommand((name: string, param: StructuredParameters<unknown[]>) =>
92
+ {
93
+ if (name == 'pm')
94
+ return pmConnectInfo;
95
+ return x.processor.handle(pm, connect, param).then(e => { throw e }, r => r);
96
+ }, 'connect', [
97
+ "param.0",
98
+ "$param"
99
+ ]));
100
+ }
101
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
102
+ pm.unregister(ac.Cli.Metadata.name);
103
+ pm.register(ac.Metadata.extractCommandMetadata(ac.Cli.Metadata));
85
104
  }
86
105
  else
106
+ pm = cliContainer;
107
+
108
+ coreModule('@akala/pm').register('container', pm);
109
+
110
+ cliContainer.register(new SelfDefinedCommand(async (connectionId: string) =>
87
111
  {
88
- if (c.options.pmSocket)
89
- pmConnectInfo = parseMetadata(c.options.pmSocket, c.options.tls);
90
- else
91
- pmConnectInfo = ac.serveMetadata('pm', { args: ['local'], options: {} })
92
- const x = await ac.connectByPreference(pmConnectInfo, { metadata: pmMeta, container: cliContainer });
93
- pm = x.container;
94
- pm.processor.useMiddleware(20, x.processor);
95
- const connect = pm.resolve('connect');
96
- pm.unregister('connect');
97
- pm.register(new SelfDefinedCommand((name: string, param: StructuredParameters<unknown[]>) =>
98
- {
99
- if (name == 'pm')
100
- return pmConnectInfo;
101
- return x.processor.handle(pm, connect, param).then(e => { throw e }, r => r);
102
- }, 'connect', [
103
- "param.0",
104
- "$param"
105
- ]));
112
+ if (!pmConnectInfo)
113
+ pmConnectInfo = await pm.dispatch('connect', 'pm');
114
+ var pm2 = await ac.connectByPreference(pmConnectInfo, { container: cliContainer });
115
+ pm2.container.processor.useMiddleware(20, pm2.processor);
116
+ pm2.container.unregister(ac.Cli.Metadata.name);
117
+ pm2.container.register(ac.Metadata.extractCommandMetadata(ac.Cli.Metadata));
118
+ pm2.container.register(ac.Metadata.extractCommandMetadata(pm.resolve('bridge')));
119
+ if (await pm2.container.dispatch('bridge', connectionId))
120
+ throw undefined;
121
+ }, '$bridge'));
122
+
123
+
124
+ if (init)
125
+ await cliContainer.dispatch(init, { options: c.options, param: c.args, _trigger: 'cli', pm: pm, context: c });
126
+
127
+ let stop: (...args: unknown[]) => Promise<void>;
128
+ try
129
+ {
130
+ const serveArgs: ac.ServeMetadata = await pm.dispatch('connect', c.options.name);
131
+ stop = await cliContainer.dispatch('$serve', serveArgs) as (...args: unknown[]) => Promise<void>;
132
+ }
133
+ catch (e)
134
+ {
135
+ if (!e || e.statusCode !== 404)
136
+ throw e;
137
+ console.warn(e.message);
106
138
  }
107
- // eslint-disable-next-line @typescript-eslint/no-var-requires
108
- pm.unregister(ac.Cli.Metadata.name);
109
- pm.register(ac.Metadata.extractCommandMetadata(ac.Cli.Metadata));
110
- }
111
- else
112
- pm = cliContainer;
113
139
 
114
- coreModule('@akala/pm').register('container', pm);
140
+ if (pm !== cliContainer)
141
+ await pm.dispatch('ready')
115
142
 
116
- cliContainer.register(new SelfDefinedCommand(async (connectionId: string) =>
117
- {
118
- if (!pmConnectInfo)
119
- pmConnectInfo = await pm.dispatch('connect', 'pm');
120
- var pm2 = await ac.connectByPreference(pmConnectInfo, { container: cliContainer });
121
- pm2.container.processor.useMiddleware(20, pm2.processor);
122
- pm2.container.unregister(ac.Cli.Metadata.name);
123
- pm2.container.register(ac.Metadata.extractCommandMetadata(ac.Cli.Metadata));
124
- pm2.container.register(ac.Metadata.extractCommandMetadata(pm.resolve('bridge')));
125
- if (await pm2.container.dispatch('bridge', connectionId))
126
- throw undefined;
127
- }, '$bridge'));
128
-
129
-
130
- if (init)
131
- await cliContainer.dispatch(init, { options: c.options, param: c.args, _trigger: 'cli', pm: pm, context: c });
132
-
133
- let stop: (...args: unknown[]) => Promise<void>;
134
- try
135
- {
136
- const serveArgs: ac.ServeMetadata = await pm.dispatch('connect', c.options.name);
137
- stop = await cliContainer.dispatch('$serve', serveArgs) as (...args: unknown[]) => Promise<void>;
138
- }
139
- catch (e)
140
- {
141
- if (!e || e.statusCode !== 404)
142
- throw e;
143
- console.warn(e.message);
144
- }
145
-
146
- if (pm !== cliContainer)
147
- await pm.dispatch('ready')
148
-
149
- if (stop && typeof stop == 'function')
150
- process.on('SIGINT', stop);
151
- process.on('SIGINT', () => process.exit());
152
- });
153
- }
154
- })
155
- .useMiddleware(initMiddleware);
143
+ if (stop && typeof stop == 'function')
144
+ process.on('SIGINT', stop);
145
+ process.on('SIGINT', () => process.exit());
146
+ });
147
+ }
148
+ },
149
+ initMiddleware));
156
150
 
157
151
  if (require.main == module)
158
152
  program.process(buildCliContextFromProcess()).catch(e =>
package/src/index.ts CHANGED
@@ -50,10 +50,10 @@ export function connect(name: string): Promise<{ connect: Promise<ServeMetadata>
50
50
  {
51
51
  if (name === 'pm')
52
52
  {
53
- let metaContainer = await container.dispatch('$metadata') as Metadata.Container;
53
+ const metaContainer = await container.dispatch('$metadata') as Metadata.Container;
54
54
  return { connect: container.dispatch('connect', name) as Promise<ServeMetadata>, container: metaContainer };
55
55
  }
56
- let metaContainer = await container.dispatch('$metadata', true) as Metadata.Container;
56
+ const metaContainer = await container.dispatch('$metadata', true) as Metadata.Container;
57
57
 
58
58
  return { connect: container.dispatch('connect', name) as Promise<ServeMetadata>, container: { name, commands: metaContainer.commands.filter(c => c.name.startsWith(name + '.')).map(c => ({ name: c.name.substring(name.length + 1), inject: c.inject, config: c.config })) } };
59
59
  })();
package/src/sidecar.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { connectByPreference, Container, ConnectionPreference, registerCommands, updateCommands } from "@akala/commands";
2
- import { module } from "@akala/core";
1
+ import { connectByPreference, ConnectionPreference, updateCommands } from "@akala/commands";
3
2
  import { SideCarConnectionPreference, Sidecar, defaultOrders, connect } from "./index";
4
3
  import * as ac from '@akala/commands'
5
4
 
package/src/state.ts CHANGED
@@ -2,7 +2,7 @@ import { Container } from "@akala/commands";
2
2
  import { ChildProcess } from "child_process";
3
3
  import { Deferred, SerializableObject, SocketAdapter } from "@akala/json-rpc-ws";
4
4
  import { ServeMetadata } from "@akala/commands";
5
- import Configuration, { ProxyConfiguration } from "@akala/config";
5
+ import { ProxyConfiguration } from "@akala/config";
6
6
 
7
7
  export default interface State
8
8
  {
@@ -35,6 +35,7 @@ export interface SidecarConfiguration<T extends string | SerializableObject = Se
35
35
  config?: T;
36
36
  }
37
37
 
38
+ //eslint-disable-next-line @typescript-eslint/no-explicit-any
38
39
  export interface RunningContainer<T extends string | SerializableObject = any> extends Container<unknown>, SidecarConfiguration<T>, SidecarMetadata
39
40
  {
40
41
  process: ChildProcess;